|
| 1 | +// utils/my-backend-request-builders.js |
1 | 2 | import { camelize } from '@ember/string';
|
2 | 3 | import { pluralize } from 'ember-inflector';
|
3 |
| -import { recordIdentifierFor } from '@ember-data/store'; |
4 |
| - |
5 |
| -import { |
6 |
| - findRecord as restFindRecord, |
7 |
| - query as restQuery, |
8 |
| - createRecord as restCreateRecord, |
9 |
| - updateRecord as restUpdateRecord, |
10 |
| - deleteRecord as restDeleteRecord, |
11 |
| -} from '@ember-data/rest/request'; |
| 4 | +import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils'; |
12 | 5 |
|
13 | 6 | const _customResourcePath = (identifierType) => {
|
14 | 7 | return `collections/${camelize(pluralize(identifierType))}/records`;
|
15 | 8 | };
|
16 | 9 |
|
17 |
| -const findRecord = (identifierType, id, options) => { |
18 |
| - options = { |
19 |
| - ...options, |
20 |
| - resourcePath: _customResourcePath(identifierType), |
21 |
| - }; |
22 |
| - return restFindRecord(identifierType, id, options); |
23 |
| -}; |
24 |
| - |
25 |
| -const query = (identifierType, query, options) => { |
26 |
| - options = { |
27 |
| - ...options, |
28 |
| - resourcePath: _customResourcePath(identifierType), |
29 |
| - }; |
30 |
| - return restQuery(identifierType, query, options); |
31 |
| -}; |
| 10 | +async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) { |
| 11 | + const identifier = typeof typeOrIdentifier === 'string' ? { type: typeOrIdentifier, id } : typeOrIdentifier; |
| 12 | + const options = ((typeof typeOrIdentifier === 'string' ? maybeOptions : idOrOptions) || {}); |
32 | 13 |
|
33 |
| -const createRecord = (record, options) => { |
34 |
| - const identifier = recordIdentifierFor(record); |
35 |
| - options = { |
36 |
| - ...options, |
| 14 | + const urlOptions = { |
| 15 | + op: 'findRecord', |
| 16 | + identifier, |
37 | 17 | resourcePath: _customResourcePath(identifier.type),
|
38 | 18 | };
|
39 |
| - return restCreateRecord(record, options); |
40 |
| -}; |
41 | 19 |
|
42 |
| -const updateRecord = (record, options) => { |
43 |
| - const identifier = recordIdentifierFor(record); |
44 |
| - options = { |
45 |
| - ...options, |
46 |
| - resourcePath: _customResourcePath(identifier.type), |
| 20 | + const url = buildBaseURL(urlOptions); |
| 21 | + const headers = new Headers(); |
| 22 | + headers.append('Accept', 'application/vnd.api+json'); |
| 23 | + headers.append('Content-Type', 'application/vnd.api+json'); |
| 24 | + |
| 25 | + return { |
| 26 | + url: options.include?.length |
| 27 | + ? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}` |
| 28 | + : url, |
| 29 | + method: 'GET', |
| 30 | + headers, |
| 31 | + op: 'findRecord', |
| 32 | + records: [identifier], |
47 | 33 | };
|
48 |
| - return restUpdateRecord(record, options); |
49 |
| -}; |
50 | 34 |
|
51 |
| -const deleteRecord = (record, options) => { |
52 |
| - const identifier = recordIdentifierFor(record); |
53 |
| - options = { |
54 |
| - ...options, |
55 |
| - resourcePath: _customResourcePath(identifier.type), |
56 |
| - }; |
57 |
| - return restDeleteRecord(record, options); |
| 35 | +} |
| 36 | + |
| 37 | +export default { |
| 38 | + findRecord |
58 | 39 | };
|
59 | 40 |
|
60 |
| -export { findRecord, query, createRecord, updateRecord, deleteRecord }; |
0 commit comments