class-resolver 2.1.1 → 4.0.0

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/index.mjs ADDED
@@ -0,0 +1,163 @@
1
+ class i {
2
+ /**
3
+ * Array of registered resolver targets
4
+ * @private
5
+ */
6
+ updaters = [];
7
+ /**
8
+ * Fallback handler function
9
+ * @private
10
+ */
11
+ fallbackHandler;
12
+ /**
13
+ * Initializes the resolver
14
+ * @param args Initial resolver targets
15
+ */
16
+ constructor(...t) {
17
+ t.length > 0 && this.set(t);
18
+ }
19
+ /**
20
+ * Processes an array of arguments
21
+ * @param args Array of resolver targets
22
+ * @returns Processed array
23
+ * @private
24
+ */
25
+ getArgs(t) {
26
+ return [...t];
27
+ }
28
+ /**
29
+ * Sets resolver targets
30
+ * @param updaters Array of resolver targets
31
+ */
32
+ set(t) {
33
+ this.updaters = t;
34
+ }
35
+ /**
36
+ * Sets resolver targets (variadic version)
37
+ * @param args Resolver targets
38
+ */
39
+ setUpdaters(...t) {
40
+ this.set(this.getArgs(t));
41
+ }
42
+ /**
43
+ * Adds a resolver target
44
+ * @param updater Resolver target to add
45
+ */
46
+ addUpdater(t) {
47
+ return this.updaters.push(t), this;
48
+ }
49
+ /**
50
+ * Sets a fallback handler for unsupported types
51
+ * @param handler Fallback handler function
52
+ * @returns This resolver instance for method chaining
53
+ */
54
+ setFallbackHandler(t) {
55
+ return this.fallbackHandler = t, this;
56
+ }
57
+ /**
58
+ * Gets the priority of a handler
59
+ * @param handler The handler to get priority for
60
+ * @returns Priority value (0 if not defined)
61
+ * @private
62
+ */
63
+ getPriority(t) {
64
+ const r = t;
65
+ return typeof r.priority == "number" ? r.priority : 0;
66
+ }
67
+ /**
68
+ * Sorts handlers by priority (highest first), maintaining registration order for equal priorities
69
+ * @param handlers Array of handlers to sort
70
+ * @returns Sorted array of handlers
71
+ * @private
72
+ */
73
+ sortByPriority(t) {
74
+ return [...t].sort((r, s) => {
75
+ const e = this.getPriority(r);
76
+ return this.getPriority(s) - e;
77
+ });
78
+ }
79
+ /**
80
+ * Resolves a resolver target for the specified type
81
+ * @param type Type to resolve
82
+ * @returns Resolved resolver target
83
+ * @throws {Error} When no resolver targets are registered
84
+ * @throws {Error} When no resolver target supporting the specified type is found and no fallback is set
85
+ */
86
+ resolve(t) {
87
+ if (this.updaters.length < 1)
88
+ throw new Error("Unassigned resolve target.");
89
+ const r = this.updaters.filter((l) => l.supports(t)), e = this.sortByPriority(r)[0];
90
+ if (!e) {
91
+ if (this.fallbackHandler)
92
+ return {
93
+ supports: () => !0,
94
+ handle: this.fallbackHandler
95
+ };
96
+ const l = typeof t == "object" && t !== null ? JSON.stringify(t) : String(t);
97
+ throw new Error(`Unsupported type: ${l}`);
98
+ }
99
+ return e;
100
+ }
101
+ /**
102
+ * Resolves all resolver targets for the specified type
103
+ * @param type Type to resolve
104
+ * @returns Array of all matching resolver targets sorted by priority (highest first).
105
+ * Returns an empty array if no handlers match and no fallback is set.
106
+ * Note: Unlike resolve(), this method does not throw when no handlers match.
107
+ * @throws {Error} When no resolver targets are registered
108
+ */
109
+ resolveAll(t) {
110
+ if (this.updaters.length < 1)
111
+ throw new Error("Unassigned resolve target.");
112
+ const r = this.updaters.filter((s) => s.supports(t));
113
+ return r.length === 0 ? this.fallbackHandler ? [
114
+ {
115
+ supports: () => !0,
116
+ handle: this.fallbackHandler
117
+ }
118
+ ] : [] : this.sortByPriority(r);
119
+ }
120
+ /**
121
+ * Executes all matching handlers for the specified type
122
+ * @param type Type to resolve
123
+ * @param args Arguments to pass to the handlers
124
+ * @returns Array of results from all matching handlers
125
+ * @throws {Error} When no resolver targets are registered
126
+ */
127
+ handleAll(t, ...r) {
128
+ return this.resolveAll(t).map((e) => e.handle(...r));
129
+ }
130
+ /**
131
+ * Executes all matching async handlers in parallel for the specified type
132
+ * @param type Type to resolve
133
+ * @param args Arguments to pass to the handlers
134
+ * @returns Promise that resolves to array of results from all matching handlers
135
+ * @throws {Error} When no resolver targets are registered
136
+ */
137
+ async handleAllAsync(t, ...r) {
138
+ const e = this.resolveAll(t).map((l) => l.handle(...r));
139
+ return Promise.all(e);
140
+ }
141
+ /**
142
+ * Executes all matching async handlers sequentially for the specified type
143
+ * Stops on first error
144
+ * @param type Type to resolve
145
+ * @param args Arguments to pass to the handlers
146
+ * @returns Promise that resolves to array of results from all matching handlers
147
+ * @throws {Error} When no resolver targets are registered
148
+ * @throws {Error} When any handler throws an error
149
+ */
150
+ async handleAllSequential(t, ...r) {
151
+ const s = this.resolveAll(t), e = [];
152
+ for (const l of s) {
153
+ const n = await l.handle(...r);
154
+ e.push(n);
155
+ }
156
+ return e;
157
+ }
158
+ }
159
+ module.exports = i;
160
+ export {
161
+ i as default
162
+ };
163
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sources":["../libs/resolver.ts","../libs/index.ts"],"sourcesContent":["import type { ResolveTarget } from './interface';\n\n/**\n * Resolver class implementing the Chain of Responsibility pattern\n * Resolves handlers for specific types\n */\nclass Resolver<\n TBase extends ResolveTarget<any[], any, any> = ResolveTarget<any[], any, any>,\n TType = string,\n> {\n /**\n * Array of registered resolver targets\n * @private\n */\n private updaters: TBase[] = [];\n\n /**\n * Fallback handler function\n * @private\n */\n private fallbackHandler?: (...args: Parameters<TBase['handle']>) => ReturnType<TBase['handle']>;\n\n /**\n * Initializes the resolver\n * @param args Initial resolver targets\n */\n constructor(...args: TBase[]) {\n if (args.length > 0) {\n this.set(args);\n }\n }\n\n /**\n * Processes an array of arguments\n * @param args Array of resolver targets\n * @returns Processed array\n * @private\n */\n private getArgs(args: TBase[]): TBase[] {\n return [...args];\n }\n\n /**\n * Sets resolver targets\n * @param updaters Array of resolver targets\n */\n public set(updaters: TBase[]): void {\n this.updaters = updaters;\n }\n\n /**\n * Sets resolver targets (variadic version)\n * @param args Resolver targets\n */\n public setUpdaters(...args: TBase[]): void {\n this.set(this.getArgs(args));\n }\n\n /**\n * Adds a resolver target\n * @param updater Resolver target to add\n */\n public addUpdater(updater: TBase): this {\n this.updaters.push(updater);\n return this;\n }\n\n /**\n * Sets a fallback handler for unsupported types\n * @param handler Fallback handler function\n * @returns This resolver instance for method chaining\n */\n public setFallbackHandler(\n handler: (...args: Parameters<TBase['handle']>) => ReturnType<TBase['handle']>\n ): this {\n this.fallbackHandler = handler;\n return this;\n }\n\n /**\n * Gets the priority of a handler\n * @param handler The handler to get priority for\n * @returns Priority value (0 if not defined)\n * @private\n */\n private getPriority(handler: TBase): number {\n const handlerWithPriority = handler as Partial<{ priority: number }>;\n return typeof handlerWithPriority.priority === 'number' ? handlerWithPriority.priority : 0;\n }\n\n /**\n * Sorts handlers by priority (highest first), maintaining registration order for equal priorities\n * @param handlers Array of handlers to sort\n * @returns Sorted array of handlers\n * @private\n */\n private sortByPriority(handlers: TBase[]): TBase[] {\n return [...handlers].sort((a, b) => {\n const priorityA = this.getPriority(a);\n const priorityB = this.getPriority(b);\n return priorityB - priorityA; // Higher priority first\n });\n }\n\n /**\n * Resolves a resolver target for the specified type\n * @param type Type to resolve\n * @returns Resolved resolver target\n * @throws {Error} When no resolver targets are registered\n * @throws {Error} When no resolver target supporting the specified type is found and no fallback is set\n */\n public resolve(type: TType): TBase {\n if (this.updaters.length < 1) {\n throw new Error('Unassigned resolve target.');\n }\n\n // Get all matching handlers and sort by priority\n const matchingHandlers = this.updaters.filter((updater) => updater.supports(type));\n const sortedHandlers = this.sortByPriority(matchingHandlers);\n const target = sortedHandlers[0];\n\n if (!target) {\n // If fallback handler is set, create a temporary target that uses it\n if (this.fallbackHandler) {\n return {\n supports: () => true,\n handle: this.fallbackHandler,\n } as unknown as TBase;\n }\n\n // Determine the string representation of the unsupported type\n // If it's a non-null object, use JSON.stringify for detailed output\n // Otherwise, use String() for basic conversion\n const typeString =\n typeof type === 'object' && type !== null ? JSON.stringify(type) : String(type);\n throw new Error(`Unsupported type: ${typeString}`);\n }\n\n return target;\n }\n\n /**\n * Resolves all resolver targets for the specified type\n * @param type Type to resolve\n * @returns Array of all matching resolver targets sorted by priority (highest first).\n * Returns an empty array if no handlers match and no fallback is set.\n * Note: Unlike resolve(), this method does not throw when no handlers match.\n * @throws {Error} When no resolver targets are registered\n */\n public resolveAll(type: TType): TBase[] {\n if (this.updaters.length < 1) {\n throw new Error('Unassigned resolve target.');\n }\n\n const targets = this.updaters.filter((updater) => updater.supports(type));\n\n if (targets.length === 0) {\n // If fallback handler is set, return it as a single-element array\n if (this.fallbackHandler) {\n return [\n {\n supports: () => true,\n handle: this.fallbackHandler,\n } as unknown as TBase,\n ];\n }\n\n // Return empty array if no handlers match\n return [];\n }\n\n // Sort by priority (highest first)\n return this.sortByPriority(targets);\n }\n\n /**\n * Executes all matching handlers for the specified type\n * @param type Type to resolve\n * @param args Arguments to pass to the handlers\n * @returns Array of results from all matching handlers\n * @throws {Error} When no resolver targets are registered\n */\n public handleAll(\n type: TType,\n ...args: Parameters<TBase['handle']>\n ): ReturnType<TBase['handle']>[] {\n const targets = this.resolveAll(type);\n return targets.map((target) => target.handle(...args));\n }\n\n /**\n * Executes all matching async handlers in parallel for the specified type\n * @param type Type to resolve\n * @param args Arguments to pass to the handlers\n * @returns Promise that resolves to array of results from all matching handlers\n * @throws {Error} When no resolver targets are registered\n */\n public async handleAllAsync(\n type: TType,\n ...args: Parameters<TBase['handle']>\n ): Promise<Awaited<ReturnType<TBase['handle']>>[]> {\n const targets = this.resolveAll(type);\n const promises = targets.map((target) => target.handle(...args));\n return Promise.all(promises);\n }\n\n /**\n * Executes all matching async handlers sequentially for the specified type\n * Stops on first error\n * @param type Type to resolve\n * @param args Arguments to pass to the handlers\n * @returns Promise that resolves to array of results from all matching handlers\n * @throws {Error} When no resolver targets are registered\n * @throws {Error} When any handler throws an error\n */\n public async handleAllSequential(\n type: TType,\n ...args: Parameters<TBase['handle']>\n ): Promise<Awaited<ReturnType<TBase['handle']>>[]> {\n const targets = this.resolveAll(type);\n const results: Awaited<ReturnType<TBase['handle']>>[] = [];\n\n for (const target of targets) {\n const result = await target.handle(...args);\n results.push(result);\n }\n\n return results;\n }\n}\n\nexport default Resolver;\n","export * from './interface';\nimport Resolver from './resolver';\nexport default Resolver;\n// Export for CommonJS compatibility\n// @ts-ignore\nmodule.exports = Resolver;\n"],"names":["Resolver","args","updaters","updater","handler","handlerWithPriority","handlers","a","b","priorityA","type","matchingHandlers","target","typeString","targets","promises","results","result"],"mappings":"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"}
package/package.json CHANGED
@@ -1,15 +1,37 @@
1
1
  {
2
2
  "name": "class-resolver",
3
- "version": "2.1.1",
3
+ "version": "4.0.0",
4
4
  "description": "Simple class resolver.",
5
- "main": "dist/index.js",
5
+ "type": "module",
6
+ "main": "dist/index.cjs",
7
+ "module": "dist/index.mjs",
6
8
  "types": "dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.mjs"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ }
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
7
24
  "scripts": {
8
- "build": "tsc",
9
- "test": "jest",
10
- "test:watch": "jest --watch",
11
- "test:dev": "jest --watch --silent=false --verbose false --coverage",
12
- "prepublishOnly": "npm run build"
25
+ "build": "vite build",
26
+ "test": "vitest run",
27
+ "test:watch": "vitest",
28
+ "test:ui": "vitest --ui",
29
+ "test:coverage": "vitest run --coverage",
30
+ "lint": "biome check .",
31
+ "lint:fix": "biome check --write .",
32
+ "format": "biome format --write .",
33
+ "prepublishOnly": "npm run build",
34
+ "release": "np"
13
35
  },
14
36
  "keywords": [
15
37
  "resolver",
@@ -19,29 +41,15 @@
19
41
  "author": "Hidetaka Okamoto <info@wp-kyoto.net> (https://wp-kyoto.net)",
20
42
  "license": "MIT",
21
43
  "devDependencies": {
22
- "@types/jest": "^29.5.0",
23
- "@types/node": "^20.0.0",
24
- "jest": "^29.5.0",
25
- "ts-jest": "^29.1.0",
26
- "typescript": "^5.0.0"
27
- },
28
- "jest": {
29
- "moduleFileExtensions": [
30
- "ts",
31
- "tsx",
32
- "js"
33
- ],
34
- "transform": {
35
- "^.+\\.(ts|tsx)$": [
36
- "ts-jest",
37
- {
38
- "tsconfig": "tsconfig.json"
39
- }
40
- ]
41
- },
42
- "testMatch": [
43
- "**/__tests__/*.+(ts|tsx|js)"
44
- ]
44
+ "@biomejs/biome": "^1.9.4",
45
+ "@types/node": "^22.0.0",
46
+ "@vitest/coverage-v8": "^4.0.9",
47
+ "@vitest/ui": "^4.0.9",
48
+ "np": "^10.2.0",
49
+ "typescript": "^5.7.2",
50
+ "vite": "^7.2.2",
51
+ "vite-plugin-dts": "^4.3.0",
52
+ "vitest": "^4.0.9"
45
53
  },
46
54
  "repository": {
47
55
  "type": "git",
@@ -49,5 +57,8 @@
49
57
  },
50
58
  "engines": {
51
59
  "node": ">=14.0.0"
60
+ },
61
+ "np": {
62
+ "yarn": false
52
63
  }
53
64
  }
package/dist/index.js DELETED
@@ -1,26 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- var __importDefault = (this && this.__importDefault) || function (mod) {
17
- return (mod && mod.__esModule) ? mod : { "default": mod };
18
- };
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- __exportStar(require("./interface"), exports);
21
- const resolver_1 = __importDefault(require("./resolver"));
22
- exports.default = resolver_1.default;
23
- // Export for CommonJS compatibility
24
- // @ts-ignore
25
- module.exports = resolver_1.default;
26
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../libs/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;AAAA,8CAA2B;AAC3B,0DAAiC;AACjC,kBAAe,kBAAQ,CAAA;AACvB,oCAAoC;AACpC,aAAa;AACb,MAAM,CAAC,OAAO,GAAG,kBAAQ,CAAA"}
@@ -1,23 +0,0 @@
1
- /**
2
- * Interface that classes which are targets for the resolver should implement
3
- */
4
- export interface ResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> {
5
- /**
6
- * Determines whether the specified type is supported
7
- * @param type The type to check for support
8
- * @returns true if supported, false otherwise
9
- */
10
- supports(type: TType): boolean;
11
- /**
12
- * Handles the request
13
- * @param args Arguments needed for processing
14
- * @returns Processing result
15
- */
16
- handle(...args: TArgs): TReturn;
17
- }
18
- export declare namespace interfaces {
19
- interface ResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> {
20
- supports(type: TType): boolean;
21
- handle(...args: TArgs): TReturn;
22
- }
23
- }
package/dist/interface.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- //# sourceMappingURL=interface.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"interface.js","sourceRoot":"","sources":["../libs/interface.ts"],"names":[],"mappings":""}
@@ -1,48 +0,0 @@
1
- import { ResolveTarget } from './interface';
2
- /**
3
- * Resolver class implementing the Chain of Responsibility pattern
4
- * Resolves handlers for specific types
5
- */
6
- declare class Resolver<TBase extends ResolveTarget<any[], any, any> = ResolveTarget<any[], any, any>, TType = string> {
7
- /**
8
- * Array of registered resolver targets
9
- * @private
10
- */
11
- private updaters;
12
- /**
13
- * Initializes the resolver
14
- * @param args Initial resolver targets
15
- */
16
- constructor(...args: TBase[]);
17
- /**
18
- * Processes an array of arguments
19
- * @param args Array of resolver targets
20
- * @returns Processed array
21
- * @private
22
- */
23
- private getArgs;
24
- /**
25
- * Sets resolver targets
26
- * @param updaters Array of resolver targets
27
- */
28
- set(updaters: TBase[]): void;
29
- /**
30
- * Sets resolver targets (variadic version)
31
- * @param args Resolver targets
32
- */
33
- setUpdaters(...args: TBase[]): void;
34
- /**
35
- * Adds a resolver target
36
- * @param updater Resolver target to add
37
- */
38
- addUpdater(updater: TBase): void;
39
- /**
40
- * Resolves a resolver target for the specified type
41
- * @param type Type to resolve
42
- * @returns Resolved resolver target
43
- * @throws {Error} When no resolver targets are registered
44
- * @throws {Error} When no resolver target supporting the specified type is found
45
- */
46
- resolve(type: TType): TBase;
47
- }
48
- export default Resolver;
package/dist/resolver.js DELETED
@@ -1,75 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /**
4
- * Resolver class implementing the Chain of Responsibility pattern
5
- * Resolves handlers for specific types
6
- */
7
- class Resolver {
8
- /**
9
- * Initializes the resolver
10
- * @param args Initial resolver targets
11
- */
12
- constructor(...args) {
13
- /**
14
- * Array of registered resolver targets
15
- * @private
16
- */
17
- this.updaters = [];
18
- if (args.length > 0) {
19
- this.set(args);
20
- }
21
- }
22
- /**
23
- * Processes an array of arguments
24
- * @param args Array of resolver targets
25
- * @returns Processed array
26
- * @private
27
- */
28
- getArgs(args) {
29
- return [...args];
30
- }
31
- /**
32
- * Sets resolver targets
33
- * @param updaters Array of resolver targets
34
- */
35
- set(updaters) {
36
- this.updaters = updaters;
37
- }
38
- /**
39
- * Sets resolver targets (variadic version)
40
- * @param args Resolver targets
41
- */
42
- setUpdaters(...args) {
43
- this.set(this.getArgs(args));
44
- }
45
- /**
46
- * Adds a resolver target
47
- * @param updater Resolver target to add
48
- */
49
- addUpdater(updater) {
50
- this.updaters.push(updater);
51
- }
52
- /**
53
- * Resolves a resolver target for the specified type
54
- * @param type Type to resolve
55
- * @returns Resolved resolver target
56
- * @throws {Error} When no resolver targets are registered
57
- * @throws {Error} When no resolver target supporting the specified type is found
58
- */
59
- resolve(type) {
60
- if (this.updaters.length < 1) {
61
- throw new Error('Unassigned resolve target.');
62
- }
63
- const target = this.updaters.find(updater => updater.supports(type));
64
- if (!target) {
65
- // Determine the string representation of the unsupported type
66
- // If it's a non-null object, use JSON.stringify for detailed output
67
- // Otherwise, use String() for basic conversion
68
- const typeString = typeof type === 'object' && type !== null ? JSON.stringify(type) : String(type);
69
- throw new Error(`Unsupported type: ${typeString}`);
70
- }
71
- return target;
72
- }
73
- }
74
- exports.default = Resolver;
75
- //# sourceMappingURL=resolver.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"resolver.js","sourceRoot":"","sources":["../libs/resolver.ts"],"names":[],"mappings":";;AAEA;;;GAGG;AACH,MAAM,QAAQ;IAOZ;;;OAGG;IACH,YAAY,GAAG,IAAa;QAV5B;;;WAGG;QACK,aAAQ,GAAY,EAAE,CAAC;QAO7B,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACpB,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACjB,CAAC;IACH,CAAC;IAED;;;;;OAKG;IACK,OAAO,CAAC,IAAa;QAC3B,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;IACnB,CAAC;IAED;;;OAGG;IACI,GAAG,CAAC,QAAiB;QAC1B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACI,WAAW,CAAC,GAAG,IAAa;QACjC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC;IAC/B,CAAC;IAED;;;OAGG;IACI,UAAU,CAAC,OAAc;QAC9B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAED;;;;;;OAMG;IACI,OAAO,CAAC,IAAW;QACxB,IAAI,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC7B,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAChD,CAAC;QAED,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QAErE,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,8DAA8D;YAC9D,oEAAoE;YACpE,+CAA+C;YAC/C,MAAM,UAAU,GAAG,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;YACnG,MAAM,IAAI,KAAK,CAAC,qBAAqB,UAAU,EAAE,CAAC,CAAC;QACrD,CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAED,kBAAe,QAAQ,CAAC"}
package/index.js DELETED
@@ -1,5 +0,0 @@
1
- 'use strict';
2
-
3
- const Resolver = require('./dist/index');
4
-
5
- module.exports = Resolver.default || Resolver;
package/libs/index.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './interface'
2
- import Resolver from './resolver'
3
- export default Resolver
4
- // Export for CommonJS compatibility
5
- // @ts-ignore
6
- module.exports = Resolver
package/libs/interface.ts DELETED
@@ -1,26 +0,0 @@
1
- /**
2
- * Interface that classes which are targets for the resolver should implement
3
- */
4
- export interface ResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> {
5
- /**
6
- * Determines whether the specified type is supported
7
- * @param type The type to check for support
8
- * @returns true if supported, false otherwise
9
- */
10
- supports(type: TType): boolean
11
-
12
- /**
13
- * Handles the request
14
- * @param args Arguments needed for processing
15
- * @returns Processing result
16
- */
17
- handle(...args: TArgs): TReturn
18
- }
19
-
20
- // Maintain namespace for backward compatibility
21
- export namespace interfaces {
22
- export interface ResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> {
23
- supports(type: TType): boolean
24
- handle(...args: TArgs): TReturn
25
- }
26
- }
package/libs/resolver.ts DELETED
@@ -1,84 +0,0 @@
1
- import { ResolveTarget } from './interface'
2
-
3
- /**
4
- * Resolver class implementing the Chain of Responsibility pattern
5
- * Resolves handlers for specific types
6
- */
7
- class Resolver<TBase extends ResolveTarget<any[], any, any> = ResolveTarget<any[], any, any>, TType = string> {
8
- /**
9
- * Array of registered resolver targets
10
- * @private
11
- */
12
- private updaters: TBase[] = [];
13
-
14
- /**
15
- * Initializes the resolver
16
- * @param args Initial resolver targets
17
- */
18
- constructor(...args: TBase[]) {
19
- if (args.length > 0) {
20
- this.set(args);
21
- }
22
- }
23
-
24
- /**
25
- * Processes an array of arguments
26
- * @param args Array of resolver targets
27
- * @returns Processed array
28
- * @private
29
- */
30
- private getArgs(args: TBase[]): TBase[] {
31
- return [...args];
32
- }
33
-
34
- /**
35
- * Sets resolver targets
36
- * @param updaters Array of resolver targets
37
- */
38
- public set(updaters: TBase[]): void {
39
- this.updaters = updaters;
40
- }
41
-
42
- /**
43
- * Sets resolver targets (variadic version)
44
- * @param args Resolver targets
45
- */
46
- public setUpdaters(...args: TBase[]): void {
47
- this.set(this.getArgs(args));
48
- }
49
-
50
- /**
51
- * Adds a resolver target
52
- * @param updater Resolver target to add
53
- */
54
- public addUpdater(updater: TBase): void {
55
- this.updaters.push(updater);
56
- }
57
-
58
- /**
59
- * Resolves a resolver target for the specified type
60
- * @param type Type to resolve
61
- * @returns Resolved resolver target
62
- * @throws {Error} When no resolver targets are registered
63
- * @throws {Error} When no resolver target supporting the specified type is found
64
- */
65
- public resolve(type: TType): TBase {
66
- if (this.updaters.length < 1) {
67
- throw new Error('Unassigned resolve target.');
68
- }
69
-
70
- const target = this.updaters.find(updater => updater.supports(type));
71
-
72
- if (!target) {
73
- // Determine the string representation of the unsupported type
74
- // If it's a non-null object, use JSON.stringify for detailed output
75
- // Otherwise, use String() for basic conversion
76
- const typeString = typeof type === 'object' && type !== null ? JSON.stringify(type) : String(type);
77
- throw new Error(`Unsupported type: ${typeString}`);
78
- }
79
-
80
- return target;
81
- }
82
- }
83
-
84
- export default Resolver;