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' ;
7
3
import { INVALID } from '../special.js' ;
8
4
9
5
/**
@@ -13,90 +9,149 @@ import { INVALID } from '../special.js';
13
9
* Optional (like variable sized Array and Values) will simply remove the item.
14
10
* @see https://dataclient.io/rest/api/Invalidate
15
11
*/
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 <
17
21
E extends EntityInterface & {
18
22
process : any ;
19
23
} ,
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.' ) ;
36
27
}
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 ) ;
37
41
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
41
62
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;
53
78
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
+ // }
60
92
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
+ // }
65
96
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);
74
108
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.
80
115
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
84
120
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
92
129
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
+ // }
97
135
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