Skip to content

Commit 247b9df

Browse files
committed
enhance: Simplify Invalidate schema code
1 parent 4939456 commit 247b9df

File tree

2 files changed

+178
-81
lines changed

2 files changed

+178
-81
lines changed

packages/endpoint/src/schema.d.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
SchemaClass,
66
IQueryDelegate,
77
INormalizeDelegate,
8+
SchemaSimple,
89
} from './interface.js';
910
import type {
1011
AbstractInstanceType,
@@ -24,7 +25,6 @@ import {
2425
default as EntityMixin,
2526
default as Entity,
2627
} from './schemas/EntityMixin.js';
27-
import { default as Invalidate } from './schemas/Invalidate.js';
2828
import { default as Query } from './schemas/Query.js';
2929
import type {
3030
CollectionConstructor,
@@ -34,14 +34,56 @@ import type {
3434
UnionResult,
3535
} from './schemaTypes.js';
3636

37-
export { EntityMap, Invalidate, Query, EntityMixin, Entity };
37+
export { EntityMap, Query, EntityMixin, Entity };
3838

3939
export type { SchemaClass };
4040

4141
export { EntityInterface } from './interface.js';
4242

4343
export * from './schemaTypes.js';
4444

45+
/**
46+
* Marks entity as Invalid.
47+
*
48+
* This triggers suspense for all endpoints requiring it.
49+
* Optional (like variable sized Array and Values) will simply remove the item.
50+
* @see https://dataclient.io/rest/api/Invalidate
51+
*/
52+
export class Invalidate<
53+
E extends EntityInterface & {
54+
process: any;
55+
},
56+
> implements SchemaSimple
57+
{
58+
/**
59+
* Marks entity as Invalid.
60+
*
61+
* This triggers suspense for all endpoints requiring it.
62+
* Optional (like variable sized Array and Values) will simply remove the item.
63+
* @see https://dataclient.io/rest/api/Invalidate
64+
*/
65+
constructor(entity: E);
66+
key: string;
67+
normalize(
68+
input: any,
69+
parent: any,
70+
key: string | undefined,
71+
args: any[],
72+
visit: (...args: any) => any,
73+
delegate: INormalizeDelegate,
74+
): string;
75+
76+
queryKey(args: any, unvisit: unknown, delegate: unknown): undefined;
77+
denormalize(
78+
id: string,
79+
args: readonly any[],
80+
unvisit: (schema: any, input: any) => any,
81+
): AbstractInstanceType<E>;
82+
83+
_denormalizeNullable(): AbstractInstanceType<E> | undefined;
84+
_normalizeNullable(): string | undefined;
85+
}
86+
4587
/**
4688
* Represents arrays
4789
* @see https://dataclient.io/rest/api/Array
Lines changed: 134 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
import type {
2-
EntityInterface,
3-
INormalizeDelegate,
4-
SchemaSimple,
5-
} from '../interface.js';
6-
import type { AbstractInstanceType } from '../normal.js';
1+
import type { EntityInterface, INormalizeDelegate } from '../interface.js';
2+
import { AbstractInstanceType } from '../normal.js';
73
import { INVALID } from '../special.js';
84

95
/**
@@ -13,90 +9,149 @@ import { INVALID } from '../special.js';
139
* Optional (like variable sized Array and Values) will simply remove the item.
1410
* @see https://dataclient.io/rest/api/Invalidate
1511
*/
16-
export default class Invalidate<
12+
// export default interface Invalidate {
13+
// new <E extends EntityInterface & { process: any }>(entity: E): E;
14+
// }
15+
// export default interface Invalidate<
16+
// E extends EntityInterface & { process: any },
17+
// > {
18+
// new (...args: any[]): E;
19+
// }
20+
export default function Invalidate<
1721
E extends EntityInterface & {
1822
process: any;
1923
},
20-
> implements SchemaSimple
21-
{
22-
declare protected _entity: E;
23-
24-
/**
25-
* Marks entity as Invalid.
26-
*
27-
* This triggers suspense for all endpoints requiring it.
28-
* Optional (like variable sized Array and Values) will simply remove the item.
29-
* @see https://dataclient.io/rest/api/Invalidate
30-
*/
31-
constructor(entity: E) {
32-
if (process.env.NODE_ENV !== 'production' && !entity) {
33-
throw new Error('Invalidate schema requires "entity" option.');
34-
}
35-
this._entity = entity;
24+
>(entity: E): E {
25+
if (process.env.NODE_ENV !== 'production' && !entity) {
26+
throw new Error('Invalidate schema requires "entity" option.');
3627
}
28+
return Object.create(entity, {
29+
normalize: {
30+
value(
31+
input: any,
32+
parent: any,
33+
key: string | undefined,
34+
args: any[],
35+
visit: (...args: any) => any,
36+
delegate: INormalizeDelegate,
37+
): string {
38+
// TODO: what's store needs to be a differing type from fromJS
39+
const processedEntity = entity.process(input, parent, key, args);
40+
let pk = entity.pk(processedEntity, parent, key, args);
3741

38-
get key() {
39-
return this._entity.key;
40-
}
42+
if (
43+
process.env.NODE_ENV !== 'production' &&
44+
(pk === undefined || pk === '' || pk === 'undefined')
45+
) {
46+
const error = new Error(
47+
`Missing usable primary key when normalizing response.
48+
49+
This is likely due to a malformed response.
50+
Try inspecting the network response or fetch() return value.
51+
Or use debugging tools: https://dataclient.io/docs/getting-started/debugging
52+
Learn more about schemas: https://dataclient.io/docs/api/schema
53+
54+
Invalidate(Entity): Invalidate(${this._entity.key})
55+
Value (processed): ${input && JSON.stringify(input, null, 2)}
56+
`,
57+
);
58+
(error as any).status = 400;
59+
throw error;
60+
}
61+
pk = `${pk}`; // ensure pk is a string
4162

42-
normalize(
43-
input: any,
44-
parent: any,
45-
key: string | undefined,
46-
args: any[],
47-
visit: (...args: any) => any,
48-
delegate: INormalizeDelegate,
49-
): string {
50-
// TODO: what's store needs to be a differing type from fromJS
51-
const processedEntity = this._entity.process(input, parent, key, args);
52-
let pk = this._entity.pk(processedEntity, parent, key, args);
63+
// any queued updates are meaningless with delete, so we should just set it
64+
// and creates will have a different pk
65+
delegate.setEntity(this as any, pk, INVALID);
66+
return pk;
67+
},
68+
},
69+
});
70+
}
71+
// export default class Invalidate<
72+
// E extends EntityInterface & {
73+
// process: any;
74+
// },
75+
// > implements SchemaSimple
76+
// {
77+
// declare protected _entity: E;
5378

54-
if (
55-
process.env.NODE_ENV !== 'production' &&
56-
(pk === undefined || pk === '' || pk === 'undefined')
57-
) {
58-
const error = new Error(
59-
`Missing usable primary key when normalizing response.
79+
// /**
80+
// * Marks entity as Invalid.
81+
// *
82+
// * This triggers suspense for all endpoints requiring it.
83+
// * Optional (like variable sized Array and Values) will simply remove the item.
84+
// * @see https://dataclient.io/rest/api/Invalidate
85+
// */
86+
// constructor(entity: E) {
87+
// if (process.env.NODE_ENV !== 'production' && !entity) {
88+
// throw new Error('Invalidate schema requires "entity" option.');
89+
// }
90+
// this._entity = entity;
91+
// }
6092

61-
This is likely due to a malformed response.
62-
Try inspecting the network response or fetch() return value.
63-
Or use debugging tools: https://dataclient.io/docs/getting-started/debugging
64-
Learn more about schemas: https://dataclient.io/docs/api/schema
93+
// get key() {
94+
// return this._entity.key;
95+
// }
6596

66-
Invalidate(Entity): Invalidate(${this._entity.key})
67-
Value (processed): ${input && JSON.stringify(input, null, 2)}
68-
`,
69-
);
70-
(error as any).status = 400;
71-
throw error;
72-
}
73-
pk = `${pk}`; // ensure pk is a string
97+
// normalize(
98+
// input: any,
99+
// parent: any,
100+
// key: string | undefined,
101+
// args: any[],
102+
// visit: (...args: any) => any,
103+
// delegate: INormalizeDelegate,
104+
// ): string {
105+
// // TODO: what's store needs to be a differing type from fromJS
106+
// const processedEntity = this._entity.process(input, parent, key, args);
107+
// let pk = this._entity.pk(processedEntity, parent, key, args);
74108

75-
// any queued updates are meaningless with delete, so we should just set it
76-
// and creates will have a different pk
77-
delegate.setEntity(this as any, pk, INVALID);
78-
return pk;
79-
}
109+
// if (
110+
// process.env.NODE_ENV !== 'production' &&
111+
// (pk === undefined || pk === '' || pk === 'undefined')
112+
// ) {
113+
// const error = new Error(
114+
// `Missing usable primary key when normalizing response.
80115

81-
queryKey(args: any, unvisit: unknown, delegate: unknown): undefined {
82-
return undefined;
83-
}
116+
// This is likely due to a malformed response.
117+
// Try inspecting the network response or fetch() return value.
118+
// Or use debugging tools: https://dataclient.io/docs/getting-started/debugging
119+
// Learn more about schemas: https://dataclient.io/docs/api/schema
84120

85-
denormalize(
86-
id: string,
87-
args: readonly any[],
88-
unvisit: (schema: any, input: any) => any,
89-
): AbstractInstanceType<E> {
90-
return unvisit(this._entity, id) as any;
91-
}
121+
// Invalidate(Entity): Invalidate(${this._entity.key})
122+
// Value (processed): ${input && JSON.stringify(input, null, 2)}
123+
// `,
124+
// );
125+
// (error as any).status = 400;
126+
// throw error;
127+
// }
128+
// pk = `${pk}`; // ensure pk is a string
92129

93-
/* istanbul ignore next */
94-
_denormalizeNullable(): AbstractInstanceType<E> | undefined {
95-
return {} as any;
96-
}
130+
// // any queued updates are meaningless with delete, so we should just set it
131+
// // and creates will have a different pk
132+
// delegate.setEntity(this as any, pk, INVALID);
133+
// return pk;
134+
// }
97135

98-
/* istanbul ignore next */
99-
_normalizeNullable(): string | undefined {
100-
return {} as any;
101-
}
102-
}
136+
// queryKey(args: any, unvisit: unknown, delegate: unknown): undefined {
137+
// return undefined;
138+
// }
139+
140+
// denormalize(
141+
// id: string,
142+
// args: readonly any[],
143+
// unvisit: (schema: any, input: any) => any,
144+
// ): AbstractInstanceType<E> {
145+
// return unvisit(this._entity, id) as any;
146+
// }
147+
148+
// /* istanbul ignore next */
149+
// _denormalizeNullable(): AbstractInstanceType<E> | undefined {
150+
// return {} as any;
151+
// }
152+
153+
// /* istanbul ignore next */
154+
// _normalizeNullable(): string | undefined {
155+
// return {} as any;
156+
// }
157+
// }

0 commit comments

Comments
 (0)