Skip to content

Commit

Permalink
enhance: No longer require 'asSchema()' (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed May 11, 2020
1 parent 924c257 commit a29c41b
Show file tree
Hide file tree
Showing 36 changed files with 181 additions and 210 deletions.
21 changes: 10 additions & 11 deletions __tests__/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
DeleteShape,
} from 'rest-hooks';
import { AbstractInstanceType, FetchOptions, MutateShape } from 'rest-hooks';

import React from 'react';

export class UserResource extends Resource {
Expand All @@ -34,7 +33,7 @@ export class ArticleResource extends Resource {
}

static schema = {
author: UserResource.asSchema(),
author: UserResource,
};

static urlRoot = 'http://test.com/article/';
Expand Down Expand Up @@ -77,7 +76,7 @@ export class ArticleResource extends Resource {
baseShape.getFetchKey({ ...params, includeUser: true }),
fetch: (params: object) =>
this.fetch('get', this.url({ ...params, includeUser: true })),
schema: this.asSchema(),
schema: this,
};
}

Expand All @@ -91,7 +90,7 @@ export class ArticleResource extends Resource {
baseShape.getFetchKey({ ...params, includeUser: true }),
fetch: (params: object) =>
this.fetch('get', this.listUrl({ ...params, includeUser: 'true' })),
schema: [this.asSchema()],
schema: [this],
};
}

Expand Down Expand Up @@ -221,14 +220,14 @@ export class PaginatedArticleResource extends OtherArticleResource {
static listShape<T extends typeof Resource>(this: T) {
return {
...super.listShape(),
schema: { results: [this.asSchema()], prevPage: '', nextPage: '' },
schema: { results: [this], prevPage: '', nextPage: '' },
};
}

static detailShape<T extends typeof Resource>(this: T) {
return {
...super.listShape(),
schema: { data: this.asSchema() },
schema: { data: this },
};
}
}
Expand All @@ -249,8 +248,8 @@ export class UnionResource extends Resource {
): ReadShape<SchemaDetail<AbstractInstanceType<T>>> {
const schema = new schemas.Union(
{
first: FirstUnionResource.asSchema(),
second: SecondUnionResource.asSchema(),
first: FirstUnionResource,
second: SecondUnionResource,
},
'type',
);
Expand All @@ -266,8 +265,8 @@ export class UnionResource extends Resource {
const schema = [
new schemas.Union(
{
first: FirstUnionResource.asSchema(),
second: SecondUnionResource.asSchema(),
first: FirstUnionResource,
second: SecondUnionResource,
},
(input: FirstUnionResource | SecondUnionResource) => input['type'],
),
Expand All @@ -292,7 +291,7 @@ export class NestedArticleResource extends OtherArticleResource {

static schema = {
...OtherArticleResource.schema,
user: UserResource.asSchema(),
user: UserResource,
};
}

Expand Down
6 changes: 3 additions & 3 deletions docs/api/Entity.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class AssetResource extends Resource {
readonly price: string = '';

static schema = {
price: LatestPrice.asSchema(),
price: LatestPrice,
};
}
```
Expand All @@ -222,7 +222,7 @@ const assets = useResource(AssetResource.listShape(), {});
Nested below:
```tsx
const price = useCache(LatestPrice.asSchema(), { symbol: 'BTC' });
const price = useCache(LatestPrice, { symbol: 'BTC' });
```
### static schema: { [k: keyof this]: Schema }
Expand All @@ -242,7 +242,7 @@ import ArticleEntity from './ArticleEntity';

export const articleListShape = {
type: 'read',
schema: { results: [ArticleEntity.asSchema()], nextPage: '', prevPage: '' },
schema: { results: [ArticleEntity], nextPage: '', prevPage: '' },
getFetchKey(params: Readonly<object>): {return `article/${JSON.stringify(params)}`;},
fetch: universalFetchFunction,
}
Expand Down
2 changes: 1 addition & 1 deletion docs/api/Resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Use in schemas when referring to this Resource.
static listShape<T extends typeof Resource>(this: T) {
return {
...super.listShape(),
schema: { results: [this.asSchema()], nextPage: '', prevPage: '' },
schema: { results: [this], nextPage: '', prevPage: '' },
};
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/api/useCache.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class PaginatedPostResource extends Resource {
static listShape<T extends typeof Resource>(this: T) {
return {
...super.listShape(),
schema: { results: [this.asSchema()], nextPage: '', lastPage: '' },
schema: { results: [this], nextPage: '', lastPage: '' },
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/api/useFetcher.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class ArticlePaginatedResource extends Resource {
static listShape<T extends Resource>() {
return {
...super.listShape(),
shape: { results: this.asSchema()[], nextPage: '' },
shape: { results: this[], nextPage: '' },
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/api/useResource.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class PaginatedPostResource extends Resource {
static listShape<T extends typeof Resource>(this: T) {
return {
...super.listShape(),
schema: { results: [this.asSchema()], nextPage: '', lastPage: '' },
schema: { results: [this], nextPage: '', lastPage: '' },
};
}
}
Expand Down
8 changes: 4 additions & 4 deletions docs/guides/endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ export default class CommentResource extends Resource {
static detailShape<T extends typeof Resource>(this: T) {
return {
...super.detailShape(),
schema: { data: this.asSchema() },
schema: { data: this },
};
}
static listShape<T extends typeof Resource>(this: T) {
return {
...super.listShape(),
schema: { data: [this.asSchema()] },
schema: { data: [this] },
};
}
}
Expand Down Expand Up @@ -271,8 +271,8 @@ export default class BirthdayResource extends BaseResource {
return this.fetch('post', `/api/birthdays/upcoming/`);
},
schema: {
withinSevenDays: [this.asSchema()],
withinThirtyDays: [this.asSchema()],
withinSevenDays: [this],
withinThirtyDays: [this],
},
};
}
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/infinite-scrolling-pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abstract class BaseResource extends Resource {
static listShape<T extends typeof Resource>(this: T) {
return {
...super.listShape(),
schema: { results: [this.asSchema()], cursor: null as string | null },
schema: { results: [this], cursor: null as string | null },
};
}

Expand Down
10 changes: 5 additions & 5 deletions docs/guides/nested-response.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export default class ArticleResource extends Resource {
static urlRoot = 'http://test.com/article/';

static schema = {
author: UserResource.asSchema(),
contributors: [UserResource.asSchema()],
author: UserResource,
contributors: [UserResource],
};
}
```
Expand Down Expand Up @@ -85,13 +85,13 @@ export default class ArticleResource extends Resource {
static urlRoot = 'http://test.com/article/';

static schema = {
author: UserResource.asSchema(),
contributors: [UserResource.asSchema()],
author: UserResource,
contributors: [UserResource],
};
}

UserResource.schema = {
posts: [ArticleResource.asSchema()],
posts: [ArticleResource],
};
```

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/network-transform.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export default class ArticleResource extends Resource {
return {
...super.listShape(),
fetch,
schema: { results: [this.asSchema()], link: '' },
schema: { results: [this], link: '' },
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default class ArticleResource extends Resource {
static listShape<T extends typeof Resource>(this: T) {
return {
...super.listShape(),
schema: { results: [this.asSchema()], nextPage: '', prevPage: '' },
schema: { results: [this], nextPage: '', prevPage: '' },
};
}
}
Expand Down Expand Up @@ -127,7 +127,7 @@ export default class ArticleResource extends Resource {
return {
...super.listShape(),
fetch,
schema: { results: [this.asSchema()], link: '' },
schema: { results: [this], link: '' },
};
}
}
Expand Down Expand Up @@ -170,7 +170,7 @@ export default class ArticleResource extends Resource {
return {
...super.listShape(),
fetch,
schema: { results: [this.asSchema()], link: '' },
schema: { results: [this], link: '' },
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions docs/guides/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class TradeResource extends Resource {
return {
...super.createShape(),
schema: {
trade: this.asSchema(),
account: AccountResource.asSchema(),
trade: this,
account: AccountResource,
},
};
}
Expand Down
4 changes: 1 addition & 3 deletions packages/normalizr/src/entities/Entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import SimpleRecord, { SimpleRecordSchema } from './SimpleRecord';
import SimpleRecord from './SimpleRecord';
import { isImmutable, denormalizeImmutable } from '../schemas/ImmutableUtils';
import * as schema from '../schema';
import { AbstractInstanceType, Schema } from '../types';
Expand Down Expand Up @@ -184,8 +184,6 @@ if (process.env.NODE_ENV !== 'production') {
};
}

export type EntitySchema<E extends typeof SimpleRecord> = SimpleRecordSchema<E>;

export function isEntity(schema: Schema | null): schema is typeof Entity {
return schema !== null && (schema as any).pk !== undefined;
}
22 changes: 1 addition & 21 deletions packages/normalizr/src/entities/SimpleRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,27 +111,7 @@ export default abstract class SimpleRecord {
return [this.fromJS(res) as any, found];
}

/** Returns this to be used in a schema definition.
* This is essential to capture the correct type to be used in inferencing.
*/
static asSchema<T extends typeof SimpleRecord>(this: T) {
return this as SimpleRecordSchema<T>;
return this;
}
}

export type SimpleRecordSchema<E extends typeof SimpleRecord> = E & {
normalize(
input: any,
parent: any,
key: any,
visit: Function,
addEntity: Function,
visitedEntities: Record<string, any>,
): any;
denormalize(
entity: any,
unvisit: Function,
): [AbstractInstanceType<E>, boolean];
_normalizeNullable(): any | undefined;
_denormalizeNullable(): [AbstractInstanceType<E> | undefined, boolean];
};
Loading

0 comments on commit a29c41b

Please sign in to comment.