Skip to content

Commit

Permalink
docs: Remove unneeded id pk from examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ntucker committed Sep 1, 2024
1 parent 9ebc6cd commit 87a65ba
Show file tree
Hide file tree
Showing 17 changed files with 62 additions and 176 deletions.
7 changes: 7 additions & 0 deletions .changeset/fast-jars-run.md
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions docs/core/concepts/normalization.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,13 @@ class Todo extends Entity {
readonly userId: number = 0;
readonly title: string = '';
readonly completed: boolean = false;
pk() {
return `${this.id}`;
}
}
```
```js
import { Entity } from '@data-client/endpoint';
class Todo extends Entity {
pk() {
return `${this.id}`;
}
}
```
Expand Down
4 changes: 0 additions & 4 deletions docs/core/guides/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ class User extends Entity {
id = '';
username = '';

pk() {
return this.id;
}

// highlight-next-line
static key = 'User';
}
Expand Down
4 changes: 0 additions & 4 deletions examples/coin-app/src/resources/Currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 0 additions & 4 deletions examples/coin-app/src/resources/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions examples/github-app/src/resources/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 0 additions & 4 deletions examples/github-app/src/resources/Label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion examples/github-app/src/resources/Push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Push extends GithubEntity {
commits: Commit[] = [];

pk() {
return `${this.pushId}`;
return this.pushId;
}
}

Expand Down
4 changes: 0 additions & 4 deletions examples/github-app/src/resources/Reaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 1 addition & 5 deletions examples/nextjs/resources/PlaceholderBaseResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
20 changes: 6 additions & 14 deletions examples/normalizr-github/schema.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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],
Expand All @@ -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],
Expand Down
20 changes: 6 additions & 14 deletions examples/normalizr-redux/src/api/schema.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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],
Expand All @@ -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],
Expand Down
14 changes: 3 additions & 11 deletions examples/normalizr-relationships/schema.js
Original file line number Diff line number Diff line change
@@ -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':
Expand All @@ -29,7 +21,7 @@ class User extends BaseEntity {
};
}
}
class Comment extends BaseEntity {
class Comment extends Entity {
static schema = {
commenter: User,
};
Expand All @@ -39,7 +31,7 @@ class Comment extends BaseEntity {
}
}

class Post extends BaseEntity {
class Post extends Entity {
static schema = {
author: User,
comments: [Comment],
Expand Down
6 changes: 1 addition & 5 deletions examples/todo-app/src/resources/PlaceholderBaseResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
122 changes: 36 additions & 86 deletions packages/endpoint/src-4.0-types/schemas/Entity.d.ts
Original file line number Diff line number Diff line change
@@ -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<new (...args: any[]) => {
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: <T extends typeof Entity>(
this: T,
props?: Partial<AbstractInstanceType<T>>,
) => AbstractInstanceType<T>;

/**
* 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: <T extends typeof Entity>(
this: T,
value: Partial<AbstractInstanceType<T>>,
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: <T extends typeof Entity>(
this: T,
input: any,
args: readonly any[],
unvisit: (schema: any, input: any) => any,
) => AbstractInstanceType<T>;
/** 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: <T extends typeof Entity>(this: T, props?: Partial<AbstractInstanceType<T>>) => AbstractInstanceType<T>;
/**
* 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: <T extends typeof Entity>(this: T, value: Partial<AbstractInstanceType<T>>, 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: <T extends typeof Entity>(this: T, input: any, args: readonly any[], unvisit: (schema: any, input: any) => any) => AbstractInstanceType<T>;
}
export {};
Loading

0 comments on commit 87a65ba

Please sign in to comment.