From 87a65ba8b5f266a299ac3d9c78b6605deee5f4e2 Mon Sep 17 00:00:00 2001 From: Nathaniel Tucker Date: Sun, 1 Sep 2024 17:53:32 +0200 Subject: [PATCH] docs: Remove unneeded id pk from examples --- .changeset/fast-jars-run.md | 7 + docs/core/concepts/normalization.md | 7 - docs/core/guides/ssr.md | 4 - examples/coin-app/src/resources/Currency.ts | 4 - examples/coin-app/src/resources/Product.ts | 4 - examples/github-app/src/resources/Base.ts | 4 - examples/github-app/src/resources/Label.tsx | 4 - examples/github-app/src/resources/Push.ts | 2 +- .../github-app/src/resources/Reaction.tsx | 4 - .../resources/PlaceholderBaseResource.ts | 6 +- examples/normalizr-github/schema.js | 20 +-- examples/normalizr-redux/src/api/schema.js | 20 +-- examples/normalizr-relationships/schema.js | 14 +- .../src/resources/PlaceholderBaseResource.ts | 6 +- .../src-4.0-types/schemas/Entity.d.ts | 122 ++++++------------ website/src/fixtures/posts-collection.ts | 4 - website/src/fixtures/profiles.ts | 6 +- 17 files changed, 62 insertions(+), 176 deletions(-) create mode 100644 .changeset/fast-jars-run.md diff --git a/.changeset/fast-jars-run.md b/.changeset/fast-jars-run.md new file mode 100644 index 000000000000..6c401d57bfe8 --- /dev/null +++ b/.changeset/fast-jars-run.md @@ -0,0 +1,7 @@ +--- +'@data-client/endpoint': patch +'@data-client/rest': patch +'@data-client/graphql': patch +--- + +Fix Entity types for TS 4.0 and below diff --git a/docs/core/concepts/normalization.md b/docs/core/concepts/normalization.md index f534213f1c2c..a612e50f4ea5 100644 --- a/docs/core/concepts/normalization.md +++ b/docs/core/concepts/normalization.md @@ -29,10 +29,6 @@ class Todo extends Entity { readonly userId: number = 0; readonly title: string = ''; readonly completed: boolean = false; - - pk() { - return `${this.id}`; - } } ``` @@ -40,9 +36,6 @@ class Todo extends Entity { import { Entity } from '@data-client/endpoint'; class Todo extends Entity { - pk() { - return `${this.id}`; - } } ``` diff --git a/docs/core/guides/ssr.md b/docs/core/guides/ssr.md index 2b47bb96a4f9..5c51b13d4022 100644 --- a/docs/core/guides/ssr.md +++ b/docs/core/guides/ssr.md @@ -94,10 +94,6 @@ class User extends Entity { id = ''; username = ''; - pk() { - return this.id; - } - // highlight-next-line static key = 'User'; } diff --git a/examples/coin-app/src/resources/Currency.ts b/examples/coin-app/src/resources/Currency.ts index f1d8666aa148..344751293d14 100644 --- a/examples/coin-app/src/resources/Currency.ts +++ b/examples/coin-app/src/resources/Currency.ts @@ -36,10 +36,6 @@ export class Currency extends Entity { return iconTable[this.id]?.img_url; } - pk(): string { - return this.id; - } - static key = 'Currency'; static process( diff --git a/examples/coin-app/src/resources/Product.ts b/examples/coin-app/src/resources/Product.ts index c12c5707d2ac..f130a7534f79 100644 --- a/examples/coin-app/src/resources/Product.ts +++ b/examples/coin-app/src/resources/Product.ts @@ -11,10 +11,6 @@ export class Product extends Entity { trading_disabled = false; stats = Stats.fromJS(); - pk(): string { - return this.id; - } - static key = 'Product'; static schema = { stats: Stats, diff --git a/examples/github-app/src/resources/Base.ts b/examples/github-app/src/resources/Base.ts index 01391650feb0..7fb3bbfd13a8 100644 --- a/examples/github-app/src/resources/Base.ts +++ b/examples/github-app/src/resources/Base.ts @@ -24,10 +24,6 @@ const HOST = 'https://api.github.com'; export class GithubEntity extends Entity { readonly id: number = -1; - - pk() { - return this.id?.toString(); - } } export const GithubGqlEndpoint = new GQLEndpoint( diff --git a/examples/github-app/src/resources/Label.tsx b/examples/github-app/src/resources/Label.tsx index a9e0378a3e41..81c52150503c 100644 --- a/examples/github-app/src/resources/Label.tsx +++ b/examples/github-app/src/resources/Label.tsx @@ -7,10 +7,6 @@ export class Label extends GithubEntity { readonly description: string = ''; readonly color: string = '000000'; readonly default: boolean = false; - - pk() { - return this.id?.toString(); - } } export const LabelResource = githubResource({ path: '/repos/:owner/:repo/labels/:name', diff --git a/examples/github-app/src/resources/Push.ts b/examples/github-app/src/resources/Push.ts index 30715dbf2922..3900ae97091f 100644 --- a/examples/github-app/src/resources/Push.ts +++ b/examples/github-app/src/resources/Push.ts @@ -10,7 +10,7 @@ export class Push extends GithubEntity { commits: Commit[] = []; pk() { - return `${this.pushId}`; + return this.pushId; } } diff --git a/examples/github-app/src/resources/Reaction.tsx b/examples/github-app/src/resources/Reaction.tsx index 58e5681f4a1c..f743af24679f 100644 --- a/examples/github-app/src/resources/Reaction.tsx +++ b/examples/github-app/src/resources/Reaction.tsx @@ -14,10 +14,6 @@ export class Reaction extends GithubEntity { return contentToIcon[this.content]; } - pk() { - return this.id?.toString(); - } - static schema = { user: User, createdAt: Temporal.Instant.from, diff --git a/examples/nextjs/resources/PlaceholderBaseResource.ts b/examples/nextjs/resources/PlaceholderBaseResource.ts index 9ad4d5fa4aba..21c92cf4c5ce 100644 --- a/examples/nextjs/resources/PlaceholderBaseResource.ts +++ b/examples/nextjs/resources/PlaceholderBaseResource.ts @@ -7,12 +7,8 @@ import { } from '@data-client/rest'; export abstract class PlaceholderEntity extends Entity { - id = 0; - // all Resources of `jsonplaceholder` use an id for the primary key - pk() { - return `${this.id}`; - } + id = 0; } /** Common patterns in the https://jsonplaceholder.typicode.com API */ diff --git a/examples/normalizr-github/schema.js b/examples/normalizr-github/schema.js index e357317ba247..cfd19253f5b3 100644 --- a/examples/normalizr-github/schema.js +++ b/examples/normalizr-github/schema.js @@ -1,16 +1,8 @@ import { schema, Entity } from '@data-client/endpoint'; -class BaseEntity extends Entity { - id = 0; +export class User extends Entity {} - pk() { - return this.id; - } -} - -export class User extends BaseEntity {} - -export class Commit extends BaseEntity { +export class Commit extends Entity { sha = ''; static schema = { @@ -23,15 +15,15 @@ export class Commit extends BaseEntity { } } -export class Label extends BaseEntity {} +export class Label extends Entity {} -export class Milestone extends BaseEntity { +export class Milestone extends Entity { static schema = { creator: User, }; } -export class Issue extends BaseEntity { +export class Issue extends Entity { static schema = { assignee: User, assignees: [User], @@ -41,7 +33,7 @@ export class Issue extends BaseEntity { }; } -export class PullRequest extends BaseEntity { +export class PullRequest extends Entity { static schema = { assignee: User, assignees: [User], diff --git a/examples/normalizr-redux/src/api/schema.js b/examples/normalizr-redux/src/api/schema.js index db1dd9994d76..cfd19253f5b3 100644 --- a/examples/normalizr-redux/src/api/schema.js +++ b/examples/normalizr-redux/src/api/schema.js @@ -1,16 +1,8 @@ import { schema, Entity } from '@data-client/endpoint'; -class BaseEntity extends Entity { - id = 0; +export class User extends Entity {} - pk() { - return `${this.id}`; - } -} - -export class User extends BaseEntity {} - -export class Commit extends BaseEntity { +export class Commit extends Entity { sha = ''; static schema = { @@ -23,15 +15,15 @@ export class Commit extends BaseEntity { } } -export class Label extends BaseEntity {} +export class Label extends Entity {} -export class Milestone extends BaseEntity { +export class Milestone extends Entity { static schema = { creator: User, }; } -export class Issue extends BaseEntity { +export class Issue extends Entity { static schema = { assignee: User, assignees: [User], @@ -41,7 +33,7 @@ export class Issue extends BaseEntity { }; } -export class PullRequest extends BaseEntity { +export class PullRequest extends Entity { static schema = { assignee: User, assignees: [User], diff --git a/examples/normalizr-relationships/schema.js b/examples/normalizr-relationships/schema.js index cdc516f44ff6..8ff6612b0776 100644 --- a/examples/normalizr-relationships/schema.js +++ b/examples/normalizr-relationships/schema.js @@ -1,14 +1,6 @@ import { Entity } from '@data-client/endpoint'; -class BaseEntity extends Entity { - id = 0; - - pk() { - return this.id; - } -} - -class User extends BaseEntity { +class User extends Entity { static process(value, parent, key) { switch (key) { case 'author': @@ -29,7 +21,7 @@ class User extends BaseEntity { }; } } -class Comment extends BaseEntity { +class Comment extends Entity { static schema = { commenter: User, }; @@ -39,7 +31,7 @@ class Comment extends BaseEntity { } } -class Post extends BaseEntity { +class Post extends Entity { static schema = { author: User, comments: [Comment], diff --git a/examples/todo-app/src/resources/PlaceholderBaseResource.ts b/examples/todo-app/src/resources/PlaceholderBaseResource.ts index f07a982b0984..7820acd1b51e 100644 --- a/examples/todo-app/src/resources/PlaceholderBaseResource.ts +++ b/examples/todo-app/src/resources/PlaceholderBaseResource.ts @@ -8,12 +8,8 @@ import { import { v4 as uuid } from 'uuid'; export abstract class PlaceholderEntity extends Entity { - id = 0; - // all Resources of `jsonplaceholder` use an id for the primary key - pk() { - return `${this.id}`; - } + id = 0; } /** Common patterns in the https://jsonplaceholder.typicode.com API */ diff --git a/packages/endpoint/src-4.0-types/schemas/Entity.d.ts b/packages/endpoint/src-4.0-types/schemas/Entity.d.ts index add13efcba00..cd591b6434e1 100644 --- a/packages/endpoint/src-4.0-types/schemas/Entity.d.ts +++ b/packages/endpoint/src-4.0-types/schemas/Entity.d.ts @@ -1,95 +1,45 @@ // we just removed instances of 'abstract new' import { AbstractInstanceType } from '../normal.js'; -declare const Entity_base: import('./EntitySchema.js').IEntityClass< - new (...args: any[]) => { - pk( - parent?: any, - key?: string | undefined, - args?: readonly any[] | undefined, - ): string | number | undefined; - } -> & - (new (...args: any[]) => { - pk( - parent?: any, - key?: string | undefined, - args?: readonly any[] | undefined, - ): string | number | undefined; - }); +declare const Entity_base: import("./EntityTypes.js").IEntityClass { + pk(parent?: any, key?: string, args?: readonly any[]): string | number | undefined; +}> & (new (...args: any[]) => { + pk(parent?: any, key?: string, args?: readonly any[]): string | number | undefined; +}); /** * Represents data that should be deduped by specifying a primary key. * @see https://dataclient.io/rest/api/Entity */ export default abstract class Entity extends Entity_base { - /** - * A unique identifier for each Entity - * - * @param [parent] When normalizing, the object which included the entity - * @param [key] When normalizing, the key where this entity was found - * @param [args] ...args sent to Endpoint - * @see https://dataclient.io/rest/api/Entity#pk - */ - abstract pk( - parent?: any, - key?: string, - args?: readonly any[], - ): string | number | undefined; - - /** Control how automatic schema validation is handled - * - * `undefined`: Defaults - throw error in worst offense - * 'warn': only ever warn - * 'silent': Don't bother with processing at all - * - * Note: this only applies to non-nested members. - */ - protected static automaticValidation?: 'warn' | 'silent'; - /** Factory method to convert from Plain JS Objects. - * - * @param [props] Plain Object of properties to assign. - * @see https://dataclient.io/rest/api/Entity#fromJS - */ - static fromJS: ( - this: T, - props?: Partial>, - ) => AbstractInstanceType; - - /** - * A unique identifier for each Entity - * - * @param [value] POJO of the entity or subset used - * @param [parent] When normalizing, the object which included the entity - * @param [key] When normalizing, the key where this entity was found - * @param [args] ...args sent to Endpoint - */ - static pk: ( - this: T, - value: Partial>, - parent?: any, - key?: string, - args?: any[], - ) => string | number | undefined; - - /** Do any transformations when first receiving input - * - * @see https://dataclient.io/rest/api/Entity#process - */ - static process( - input: any, - parent: any, - key: string | undefined, - args: any[], - ): any; - - /** Returning a string indicates an error (the string is the message) - * @see https://dataclient.io/rest/api/Entity#validate - */ - static validate(processedEntity: any): string | undefined; - static denormalize: ( - this: T, - input: any, - args: readonly any[], - unvisit: (schema: any, input: any) => any, - ) => AbstractInstanceType; + /** Control how automatic schema validation is handled + * + * `undefined`: Defaults - throw error in worst offense + * 'warn': only ever warn + * 'silent': Don't bother with processing at all + * + * Note: this only applies to non-nested members. + */ + protected static automaticValidation?: 'warn' | 'silent'; + /** Factory method to convert from Plain JS Objects. + * + * @see https://dataclient.io/rest/api/Entity#fromJS + * @param [props] Plain Object of properties to assign. + */ + static fromJS: (this: T, props?: Partial>) => AbstractInstanceType; + /** + * A unique identifier for each Entity + * + * @see https://dataclient.io/rest/api/Entity#pk + * @param [value] POJO of the entity or subset used + * @param [parent] When normalizing, the object which included the entity + * @param [key] When normalizing, the key where this entity was found + * @param [args] ...args sent to Endpoint + */ + static pk: (this: T, value: Partial>, parent?: any, key?: string, args?: any[]) => string | number | undefined; + /** Do any transformations when first receiving input + * + * @see https://dataclient.io/rest/api/Entity#process + */ + static process(input: any, parent: any, key: string | undefined, args: any[]): any; + static denormalize: (this: T, input: any, args: readonly any[], unvisit: (schema: any, input: any) => any) => AbstractInstanceType; } export {}; diff --git a/website/src/fixtures/posts-collection.ts b/website/src/fixtures/posts-collection.ts index c53ff9bb4aef..026f43a063d7 100644 --- a/website/src/fixtures/posts-collection.ts +++ b/website/src/fixtures/posts-collection.ts @@ -6,10 +6,6 @@ class Post extends Entity { title = ''; group = ''; author = ''; - - pk() { - return this.id; - } } export const getPosts = new RestEndpoint({ path: '/:group/posts', diff --git a/website/src/fixtures/profiles.ts b/website/src/fixtures/profiles.ts index ddafc0150dcb..377ffb07148b 100644 --- a/website/src/fixtures/profiles.ts +++ b/website/src/fixtures/profiles.ts @@ -1,4 +1,4 @@ -import { Entity, resource, RestEndpoint } from '@data-client/rest'; +import { Entity, resource } from '@data-client/rest'; export class Profile extends Entity { id: number | undefined = undefined; @@ -6,10 +6,6 @@ export class Profile extends Entity { fullName = ''; bio = ''; - pk() { - return this.id?.toString(); - } - static key = 'Profile'; }