class-resolver 4.0.0 → 4.0.2

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/README.md CHANGED
@@ -643,15 +643,19 @@ Version 2.0.0 introduces generic type support for better type safety. This chang
643
643
  ## Contributing
644
644
 
645
645
  ```bash
646
- $ npm install
646
+ $ pnpm install
647
647
  $ git checkout -b YOUR_TOPIC_BRANCH
648
- $ npm test
649
- $ npm run build
648
+ $ pnpm run lint
649
+ $ pnpm run typecheck
650
+ $ pnpm test
651
+ $ pnpm run build
650
652
  $ git add ./
651
- $ git commit -m "YOUR UPDATE DESCRIPTION"
653
+ $ git commit -m "feat: YOUR UPDATE DESCRIPTION"
652
654
  $ git push YOUR_ORIGIN YOUR_TOPIC_BRANCH
653
655
  ```
654
656
 
657
+ Commit messages must follow [Conventional Commits](https://www.conventionalcommits.org/) (`feat:`, `fix:`, `chore:`, ...). Releases are automated: merging to `main` runs semantic-release on CircleCI, which picks the next version from the commits, pushes the `v*` tag, publishes to npm and creates the GitHub Release. Do not bump `version` in `package.json` by hand.
658
+
655
659
  ## License
656
660
 
657
661
  MIT
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";class i{updaters=[];fallbackHandler;constructor(...t){t.length>0&&this.set(t)}getArgs(t){return[...t]}set(t){this.updaters=t}setUpdaters(...t){this.set(this.getArgs(t))}addUpdater(t){return this.updaters.push(t),this}setFallbackHandler(t){return this.fallbackHandler=t,this}getPriority(t){const r=t;return typeof r.priority=="number"?r.priority:0}sortByPriority(t){return[...t].sort((r,e)=>{const s=this.getPriority(r);return this.getPriority(e)-s})}resolve(t){if(this.updaters.length<1)throw new Error("Unassigned resolve target.");const r=this.updaters.filter(n=>n.supports(t)),s=this.sortByPriority(r)[0];if(!s){if(this.fallbackHandler)return{supports:()=>!0,handle:this.fallbackHandler};const n=typeof t=="object"&&t!==null?JSON.stringify(t):String(t);throw new Error(`Unsupported type: ${n}`)}return s}resolveAll(t){if(this.updaters.length<1)throw new Error("Unassigned resolve target.");const r=this.updaters.filter(e=>e.supports(t));return r.length===0?this.fallbackHandler?[{supports:()=>!0,handle:this.fallbackHandler}]:[]:this.sortByPriority(r)}handleAll(t,...r){return this.resolveAll(t).map(s=>s.handle(...r))}async handleAllAsync(t,...r){const s=this.resolveAll(t).map(n=>n.handle(...r));return Promise.all(s)}async handleAllSequential(t,...r){const e=this.resolveAll(t),s=[];for(const n of e){const l=await n.handle(...r);s.push(l)}return s}}module.exports=i;module.exports=i;
1
+ "use strict";class l{updaters=[];fallbackHandler;constructor(...t){t.length>0&&this.set(t)}getArgs(t){return[...t]}set(t){this.updaters=t}setUpdaters(...t){this.set(this.getArgs(t))}addUpdater(t){return this.updaters.push(t),this}setFallbackHandler(t){return this.fallbackHandler=t,this}getPriority(t){const r=t;return typeof r.priority=="number"?r.priority:0}sortByPriority(t){return[...t].sort((r,e)=>{const s=this.getPriority(r);return this.getPriority(e)-s})}resolve(t){if(this.updaters.length<1)throw new Error("Unassigned resolve target.");const r=this.updaters.filter(n=>n.supports(t)),s=this.sortByPriority(r)[0];if(!s){if(this.fallbackHandler)return{supports:()=>!0,handle:this.fallbackHandler};const n=typeof t=="object"&&t!==null?JSON.stringify(t):String(t);throw new Error(`Unsupported type: ${n}`)}return s}resolveAll(t){if(this.updaters.length<1)throw new Error("Unassigned resolve target.");const r=this.updaters.filter(e=>e.supports(t));return r.length===0?this.fallbackHandler?[{supports:()=>!0,handle:this.fallbackHandler}]:[]:this.sortByPriority(r)}handleAll(t,...r){return this.resolveAll(t).map(s=>s.handle(...r))}async handleAllAsync(t,...r){const s=this.resolveAll(t).map(n=>n.handle(...r));return Promise.all(s)}async handleAllSequential(t,...r){const e=this.resolveAll(t),s=[];for(const n of e){const i=await n.handle(...r);s.push(i)}return s}}module.exports=l;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","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,CAGJ,CAKQ,SAAoB,CAAA,EAMpB,gBAMR,eAAeC,EAAe,CACxBA,EAAK,OAAS,GAChB,KAAK,IAAIA,CAAI,CAEjB,CAQQ,QAAQA,EAAwB,CACtC,MAAO,CAAC,GAAGA,CAAI,CACjB,CAMO,IAAIC,EAAyB,CAClC,KAAK,SAAWA,CAClB,CAMO,eAAeD,EAAqB,CACzC,KAAK,IAAI,KAAK,QAAQA,CAAI,CAAC,CAC7B,CAMO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AO,OAAO,GAAGX,CAAI,CAAC,CACvD,CASA,MAAa,eACXS,KACGT,EAC8C,CAEjD,MAAMc,EADU,KAAK,WAAWL,CAAI,EACX,IAAKE,GAAWA,EAAO,OAAO,GAAGX,CAAI,CAAC,EAC/D,OAAO,QAAQ,IAAIc,CAAQ,CAC7B,CAWA,MAAa,oBACXL,KACGT,EAC8C,CACjD,MAAMa,EAAU,KAAK,WAAWJ,CAAI,EAC9BM,EAAkD,CAAA,EAExD,UAAWJ,KAAUE,EAAS,CAC5B,MAAMG,EAAS,MAAML,EAAO,OAAO,GAAGX,CAAI,EAC1Ce,EAAQ,KAAKC,CAAM,CACrB,CAEA,OAAOD,CACT,CACF,CChOA,OAAO,QAAUhB"}
1
+ {"version":3,"file":"index.cjs","sources":["../libs/resolver.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"],"names":["Resolver","args","updaters","updater","handler","handlerWithPriority","handlers","a","b","priorityA","type","matchingHandlers","target","typeString","targets","promises","results","result"],"mappings":"aAMA,MAAMA,CAGJ,CAKQ,SAAoB,CAAA,EAMpB,gBAMR,eAAeC,EAAe,CACxBA,EAAK,OAAS,GAChB,KAAK,IAAIA,CAAI,CAEjB,CAQQ,QAAQA,EAAwB,CACtC,MAAO,CAAC,GAAGA,CAAI,CACjB,CAMO,IAAIC,EAAyB,CAClC,KAAK,SAAWA,CAClB,CAMO,eAAeD,EAAqB,CACzC,KAAK,IAAI,KAAK,QAAQA,CAAI,CAAC,CAC7B,CAMO,WAAWE,EAAsB,CACtC,YAAK,SAAS,KAAKA,CAAO,EACnB,IACT,CAOO,mBACLC,EACM,CACN,YAAK,gBAAkBA,EAChB,IACT,CAQQ,YAAYA,EAAwB,CAC1C,MAAMC,EAAsBD,EAC5B,OAAO,OAAOC,EAAoB,UAAa,SAAWA,EAAoB,SAAW,CAC3F,CAQQ,eAAeC,EAA4B,CACjD,MAAO,CAAC,GAAGA,CAAQ,EAAE,KAAK,CAACC,EAAGC,IAAM,CAClC,MAAMC,EAAY,KAAK,YAAYF,CAAC,EAEpC,OADkB,KAAK,YAAYC,CAAC,EACjBC,CACrB,CAAC,CACH,CASO,QAAQC,EAAoB,CACjC,GAAI,KAAK,SAAS,OAAS,EACzB,MAAM,IAAI,MAAM,4BAA4B,EAI9C,MAAMC,EAAmB,KAAK,SAAS,OAAQR,GAAYA,EAAQ,SAASO,CAAI,CAAC,EAE3EE,EADiB,KAAK,eAAeD,CAAgB,EAC7B,CAAC,EAE/B,GAAI,CAACC,EAAQ,CAEX,GAAI,KAAK,gBACP,MAAO,CACL,SAAU,IAAM,GAChB,OAAQ,KAAK,eAAA,EAOjB,MAAMC,EACJ,OAAOH,GAAS,UAAYA,IAAS,KAAO,KAAK,UAAUA,CAAI,EAAI,OAAOA,CAAI,EAChF,MAAM,IAAI,MAAM,qBAAqBG,CAAU,EAAE,CACnD,CAEA,OAAOD,CACT,CAUO,WAAWF,EAAsB,CACtC,GAAI,KAAK,SAAS,OAAS,EACzB,MAAM,IAAI,MAAM,4BAA4B,EAG9C,MAAMI,EAAU,KAAK,SAAS,OAAQX,GAAYA,EAAQ,SAASO,CAAI,CAAC,EAExE,OAAII,EAAQ,SAAW,EAEjB,KAAK,gBACA,CACL,CACE,SAAU,IAAM,GAChB,OAAQ,KAAK,eAAA,CACf,EAKG,CAAA,EAIF,KAAK,eAAeA,CAAO,CACpC,CASO,UACLJ,KACGT,EAC4B,CAE/B,OADgB,KAAK,WAAWS,CAAI,EACrB,IAAKE,GAAWA,EAAO,OAAO,GAAGX,CAAI,CAAC,CACvD,CASA,MAAa,eACXS,KACGT,EAC8C,CAEjD,MAAMc,EADU,KAAK,WAAWL,CAAI,EACX,IAAKE,GAAWA,EAAO,OAAO,GAAGX,CAAI,CAAC,EAC/D,OAAO,QAAQ,IAAIc,CAAQ,CAC7B,CAWA,MAAa,oBACXL,KACGT,EAC8C,CACjD,MAAMa,EAAU,KAAK,WAAWJ,CAAI,EAC9BM,EAAkD,CAAA,EAExD,UAAWJ,KAAUE,EAAS,CAC5B,MAAMG,EAAS,MAAML,EAAO,OAAO,GAAGX,CAAI,EAC1Ce,EAAQ,KAAKC,CAAM,CACrB,CAEA,OAAOD,CACT,CACF"}
@@ -0,0 +1,194 @@
1
+ /**
2
+ * Interface for async resolver targets
3
+ * Handle method returns a Promise
4
+ */
5
+ declare interface AsyncResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> extends SupportsType<TType> {
6
+ /**
7
+ * Handles the request asynchronously
8
+ * @param args Arguments needed for processing
9
+ * @returns Promise with processing result
10
+ */
11
+ handle(...args: TArgs): Promise<TReturn>;
12
+ }
13
+
14
+ /**
15
+ * Interface for prioritized handlers
16
+ * Higher priority values are executed first
17
+ */
18
+ declare interface HasPriority {
19
+ /**
20
+ * Priority for this handler (higher values execute first)
21
+ * Default is 0 if not specified
22
+ */
23
+ priority?: number;
24
+ }
25
+
26
+ declare namespace interfaces {
27
+ export interface ResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> {
28
+ supports(type: TType): boolean;
29
+ handle(...args: TArgs): TReturn;
30
+ }
31
+ export interface PrioritizedResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> extends ResolveTarget<TArgs, TReturn, TType> {
32
+ priority?: number;
33
+ }
34
+ export interface AsyncResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> {
35
+ supports(type: TType): boolean;
36
+ handle(...args: TArgs): Promise<TReturn>;
37
+ }
38
+ export interface PrioritizedAsyncResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> extends AsyncResolveTarget<TArgs, TReturn, TType> {
39
+ priority?: number;
40
+ }
41
+ }
42
+
43
+ /**
44
+ * Interface for async resolver targets with priority support
45
+ * Higher priority values are executed first
46
+ */
47
+ declare interface PrioritizedAsyncResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> extends AsyncResolveTarget<TArgs, TReturn, TType>, HasPriority {
48
+ }
49
+
50
+ /**
51
+ * Interface for resolver targets with priority support
52
+ * Higher priority values are executed first
53
+ */
54
+ declare interface PrioritizedResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> extends ResolveTarget<TArgs, TReturn, TType>, HasPriority {
55
+ }
56
+
57
+ /**
58
+ * Resolver class implementing the Chain of Responsibility pattern
59
+ * Resolves handlers for specific types
60
+ */
61
+ declare class Resolver<TBase extends ResolveTarget<any[], any, any> = ResolveTarget<any[], any, any>, TType = string> {
62
+ /**
63
+ * Array of registered resolver targets
64
+ * @private
65
+ */
66
+ private updaters;
67
+ /**
68
+ * Fallback handler function
69
+ * @private
70
+ */
71
+ private fallbackHandler?;
72
+ /**
73
+ * Initializes the resolver
74
+ * @param args Initial resolver targets
75
+ */
76
+ constructor(...args: TBase[]);
77
+ /**
78
+ * Processes an array of arguments
79
+ * @param args Array of resolver targets
80
+ * @returns Processed array
81
+ * @private
82
+ */
83
+ private getArgs;
84
+ /**
85
+ * Sets resolver targets
86
+ * @param updaters Array of resolver targets
87
+ */
88
+ set(updaters: TBase[]): void;
89
+ /**
90
+ * Sets resolver targets (variadic version)
91
+ * @param args Resolver targets
92
+ */
93
+ setUpdaters(...args: TBase[]): void;
94
+ /**
95
+ * Adds a resolver target
96
+ * @param updater Resolver target to add
97
+ */
98
+ addUpdater(updater: TBase): this;
99
+ /**
100
+ * Sets a fallback handler for unsupported types
101
+ * @param handler Fallback handler function
102
+ * @returns This resolver instance for method chaining
103
+ */
104
+ setFallbackHandler(handler: (...args: Parameters<TBase['handle']>) => ReturnType<TBase['handle']>): this;
105
+ /**
106
+ * Gets the priority of a handler
107
+ * @param handler The handler to get priority for
108
+ * @returns Priority value (0 if not defined)
109
+ * @private
110
+ */
111
+ private getPriority;
112
+ /**
113
+ * Sorts handlers by priority (highest first), maintaining registration order for equal priorities
114
+ * @param handlers Array of handlers to sort
115
+ * @returns Sorted array of handlers
116
+ * @private
117
+ */
118
+ private sortByPriority;
119
+ /**
120
+ * Resolves a resolver target for the specified type
121
+ * @param type Type to resolve
122
+ * @returns Resolved resolver target
123
+ * @throws {Error} When no resolver targets are registered
124
+ * @throws {Error} When no resolver target supporting the specified type is found and no fallback is set
125
+ */
126
+ resolve(type: TType): TBase;
127
+ /**
128
+ * Resolves all resolver targets for the specified type
129
+ * @param type Type to resolve
130
+ * @returns Array of all matching resolver targets sorted by priority (highest first).
131
+ * Returns an empty array if no handlers match and no fallback is set.
132
+ * Note: Unlike resolve(), this method does not throw when no handlers match.
133
+ * @throws {Error} When no resolver targets are registered
134
+ */
135
+ resolveAll(type: TType): TBase[];
136
+ /**
137
+ * Executes all matching handlers for the specified type
138
+ * @param type Type to resolve
139
+ * @param args Arguments to pass to the handlers
140
+ * @returns Array of results from all matching handlers
141
+ * @throws {Error} When no resolver targets are registered
142
+ */
143
+ handleAll(type: TType, ...args: Parameters<TBase['handle']>): ReturnType<TBase['handle']>[];
144
+ /**
145
+ * Executes all matching async handlers in parallel for the specified type
146
+ * @param type Type to resolve
147
+ * @param args Arguments to pass to the handlers
148
+ * @returns Promise that resolves to array of results from all matching handlers
149
+ * @throws {Error} When no resolver targets are registered
150
+ */
151
+ handleAllAsync(type: TType, ...args: Parameters<TBase['handle']>): Promise<Awaited<ReturnType<TBase['handle']>>[]>;
152
+ /**
153
+ * Executes all matching async handlers sequentially for the specified type
154
+ * Stops on first error
155
+ * @param type Type to resolve
156
+ * @param args Arguments to pass to the handlers
157
+ * @returns Promise that resolves to array of results from all matching handlers
158
+ * @throws {Error} When no resolver targets are registered
159
+ * @throws {Error} When any handler throws an error
160
+ */
161
+ handleAllSequential(type: TType, ...args: Parameters<TBase['handle']>): Promise<Awaited<ReturnType<TBase['handle']>>[]>;
162
+ }
163
+
164
+
165
+ /**
166
+ * Interface that classes which are targets for the resolver should implement
167
+ */
168
+ declare interface ResolveTarget<TArgs extends any[] = any[], TReturn = any, TType = string> extends SupportsType<TType> {
169
+ /**
170
+ * Handles the request
171
+ * @param args Arguments needed for processing
172
+ * @returns Processing result
173
+ */
174
+ handle(...args: TArgs): TReturn;
175
+ }
176
+
177
+ /**
178
+ * Base interface for type matching
179
+ * Extracted to reduce code duplication across sync and async targets
180
+ */
181
+ declare interface SupportsType<TType = string> {
182
+ /**
183
+ * Determines whether the specified type is supported
184
+ * @param type The type to check for support
185
+ * @returns true if supported, false otherwise
186
+ */
187
+ supports(type: TType): boolean;
188
+ }
189
+
190
+ declare namespace Resolver {
191
+ export { AsyncResolveTarget, HasPriority, interfaces, PrioritizedAsyncResolveTarget, PrioritizedResolveTarget, ResolveTarget, SupportsType };
192
+ }
193
+
194
+ export = Resolver;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- class i {
1
+ class a {
2
2
  /**
3
3
  * Array of registered resolver targets
4
4
  * @private
@@ -86,15 +86,15 @@ class i {
86
86
  resolve(t) {
87
87
  if (this.updaters.length < 1)
88
88
  throw new Error("Unassigned resolve target.");
89
- const r = this.updaters.filter((l) => l.supports(t)), e = this.sortByPriority(r)[0];
89
+ const r = this.updaters.filter((n) => n.supports(t)), e = this.sortByPriority(r)[0];
90
90
  if (!e) {
91
91
  if (this.fallbackHandler)
92
92
  return {
93
93
  supports: () => !0,
94
94
  handle: this.fallbackHandler
95
95
  };
96
- const l = typeof t == "object" && t !== null ? JSON.stringify(t) : String(t);
97
- throw new Error(`Unsupported type: ${l}`);
96
+ const n = typeof t == "object" && t !== null ? JSON.stringify(t) : String(t);
97
+ throw new Error(`Unsupported type: ${n}`);
98
98
  }
99
99
  return e;
100
100
  }
@@ -135,7 +135,7 @@ class i {
135
135
  * @throws {Error} When no resolver targets are registered
136
136
  */
137
137
  async handleAllAsync(t, ...r) {
138
- const e = this.resolveAll(t).map((l) => l.handle(...r));
138
+ const e = this.resolveAll(t).map((n) => n.handle(...r));
139
139
  return Promise.all(e);
140
140
  }
141
141
  /**
@@ -149,15 +149,14 @@ class i {
149
149
  */
150
150
  async handleAllSequential(t, ...r) {
151
151
  const s = this.resolveAll(t), e = [];
152
- for (const l of s) {
153
- const n = await l.handle(...r);
154
- e.push(n);
152
+ for (const n of s) {
153
+ const l = await n.handle(...r);
154
+ e.push(l);
155
155
  }
156
156
  return e;
157
157
  }
158
158
  }
159
- module.exports = i;
160
159
  export {
161
- i as default
160
+ a as default
162
161
  };
163
162
  //# sourceMappingURL=index.mjs.map
@@ -1 +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;AAAA;AAAA;AAAA,EAMR,eAAeC,GAAe;AAC5B,IAAIA,EAAK,SAAS,KAChB,KAAK,IAAIA,CAAI;AAAA,EAEjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,QAAQA,GAAwB;AACtC,WAAO,CAAC,GAAGA,CAAI;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,IAAIC,GAAyB;AAClC,SAAK,WAAWA;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,eAAeD,GAAqB;AACzC,SAAK,IAAI,KAAK,QAAQA,CAAI,CAAC;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,WAAWE,GAAsB;AACtC,gBAAK,SAAS,KAAKA,CAA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A,EAAQ,SAASO,CAAI,CAAC;AAExE,WAAII,EAAQ,WAAW,IAEjB,KAAK,kBACA;AAAA,MACL;AAAA,QACE,UAAU,MAAM;AAAA,QAChB,QAAQ,KAAK;AAAA,MAAA;AAAA,IACf,IAKG,CAAA,IAIF,KAAK,eAAeA,CAAO;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,UACLJ,MACGT,GAC4B;AAE/B,WADgB,KAAK,WAAWS,CAAI,EACrB,IAAI,CAACE,MAAWA,EAAO,OAAO,GAAGX,CAAI,CAAC;AAAA,EACvD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAa,eACXS,MACGT,GAC8C;AAEjD,UAAMc,IADU,KAAK,WAAWL,CAAI,EACX,IAAI,CAACE,MAAWA,EAAO,OAAO,GAAGX,CAAI,CAAC;AAC/D,WAAO,QAAQ,IAAIc,CAAQ;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,MAAa,oBACXL,MACGT,GAC8C;AACjD,UAAMa,IAAU,KAAK,WAAWJ,CAAI,GAC9BM,IAAkD,CAAA;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;"}
1
+ {"version":3,"file":"index.mjs","sources":["../libs/resolver.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"],"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;AAAA;AAAA;AAAA,EAMR,eAAeC,GAAe;AAC5B,IAAIA,EAAK,SAAS,KAChB,KAAK,IAAIA,CAAI;AAAA,EAEjB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQQ,QAAQA,GAAwB;AACtC,WAAO,CAAC,GAAGA,CAAI;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,IAAIC,GAAyB;AAClC,SAAK,WAAWA;AAAA,EAClB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,eAAeD,GAAqB;AACzC,SAAK,IAAI,KAAK,QAAQA,CAAI,CAAC;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMO,WAAWE,GAAsB;AACtC,gBAAK,SAAS,KAAKA,CAAO,GACnB;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;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Q,WAAW,IAEjB,KAAK,kBACA;AAAA,MACL;AAAA,QACE,UAAU,MAAM;AAAA,QAChB,QAAQ,KAAK;AAAA,MAAA;AAAA,IACf,IAKG,CAAA,IAIF,KAAK,eAAeA,CAAO;AAAA,EACpC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASO,UACLJ,MACGT,GAC4B;AAE/B,WADgB,KAAK,WAAWS,CAAI,EACrB,IAAI,CAACE,MAAWA,EAAO,OAAO,GAAGX,CAAI,CAAC;AAAA,EACvD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAa,eACXS,MACGT,GAC8C;AAEjD,UAAMc,IADU,KAAK,WAAWL,CAAI,EACX,IAAI,CAACE,MAAWA,EAAO,OAAO,GAAGX,CAAI,CAAC;AAC/D,WAAO,QAAQ,IAAIc,CAAQ;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,MAAa,oBACXL,MACGT,GAC8C;AACjD,UAAMa,IAAU,KAAK,WAAWJ,CAAI,GAC9BM,IAAkD,CAAA;AAExD,eAAWJ,KAAUE,GAAS;AAC5B,YAAMG,IAAS,MAAML,EAAO,OAAO,GAAGX,CAAI;AAC1C,MAAAe,EAAQ,KAAKC,CAAM;AAAA,IACrB;AAEA,WAAOD;AAAA,EACT;AACF;"}
package/package.json CHANGED
@@ -1,19 +1,18 @@
1
1
  {
2
2
  "name": "class-resolver",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "description": "Simple class resolver.",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
7
7
  "module": "dist/index.mjs",
8
- "types": "dist/index.d.ts",
9
8
  "exports": {
10
9
  ".": {
11
10
  "import": {
12
- "types": "./dist/index.d.ts",
11
+ "types": "./dist/index.d.mts",
13
12
  "default": "./dist/index.mjs"
14
13
  },
15
14
  "require": {
16
- "types": "./dist/index.d.ts",
15
+ "types": "./dist/index.d.cts",
17
16
  "default": "./dist/index.cjs"
18
17
  }
19
18
  }
@@ -22,7 +21,8 @@
22
21
  "dist"
23
22
  ],
24
23
  "scripts": {
25
- "build": "vite build",
24
+ "build": "vite build && node scripts/emit-cts-types.js",
25
+ "verify-build": "node scripts/verify-build.js",
26
26
  "test": "vitest run",
27
27
  "test:watch": "vitest",
28
28
  "test:ui": "vitest --ui",
@@ -30,6 +30,7 @@
30
30
  "lint": "biome check .",
31
31
  "lint:fix": "biome check --write .",
32
32
  "format": "biome format --write .",
33
+ "typecheck": "tsc --noEmit",
33
34
  "prepublishOnly": "npm run build",
34
35
  "release": "np"
35
36
  },
@@ -43,22 +44,28 @@
43
44
  "devDependencies": {
44
45
  "@biomejs/biome": "^1.9.4",
45
46
  "@types/node": "^22.0.0",
46
- "@vitest/coverage-v8": "^4.0.9",
47
- "@vitest/ui": "^4.0.9",
47
+ "@vitest/coverage-v8": "^4.1.12",
48
+ "@vitest/ui": "^4.1.12",
49
+ "esbuild": "^0.27.2",
48
50
  "np": "^10.2.0",
49
51
  "typescript": "^5.7.2",
50
52
  "vite": "^7.2.2",
51
53
  "vite-plugin-dts": "^4.3.0",
52
- "vitest": "^4.0.9"
54
+ "vitest": "^4.1.12"
53
55
  },
54
56
  "repository": {
55
57
  "type": "git",
56
58
  "url": "https://github.com/hideokamoto/class-resolver.git"
57
59
  },
60
+ "homepage": "https://github.com/hideokamoto/class-resolver#readme",
61
+ "bugs": {
62
+ "url": "https://github.com/hideokamoto/class-resolver/issues"
63
+ },
58
64
  "engines": {
59
65
  "node": ">=14.0.0"
60
66
  },
61
67
  "np": {
62
68
  "yarn": false
63
- }
69
+ },
70
+ "packageManager": "pnpm@10.34.9"
64
71
  }
File without changes