diff --git a/packages/normalizr/src/index.ts b/packages/normalizr/src/index.ts index 82db078c51f8..e703c97e33e2 100644 --- a/packages/normalizr/src/index.ts +++ b/packages/normalizr/src/index.ts @@ -1,7 +1,6 @@ import { denormalize } from './denormalize'; import { normalize } from './normalize'; import * as schema from './schema'; -export type { EntitySchema } from './entities/Entity'; import Entity, { isEntity } from './entities/Entity'; import SimpleRecord from './entities/SimpleRecord'; export { default as FlatEntity } from './entities/FlatEntity'; diff --git a/packages/normalizr/src/schema.d.ts b/packages/normalizr/src/schema.d.ts index 0477bfac4efc..a54d93923d4d 100644 --- a/packages/normalizr/src/schema.d.ts +++ b/packages/normalizr/src/schema.d.ts @@ -1,5 +1,5 @@ import { Schema, AbstractInstanceType } from './types'; -import { EntitySchema } from './entities/Entity'; +import Entity from './entities/Entity'; export type StrategyFunction = (value: any, parent: any, key: string) => T; export type SchemaFunction = ( @@ -13,7 +13,7 @@ export type SchemaAttributeFunction = ( parent: any, key: string, ) => S; -export type EntityMap = Record>; +export type EntityMap = Record>; export type UnvisitFunction = (input: any, schema: any) => [any, boolean]; export type UnionResult = { id: string; @@ -42,6 +42,7 @@ export interface SchemaClass extends SchemaSimple { interface EntityInterface extends SchemaSimple { pk(params: any, parent?: any, key?: string): string | undefined; readonly key: string; + prototype: T; normalize( input: any, parent: any, diff --git a/packages/normalizr/src/types.ts b/packages/normalizr/src/types.ts index be0fb4de5267..292a236bcdd2 100644 --- a/packages/normalizr/src/types.ts +++ b/packages/normalizr/src/types.ts @@ -39,8 +39,8 @@ export type NormalizeReturnType = T extends (...args: any) => infer R : never; export type Denormalize = S extends { - pk(): string; - fromJS: Function; + pk(params: any, parent?: any, key?: string): string | undefined; + readonly key: string; prototype: infer U; } ? U @@ -54,8 +54,12 @@ export type Denormalize = S extends { ? DenormalizeObject : S; -export type DenormalizeNullable = S extends typeof Entity - ? AbstractInstanceType | undefined +export type DenormalizeNullable = S extends { + pk(params: any, parent?: any, key?: string): string | undefined; + readonly key: string; + prototype: infer U; +} + ? U | undefined : S extends { schema: Record; fromJS: Function } ? AbstractInstanceType : S extends schema.SchemaClass @@ -67,10 +71,11 @@ export type DenormalizeNullable = S extends typeof Entity : S; export type Normalize = S extends { - pk(): string; - fromJS: Function; + pk(params: any, parent?: any, key?: string): string | undefined; + readonly key: string; + normalize: (...args: any) => infer R; } - ? string + ? R : S extends { schema: Record; fromJS: Function } ? NormalizeObject : S extends schema.SchemaClass @@ -82,10 +87,11 @@ export type Normalize = S extends { : S; export type NormalizeNullable = S extends { - pk(): string; - fromJS: Function; + pk(params: any, parent?: any, key?: string): string | undefined; + readonly key: string; + normalize: (...args: any) => infer R; } - ? string | undefined + ? R | undefined : S extends { schema: Record; fromJS: Function } ? NormalizedNullableObject : S extends schema.SchemaClass diff --git a/packages/normalizr/typescript-tests/object.ts b/packages/normalizr/typescript-tests/object.ts index 17b78670b070..7233daa2c9a6 100644 --- a/packages/normalizr/typescript-tests/object.ts +++ b/packages/normalizr/typescript-tests/object.ts @@ -7,9 +7,9 @@ const data = { class User extends IDEntity {} const responseSchema = new schema.Object({ - users: new schema.Array(User, + users: new schema.Array(User), }); const normalizedData = normalize(data, responseSchema); -const responseSchemaAlt = { users: new schema.Array(User }; +const responseSchemaAlt = { users: new schema.Array(User) }; const normalizedDataAlt = normalize(data, responseSchemaAlt); diff --git a/packages/rest-hooks/src/index.ts b/packages/rest-hooks/src/index.ts index 0522027641c6..1b1df9e51fc2 100644 --- a/packages/rest-hooks/src/index.ts +++ b/packages/rest-hooks/src/index.ts @@ -56,7 +56,6 @@ export type { } from './resource/shapes'; export * from './resource/normal'; export type { SetShapeParams, ParamsFromShape } from './resource/publicTypes'; -export type { EntitySchema } from '@rest-hooks/normalizr'; export { Resource, SimpleResource, diff --git a/packages/rest-hooks/src/state/__tests__/networkManager.ts b/packages/rest-hooks/src/state/__tests__/networkManager.ts index 1aba55ccdaef..2e2211edf249 100644 --- a/packages/rest-hooks/src/state/__tests__/networkManager.ts +++ b/packages/rest-hooks/src/state/__tests__/networkManager.ts @@ -327,7 +327,7 @@ describe('NetworkManager', () => { type: FETCH_TYPE, payload: () => promise, meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: ArticleResource.url({ id: 5 }), type: ArticleResource.detailShape().type, throttle: true, diff --git a/packages/rest-hooks/src/state/__tests__/pollingSubscription.ts b/packages/rest-hooks/src/state/__tests__/pollingSubscription.ts index d1c918a2169c..4932447ff9ef 100644 --- a/packages/rest-hooks/src/state/__tests__/pollingSubscription.ts +++ b/packages/rest-hooks/src/state/__tests__/pollingSubscription.ts @@ -73,7 +73,7 @@ describe('PollingSubscription', () => { const sub = new PollingSubscription( { key: 'test.com', - schema: PollingArticleResource.getEntitySchema(), + schema: PollingArticleResource, fetch, frequency: 5000, }, @@ -90,7 +90,7 @@ describe('PollingSubscription', () => { new PollingSubscription( { key: 'test.com', - schema: PollingArticleResource.getEntitySchema(), + schema: PollingArticleResource, fetch, }, dispatch, @@ -193,7 +193,7 @@ describe('PollingSubscription', () => { const sub = new PollingSubscription( { key: 'test.com', - schema: PollingArticleResource.getEntitySchema(), + schema: PollingArticleResource, fetch, frequency: 5000, }, @@ -230,7 +230,7 @@ describe('PollingSubscription', () => { const pollingSubscription = new PollingSubscription( { key: 'test.com', - schema: PollingArticleResource.getEntitySchema(), + schema: PollingArticleResource, fetch, frequency: 5000, }, diff --git a/packages/rest-hooks/src/state/__tests__/reducer.ts b/packages/rest-hooks/src/state/__tests__/reducer.ts index ee10e852de24..fad3c6f9b3a3 100644 --- a/packages/rest-hooks/src/state/__tests__/reducer.ts +++ b/packages/rest-hooks/src/state/__tests__/reducer.ts @@ -30,7 +30,7 @@ describe('reducer', () => { type: RECEIVE_TYPE, payload, meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: ArticleResource.url({ id }), date: 5000000000, expiresAt: 5000500000, @@ -83,7 +83,7 @@ describe('reducer', () => { type: RECEIVE_TYPE, payload, meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: ArticleResource.listUrl(payload), date: 0, expiresAt: 1000000000000, @@ -101,7 +101,7 @@ describe('reducer', () => { const action: PurgeAction = { type: RECEIVE_DELETE_TYPE, meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: id.toString(), date: 0, }, @@ -226,7 +226,7 @@ describe('reducer', () => { updaters: { [key: string]: UpdateFunction< typeof createShape['schema'], - ReturnType[] + typeof ArticleResource[] >; }, ) { @@ -234,7 +234,7 @@ describe('reducer', () => { type: RECEIVE_TYPE, payload, meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: ArticleResource.createShape().getFetchKey({}), updaters, date: 0, @@ -365,7 +365,7 @@ describe('reducer', () => { type: RECEIVE_TYPE, payload: error, meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: ArticleResource.url({ id }), date: 5000000000, expiresAt: 5000500000, @@ -383,7 +383,7 @@ describe('reducer', () => { type: RECEIVE_TYPE, payload: error, meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: ArticleResource.url({ id }), date: 0, expiresAt: 10000000000000000000, @@ -402,7 +402,7 @@ describe('reducer', () => { type: RECEIVE_DELETE_TYPE, payload: error, meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: ArticleResource.url({ id }), date: 0, }, @@ -428,7 +428,7 @@ describe('reducer', () => { type: FETCH_TYPE, payload: () => new Promise(() => null), meta: { - schema: ArticleResource.getEntitySchema(), + schema: ArticleResource, key: ArticleResource.url({ id: 5 }), type: 'read' as const, throttle: true, diff --git a/packages/rest-hooks/src/state/__tests__/subscriptionManager.ts b/packages/rest-hooks/src/state/__tests__/subscriptionManager.ts index d06913c43beb..c763bef3d265 100644 --- a/packages/rest-hooks/src/state/__tests__/subscriptionManager.ts +++ b/packages/rest-hooks/src/state/__tests__/subscriptionManager.ts @@ -57,7 +57,7 @@ describe('SubscriptionManager', () => { return { type: SUBSCRIBE_TYPE, meta: { - schema: PollingArticleResource.getEntitySchema(), + schema: PollingArticleResource, key: PollingArticleResource.url(payload), fetch, options: { pollFrequency: 1000 }, diff --git a/packages/rest-hooks/src/state/selectors/__tests__/buildInferredResults.ts b/packages/rest-hooks/src/state/selectors/__tests__/buildInferredResults.ts index bf54d981ffa8..3b9c566ea732 100644 --- a/packages/rest-hooks/src/state/selectors/__tests__/buildInferredResults.ts +++ b/packages/rest-hooks/src/state/selectors/__tests__/buildInferredResults.ts @@ -11,7 +11,7 @@ describe('buildInferredResults()', () => { it('should work with Object', () => { const schema = new schemas.Object({ data: new schemas.Object({ - article: CoolerArticleResource.getEntitySchema(), + article: CoolerArticleResource, }), }); expect(buildInferredResults(schema, { id: 5 }, {})).toEqual({ @@ -21,14 +21,14 @@ describe('buildInferredResults()', () => { it('should be undefined with Array', () => { const schema = { - data: new schemas.Array(CoolerArticleResource.getEntitySchema()), + data: new schemas.Array(CoolerArticleResource), }; expect(buildInferredResults(schema, { id: 5 }, {})).toStrictEqual({ data: undefined, }); const schema2 = { - data: [CoolerArticleResource.getEntitySchema()], + data: [CoolerArticleResource], }; expect(buildInferredResults(schema2, { id: 5 }, {})).toStrictEqual({ data: undefined, @@ -37,7 +37,7 @@ describe('buildInferredResults()', () => { it('should be {} with Values', () => { const schema = { - data: new schemas.Values(CoolerArticleResource.getEntitySchema()), + data: new schemas.Values(CoolerArticleResource), }; expect(buildInferredResults(schema, { id: 5 }, {})).toStrictEqual({ data: {}, @@ -63,7 +63,7 @@ describe('buildInferredResults()', () => { it('should work with primitive defaults', () => { const schema = { pagination: { next: '', previous: '' }, - data: CoolerArticleResource.getEntitySchema(), + data: CoolerArticleResource, }; expect(buildInferredResults(schema, { id: 5 }, {})).toEqual({ pagination: { next: '', previous: '' }, diff --git a/packages/rest-hooks/src/state/selectors/__tests__/getEntityPath.ts b/packages/rest-hooks/src/state/selectors/__tests__/getEntityPath.ts index 7735cb37c279..5186a35f19b1 100644 --- a/packages/rest-hooks/src/state/selectors/__tests__/getEntityPath.ts +++ b/packages/rest-hooks/src/state/selectors/__tests__/getEntityPath.ts @@ -4,7 +4,7 @@ import { schemas } from '../../../resource/normal'; import getEntityPath from '../getEntityPath'; describe('getEntityPath()', () => { - const entity = CoolerArticleResource.getEntitySchema(); + const entity = CoolerArticleResource; it('should find base Entity', () => { const path = getEntityPath(entity);