class-resolver 2.2.0 → 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.
@@ -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":"AAMA,MAAMA,EAGJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAKQ,WAAoB,CAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMpB;AAAA;AAAA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AAExD,eAAWJ,KAAUE,GAAS;AAC5B,YAAMG,IAAS,MAAML,EAAO,OAAO,GAAGX,CAAI;AAC1C,MAAAe,EAAQ,KAAKC,CAAM;AAAA,IACrB;AAEA,WAAOD;AAAA,EACT;AACF;AChOA,OAAO,UAAUhB;"}
package/package.json CHANGED
@@ -1,15 +1,37 @@
1
1
  {
2
2
  "name": "class-resolver",
3
- "version": "2.2.0",
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,59 +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
- * Fallback handler function
14
- * @private
15
- */
16
- private fallbackHandler?;
17
- /**
18
- * Initializes the resolver
19
- * @param args Initial resolver targets
20
- */
21
- constructor(...args: TBase[]);
22
- /**
23
- * Processes an array of arguments
24
- * @param args Array of resolver targets
25
- * @returns Processed array
26
- * @private
27
- */
28
- private getArgs;
29
- /**
30
- * Sets resolver targets
31
- * @param updaters Array of resolver targets
32
- */
33
- set(updaters: TBase[]): void;
34
- /**
35
- * Sets resolver targets (variadic version)
36
- * @param args Resolver targets
37
- */
38
- setUpdaters(...args: TBase[]): void;
39
- /**
40
- * Adds a resolver target
41
- * @param updater Resolver target to add
42
- */
43
- addUpdater(updater: TBase): this;
44
- /**
45
- * Sets a fallback handler for unsupported types
46
- * @param handler Fallback handler function
47
- * @returns This resolver instance for method chaining
48
- */
49
- setFallbackHandler(handler: (...args: Parameters<TBase['handle']>) => ReturnType<TBase['handle']>): this;
50
- /**
51
- * Resolves a resolver target for the specified type
52
- * @param type Type to resolve
53
- * @returns Resolved resolver target
54
- * @throws {Error} When no resolver targets are registered
55
- * @throws {Error} When no resolver target supporting the specified type is found and no fallback is set
56
- */
57
- resolve(type: TType): TBase;
58
- }
59
- export default Resolver;
package/dist/resolver.js DELETED
@@ -1,92 +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
- return this;
52
- }
53
- /**
54
- * Sets a fallback handler for unsupported types
55
- * @param handler Fallback handler function
56
- * @returns This resolver instance for method chaining
57
- */
58
- setFallbackHandler(handler) {
59
- this.fallbackHandler = handler;
60
- return this;
61
- }
62
- /**
63
- * Resolves a resolver target for the specified type
64
- * @param type Type to resolve
65
- * @returns Resolved resolver target
66
- * @throws {Error} When no resolver targets are registered
67
- * @throws {Error} When no resolver target supporting the specified type is found and no fallback is set
68
- */
69
- resolve(type) {
70
- if (this.updaters.length < 1) {
71
- throw new Error('Unassigned resolve target.');
72
- }
73
- const target = this.updaters.find(updater => updater.supports(type));
74
- if (!target) {
75
- // If fallback handler is set, create a temporary target that uses it
76
- if (this.fallbackHandler) {
77
- return {
78
- supports: () => true,
79
- handle: this.fallbackHandler
80
- };
81
- }
82
- // Determine the string representation of the unsupported type
83
- // If it's a non-null object, use JSON.stringify for detailed output
84
- // Otherwise, use String() for basic conversion
85
- const typeString = typeof type === 'object' && type !== null ? JSON.stringify(type) : String(type);
86
- throw new Error(`Unsupported type: ${typeString}`);
87
- }
88
- return target;
89
- }
90
- }
91
- exports.default = Resolver;
92
- //# 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;IAaZ;;;OAGG;IACH,YAAY,GAAG,IAAa;QAhB5B;;;WAGG;QACK,aAAQ,GAAY,EAAE,CAAC;QAa7B,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,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B,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,111 +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
- * Fallback handler function
16
- * @private
17
- */
18
- private fallbackHandler?: (...args: Parameters<TBase['handle']>) => ReturnType<TBase['handle']>;
19
-
20
- /**
21
- * Initializes the resolver
22
- * @param args Initial resolver targets
23
- */
24
- constructor(...args: TBase[]) {
25
- if (args.length > 0) {
26
- this.set(args);
27
- }
28
- }
29
-
30
- /**
31
- * Processes an array of arguments
32
- * @param args Array of resolver targets
33
- * @returns Processed array
34
- * @private
35
- */
36
- private getArgs(args: TBase[]): TBase[] {
37
- return [...args];
38
- }
39
-
40
- /**
41
- * Sets resolver targets
42
- * @param updaters Array of resolver targets
43
- */
44
- public set(updaters: TBase[]): void {
45
- this.updaters = updaters;
46
- }
47
-
48
- /**
49
- * Sets resolver targets (variadic version)
50
- * @param args Resolver targets
51
- */
52
- public setUpdaters(...args: TBase[]): void {
53
- this.set(this.getArgs(args));
54
- }
55
-
56
- /**
57
- * Adds a resolver target
58
- * @param updater Resolver target to add
59
- */
60
- public addUpdater(updater: TBase): this {
61
- this.updaters.push(updater);
62
- return this;
63
- }
64
-
65
- /**
66
- * Sets a fallback handler for unsupported types
67
- * @param handler Fallback handler function
68
- * @returns This resolver instance for method chaining
69
- */
70
- public setFallbackHandler(
71
- handler: (...args: Parameters<TBase['handle']>) => ReturnType<TBase['handle']>
72
- ): this {
73
- this.fallbackHandler = handler;
74
- return this;
75
- }
76
-
77
- /**
78
- * Resolves a resolver target for the specified type
79
- * @param type Type to resolve
80
- * @returns Resolved resolver target
81
- * @throws {Error} When no resolver targets are registered
82
- * @throws {Error} When no resolver target supporting the specified type is found and no fallback is set
83
- */
84
- public resolve(type: TType): TBase {
85
- if (this.updaters.length < 1) {
86
- throw new Error('Unassigned resolve target.');
87
- }
88
-
89
- const target = this.updaters.find(updater => updater.supports(type));
90
-
91
- if (!target) {
92
- // If fallback handler is set, create a temporary target that uses it
93
- if (this.fallbackHandler) {
94
- return {
95
- supports: () => true,
96
- handle: this.fallbackHandler
97
- } as unknown as TBase;
98
- }
99
-
100
- // Determine the string representation of the unsupported type
101
- // If it's a non-null object, use JSON.stringify for detailed output
102
- // Otherwise, use String() for basic conversion
103
- const typeString = typeof type === 'object' && type !== null ? JSON.stringify(type) : String(type);
104
- throw new Error(`Unsupported type: ${typeString}`);
105
- }
106
-
107
- return target;
108
- }
109
- }
110
-
111
- export default Resolver;