Skip to content

Commit

Permalink
enhance: remove .getEntitySchema()
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed May 11, 2020
1 parent 7e4cb49 commit b4fa17f
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 37 deletions.
1 change: 0 additions & 1 deletion packages/normalizr/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
5 changes: 3 additions & 2 deletions packages/normalizr/src/schema.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Schema, AbstractInstanceType } from './types';
import { EntitySchema } from './entities/Entity';
import Entity from './entities/Entity';

export type StrategyFunction<T> = (value: any, parent: any, key: string) => T;
export type SchemaFunction<K = string> = (
Expand All @@ -13,7 +13,7 @@ export type SchemaAttributeFunction<S extends Schema> = (
parent: any,
key: string,
) => S;
export type EntityMap<T = any> = Record<string, EntitySchema<T>>;
export type EntityMap<T = any> = Record<string, Entity<T>>;
export type UnvisitFunction = (input: any, schema: any) => [any, boolean];
export type UnionResult<Choices extends EntityMap> = {
id: string;
Expand Down Expand Up @@ -42,6 +42,7 @@ export interface SchemaClass extends SchemaSimple {
interface EntityInterface<T = any> extends SchemaSimple {
pk(params: any, parent?: any, key?: string): string | undefined;
readonly key: string;
prototype: T;
normalize(
input: any,
parent: any,
Expand Down
26 changes: 16 additions & 10 deletions packages/normalizr/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export type NormalizeReturnType<T> = T extends (...args: any) => infer R
: never;

export type Denormalize<S> = S extends {
pk(): string;
fromJS: Function;
pk(params: any, parent?: any, key?: string): string | undefined;
readonly key: string;
prototype: infer U;
}
? U
Expand All @@ -54,8 +54,12 @@ export type Denormalize<S> = S extends {
? DenormalizeObject<S>
: S;

export type DenormalizeNullable<S> = S extends typeof Entity
? AbstractInstanceType<S> | undefined
export type DenormalizeNullable<S> = S extends {
pk(params: any, parent?: any, key?: string): string | undefined;
readonly key: string;
prototype: infer U;
}
? U | undefined
: S extends { schema: Record<string, Schema>; fromJS: Function }
? AbstractInstanceType<S>
: S extends schema.SchemaClass
Expand All @@ -67,10 +71,11 @@ export type DenormalizeNullable<S> = S extends typeof Entity
: S;

export type Normalize<S> = 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<string, Schema>; fromJS: Function }
? NormalizeObject<S['schema']>
: S extends schema.SchemaClass
Expand All @@ -82,10 +87,11 @@ export type Normalize<S> = S extends {
: S;

export type NormalizeNullable<S> = 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<string, Schema>; fromJS: Function }
? NormalizedNullableObject<S['schema']>
: S extends schema.SchemaClass
Expand Down
4 changes: 2 additions & 2 deletions packages/normalizr/typescript-tests/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 0 additions & 1 deletion packages/rest-hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/rest-hooks/src/state/__tests__/networkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('PollingSubscription', () => {
const sub = new PollingSubscription(
{
key: 'test.com',
schema: PollingArticleResource.getEntitySchema(),
schema: PollingArticleResource,
fetch,
frequency: 5000,
},
Expand All @@ -90,7 +90,7 @@ describe('PollingSubscription', () => {
new PollingSubscription(
{
key: 'test.com',
schema: PollingArticleResource.getEntitySchema(),
schema: PollingArticleResource,
fetch,
},
dispatch,
Expand Down Expand Up @@ -193,7 +193,7 @@ describe('PollingSubscription', () => {
const sub = new PollingSubscription(
{
key: 'test.com',
schema: PollingArticleResource.getEntitySchema(),
schema: PollingArticleResource,
fetch,
frequency: 5000,
},
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('PollingSubscription', () => {
const pollingSubscription = new PollingSubscription(
{
key: 'test.com',
schema: PollingArticleResource.getEntitySchema(),
schema: PollingArticleResource,
fetch,
frequency: 5000,
},
Expand Down
18 changes: 9 additions & 9 deletions packages/rest-hooks/src/state/__tests__/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('reducer', () => {
type: RECEIVE_TYPE,
payload,
meta: {
schema: ArticleResource.getEntitySchema(),
schema: ArticleResource,
key: ArticleResource.url({ id }),
date: 5000000000,
expiresAt: 5000500000,
Expand Down Expand Up @@ -83,7 +83,7 @@ describe('reducer', () => {
type: RECEIVE_TYPE,
payload,
meta: {
schema: ArticleResource.getEntitySchema(),
schema: ArticleResource,
key: ArticleResource.listUrl(payload),
date: 0,
expiresAt: 1000000000000,
Expand All @@ -101,7 +101,7 @@ describe('reducer', () => {
const action: PurgeAction = {
type: RECEIVE_DELETE_TYPE,
meta: {
schema: ArticleResource.getEntitySchema(),
schema: ArticleResource,
key: id.toString(),
date: 0,
},
Expand Down Expand Up @@ -226,15 +226,15 @@ describe('reducer', () => {
updaters: {
[key: string]: UpdateFunction<
typeof createShape['schema'],
ReturnType<typeof ArticleResource.getEntitySchema>[]
typeof ArticleResource[]
>;
},
) {
return {
type: RECEIVE_TYPE,
payload,
meta: {
schema: ArticleResource.getEntitySchema(),
schema: ArticleResource,
key: ArticleResource.createShape().getFetchKey({}),
updaters,
date: 0,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -402,7 +402,7 @@ describe('reducer', () => {
type: RECEIVE_DELETE_TYPE,
payload: error,
meta: {
schema: ArticleResource.getEntitySchema(),
schema: ArticleResource,
key: ArticleResource.url({ id }),
date: 0,
},
Expand All @@ -428,7 +428,7 @@ describe('reducer', () => {
type: FETCH_TYPE,
payload: () => new Promise<any>(() => null),
meta: {
schema: ArticleResource.getEntitySchema(),
schema: ArticleResource,
key: ArticleResource.url({ id: 5 }),
type: 'read' as const,
throttle: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('SubscriptionManager', () => {
return {
type: SUBSCRIBE_TYPE,
meta: {
schema: PollingArticleResource.getEntitySchema(),
schema: PollingArticleResource,
key: PollingArticleResource.url(payload),
fetch,
options: { pollFrequency: 1000 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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,
Expand All @@ -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: {},
Expand All @@ -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: '' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit b4fa17f

Please sign in to comment.