Skip to content

Commit 4524c1e

Browse files
committed
Better example for making own builders
1 parent 83afdad commit 4524c1e

File tree

2 files changed

+26
-48
lines changed

2 files changed

+26
-48
lines changed
Lines changed: 25 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,40 @@
1+
// utils/my-backend-request-builders.js
12
import { camelize } from '@ember/string';
23
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';
125

136
const _customResourcePath = (identifierType) => {
147
return `collections/${camelize(pluralize(identifierType))}/records`;
158
};
169

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) || {});
3213

33-
const createRecord = (record, options) => {
34-
const identifier = recordIdentifierFor(record);
35-
options = {
36-
...options,
14+
const urlOptions = {
15+
op: 'findRecord',
16+
identifier,
3717
resourcePath: _customResourcePath(identifier.type),
3818
};
39-
return restCreateRecord(record, options);
40-
};
4119

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],
4733
};
48-
return restUpdateRecord(record, options);
49-
};
5034

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
5839
};
5940

60-
export { findRecord, query, createRecord, updateRecord, deleteRecord };
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { query } from '@ember-data/json-api/request';
22

3-
const result = await store.request(query('user'));
4-
const users = result.content.data;
5-
// or
3+
await store.request(query('user'));
64
const users = store.peekAll('user')

0 commit comments

Comments
 (0)