diff --git a/snippets/adapters/path-for-type/utils.js b/snippets/adapters/path-for-type/utils.js index 9616a23..a12458f 100644 --- a/snippets/adapters/path-for-type/utils.js +++ b/snippets/adapters/path-for-type/utils.js @@ -1,60 +1,40 @@ +// utils/my-backend-request-builders.js import { camelize } from '@ember/string'; import { pluralize } from 'ember-inflector'; -import { recordIdentifierFor } from '@ember-data/store'; - -import { - findRecord as restFindRecord, - query as restQuery, - createRecord as restCreateRecord, - updateRecord as restUpdateRecord, - deleteRecord as restDeleteRecord, -} from '@ember-data/rest/request'; +import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils'; const _customResourcePath = (identifierType) => { return `collections/${camelize(pluralize(identifierType))}/records`; }; -const findRecord = (identifierType, id, options) => { - options = { - ...options, - resourcePath: _customResourcePath(identifierType), - }; - return restFindRecord(identifierType, id, options); -}; - -const query = (identifierType, query, options) => { - options = { - ...options, - resourcePath: _customResourcePath(identifierType), - }; - return restQuery(identifierType, query, options); -}; +async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) { + const identifier = typeof typeOrIdentifier === 'string' ? { type: typeOrIdentifier, id } : typeOrIdentifier; + const options = ((typeof typeOrIdentifier === 'string' ? maybeOptions : idOrOptions) || {}); -const createRecord = (record, options) => { - const identifier = recordIdentifierFor(record); - options = { - ...options, + const urlOptions = { + op: 'findRecord', + identifier, resourcePath: _customResourcePath(identifier.type), }; - return restCreateRecord(record, options); -}; -const updateRecord = (record, options) => { - const identifier = recordIdentifierFor(record); - options = { - ...options, - resourcePath: _customResourcePath(identifier.type), + const url = buildBaseURL(urlOptions); + const headers = new Headers(); + headers.append('Accept', 'application/vnd.api+json'); + headers.append('Content-Type', 'application/vnd.api+json'); + + return { + url: options.include?.length + ? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}` + : url, + method: 'GET', + headers, + op: 'findRecord', + records: [identifier], }; - return restUpdateRecord(record, options); -}; -const deleteRecord = (record, options) => { - const identifier = recordIdentifierFor(record); - options = { - ...options, - resourcePath: _customResourcePath(identifier.type), - }; - return restDeleteRecord(record, options); +} + +export default { + findRecord }; -export { findRecord, query, createRecord, updateRecord, deleteRecord }; diff --git a/snippets/fetching-data/find-all/new.js b/snippets/fetching-data/find-all/new.js index b7ace05..528e25c 100644 --- a/snippets/fetching-data/find-all/new.js +++ b/snippets/fetching-data/find-all/new.js @@ -1,6 +1,4 @@ import { query } from '@ember-data/json-api/request'; -const result = await store.request(query('user')); -const users = result.content.data; -// or +await store.request(query('user')); const users = store.peekAll('user')