From 7fe2107ce98cfc9434a1889530f00c632fa32721 Mon Sep 17 00:00:00 2001 From: Kirill Shaplyko Date: Fri, 10 May 2024 13:01:21 +0200 Subject: [PATCH] Update findRecord with type declarations and own builders --- app/routes/application.js | 4 +-- snippets/fetching-data/find-record/new.ts | 5 ++++ snippets/fetching-data/find-record/old.ts | 3 ++ .../{own-builder.js => own-builder.ts} | 29 ++++++++++--------- .../fetching-data/find-all/en-us.yaml | 1 + translations/serializers/general/en-us.yaml | 2 +- 6 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 snippets/fetching-data/find-record/new.ts create mode 100644 snippets/fetching-data/find-record/old.ts rename snippets/fetching-data/find-record/{own-builder.js => own-builder.ts} (51%) diff --git a/app/routes/application.js b/app/routes/application.js index 6c602f4..4b9f63f 100644 --- a/app/routes/application.js +++ b/app/routes/application.js @@ -8,8 +8,8 @@ export default class ApplicationRoute extends Route { subsections: [ { id: 'find-record', - classicFiles: ['old.js'], - octaneFiles: ['new.js', 'replicate-store.js', 'own-builder.js'], + classicFiles: ['old.js', 'old.ts'], + octaneFiles: ['new.js', 'new.ts', 'own-builder.ts'], }, { id: 'find-all', diff --git a/snippets/fetching-data/find-record/new.ts b/snippets/fetching-data/find-record/new.ts new file mode 100644 index 0000000..f3492a5 --- /dev/null +++ b/snippets/fetching-data/find-record/new.ts @@ -0,0 +1,5 @@ +import { findRecord } from '@ember-data/json-api/request'; +import type { User } from 'my-app/models/user'; + +const { content } = await store.request(findRecord('user', '1')); +const user = content.data diff --git a/snippets/fetching-data/find-record/old.ts b/snippets/fetching-data/find-record/old.ts new file mode 100644 index 0000000..45a6ce3 --- /dev/null +++ b/snippets/fetching-data/find-record/old.ts @@ -0,0 +1,3 @@ +import type User from "my-app/models/user"; + +const user = await this.store.findRecord('user', '1'); diff --git a/snippets/fetching-data/find-record/own-builder.js b/snippets/fetching-data/find-record/own-builder.ts similarity index 51% rename from snippets/fetching-data/find-record/own-builder.js rename to snippets/fetching-data/find-record/own-builder.ts index 8799d28..bae77f1 100644 --- a/snippets/fetching-data/find-record/own-builder.js +++ b/snippets/fetching-data/find-record/own-builder.ts @@ -1,10 +1,21 @@ // Bring your own builder import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils' import { pluralize } from 'ember-inflector'; +import type { RequestSignature } from '@warp-drive/core-types/symbols'; +import type { TypeFromInstance } from '@warp-drive/core-types/record'; +import type { FindRecordOptions } from '@warp-drive/core-types/request'; + +type MyRequest = { + url: string + method: 'GET' + headers: Headers + op: 'findRecord' + records: Array<{ type: TypeFromInstance, id: string }>; + [RequestSignature]: Type +} -async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) { - const identifier = typeof typeOrIdentifier === 'string' ? { type: typeOrIdentifier, id } : typeOrIdentifier; - const options = ((typeof typeOrIdentifier === 'string' ? maybeOptions : idOrOptions) || {}); +function findRecord(type: TypeFromInstance, id: string, options: FindRecordOptions): MyRequest { + const identifier = { type, id }; const urlOptions = { op: 'findRecord', @@ -17,7 +28,7 @@ async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) { headers.append('Accept', 'application/vnd.api+json'); headers.append('Content-Type', 'application/vnd.api+json'); - return { + const result = { url: options.include?.length ? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}` : url, @@ -27,19 +38,11 @@ async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) { records: [identifier], }; + return result as MyRequest; } export default { findRecord }; -// Somewhere in app -const fetchOptions = findRecord('user', '1', { include: 'friends' }); -const result = await 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 store.request(fetchOptions) -const user = result.content.data - diff --git a/translations/fetching-data/find-all/en-us.yaml b/translations/fetching-data/find-all/en-us.yaml index 749738e..f47481b 100644 --- a/translations/fetching-data/find-all/en-us.yaml +++ b/translations/fetching-data/find-all/en-us.yaml @@ -1,3 +1,4 @@ title: findAll description: | There is no direct replacement for findAll, you can use query without extra options instead. Here is how to achieve exact findAll behavior: + We discourage using peekAll. When you made a request, you need to guarantee that everything you requested is part of response you get back. diff --git a/translations/serializers/general/en-us.yaml b/translations/serializers/general/en-us.yaml index 5fb8ef9..36a7f4b 100644 --- a/translations/serializers/general/en-us.yaml +++ b/translations/serializers/general/en-us.yaml @@ -1,6 +1,6 @@ title: Serializers in general description: | - In order to provide migration support for Adapters and Serializers, a ''LegacyNetworkHandler'' is provided. This handler takes a request and converts it into the older form, calling the appropriate Adapter and Serializer methods. If no serializer exists for the type (including no application serializer), this handler calls ''next''. In this manner an app can incrementally migrate request-handling to this new paradigm on a per-type basis as desired. + In order to provide migration support for Adapters and Serializers, a ''LegacyNetworkHandler'' is provided. This handler takes a request and converts it into the older form, calling the appropriate Adapter and Serializer methods. If no adapter exists for the type (including no application adapter), this handler calls ''next''. In this manner an app can incrementally migrate request-handling to this new paradigm on a per-type basis as desired.
The package ''ember-data'' automatically configures this handler. If not using the ''ember-data'' package, this configuration needs to be done explicitly.