Skip to content

Commit

Permalink
Update findRecord with type declarations and own builders
Browse files Browse the repository at this point in the history
  • Loading branch information
Baltazore committed May 10, 2024
1 parent 91ed09a commit 7fe2107
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions app/routes/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions snippets/fetching-data/find-record/new.ts
Original file line number Diff line number Diff line change
@@ -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>('user', '1'));
const user = content.data
3 changes: 3 additions & 0 deletions snippets/fetching-data/find-record/old.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type User from "my-app/models/user";

const user = await this.store.findRecord<User>('user', '1');
Original file line number Diff line number Diff line change
@@ -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<Type> = {
url: string
method: 'GET'
headers: Headers
op: 'findRecord'
records: Array<{ type: TypeFromInstance<Type>, 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>(type: TypeFromInstance<Type>, id: string, options: FindRecordOptions<Type>): MyRequest<Type> {
const identifier = { type, id };

const urlOptions = {
op: 'findRecord',
Expand All @@ -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,
Expand All @@ -27,19 +38,11 @@ async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) {
records: [identifier],
};

return result as MyRequest<Type>;
}

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


1 change: 1 addition & 0 deletions translations/fetching-data/find-all/en-us.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
title: findAll
description: |
There is no direct replacement for <code>findAll</code>, you can use <code>query</code> without extra options instead. Here is how to achieve exact <code>findAll</code> behavior:
We discourage using <code>peekAll</code>. When you made a request, you need to guarantee that everything you requested is part of response you get back.
2 changes: 1 addition & 1 deletion translations/serializers/general/en-us.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: Serializers in general
description: |
In order to provide migration support for Adapters and Serializers, a '<code>'LegacyNetworkHandler'</code>' 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 '<code>'next'</code>'. 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 '<code>'LegacyNetworkHandler'</code>' 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 '<code>'next'</code>'. In this manner an app can incrementally migrate request-handling to this new paradigm on a per-type basis as desired.
<br />
The package '<code>'ember-data'</code>' automatically configures this handler. If not using the '<code>'ember-data'</code>' package, this configuration needs to be done explicitly.
<br />
Expand Down

0 comments on commit 7fe2107

Please sign in to comment.