@layerzerolabs/base-definitions 0.2.68 → 0.2.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{G6XKLFF4.js → FN4EJGD7.js} +2 -2
- package/dist/{G6XKLFF4.js.map → FN4EJGD7.js.map} +1 -1
- package/dist/{5V2VE36W.js → IRTAOK6O.js} +17 -2
- package/dist/IRTAOK6O.js.map +1 -0
- package/dist/{2IH6HX7W.cjs → KY4LVDYW.cjs} +4 -4
- package/dist/{2IH6HX7W.cjs.map → KY4LVDYW.cjs.map} +1 -1
- package/dist/{PG5UTJ4W.js → XASERBNQ.js} +3 -3
- package/dist/{PG5UTJ4W.js.map → XASERBNQ.js.map} +1 -1
- package/dist/{2YGTKLAP.cjs → XFGZZMRR.cjs} +2 -2
- package/dist/{2YGTKLAP.cjs.map → XFGZZMRR.cjs.map} +1 -1
- package/dist/{Y4WRKZUM.cjs → ZBUMYCUA.cjs} +17 -2
- package/dist/ZBUMYCUA.cjs.map +1 -0
- package/dist/definitions.cjs +5 -5
- package/dist/definitions.d.ts +1 -1
- package/dist/definitions.d.ts.map +1 -1
- package/dist/definitions.js +1 -1
- package/dist/index.cjs +10 -10
- package/dist/index.js +3 -3
- package/dist/registrarBuilder.cjs +3 -3
- package/dist/registrarBuilder.d.ts +6 -0
- package/dist/registrarBuilder.d.ts.map +1 -1
- package/dist/registrarBuilder.js +1 -1
- package/dist/utils.cjs +3 -3
- package/dist/utils.js +2 -2
- package/package.json +10 -7
- package/.turbo/turbo-build.log +0 -55
- package/.turbo/turbo-lint.log +0 -8
- package/.turbo/turbo-test.log +0 -17
- package/dist/5V2VE36W.js.map +0 -1
- package/dist/Y4WRKZUM.cjs.map +0 -1
- package/src/definitions.ts +0 -321
- package/src/index.ts +0 -4
- package/src/lzyringe.ts +0 -71
- package/src/registrarBuilder.ts +0 -348
- package/src/utils.ts +0 -25
- package/test/registrarBuilder.test.ts +0 -175
- package/tsconfig.json +0 -20
- package/tsup.config.ts +0 -8
package/src/lzyringe.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
type StoreValue = {
|
|
2
|
-
type: 'value';
|
|
3
|
-
value: any;
|
|
4
|
-
};
|
|
5
|
-
type StoreFactory = {
|
|
6
|
-
type: 'factory';
|
|
7
|
-
value: () => any;
|
|
8
|
-
};
|
|
9
|
-
type StoreAny = StoreValue | StoreFactory;
|
|
10
|
-
|
|
11
|
-
export class DependencyStore {
|
|
12
|
-
private store: Record<string, StoreAny | undefined> = {};
|
|
13
|
-
|
|
14
|
-
private throwIfRegistered(key: string) {
|
|
15
|
-
if (this.store[key] != null) {
|
|
16
|
-
throw new Error(`${key} is already registered`);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
private throwIfUnregistered(key: string) {
|
|
21
|
-
if (this.store[key] == null) {
|
|
22
|
-
throw new Error(`${key} is not registered`);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Registers a value that will be resolved literally by resolve()
|
|
27
|
-
* @throws if already registered
|
|
28
|
-
*/
|
|
29
|
-
public register<T>(key: string, value: T extends Promise<any> ? never : T) {
|
|
30
|
-
this.throwIfRegistered(key);
|
|
31
|
-
this.store[key] = { type: 'value', value };
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Registers a promise value that will be resolved literally by resolve()
|
|
35
|
-
* @throws if already registered
|
|
36
|
-
*/
|
|
37
|
-
public registerPromise(key: string, value: Promise<any>) {
|
|
38
|
-
this.throwIfRegistered(key);
|
|
39
|
-
this.store[key] = { type: 'value', value };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** Registers a factory function that will be called by resolve()
|
|
43
|
-
* @throws if already registered
|
|
44
|
-
*/
|
|
45
|
-
public registerFactory(key: string, factory: () => any) {
|
|
46
|
-
this.throwIfRegistered(key);
|
|
47
|
-
this.store[key] = { type: 'factory', value: factory };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/** Unregisters a key
|
|
51
|
-
* @throws if not registered
|
|
52
|
-
*/
|
|
53
|
-
public unregister(key: string) {
|
|
54
|
-
this.throwIfUnregistered(key);
|
|
55
|
-
this.store[key] = undefined;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Drops all registered values
|
|
60
|
-
*/
|
|
61
|
-
public invalidate() {
|
|
62
|
-
this.store = {};
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** Resolves a key to a value */
|
|
66
|
-
public resolve(key: string) {
|
|
67
|
-
this.throwIfUnregistered(key);
|
|
68
|
-
const stored = this.store[key]!;
|
|
69
|
-
return stored.type === 'factory' ? stored.value() : stored.value;
|
|
70
|
-
}
|
|
71
|
-
}
|
package/src/registrarBuilder.ts
DELETED
|
@@ -1,348 +0,0 @@
|
|
|
1
|
-
import type { Dependencies } from '@layerzerolabs/dependency-graph';
|
|
2
|
-
import { DependencyNode } from '@layerzerolabs/dependency-graph';
|
|
3
|
-
import type { Registrar } from '@layerzerolabs/dfs';
|
|
4
|
-
import { dfs } from '@layerzerolabs/dfs';
|
|
5
|
-
export type { Registrar };
|
|
6
|
-
|
|
7
|
-
type NodeClass = new (...args: any[]) => DependencyNode;
|
|
8
|
-
type AbstractNodeClass = abstract new (...args: any[]) => DependencyNode;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* <!-- anchor:RegistrarBuilder -->
|
|
12
|
-
* A builder class for a {@link Registrar}.
|
|
13
|
-
* Builds a registrar that switches on node class--i.e., enables defining
|
|
14
|
-
* specific registration behaviour for specific node classes.
|
|
15
|
-
* Intelligently handles inheritance, always using the most specific
|
|
16
|
-
* handler defined for any given class.
|
|
17
|
-
* The registrar built shall return an object that maps from a handler ID,
|
|
18
|
-
* to a map from node name to the return of each handler,
|
|
19
|
-
* e.g., { objectNodes: { myObjectNode: 'my handler return' }}.
|
|
20
|
-
* This can be used to extract a specific resolved value after resolving the tree,
|
|
21
|
-
* given that the handlers return the resolved nodes.
|
|
22
|
-
*/
|
|
23
|
-
export class RegistrarBuilder<_ReturnTypes> {
|
|
24
|
-
protected defaultHandler: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;
|
|
25
|
-
|
|
26
|
-
protected nodeHandlers: Record<
|
|
27
|
-
string,
|
|
28
|
-
{
|
|
29
|
-
fn: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;
|
|
30
|
-
identifier: string;
|
|
31
|
-
}
|
|
32
|
-
> = {};
|
|
33
|
-
|
|
34
|
-
protected classHandlers: {
|
|
35
|
-
nodeClass: AbstractNodeClass;
|
|
36
|
-
identifier: string;
|
|
37
|
-
fn: (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>;
|
|
38
|
-
}[] = [];
|
|
39
|
-
|
|
40
|
-
protected classAliases: {
|
|
41
|
-
fromNodeClass: AbstractNodeClass;
|
|
42
|
-
getToNode: (from: DependencyNode) => DependencyNode;
|
|
43
|
-
}[] = [];
|
|
44
|
-
|
|
45
|
-
protected nodeAliases: Record<string, (node: DependencyNode) => DependencyNode> = {};
|
|
46
|
-
|
|
47
|
-
protected getEntrypoints: (
|
|
48
|
-
node: DependencyNode,
|
|
49
|
-
) => DependencyNode[] | Promise<DependencyNode[]> = (node) => [node];
|
|
50
|
-
|
|
51
|
-
constructor() {
|
|
52
|
-
this.defaultHandler = async (node: DependencyNode) => {
|
|
53
|
-
throw new Error(
|
|
54
|
-
`This registrar does not provide a handler for the dependency type used by ${JSON.stringify(node)}`,
|
|
55
|
-
);
|
|
56
|
-
};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
public addNodeHandler<
|
|
60
|
-
Node extends DependencyNode,
|
|
61
|
-
Function extends (node: Node, ancestry: DependencyNode[]) => Promise<any>,
|
|
62
|
-
>(node: DependencyNode, identifier: string, fn: Function) {
|
|
63
|
-
if (this.nodeHandlers[node.name]) {
|
|
64
|
-
throw new Error(
|
|
65
|
-
`Cannot define a node handler for an identifier that already has a handler: ${node.name}`,
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
this.nodeHandlers[node.name] = {
|
|
69
|
-
fn: fn as unknown as (node: DependencyNode, ancestry: DependencyNode[]) => Promise<any>,
|
|
70
|
-
identifier,
|
|
71
|
-
};
|
|
72
|
-
return this as RegistrarBuilder<
|
|
73
|
-
_ReturnTypes & {
|
|
74
|
-
['nodeHandlers']: { [nodeName: string]: Awaited<ReturnType<Function>> };
|
|
75
|
-
}
|
|
76
|
-
>;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Returns the class handler identifier that would be used for a given node,
|
|
81
|
-
* or undefined if no class handler matches.
|
|
82
|
-
*/
|
|
83
|
-
public getClassIdentifierForNode(node: DependencyNode): string | undefined {
|
|
84
|
-
for (const { nodeClass, identifier } of this.classHandlers) {
|
|
85
|
-
if (node instanceof nodeClass) {
|
|
86
|
-
return identifier;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return undefined;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Add a function fn to be called on all nodes of class
|
|
94
|
-
* @param nodeClass
|
|
95
|
-
* @param identifier
|
|
96
|
-
* @param fn
|
|
97
|
-
*/
|
|
98
|
-
public addClassHandler<
|
|
99
|
-
Identifier extends string,
|
|
100
|
-
_NodeClass extends NodeClass,
|
|
101
|
-
Function extends (node: InstanceType<_NodeClass>, ancestry: DependencyNode[]) => any,
|
|
102
|
-
>(nodeClass: _NodeClass, identifier: Identifier, fn: Function) {
|
|
103
|
-
if (this.classAliases.find(({ fromNodeClass }) => fromNodeClass === nodeClass)) {
|
|
104
|
-
throw new Error(
|
|
105
|
-
`Cannot define a handler for a class that is aliased to something else. This would be pointless`,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
let i = 0;
|
|
110
|
-
for (const handler of this.classHandlers) {
|
|
111
|
-
//check if we are overriding an existing handler
|
|
112
|
-
if (handler.nodeClass === nodeClass) {
|
|
113
|
-
this.classHandlers[i] = {
|
|
114
|
-
nodeClass,
|
|
115
|
-
identifier,
|
|
116
|
-
fn: fn as unknown as (node: DependencyNode) => Promise<any>,
|
|
117
|
-
};
|
|
118
|
-
|
|
119
|
-
return this as RegistrarBuilder<
|
|
120
|
-
_ReturnTypes & {
|
|
121
|
-
[_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };
|
|
122
|
-
}
|
|
123
|
-
>;
|
|
124
|
-
}
|
|
125
|
-
//check if the new handler has an ordering requirement against an existing handler
|
|
126
|
-
//(we always want to use more specific handlers if they exist)
|
|
127
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
128
|
-
if (handler.nodeClass.prototype.isPrototypeOf(nodeClass.prototype)) {
|
|
129
|
-
this.classHandlers.splice(i, 0, {
|
|
130
|
-
nodeClass,
|
|
131
|
-
identifier,
|
|
132
|
-
fn: fn as unknown as (node: DependencyNode) => Promise<any>,
|
|
133
|
-
});
|
|
134
|
-
|
|
135
|
-
return this as RegistrarBuilder<
|
|
136
|
-
_ReturnTypes & {
|
|
137
|
-
[_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };
|
|
138
|
-
}
|
|
139
|
-
>;
|
|
140
|
-
}
|
|
141
|
-
i++;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
this.classHandlers.push({
|
|
145
|
-
nodeClass,
|
|
146
|
-
identifier,
|
|
147
|
-
fn: fn as unknown as (node: DependencyNode) => Promise<any>,
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
return this as RegistrarBuilder<
|
|
151
|
-
_ReturnTypes & {
|
|
152
|
-
[_K in Identifier]: { [nodeName: string]: Awaited<ReturnType<Function>> };
|
|
153
|
-
}
|
|
154
|
-
>;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Alias some class *from*, such that any node of that class in the tree will be
|
|
159
|
-
* treated as though it were some other node *to*. The *to* node is given as a
|
|
160
|
-
* function returning a class instance, so that its dependencies, name, etc can be fixed.
|
|
161
|
-
* The registrar will ignore the dependencies of the original node, and follow the alias's
|
|
162
|
-
* dependencies instead.
|
|
163
|
-
*
|
|
164
|
-
* Additionally, this method does not affect the typing of the registrar. The output type
|
|
165
|
-
* of the build() method will not reflect the alias.
|
|
166
|
-
*
|
|
167
|
-
* @param fromNodeClass alias instances of this class
|
|
168
|
-
* @param getToNode method that returns the *to* part of the alias
|
|
169
|
-
*/
|
|
170
|
-
public addClassAlias<FromNodeClass extends AbstractNodeClass>(
|
|
171
|
-
fromNodeClass: FromNodeClass,
|
|
172
|
-
getToNode: (fromNodeInstance: InstanceType<FromNodeClass>) => DependencyNode,
|
|
173
|
-
) {
|
|
174
|
-
if (this.classHandlers.find(({ nodeClass }) => nodeClass === fromNodeClass)) {
|
|
175
|
-
throw new Error(`Cannot define an alias for a class that already has a handler`);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
let i = 0;
|
|
179
|
-
for (const alias of this.classAliases) {
|
|
180
|
-
//check if we are overriding an existing alias
|
|
181
|
-
if (alias.fromNodeClass === fromNodeClass) {
|
|
182
|
-
this.classAliases[i] = {
|
|
183
|
-
fromNodeClass,
|
|
184
|
-
getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
return this;
|
|
188
|
-
}
|
|
189
|
-
//check if the new alias has an ordering requirement against an existing alias
|
|
190
|
-
//(we always want to use more specific handlers if they exist)
|
|
191
|
-
// eslint-disable-next-line no-prototype-builtins
|
|
192
|
-
if (alias.fromNodeClass.prototype.isPrototypeOf(fromNodeClass.prototype)) {
|
|
193
|
-
this.classAliases.splice(i, 0, {
|
|
194
|
-
fromNodeClass,
|
|
195
|
-
getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
return this;
|
|
199
|
-
}
|
|
200
|
-
i++;
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
this.classAliases.push({
|
|
204
|
-
fromNodeClass,
|
|
205
|
-
getToNode: getToNode as unknown as (from: DependencyNode) => DependencyNode,
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
return this;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Alias a specific named node, such that when encountered in the tree,
|
|
213
|
-
* it will be transformed to a different node before processing.
|
|
214
|
-
* The transformed node's dependencies will be traversed instead.
|
|
215
|
-
* This is useful for short-circuiting dependency traversal for specific nodes.
|
|
216
|
-
*
|
|
217
|
-
* @param nodeName name of the node to alias
|
|
218
|
-
* @param getToNode method that returns the transformed node
|
|
219
|
-
*/
|
|
220
|
-
public addNodeAlias(
|
|
221
|
-
node: DependencyNode,
|
|
222
|
-
getToNode: (fromNodeInstance: DependencyNode) => DependencyNode,
|
|
223
|
-
) {
|
|
224
|
-
this.nodeAliases[node.name] = getToNode;
|
|
225
|
-
return this;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Set a custom entrypoint getter function. By default, the entrypoint getter
|
|
230
|
-
* returns the node passed in as the sole entrypoint.
|
|
231
|
-
*/
|
|
232
|
-
public setEntrypointsGetter(
|
|
233
|
-
fn: (node: DependencyNode) => DependencyNode[] | Promise<DependencyNode[]>,
|
|
234
|
-
) {
|
|
235
|
-
this.getEntrypoints = fn;
|
|
236
|
-
return this;
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Add a default function fn to be used on nodes whose types do not have handlers.
|
|
241
|
-
* If this is not set, the default behaviour will be to throw
|
|
242
|
-
* @param fn
|
|
243
|
-
*/
|
|
244
|
-
public addDefault(fn: (node: DependencyNode, ancestry: DependencyNode[]) => any) {
|
|
245
|
-
this.defaultHandler = fn;
|
|
246
|
-
return this;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
public build(): Registrar<_ReturnTypes> {
|
|
250
|
-
const mergedNodePrehandler = (node: DependencyNode) => {
|
|
251
|
-
// Check node aliases first (more specific)
|
|
252
|
-
const nodeAlias = this.nodeAliases[node.name];
|
|
253
|
-
if (nodeAlias) {
|
|
254
|
-
return nodeAlias(node);
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Then check class aliases
|
|
258
|
-
for (const { fromNodeClass, getToNode: toNode } of this.classAliases) {
|
|
259
|
-
if (node instanceof fromNodeClass) {
|
|
260
|
-
return toNode(node);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
return node;
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
const mergedNodeHandler = async (node: DependencyNode, ancestry: DependencyNode[]) => {
|
|
267
|
-
if (node.name === '_virtualEntrypoint') {
|
|
268
|
-
return {
|
|
269
|
-
key: node.name,
|
|
270
|
-
value: undefined,
|
|
271
|
-
};
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
const nodeHandler = this.nodeHandlers[node.name];
|
|
275
|
-
if (nodeHandler) {
|
|
276
|
-
return {
|
|
277
|
-
key: nodeHandler.identifier ?? node.name,
|
|
278
|
-
value: await nodeHandler.fn(node, ancestry),
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
for (const { nodeClass, identifier, fn } of this.classHandlers) {
|
|
283
|
-
if (node instanceof nodeClass) {
|
|
284
|
-
return {
|
|
285
|
-
key: identifier,
|
|
286
|
-
value: await fn(node, ancestry),
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
return {
|
|
292
|
-
key: 'default',
|
|
293
|
-
value: await this.defaultHandler(node, ancestry),
|
|
294
|
-
};
|
|
295
|
-
};
|
|
296
|
-
return {
|
|
297
|
-
traverseDependencies: async (node: DependencyNode) => {
|
|
298
|
-
const entrypoints = await this.getEntrypoints(node);
|
|
299
|
-
if (entrypoints.length === 1)
|
|
300
|
-
return await dfs<_ReturnTypes>(node, mergedNodeHandler, mergedNodePrehandler)();
|
|
301
|
-
|
|
302
|
-
const dependencies = entrypoints.reduce((acc, cur) => {
|
|
303
|
-
acc[cur.name] = cur;
|
|
304
|
-
return acc;
|
|
305
|
-
}, {} as Dependencies);
|
|
306
|
-
|
|
307
|
-
class VirtualNode extends DependencyNode {}
|
|
308
|
-
|
|
309
|
-
const virtualEntrypoint = new VirtualNode({
|
|
310
|
-
name: '_virtualEntrypoint',
|
|
311
|
-
dependencies,
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
return await dfs<_ReturnTypes>(
|
|
315
|
-
virtualEntrypoint,
|
|
316
|
-
mergedNodeHandler,
|
|
317
|
-
mergedNodePrehandler,
|
|
318
|
-
)();
|
|
319
|
-
},
|
|
320
|
-
};
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
export const multiplexOrderedRegistrars = <
|
|
325
|
-
const T extends [name: string, registrar: Registrar<{}>][],
|
|
326
|
-
>(
|
|
327
|
-
...registrars: T
|
|
328
|
-
) => {
|
|
329
|
-
const multiplexRegistrar: Registrar<{
|
|
330
|
-
[K in Extract<keyof T, number> as T[K] extends readonly [infer Name extends string, any]
|
|
331
|
-
? Name
|
|
332
|
-
: never]: T[K] extends readonly [any, Registrar<infer Ret>] ? Ret : never;
|
|
333
|
-
}> = {
|
|
334
|
-
traverseDependencies: async (
|
|
335
|
-
...params: Parameters<Registrar<any>['traverseDependencies']>
|
|
336
|
-
) => {
|
|
337
|
-
const acc: Record<string, any> = {};
|
|
338
|
-
for (const [name, registrar] of registrars) {
|
|
339
|
-
const result = await registrar.traverseDependencies(...params);
|
|
340
|
-
acc[name] = result;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
return acc;
|
|
344
|
-
},
|
|
345
|
-
} as any;
|
|
346
|
-
|
|
347
|
-
return multiplexRegistrar;
|
|
348
|
-
};
|
package/src/utils.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type z from 'zod';
|
|
2
|
-
|
|
3
|
-
import type { AdvancedRecord } from '@layerzerolabs/typescript-utils';
|
|
4
|
-
|
|
5
|
-
import type { Factory } from './definitions';
|
|
6
|
-
import { type FactoryDefinition, serializeDimensions } from './definitions';
|
|
7
|
-
|
|
8
|
-
export const extractSchemaFromFactory = <Dim extends z.ZodObject>(
|
|
9
|
-
factory: FactoryDefinition<any, Dim, any, any, any> | Factory<any, any, any, any>,
|
|
10
|
-
dimension: z.infer<Dim>,
|
|
11
|
-
): z.ZodObject => {
|
|
12
|
-
const schema =
|
|
13
|
-
(factory.dimensionToSchemaMap.byDimension &&
|
|
14
|
-
Object.values(factory.dimensionToSchemaMap.byDimension as AdvancedRecord).find(
|
|
15
|
-
([dim]) => serializeDimensions(dim) === serializeDimensions(dimension),
|
|
16
|
-
)?.[1]) ??
|
|
17
|
-
factory.dimensionToSchemaMap.base;
|
|
18
|
-
if (!schema?.shape) {
|
|
19
|
-
throw new Error(
|
|
20
|
-
`Couldn't resolve the schema of factory ${factory.name} for dim ${JSON.stringify(dimension)}`,
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return schema;
|
|
25
|
-
};
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { describe, expect, test } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import { Dependencies, DependencyNode } from '@layerzerolabs/dependency-graph';
|
|
4
|
-
|
|
5
|
-
import { RegistrarBuilder } from '../src/registrarBuilder';
|
|
6
|
-
|
|
7
|
-
const SimpleClassA = class<
|
|
8
|
-
Name extends string,
|
|
9
|
-
_Dependencies extends Dependencies,
|
|
10
|
-
> extends DependencyNode<Name, _Dependencies> {};
|
|
11
|
-
|
|
12
|
-
const mySimpleClassA1 = new SimpleClassA({ name: 'MySimpleClassA' });
|
|
13
|
-
const mySimpleClassA2 = new SimpleClassA({ name: 'MySimpleClassA' });
|
|
14
|
-
|
|
15
|
-
const ExtendsClassA = class<
|
|
16
|
-
Name extends string,
|
|
17
|
-
_Dependencies extends Dependencies,
|
|
18
|
-
> extends SimpleClassA<Name, _Dependencies> {};
|
|
19
|
-
|
|
20
|
-
const myExtendsClassA1 = new ExtendsClassA({ name: 'MyExtendsClassA' });
|
|
21
|
-
|
|
22
|
-
const ExtendsExtendsClassA = class<
|
|
23
|
-
Name extends string,
|
|
24
|
-
_Dependencies extends Dependencies,
|
|
25
|
-
> extends ExtendsClassA<Name, _Dependencies> {};
|
|
26
|
-
|
|
27
|
-
const myExtendsExtendsClassA1 = new ExtendsExtendsClassA({ name: 'MyExtendsExtendsClassA' });
|
|
28
|
-
|
|
29
|
-
const SimpleClassB = class<
|
|
30
|
-
Name extends string,
|
|
31
|
-
_Dependencies extends Dependencies,
|
|
32
|
-
> extends DependencyNode<Name, _Dependencies> {};
|
|
33
|
-
|
|
34
|
-
const mySimpleClassB1 = new SimpleClassB({ name: 'MySimpleClassB' });
|
|
35
|
-
const mySimpleClassB2 = new SimpleClassB({
|
|
36
|
-
name: 'MySimpleClassB',
|
|
37
|
-
dependencies: { mySimpleClassA1 },
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
const SimpleClassC = class<
|
|
41
|
-
Name extends string,
|
|
42
|
-
_Dependencies extends Dependencies,
|
|
43
|
-
> extends DependencyNode<Name, _Dependencies> {};
|
|
44
|
-
|
|
45
|
-
const mySimpleClassC1 = new SimpleClassC({ name: 'MySimpleClassC' });
|
|
46
|
-
const mySimpleClassC2 = new SimpleClassC({
|
|
47
|
-
name: 'MySimpleClassC',
|
|
48
|
-
dependencies: { mySimpleClassB2, mySimpleClassA1 },
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe('Registrar builder', () => {
|
|
52
|
-
test('The registered callbacks should be called in order, such that dependencies are called before dependants', async () => {
|
|
53
|
-
let order: string[] = [];
|
|
54
|
-
const builder = new RegistrarBuilder()
|
|
55
|
-
.addClassHandler(SimpleClassA, 'handler', () => order.push('A'))
|
|
56
|
-
.addClassHandler(SimpleClassB, 'handler', () => order.push('B'))
|
|
57
|
-
.addDefault(() => {})
|
|
58
|
-
.build();
|
|
59
|
-
|
|
60
|
-
await builder.traverseDependencies(mySimpleClassB1);
|
|
61
|
-
expect(order).toStrictEqual(['B']);
|
|
62
|
-
order = [];
|
|
63
|
-
await builder.traverseDependencies(mySimpleClassA1);
|
|
64
|
-
expect(order).toStrictEqual(['A']);
|
|
65
|
-
order = [];
|
|
66
|
-
await builder.traverseDependencies(mySimpleClassB2);
|
|
67
|
-
expect(order).toStrictEqual(['A', 'B']);
|
|
68
|
-
});
|
|
69
|
-
test('The registered callback should be called only once for each unique definition', async () => {
|
|
70
|
-
let calledCount = 0;
|
|
71
|
-
let defaultCount = 0;
|
|
72
|
-
const builder = new RegistrarBuilder()
|
|
73
|
-
.addClassHandler(SimpleClassA, 'handler', () => calledCount++)
|
|
74
|
-
.addDefault(() => defaultCount++)
|
|
75
|
-
.build();
|
|
76
|
-
|
|
77
|
-
await builder.traverseDependencies(mySimpleClassC1);
|
|
78
|
-
expect(calledCount).toBe(0);
|
|
79
|
-
await builder.traverseDependencies(mySimpleClassC2);
|
|
80
|
-
expect(calledCount).toBe(1);
|
|
81
|
-
await builder.traverseDependencies(mySimpleClassA1);
|
|
82
|
-
await builder.traverseDependencies(mySimpleClassA2);
|
|
83
|
-
expect(calledCount).toBe(3);
|
|
84
|
-
expect(defaultCount).toBe(3);
|
|
85
|
-
await builder.traverseDependencies(mySimpleClassC1);
|
|
86
|
-
expect(calledCount).toBe(3);
|
|
87
|
-
expect(defaultCount).toBe(4);
|
|
88
|
-
});
|
|
89
|
-
test('The most specific applicable handler should always be called, regardless of definition order', async () => {
|
|
90
|
-
let calledCount: number[] = [0, 0, 0];
|
|
91
|
-
const builder = new RegistrarBuilder()
|
|
92
|
-
.addClassHandler(ExtendsClassA, 'handler', () => calledCount[1]++)
|
|
93
|
-
.addClassHandler(SimpleClassA, 'handler', () => calledCount[0]++)
|
|
94
|
-
.addClassHandler(ExtendsExtendsClassA, 'handler', () => calledCount[2]++)
|
|
95
|
-
.addDefault(() => {})
|
|
96
|
-
.build();
|
|
97
|
-
|
|
98
|
-
await builder.traverseDependencies(myExtendsClassA1);
|
|
99
|
-
expect(calledCount).toStrictEqual([0, 1, 0]);
|
|
100
|
-
await builder.traverseDependencies(mySimpleClassA1);
|
|
101
|
-
expect(calledCount).toStrictEqual([1, 1, 0]);
|
|
102
|
-
await builder.traverseDependencies(myExtendsExtendsClassA1);
|
|
103
|
-
await builder.traverseDependencies(myExtendsExtendsClassA1);
|
|
104
|
-
expect(calledCount).toStrictEqual([1, 1, 2]);
|
|
105
|
-
});
|
|
106
|
-
test('The traverseDependencies method should return an object with the keys defined in the handlers, and the returns of those handlers', async () => {
|
|
107
|
-
let inc = 0;
|
|
108
|
-
const builder = new RegistrarBuilder()
|
|
109
|
-
.addClassHandler(SimpleClassA, 'simpleA', () => inc++)
|
|
110
|
-
.addClassHandler(SimpleClassB, 'simpleB', () => inc++)
|
|
111
|
-
.addDefault(() => {})
|
|
112
|
-
.build();
|
|
113
|
-
|
|
114
|
-
const ret = await builder.traverseDependencies(mySimpleClassA1);
|
|
115
|
-
expect(ret).toStrictEqual({ simpleA: { MySimpleClassA: 0 } });
|
|
116
|
-
const ret2 = await builder.traverseDependencies(mySimpleClassB2);
|
|
117
|
-
expect(ret2).toStrictEqual({
|
|
118
|
-
simpleA: { MySimpleClassA: 1 },
|
|
119
|
-
simpleB: { MySimpleClassB: 2 },
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
describe('Node handlers', () => {
|
|
125
|
-
test('Should take priority over class handlers', async () => {
|
|
126
|
-
let classHandlerCalled = false;
|
|
127
|
-
let namedHandlerCalled = false;
|
|
128
|
-
|
|
129
|
-
const builder = new RegistrarBuilder()
|
|
130
|
-
.addClassHandler(SimpleClassA, 'simpleA', async () => {
|
|
131
|
-
classHandlerCalled = true;
|
|
132
|
-
return 'from-class';
|
|
133
|
-
})
|
|
134
|
-
.addNodeHandler(mySimpleClassA1, 'MySimpleClassA', async () => {
|
|
135
|
-
namedHandlerCalled = true;
|
|
136
|
-
return 'from-named';
|
|
137
|
-
})
|
|
138
|
-
.build();
|
|
139
|
-
|
|
140
|
-
await builder.traverseDependencies(mySimpleClassA1);
|
|
141
|
-
|
|
142
|
-
expect(namedHandlerCalled).toBe(true);
|
|
143
|
-
expect(classHandlerCalled).toBe(false);
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
test('Should be invoked with correct arguments and return values', async () => {
|
|
147
|
-
let receivedNode: DependencyNode | undefined;
|
|
148
|
-
let receivedAncestry: DependencyNode[] | undefined;
|
|
149
|
-
|
|
150
|
-
const builder = new RegistrarBuilder()
|
|
151
|
-
.addNodeHandler(mySimpleClassA1, 'MySimpleClassA', async (node, ancestry) => {
|
|
152
|
-
receivedNode = node;
|
|
153
|
-
receivedAncestry = ancestry;
|
|
154
|
-
return 'named-result';
|
|
155
|
-
})
|
|
156
|
-
.build();
|
|
157
|
-
|
|
158
|
-
const result = await builder.traverseDependencies(mySimpleClassA1);
|
|
159
|
-
|
|
160
|
-
expect(receivedNode).toBe(mySimpleClassA1);
|
|
161
|
-
expect(receivedAncestry).toStrictEqual([]);
|
|
162
|
-
// Result is grouped by handler identifier (node name for named handlers)
|
|
163
|
-
expect(result).toStrictEqual({ MySimpleClassA: { MySimpleClassA: 'named-result' } });
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
test('Registering duplicate node handlers should throw', () => {
|
|
167
|
-
expect(() => {
|
|
168
|
-
new RegistrarBuilder()
|
|
169
|
-
.addNodeHandler(mySimpleClassA1, 'MySimpleClassA', async () => 'first')
|
|
170
|
-
.addNodeHandler(mySimpleClassA1, 'MySimpleClassA', async () => 'second');
|
|
171
|
-
}).toThrow(
|
|
172
|
-
'Cannot define a node handler for an identifier that already has a handler: MySimpleClassA',
|
|
173
|
-
);
|
|
174
|
-
});
|
|
175
|
-
});
|
package/tsconfig.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "@layerzerolabs/typescript-configuration/tsconfig.base.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"rootDir": "./src",
|
|
5
|
-
"outDir": "./dist",
|
|
6
|
-
"strictPropertyInitialization": false,
|
|
7
|
-
"noUnusedLocals": false,
|
|
8
|
-
"noUnusedParameters": false,
|
|
9
|
-
"jsx": "react-jsx"
|
|
10
|
-
},
|
|
11
|
-
"exclude": [
|
|
12
|
-
"node_modules",
|
|
13
|
-
"**/__mocks__/*",
|
|
14
|
-
"**/__tests__/*",
|
|
15
|
-
"**/*.spec.ts",
|
|
16
|
-
"**/*.test.ts",
|
|
17
|
-
"dist"
|
|
18
|
-
],
|
|
19
|
-
"include": ["src/**/*"]
|
|
20
|
-
}
|