From efcfecd11e561237eb12a83197300daaf6af49f0 Mon Sep 17 00:00:00 2001 From: Kirill Shaplyko Date: Mon, 16 Oct 2023 13:42:36 +0200 Subject: [PATCH] Fix review comments from Chris --- snippets/fetching-data/find-all/new.js | 4 +- snippets/fetching-data/find-record/new.js | 3 +- .../fetching-data/find-record/own-builder.js | 38 ++++++++++++++++--- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/snippets/fetching-data/find-all/new.js b/snippets/fetching-data/find-all/new.js index eb85526..3486b8d 100644 --- a/snippets/fetching-data/find-all/new.js +++ b/snippets/fetching-data/find-all/new.js @@ -1,4 +1,4 @@ import { query } from '@ember-data/json-api/request'; -const result = await store.request(query('user')); -const users = result.content.data; +await this.store.request(query('user')); +const users = this.store.peekAll('user') diff --git a/snippets/fetching-data/find-record/new.js b/snippets/fetching-data/find-record/new.js index 35125d5..f0fb988 100644 --- a/snippets/fetching-data/find-record/new.js +++ b/snippets/fetching-data/find-record/new.js @@ -1,3 +1,4 @@ import { findRecord } from '@ember-data/json-api/request'; -const { content: { data: user } } = await this.store.request(findRecord('user', '1')); +const result = await this.store.request(findRecord('user', '1')); +const user = result.content.data diff --git a/snippets/fetching-data/find-record/own-builder.js b/snippets/fetching-data/find-record/own-builder.js index e23da3a..83094cc 100644 --- a/snippets/fetching-data/find-record/own-builder.js +++ b/snippets/fetching-data/find-record/own-builder.js @@ -1,10 +1,32 @@ // Bring your own builder -import { findRecord as edFindRecord } from '@ember-data/json-api/request'; +import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils' +import { pluralize } from 'ember-inflector'; -async function findRecord(typeOrIdentifier, id, options) { - const result = await edFindRecord(typeOrIdentifier, id, options); +async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) { + const identifier = typeof typeOrIdentifier === 'string' ? { type: typeOrIdentifier, id } : typeOrIdentifier; + const options = ((typeof typeOrIdentifier === 'string' ? maybeOptions : idOrOptions) || {}); + + const urlOptions = { + op: 'findRecord', + identifier, + resourcePath: pluralize(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 result.content.data; } export default { @@ -12,6 +34,12 @@ export default { }; // Somewhere in app -const user = await this.store.findRecord('user', '1'); +const fetchOptions = findRecord('user', '1', { include: 'friends' }); +const result = await this.store.request(fetchOptions) +const user = result.content.data +// or using identifier for findRecord builder +const fetchOptions = findRecord({ type: 'user', id: '1' }, { include: 'friends' }); +const result = await this.store.request(fetchOptions) +const user = result.content.data