Skip to content

Commit 51f219d

Browse files
committed
Update findRecord with type declarations and own builders
1 parent aa605bb commit 51f219d

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

app/routes/application.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export default class ApplicationRoute extends Route {
88
subsections: [
99
{
1010
id: 'find-record',
11-
classicFiles: ['old.js'],
12-
octaneFiles: ['new.js', 'replicate-store.js', 'own-builder.js'],
11+
classicFiles: ['old.js', 'old.ts'],
12+
octaneFiles: ['new.js', 'new.ts', 'own-builder.ts'],
1313
},
1414
{
1515
id: 'find-all',
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { findRecord } from '@ember-data/json-api/request';
2+
import type { User } from 'my-app/models/user';
3+
4+
const { content } = await store.request(findRecord<User>('user', '1'));
5+
const user = content.data
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import type User from "my-app/models/user";
2+
3+
const user = await this.store.findRecord<User>('user', '1');
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
// Bring your own builder
22
import { buildBaseURL, buildQueryParams } from '@ember-data/request-utils'
33
import { pluralize } from 'ember-inflector';
4+
import type { RequestSignature } from '@warp-drive/core-types/symbols';
5+
import type { TypeFromInstance } from '@warp-drive/core-types/record';
6+
import type { FindRecordOptions } from '@warp-drive/core-types/request';
7+
8+
type MyRequest<Type> = {
9+
url: string
10+
method: 'GET'
11+
headers: Headers
12+
op: 'findRecord'
13+
records: Array<{ type: TypeFromInstance<Type>, id: string }>;
14+
[RequestSignature]: Type
15+
}
416

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) || {});
17+
function findRecord<Type>(type: TypeFromInstance<Type>, id: string, options: FindRecordOptions<Type>): MyRequest<Type> {
18+
const identifier = { type, id };
819

920
const urlOptions = {
1021
op: 'findRecord',
@@ -17,7 +28,7 @@ async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) {
1728
headers.append('Accept', 'application/vnd.api+json');
1829
headers.append('Content-Type', 'application/vnd.api+json');
1930

20-
return {
31+
const result = {
2132
url: options.include?.length
2233
? `${url}?${buildQueryParams({ include: options.include }, options.urlParamsSettings)}`
2334
: url,
@@ -27,19 +38,11 @@ async function findRecord(typeOrIdentifier, idOrOptions, maybeOptions) {
2738
records: [identifier],
2839
};
2940

41+
return result as MyRequest<Type>;
3042
}
3143

3244
export default {
3345
findRecord
3446
};
3547

36-
// Somewhere in app
37-
const fetchOptions = findRecord('user', '1', { include: 'friends' });
38-
const result = await 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 store.request(fetchOptions)
43-
const user = result.content.data
44-
4548

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
title: findAll
22
description: |
33
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:
4+
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.

translations/serializers/general/en-us.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
title: Serializers in general
22
description: |
3-
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.
3+
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.
44
<br />
55
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.
66
<br />

0 commit comments

Comments
 (0)