Skip to content

Commit 114d5ab

Browse files
committed
enhance: Simplify Invalidate schema code
1 parent 4939456 commit 114d5ab

File tree

1 file changed

+125
-79
lines changed

1 file changed

+125
-79
lines changed
Lines changed: 125 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
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';
72
import { INVALID } from '../special.js';
83

94
/**
@@ -13,90 +8,141 @@ import { INVALID } from '../special.js';
138
* Optional (like variable sized Array and Values) will simply remove the item.
149
* @see https://dataclient.io/rest/api/Invalidate
1510
*/
16-
export default class Invalidate<
11+
export default function Invalidate<
1712
E extends EntityInterface & {
1813
process: any;
1914
},
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;
15+
>(entity: E): E {
16+
if (process.env.NODE_ENV !== 'production' && !entity) {
17+
throw new Error('Invalidate schema requires "entity" option.');
3618
}
19+
return Object.create(entity, {
20+
normalize: {
21+
value(
22+
input: any,
23+
parent: any,
24+
key: string | undefined,
25+
args: any[],
26+
visit: (...args: any) => any,
27+
delegate: INormalizeDelegate,
28+
): string {
29+
// TODO: what's store needs to be a differing type from fromJS
30+
const processedEntity = entity.process(input, parent, key, args);
31+
let pk = entity.pk(processedEntity, parent, key, args);
3732

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

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);
54+
// any queued updates are meaningless with delete, so we should just set it
55+
// and creates will have a different pk
56+
delegate.setEntity(this as any, pk, INVALID);
57+
return pk;
58+
},
59+
},
60+
});
61+
}
62+
// export default class Invalidate<
63+
// E extends EntityInterface & {
64+
// process: any;
65+
// },
66+
// > implements SchemaSimple
67+
// {
68+
// declare protected _entity: E;
5369

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.
70+
// /**
71+
// * Marks entity as Invalid.
72+
// *
73+
// * This triggers suspense for all endpoints requiring it.
74+
// * Optional (like variable sized Array and Values) will simply remove the item.
75+
// * @see https://dataclient.io/rest/api/Invalidate
76+
// */
77+
// constructor(entity: E) {
78+
// if (process.env.NODE_ENV !== 'production' && !entity) {
79+
// throw new Error('Invalidate schema requires "entity" option.');
80+
// }
81+
// this._entity = entity;
82+
// }
6083

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
84+
// get key() {
85+
// return this._entity.key;
86+
// }
6587

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
88+
// normalize(
89+
// input: any,
90+
// parent: any,
91+
// key: string | undefined,
92+
// args: any[],
93+
// visit: (...args: any) => any,
94+
// delegate: INormalizeDelegate,
95+
// ): string {
96+
// // TODO: what's store needs to be a differing type from fromJS
97+
// const processedEntity = this._entity.process(input, parent, key, args);
98+
// let pk = this._entity.pk(processedEntity, parent, key, args);
7499

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-
}
100+
// if (
101+
// process.env.NODE_ENV !== 'production' &&
102+
// (pk === undefined || pk === '' || pk === 'undefined')
103+
// ) {
104+
// const error = new Error(
105+
// `Missing usable primary key when normalizing response.
80106

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

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-
}
112+
// Invalidate(Entity): Invalidate(${this._entity.key})
113+
// Value (processed): ${input && JSON.stringify(input, null, 2)}
114+
// `,
115+
// );
116+
// (error as any).status = 400;
117+
// throw error;
118+
// }
119+
// pk = `${pk}`; // ensure pk is a string
92120

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

98-
/* istanbul ignore next */
99-
_normalizeNullable(): string | undefined {
100-
return {} as any;
101-
}
102-
}
127+
// queryKey(args: any, unvisit: unknown, delegate: unknown): undefined {
128+
// return undefined;
129+
// }
130+
131+
// denormalize(
132+
// id: string,
133+
// args: readonly any[],
134+
// unvisit: (schema: any, input: any) => any,
135+
// ): AbstractInstanceType<E> {
136+
// return unvisit(this._entity, id) as any;
137+
// }
138+
139+
// /* istanbul ignore next */
140+
// _denormalizeNullable(): AbstractInstanceType<E> | undefined {
141+
// return {} as any;
142+
// }
143+
144+
// /* istanbul ignore next */
145+
// _normalizeNullable(): string | undefined {
146+
// return {} as any;
147+
// }
148+
// }

0 commit comments

Comments
 (0)