Skip to content

feat: Add schema.Collection #2593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/big-horses-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rest-hooks/rest': minor
---

Added RestEndpoint.paginated('cursor').
Place the name of the pagination field instead of writing a function
5 changes: 5 additions & 0 deletions .changeset/cuddly-horses-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rest-hooks/normalizr': major
---

normalize/denormalize both take args array
7 changes: 7 additions & 0 deletions .changeset/fluffy-students-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rest-hooks/normalizr': minor
'@rest-hooks/react': minor
'@rest-hooks/core': minor
---

Support Collections
5 changes: 5 additions & 0 deletions .changeset/giant-teachers-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rest-hooks/rest': minor
---

Add RestEndpoint collections extenders: push/unshift/assign/insert
10 changes: 10 additions & 0 deletions .changeset/grumpy-walls-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@rest-hooks/normalizr': minor
'@rest-hooks/core': minor
'@rest-hooks/react': minor
'@rest-hooks/endpoint': minor
'@rest-hooks/rest': minor
'rest-hooks/legacy': minor
---

Support using args[] during normalization/denormalization
7 changes: 7 additions & 0 deletions .changeset/khaki-keys-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rest-hooks/normalizr': minor
'@rest-hooks/endpoint': minor
'@rest-hooks/rest': minor
---

Entity.pk() has a fourth argument: args[]
5 changes: 5 additions & 0 deletions .changeset/khaki-toys-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rest-hooks/rest': minor
---

Add next/createResource: Uses Collections and has more options from RestEndpoint
5 changes: 5 additions & 0 deletions .changeset/tender-cooks-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rest-hooks/test': patch
---

enhance: Updated warning example with modern syntax
6 changes: 6 additions & 0 deletions .changeset/unlucky-rockets-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rest-hooks/endpoint': minor
'@rest-hooks/rest': minor
---

Add Collections - entities but for Arrays or Values
5 changes: 5 additions & 0 deletions .changeset/violet-wombats-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rest-hooks/test': patch
---

enhance: renderRestHook interceptors must be defined to warn about unmatched fetch
8 changes: 8 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
"endsPattern": "compiled successfully"
}
}
},
{
"type": "npm",
"script": "build",
"group": "build",
"problemMatcher": [],
"label": "npm: build",
"detail": "yarn build:types && yarn workspaces foreach -ptiv --no-private run build"
}
]
}
5 changes: 2 additions & 3 deletions __tests__/new.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React, { createContext, useContext } from 'react';

import { SimpleRecord } from '@rest-hooks/legacy';
import {
AbstractInstanceType,
Expand All @@ -17,6 +15,7 @@ import {
RestType,
MutateEndpoint,
} from '@rest-hooks/rest';
import React, { createContext, useContext } from 'react';

/** Represents data with primary key being from 'id' field. */
export class IDEntity extends Entity {
Expand Down Expand Up @@ -604,7 +603,7 @@ export class SecondUnion extends UnionBase {
readonly secondeOnlyField: number = 10;
}

const UnionSchema = new schema.Union(
export const UnionSchema = new schema.Union(
{
first: FirstUnion,
second: SecondUnion,
Expand Down
5 changes: 4 additions & 1 deletion docs/rest/guides/summary-list.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ delay: 150,
},
]}>

```typescript title="api/Article.ts" {9}
```typescript title="api/Article.ts" {11,24}
import { validateRequired } from '@rest-hooks/rest';

class ArticleSummary extends Entity {
readonly id: string = '';
readonly title: string = '';
Expand Down Expand Up @@ -170,6 +172,7 @@ class Article extends ArticleSummary {

static validate(processedEntity) {
return (
// highlight-next-line
validateRequired(processedEntity, this.defaults) ||
super.validate(processedEntity)
);
Expand Down
6 changes: 6 additions & 0 deletions examples/benchmark/normalizr.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ export default function addNormlizrSuite(suite) {
entities,
denormCache.entities,
denormCache.results['/fake'],
[],
);
denormalizeCached(
queryState.result,
ProjectQuery,
queryState.entities,
denormCache.entities,
denormCache.results['/fakeQuery'],
[],
);
%OptimizeFunctionOnNextCall(denormalizeCached);
%OptimizeFunctionOnNextCall(normalize);
Expand All @@ -73,6 +75,7 @@ export default function addNormlizrSuite(suite) {
return normalize(
data,
ProjectSchema,
[],
initialState.entities,
initialState.indexes,
initialState.entityMeta,
Expand Down Expand Up @@ -113,6 +116,7 @@ export default function addNormlizrSuite(suite) {
entities,
denormCache.entities,
denormCache.results['/fake'],
[],
);
})
.add('denormalizeLong All withCache', () => {
Expand All @@ -122,6 +126,7 @@ export default function addNormlizrSuite(suite) {
queryState.entities,
denormCache.entities,
denormCache.results['/fakeQuery'],
[],
);
})
.add('denormalizeLong Query-sorted withCache', () => {
Expand All @@ -131,6 +136,7 @@ export default function addNormlizrSuite(suite) {
queryState.entities,
denormCache.entities,
denormCache.results['/fakeQuery'],
[],
);
})
.on('complete', function () {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/controller/BaseController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ export default class Controller<
state.entities,
this.globalCache.entities,
this.globalCache.results[key],
args,
) as { data: DenormalizeNullable<E['schema']>; paths: Path[] };
const invalidDenormalize = typeof data === 'symbol';

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/next/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export default class Controller<

if (endpoint.schema) {
return action.meta.promise.then(input =>
denormalize(input, endpoint.schema, {}),
denormalize(input, endpoint.schema, {}, args),
) as any;
}
return action.meta.promise as any;
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/state/reducer/setReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export function setReducer(
const { result, entities, indexes, entityMeta } = normalize(
payload,
action.meta.schema,
action.meta.args as any,
state.entities,
state.indexes,
state.entityMeta,
Expand Down
10 changes: 8 additions & 2 deletions packages/endpoint/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ export interface SchemaSimple<T = any> {
visit: (...args: any) => any,
addEntity: (...args: any) => any,
visitedEntities: Record<string, any>,
storeEntities: any,
args: any[],
): any;
denormalize(
// eslint-disable-next-line @typescript-eslint/ban-types
input: {},
unvisit: UnvisitFunction,
): [denormalized: T, found: boolean, suspend: boolean];
denormalizeOnly?(input: {}, unvisit: (input: any, schema: any) => any): T;
denormalizeOnly?(
input: {},
args: any,
unvisit: (input: any, schema: any) => any,
): T;
infer(
args: readonly any[],
indexes: NormalizedIndex,
Expand All @@ -50,7 +56,7 @@ export interface SchemaClass<T = any, N = T | undefined>

export interface EntityInterface<T = any> extends SchemaSimple {
createIfValid?(props: any): any;
pk(params: any, parent?: any, key?: string): string | undefined;
pk(params: any, parent?: any, key?: string, args?: any[]): string | undefined;
readonly key: string;
merge(existing: any, incoming: any): any;
expiresAt?(meta: any, input: any): number;
Expand Down
8 changes: 4 additions & 4 deletions packages/endpoint/src/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type Denormalize<S> = S extends EntityInterface<infer U>
? AbstractInstanceType<S>
: S extends { denormalizeOnly: (...args: any) => any }
? ReturnType<S['denormalizeOnly']>
: S extends SchemaClass
: S extends { denormalize: (...args: any) => any }
? DenormalizeReturnType<S['denormalize']>
: S extends Serializable<infer T>
? T
Expand All @@ -84,7 +84,7 @@ export type DenormalizeNullable<S> = S extends EntityInterface<any>
? DenormalizeNullableNestedSchema<S> | undefined
: S extends RecordClass
? DenormalizeNullableNestedSchema<S>
: S extends SchemaClass
: S extends { _denormalizeNullable: (...args: any) => any }
? DenormalizeReturnType<S['_denormalizeNullable']>
: S extends Serializable<infer T>
? T
Expand All @@ -98,7 +98,7 @@ export type Normalize<S> = S extends EntityInterface
? string
: S extends RecordClass
? NormalizeObject<S['schema']>
: S extends SchemaClass
: S extends { normalize: (...args: any) => any }
? NormalizeReturnType<S['normalize']>
: S extends Serializable<infer T>
? T
Expand All @@ -112,7 +112,7 @@ export type NormalizeNullable<S> = S extends EntityInterface
? string | undefined
: S extends RecordClass
? NormalizedNullableObject<S['schema']>
: S extends SchemaClass
: S extends { _normalizeNullable: (...args: any) => any }
? NormalizeReturnType<S['_normalizeNullable']>
: S extends Serializable<infer T>
? T
Expand Down
9 changes: 7 additions & 2 deletions packages/endpoint/src/queryEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ export class Query<
if (schema.denormalizeOnly)
query.denormalizeOnly = (
{ args, input }: { args: P; input: any },
_: P,
unvisit: any,
) => {
if (input === undefined) return undefined;
const value = (schema as any).denormalizeOnly(input, unvisit);
const value = (schema as any).denormalizeOnly(input, args, unvisit);
return typeof value === 'symbol'
? undefined
: this.process(value, ...args);
Expand Down Expand Up @@ -83,5 +84,9 @@ type QuerySchema<Schema, R> = Exclude<
input: {},
unvisit: UnvisitFunction,
): [denormalized: R | undefined, found: boolean, suspend: boolean];
denormalizeOnly(input: {}, unvisit: (input: any, schema: any) => any): R;
denormalizeOnly(
input: {},
args: readonly any[],
unvisit: (input: any, schema: any) => any,
): R;
};
Loading