Skip to content

Commit 2146759

Browse files
committed
feat: Add schema.Collection
fix: changes to
1 parent 950a982 commit 2146759

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2305
-226
lines changed

.vscode/tasks.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@
4646
"endsPattern": "compiled successfully"
4747
}
4848
}
49+
},
50+
{
51+
"type": "npm",
52+
"script": "build",
53+
"group": "build",
54+
"problemMatcher": [],
55+
"label": "npm: build",
56+
"detail": "yarn build:types && yarn workspaces foreach -ptiv --no-private run build"
4957
}
5058
]
5159
}

__tests__/new.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import React, { createContext, useContext } from 'react';
2-
31
import { SimpleRecord } from '@rest-hooks/legacy';
42
import {
53
AbstractInstanceType,
@@ -17,6 +15,7 @@ import {
1715
RestType,
1816
MutateEndpoint,
1917
} from '@rest-hooks/rest';
18+
import React, { createContext, useContext } from 'react';
2019

2120
/** Represents data with primary key being from 'id' field. */
2221
export class IDEntity extends Entity {

docs/rest/guides/summary-list.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ delay: 150,
4545
},
4646
]}>
4747

48-
```typescript title="api/Article.ts" {9}
48+
```typescript title="api/Article.ts" {11,24}
49+
import { validateRequired } from '@rest-hooks/rest';
50+
4951
class ArticleSummary extends Entity {
5052
readonly id: string = '';
5153
readonly title: string = '';
@@ -170,6 +172,7 @@ class Article extends ArticleSummary {
170172

171173
static validate(processedEntity) {
172174
return (
175+
// highlight-next-line
173176
validateRequired(processedEntity, this.defaults) ||
174177
super.validate(processedEntity)
175178
);

examples/benchmark/normalizr.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ export default function addNormlizrSuite(suite) {
5757
entities,
5858
denormCache.entities,
5959
denormCache.results['/fake'],
60+
[],
6061
);
6162
denormalizeCached(
6263
queryState.result,
6364
ProjectQuery,
6465
queryState.entities,
6566
denormCache.entities,
6667
denormCache.results['/fakeQuery'],
68+
[],
6769
);
6870
%OptimizeFunctionOnNextCall(denormalizeCached);
6971
%OptimizeFunctionOnNextCall(normalize);
@@ -73,6 +75,7 @@ export default function addNormlizrSuite(suite) {
7375
return normalize(
7476
data,
7577
ProjectSchema,
78+
[],
7679
initialState.entities,
7780
initialState.indexes,
7881
initialState.entityMeta,
@@ -113,6 +116,7 @@ export default function addNormlizrSuite(suite) {
113116
entities,
114117
denormCache.entities,
115118
denormCache.results['/fake'],
119+
[],
116120
);
117121
})
118122
.add('denormalizeLong All withCache', () => {
@@ -122,6 +126,7 @@ export default function addNormlizrSuite(suite) {
122126
queryState.entities,
123127
denormCache.entities,
124128
denormCache.results['/fakeQuery'],
129+
[],
125130
);
126131
})
127132
.add('denormalizeLong Query-sorted withCache', () => {
@@ -131,6 +136,7 @@ export default function addNormlizrSuite(suite) {
131136
queryState.entities,
132137
denormCache.entities,
133138
denormCache.results['/fakeQuery'],
139+
[],
134140
);
135141
})
136142
.on('complete', function () {

packages/core/src/controller/BaseController.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ export default class Controller<
401401
state.entities,
402402
this.globalCache.entities,
403403
this.globalCache.results[key],
404+
args,
404405
) as { data: DenormalizeNullable<E['schema']>; paths: Path[] };
405406
const invalidDenormalize = typeof data === 'symbol';
406407

packages/core/src/next/Controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default class Controller<
3030

3131
if (endpoint.schema) {
3232
return action.meta.promise.then(input =>
33-
denormalize(input, endpoint.schema, {}),
33+
denormalize(input, endpoint.schema, {}, args),
3434
) as any;
3535
}
3636
return action.meta.promise as any;

packages/core/src/state/reducer/setReducer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export function setReducer(
4040
const { result, entities, indexes, entityMeta } = normalize(
4141
payload,
4242
action.meta.schema,
43+
action.meta.args as any,
4344
state.entities,
4445
state.indexes,
4546
state.entityMeta,

packages/endpoint/src/interface.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ export interface SchemaSimple<T = any> {
2525
visit: (...args: any) => any,
2626
addEntity: (...args: any) => any,
2727
visitedEntities: Record<string, any>,
28+
storeEntities: any,
29+
args: any[],
2830
): any;
2931
denormalize(
3032
// eslint-disable-next-line @typescript-eslint/ban-types
3133
input: {},
3234
unvisit: UnvisitFunction,
3335
): [denormalized: T, found: boolean, suspend: boolean];
34-
denormalizeOnly?(input: {}, unvisit: (input: any, schema: any) => any): T;
36+
denormalizeOnly?(
37+
input: {},
38+
args: any,
39+
unvisit: (input: any, schema: any) => any,
40+
): T;
3541
infer(
3642
args: readonly any[],
3743
indexes: NormalizedIndex,
@@ -50,7 +56,7 @@ export interface SchemaClass<T = any, N = T | undefined>
5056

5157
export interface EntityInterface<T = any> extends SchemaSimple {
5258
createIfValid?(props: any): any;
53-
pk(params: any, parent?: any, key?: string): string | undefined;
59+
pk(params: any, parent?: any, key?: string, args?: any[]): string | undefined;
5460
readonly key: string;
5561
merge(existing: any, incoming: any): any;
5662
expiresAt?(meta: any, input: any): number;

packages/endpoint/src/normal.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export type Denormalize<S> = S extends EntityInterface<infer U>
7070
? AbstractInstanceType<S>
7171
: S extends { denormalizeOnly: (...args: any) => any }
7272
? ReturnType<S['denormalizeOnly']>
73-
: S extends SchemaClass
73+
: S extends { denormalize: (...args: any) => any }
7474
? DenormalizeReturnType<S['denormalize']>
7575
: S extends Serializable<infer T>
7676
? T
@@ -84,7 +84,7 @@ export type DenormalizeNullable<S> = S extends EntityInterface<any>
8484
? DenormalizeNullableNestedSchema<S> | undefined
8585
: S extends RecordClass
8686
? DenormalizeNullableNestedSchema<S>
87-
: S extends SchemaClass
87+
: S extends { _denormalizeNullable: (...args: any) => any }
8888
? DenormalizeReturnType<S['_denormalizeNullable']>
8989
: S extends Serializable<infer T>
9090
? T
@@ -98,7 +98,7 @@ export type Normalize<S> = S extends EntityInterface
9898
? string
9999
: S extends RecordClass
100100
? NormalizeObject<S['schema']>
101-
: S extends SchemaClass
101+
: S extends { normalize: (...args: any) => any }
102102
? NormalizeReturnType<S['normalize']>
103103
: S extends Serializable<infer T>
104104
? T
@@ -112,7 +112,7 @@ export type NormalizeNullable<S> = S extends EntityInterface
112112
? string | undefined
113113
: S extends RecordClass
114114
? NormalizedNullableObject<S['schema']>
115-
: S extends SchemaClass
115+
: S extends { _normalizeNullable: (...args: any) => any }
116116
? NormalizeReturnType<S['_normalizeNullable']>
117117
: S extends Serializable<infer T>
118118
? T

packages/endpoint/src/queryEndpoint.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ export class Query<
4646
if (schema.denormalizeOnly)
4747
query.denormalizeOnly = (
4848
{ args, input }: { args: P; input: any },
49+
_: P,
4950
unvisit: any,
5051
) => {
5152
if (input === undefined) return undefined;
52-
const value = (schema as any).denormalizeOnly(input, unvisit);
53+
const value = (schema as any).denormalizeOnly(input, args, unvisit);
5354
return typeof value === 'symbol'
5455
? undefined
5556
: this.process(value, ...args);
@@ -83,5 +84,9 @@ type QuerySchema<Schema, R> = Exclude<
8384
input: {},
8485
unvisit: UnvisitFunction,
8586
): [denormalized: R | undefined, found: boolean, suspend: boolean];
86-
denormalizeOnly(input: {}, unvisit: (input: any, schema: any) => any): R;
87+
denormalizeOnly(
88+
input: {},
89+
args: readonly any[],
90+
unvisit: (input: any, schema: any) => any,
91+
): R;
8792
};

0 commit comments

Comments
 (0)