|
1 | 1 | // 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'; |
3 | 4 |
|
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 | + }; |
6 | 29 |
|
7 |
| - return result.content.data; |
8 | 30 | }
|
9 | 31 |
|
10 | 32 | export default {
|
11 | 33 | findRecord
|
12 | 34 | };
|
13 | 35 |
|
14 | 36 | // 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 |
16 | 44 |
|
17 | 45 |
|
0 commit comments