Skip to content

Commit 6c17ce0

Browse files
committed
Fix review comments from Chris
1 parent 7245f10 commit 6c17ce0

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +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;
3+
await this.store.request(query('user'));
4+
const users = this.store.peekAll('user')
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import { findRecord } from '@ember-data/json-api/request';
22

3-
const { content: { data: user } } = await this.store.request(findRecord('user', '1'));
3+
const result = await this.store.request(findRecord('user', '1'));
4+
const user = result.content.data
Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,45 @@
11
// Bring your own builder
2-
import { findRecord as edFindRecord } from '@ember-data/json-api/request';
2+
import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils'
3+
import { pluralize } from 'ember-inflector';
34

4-
async function findRecord(typeOrIdentifier, id, options) {
5-
const result = await edFindRecord(typeOrIdentifier, id, options);
5+
async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) {
6+
const identifier = typeof typeOrIdentifier === 'string' ? { type: typeOrIdentifier, id } : typeOrIdentifier;
7+
const options = ((typeof typeOrIdentifier === 'string' ? maybeOptions : idOrOptions) || {});
8+
9+
const urlOptions = {
10+
op: 'findRecord',
11+
identifier,
12+
resourcePath: pluralize(identifier.type),
13+
};
14+
15+
const url = buildBaseURL(urlOptions);
16+
const headers = new Headers();
17+
headers.append('Accept', 'application/vnd.api+json');
18+
headers.append('Content-Type', 'application/vnd.api+json');
19+
20+
return {
21+
url: options.include?.length
22+
? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}`
23+
: url,
24+
method: 'GET',
25+
headers,
26+
op: 'findRecord',
27+
records: [identifier],
28+
};
629

7-
return result.content.data;
830
}
931

1032
export default {
1133
findRecord
1234
};
1335

1436
// Somewhere in app
15-
const user = await this.store.findRecord('user', '1');
37+
const fetchOptions = findRecord('user', '1', { include: 'friends' });
38+
const result = await this.store.request(fetchOptions)
39+
const user = result.content.data
40+
// or using identifier for findRecord builder
41+
const fetchOptions = findRecord({ type: 'user', id: '1' }, { include: 'friends' });
42+
const result = await this.store.request(fetchOptions)
43+
const user = result.content.data
1644

1745

0 commit comments

Comments
 (0)