awilixify 2.2.1 → 3.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.
Files changed (127) hide show
  1. package/README.md +12 -6
  2. package/dist/index.d.ts +18 -0
  3. package/dist/index.js +15 -0
  4. package/dist/lib/decorators/decorator-state.d.ts +15 -0
  5. package/dist/lib/decorators/decorator-state.js +72 -0
  6. package/dist/lib/decorators/decorator-state.types.d.ts +35 -0
  7. package/dist/lib/decorators/decorator-state.types.js +1 -0
  8. package/dist/lib/di/common.types.d.ts +6 -0
  9. package/dist/lib/di/common.types.js +1 -0
  10. package/dist/lib/di/contexts/container-context-base.d.ts +53 -0
  11. package/dist/lib/di/contexts/container-context-base.js +194 -0
  12. package/dist/lib/di/contexts/context-overrides.d.ts +22 -0
  13. package/dist/lib/di/contexts/context-overrides.js +93 -0
  14. package/dist/lib/di/contexts/di-context-async.d.ts +14 -0
  15. package/dist/lib/di/contexts/di-context-async.js +112 -0
  16. package/dist/lib/di/contexts/di-context-base.d.ts +19 -0
  17. package/dist/lib/di/contexts/di-context-base.js +32 -0
  18. package/dist/lib/di/contexts/di-context.d.ts +7 -0
  19. package/dist/lib/di/contexts/di-context.js +9 -0
  20. package/dist/lib/di/contexts/module-graph-ensurer.d.ts +31 -0
  21. package/dist/lib/di/contexts/module-graph-ensurer.js +45 -0
  22. package/dist/lib/di/contexts/module-overrides.d.ts +70 -0
  23. package/dist/lib/di/contexts/module-overrides.js +3 -0
  24. package/dist/lib/di/contexts/override-value.types.d.ts +6 -0
  25. package/dist/lib/di/contexts/override-value.types.js +1 -0
  26. package/dist/lib/di/contexts/overrides-processor.d.ts +22 -0
  27. package/dist/lib/di/contexts/overrides-processor.js +93 -0
  28. package/dist/lib/di/contexts/provider-overrides.types.d.ts +19 -0
  29. package/dist/lib/di/contexts/provider-overrides.types.js +1 -0
  30. package/dist/lib/di/errors.d.ts +66 -0
  31. package/dist/lib/di/errors.js +148 -0
  32. package/dist/lib/di/modules/module-def.types.d.ts +117 -0
  33. package/dist/lib/di/modules/module-def.types.js +1 -0
  34. package/dist/lib/di/modules/module-factories.d.ts +16 -0
  35. package/dist/lib/di/modules/module-factories.js +46 -0
  36. package/dist/lib/di/modules/module-overrides.d.ts +70 -0
  37. package/dist/lib/di/modules/module-overrides.js +3 -0
  38. package/dist/lib/di/modules/module-ref.types.d.ts +13 -0
  39. package/dist/lib/di/modules/module-ref.types.js +1 -0
  40. package/dist/lib/di/modules/module.types.d.ts +178 -0
  41. package/dist/lib/di/modules/module.types.js +1 -0
  42. package/dist/lib/di/modules/provider-overrides.types.d.ts +19 -0
  43. package/dist/lib/di/modules/provider-overrides.types.js +1 -0
  44. package/dist/lib/di/modules/runtime-module.types.d.ts +25 -0
  45. package/dist/lib/di/modules/runtime-module.types.js +1 -0
  46. package/dist/lib/di/processors/controller-processor.d.ts +13 -0
  47. package/dist/lib/di/processors/controller-processor.js +76 -0
  48. package/dist/lib/di/processors/handler-processor.d.ts +15 -0
  49. package/dist/lib/di/processors/handler-processor.js +69 -0
  50. package/dist/lib/di/processors/initializer-processor.d.ts +19 -0
  51. package/dist/lib/di/processors/initializer-processor.js +91 -0
  52. package/dist/lib/di/processors/interceptor-processor.d.ts +18 -0
  53. package/dist/lib/di/processors/interceptor-processor.js +101 -0
  54. package/dist/lib/di/processors/keyed-feature-registrar.d.ts +19 -0
  55. package/dist/lib/di/processors/keyed-feature-registrar.js +112 -0
  56. package/dist/lib/di/processors/lifecycle-processor.d.ts +38 -0
  57. package/dist/lib/di/processors/lifecycle-processor.js +178 -0
  58. package/dist/lib/di/processors/overrides-processor.d.ts +24 -0
  59. package/dist/lib/di/processors/overrides-processor.js +69 -0
  60. package/dist/lib/di/providers/basic-provider-resolver.d.ts +35 -0
  61. package/dist/lib/di/providers/basic-provider-resolver.js +88 -0
  62. package/dist/lib/di/providers/provider-dependency-sorter.d.ts +9 -0
  63. package/dist/lib/di/providers/provider-dependency-sorter.js +76 -0
  64. package/dist/lib/di/providers/provider-resolver.d.ts +31 -0
  65. package/dist/lib/di/providers/provider-resolver.js +128 -0
  66. package/dist/lib/di/providers/provider.types.d.ts +1 -0
  67. package/dist/lib/di/providers/provider.types.js +1 -0
  68. package/dist/lib/di/providers/types/controller.types.d.ts +9 -0
  69. package/dist/lib/di/providers/types/controller.types.js +1 -0
  70. package/dist/lib/di/providers/types/decorator-context.types.d.ts +9 -0
  71. package/dist/lib/di/providers/types/decorator-context.types.js +1 -0
  72. package/dist/lib/di/providers/types/handler.types.d.ts +9 -0
  73. package/dist/lib/di/providers/types/handler.types.js +1 -0
  74. package/dist/lib/di/providers/types/index.d.ts +7 -0
  75. package/dist/lib/di/providers/types/index.js +7 -0
  76. package/dist/lib/di/providers/types/initializer.types.d.ts +16 -0
  77. package/dist/lib/di/providers/types/initializer.types.js +3 -0
  78. package/dist/lib/di/providers/types/interceptor.types.d.ts +18 -0
  79. package/dist/lib/di/providers/types/interceptor.types.js +1 -0
  80. package/dist/lib/di/providers/types/middleware.types.d.ts +10 -0
  81. package/dist/lib/di/providers/types/middleware.types.js +1 -0
  82. package/dist/lib/di/providers/types/provider.types.d.ts +26 -0
  83. package/dist/lib/di/providers/types/provider.types.js +1 -0
  84. package/dist/lib/di/request-scope-context.d.ts +5 -0
  85. package/dist/lib/di/request-scope-context.js +28 -0
  86. package/dist/lib/di/type-guards.d.ts +35 -0
  87. package/dist/lib/di/type-guards.js +84 -0
  88. package/dist/lib/http/decorators.d.ts +33 -0
  89. package/dist/lib/http/decorators.js +151 -0
  90. package/dist/lib/http/exceptions.d.ts +58 -0
  91. package/dist/lib/http/exceptions.js +71 -0
  92. package/dist/lib/http/http-verbs.d.ts +12 -0
  93. package/dist/lib/http/http-verbs.js +11 -0
  94. package/dist/lib/http/index.d.ts +3 -0
  95. package/dist/lib/http/index.js +3 -0
  96. package/dist/lib/http/openapi-builder.d.ts +27 -0
  97. package/dist/lib/http/openapi-builder.js +83 -0
  98. package/dist/lib/mediator/contract.types.d.ts +159 -0
  99. package/dist/lib/mediator/contract.types.js +1 -0
  100. package/dist/lib/mediator/errors.d.ts +15 -0
  101. package/dist/lib/mediator/errors.js +33 -0
  102. package/dist/lib/mediator/global-middlewares.types.d.ts +13 -0
  103. package/dist/lib/mediator/global-middlewares.types.js +1 -0
  104. package/dist/lib/mediator/handler.types.d.ts +16 -0
  105. package/dist/lib/mediator/handler.types.js +1 -0
  106. package/dist/lib/mediator/mediator.d.ts +21 -0
  107. package/dist/lib/mediator/mediator.js +123 -0
  108. package/dist/lib/mediator/mediator.types.d.ts +64 -0
  109. package/dist/lib/mediator/mediator.types.js +1 -0
  110. package/dist/lib/mediator/middleware.types.d.ts +41 -0
  111. package/dist/lib/mediator/middleware.types.js +1 -0
  112. package/dist/lib/mediator/result.d.ts +18 -0
  113. package/dist/lib/mediator/result.js +24 -0
  114. package/dist/lib/react/errors.d.ts +12 -0
  115. package/dist/lib/react/errors.js +24 -0
  116. package/dist/lib/react/react-component-processor.d.ts +10 -0
  117. package/dist/lib/react/react-component-processor.js +62 -0
  118. package/dist/lib/react/react-di-context.d.ts +21 -0
  119. package/dist/lib/react/react-di-context.js +55 -0
  120. package/dist/lib/react/react-module-factories.d.ts +6 -0
  121. package/dist/lib/react/react-module-factories.js +34 -0
  122. package/dist/lib/react/react-module.types.d.ts +183 -0
  123. package/dist/lib/react/react-module.types.js +1 -0
  124. package/dist/react.d.ts +4 -0
  125. package/dist/react.js +4 -0
  126. package/dist/tsconfig.tsbuildinfo +1 -0
  127. package/package.json +29 -3
@@ -0,0 +1,9 @@
1
+ import type { BuildResolverOptions, Constructor } from "awilix";
2
+ import type { AnyContract } from "lib/mediator/contract.types.js";
3
+ import type { Handler } from "lib/mediator/handler.types.js";
4
+ export interface ConstructorHandler<C extends AnyContract = AnyContract> {
5
+ new (...args: any[]): Handler<C>;
6
+ }
7
+ export type ClassHandler<H extends Constructor<any> = Constructor<any>> = {
8
+ useClass: H;
9
+ } & BuildResolverOptions<any>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,7 @@
1
+ export * from "./controller.types.js";
2
+ export * from "./decorator-context.types.js";
3
+ export * from "./handler.types.js";
4
+ export * from "./initializer.types.js";
5
+ export * from "./interceptor.types.js";
6
+ export * from "./middleware.types.js";
7
+ export * from "./provider.types.js";
@@ -0,0 +1,7 @@
1
+ export * from "./controller.types.js";
2
+ export * from "./decorator-context.types.js";
3
+ export * from "./handler.types.js";
4
+ export * from "./initializer.types.js";
5
+ export * from "./interceptor.types.js";
6
+ export * from "./middleware.types.js";
7
+ export * from "./provider.types.js";
@@ -0,0 +1,16 @@
1
+ import type { Constructor } from "awilix";
2
+ import type { AnyDecoratorToken, DecoratorExecutionContext } from "./decorator-context.types.js";
3
+ export type DefInitializerMap = Record<string, ConstructorInitializer>;
4
+ export interface ConstructorInitializer {
5
+ new (...args: any[]): Initializer<any, boolean>;
6
+ }
7
+ export type MetadataInitializerContext<TToken extends AnyDecoratorToken = AnyDecoratorToken> = DecoratorExecutionContext<TToken, Constructor<any>>;
8
+ export type InitializerContext<TToken extends AnyDecoratorToken = AnyDecoratorToken> = MetadataInitializerContext<TToken> & {
9
+ invoke: (...args: unknown[]) => unknown | Promise<unknown>;
10
+ };
11
+ export declare abstract class Initializer<TToken extends AnyDecoratorToken = AnyDecoratorToken, TUsesInvoke extends boolean = true> {
12
+ abstract readonly token: TToken;
13
+ readonly usesInvoke: TUsesInvoke;
14
+ abstract initialize(context: TUsesInvoke extends true ? InitializerContext<TToken> : MetadataInitializerContext<TToken>): void | Promise<void>;
15
+ }
16
+ export type AnyInitializer = ConstructorInitializer;
@@ -0,0 +1,3 @@
1
+ export class Initializer {
2
+ usesInvoke = true;
3
+ }
@@ -0,0 +1,18 @@
1
+ import type { BuildResolverOptions, Constructor } from "awilix";
2
+ import type { AnyDecoratorToken, DecoratorExecutionContext } from "./decorator-context.types.js";
3
+ export type DefInterceptorMap = Record<string, object>;
4
+ export interface ConstructorInterceptor {
5
+ new (...args: any[]): Interceptor;
6
+ }
7
+ export type ClassInterceptor<I extends Constructor<any> = Constructor<any>> = {
8
+ useClass: I;
9
+ } & BuildResolverOptions<any>;
10
+ export type AnyInterceptor = ClassInterceptor | ConstructorInterceptor;
11
+ export type InterceptContext<TToken extends AnyDecoratorToken = AnyDecoratorToken> = DecoratorExecutionContext<TToken> & {
12
+ args: unknown[];
13
+ proceed: () => unknown | Promise<unknown>;
14
+ };
15
+ export interface Interceptor<TToken extends AnyDecoratorToken = AnyDecoratorToken> {
16
+ token: TToken;
17
+ intercept(context: InterceptContext<TToken>): unknown | Promise<unknown>;
18
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import type { BuildResolverOptions, Constructor } from "awilix";
2
+ import type { AnyMiddlewareContract, Middleware } from "lib/mediator/middleware.types.js";
3
+ export type DefPreHandlerMap = Record<string, object>;
4
+ export interface ConstructorMiddleware<C extends AnyMiddlewareContract = AnyMiddlewareContract> {
5
+ new (...args: any[]): Middleware<C>;
6
+ }
7
+ export type ClassMiddleware<M extends Constructor<any> = Constructor<any>> = {
8
+ useClass: M;
9
+ } & BuildResolverOptions<any>;
10
+ export type AnyMiddleware = ClassMiddleware | Constructor<Middleware>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,26 @@
1
+ import type { BuildResolverOptions, Constructor } from "awilix";
2
+ export type DefProviderMap = Record<string, object | string | boolean | number>;
3
+ export type ConstructorProvider<T extends object = object> = Constructor<T>;
4
+ export type PrimitiveProvider = string | number | boolean | symbol | bigint;
5
+ export type FunctionProvider = (...args: any[]) => any;
6
+ export type FactoryProvider<T extends object, DepsMap extends Record<string, unknown>, Keys extends readonly Extract<keyof DepsMap, string>[], Strict extends boolean = true> = {
7
+ inject?: Keys;
8
+ initAfter?: readonly Extract<keyof DepsMap, string>[];
9
+ eager?: boolean;
10
+ useFactory: Strict extends true ? (...args: MapKeysToValues<DepsMap, Keys>) => T | Promise<T> : (...args: any[]) => T | Promise<T>;
11
+ } & BuildResolverOptions<T>;
12
+ export type ClassProvider<T extends object, DepsMap extends Record<string, unknown> = Record<string, unknown>> = {
13
+ useClass: Constructor<T>;
14
+ allowCircular?: boolean;
15
+ eager?: boolean;
16
+ initAfter?: readonly Extract<keyof DepsMap, string>[];
17
+ } & BuildResolverOptions<T>;
18
+ export interface ProviderInit {
19
+ init(): void | Promise<void>;
20
+ }
21
+ export type Provider<T extends object, DepsMap extends Record<string, unknown> = Record<string, unknown>> = FactoryProvider<T, DepsMap, readonly Extract<keyof DepsMap, string>[], false> | ClassProvider<T, DepsMap> | ConstructorProvider<T>;
22
+ export type AnyProvider = FactoryProvider<any, any, readonly string[], false> | ClassProvider<any> | ConstructorProvider<any> | PrimitiveProvider | FunctionProvider | object;
23
+ type MapKeysToValues<DepsMap extends Record<string, unknown>, Keys extends readonly Extract<keyof DepsMap, string>[]> = {
24
+ [K in keyof Keys]: Keys[K] extends keyof DepsMap ? DepsMap[Keys[K]] : never;
25
+ };
26
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type * as Awilix from "awilix";
2
+ export declare function runInRequestScopeContext<T>(fn: () => T): T;
3
+ export declare function hasRequestScopeContext(): boolean;
4
+ export declare function getOrCreateRequestScope(scope: Awilix.AwilixContainer): Awilix.AwilixContainer;
5
+ export declare function resolveFromRequestScope<T>(scope: Awilix.AwilixContainer, token: string | symbol): T;
@@ -0,0 +1,28 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ const requestScopeStorage = new AsyncLocalStorage();
3
+ export function runInRequestScopeContext(fn) {
4
+ const existingStore = requestScopeStorage.getStore();
5
+ if (existingStore) {
6
+ return fn();
7
+ }
8
+ return requestScopeStorage.run(new Map(), fn);
9
+ }
10
+ export function hasRequestScopeContext() {
11
+ return !!requestScopeStorage.getStore();
12
+ }
13
+ export function getOrCreateRequestScope(scope) {
14
+ const store = requestScopeStorage.getStore();
15
+ if (!store) {
16
+ return scope.createScope();
17
+ }
18
+ const existingScope = store.get(scope);
19
+ if (existingScope) {
20
+ return existingScope;
21
+ }
22
+ const requestScope = scope.createScope();
23
+ store.set(scope, requestScope);
24
+ return requestScope;
25
+ }
26
+ export function resolveFromRequestScope(scope, token) {
27
+ return getOrCreateRequestScope(scope).resolve(token);
28
+ }
@@ -0,0 +1,35 @@
1
+ import type { BuildResolverOptions, Constructor } from "awilix";
2
+ import type { ForwardRef } from "./modules/module-ref.types.js";
3
+ import type { FactoryProvider, FunctionProvider, PrimitiveProvider } from "./providers/provider.types.js";
4
+ export declare function hasUseClass<T extends object = object>(value: unknown): value is {
5
+ useClass: Constructor<T>;
6
+ allowCircular?: boolean;
7
+ eager?: boolean;
8
+ initAfter?: readonly string[];
9
+ } & Partial<BuildResolverOptions<any>>;
10
+ export declare function isFactoryProvider<T extends object>(provider: unknown): provider is FactoryProvider<T, any, readonly string[], false>;
11
+ export declare function isAsyncFactoryProvider(provider: unknown): boolean;
12
+ export declare function isCostructorProvider<T extends object>(provider: unknown): provider is Constructor<T>;
13
+ export declare function isPrimitive(provider: unknown): provider is PrimitiveProvider;
14
+ export declare function isPlainFunction(provider: unknown): provider is FunctionProvider;
15
+ export declare function isForwardRef(value: unknown): value is ForwardRef;
16
+ export declare function isPromiseLike<T = unknown>(value: unknown): value is Promise<T>;
17
+ export declare function isEagerProvider(provider: unknown): provider is {
18
+ eager: true;
19
+ };
20
+ export declare function hasProviderInit(value: unknown): value is {
21
+ init: () => void | Promise<void>;
22
+ };
23
+ export declare function hasProviderPostInit(value: unknown): value is {
24
+ postInit: () => void | Promise<void>;
25
+ };
26
+ export declare function hasInitAfter(provider: unknown): provider is {
27
+ initAfter: readonly string[];
28
+ };
29
+ export declare function isResultLike(value: unknown): value is {
30
+ ok: true;
31
+ value: unknown;
32
+ } | {
33
+ ok: false;
34
+ error: unknown;
35
+ };
@@ -0,0 +1,84 @@
1
+ export function hasUseClass(value) {
2
+ return typeof value === "object" && value !== null && "useClass" in value;
3
+ }
4
+ export function isFactoryProvider(provider) {
5
+ return (typeof provider === "object" &&
6
+ provider !== null &&
7
+ "useFactory" in provider);
8
+ }
9
+ export function isAsyncFactoryProvider(provider) {
10
+ return (isFactoryProvider(provider) &&
11
+ provider.useFactory.constructor.name === "AsyncFunction");
12
+ }
13
+ export function isCostructorProvider(provider) {
14
+ if (typeof provider !== "function")
15
+ return false;
16
+ if (!("prototype" in provider))
17
+ return false;
18
+ const proto = provider.prototype;
19
+ if (!proto || typeof proto !== "object")
20
+ return false;
21
+ const protoKeys = Object.getOwnPropertyNames(proto);
22
+ if (protoKeys.length === 0 ||
23
+ (protoKeys.length === 1 && protoKeys[0] === "constructor")) {
24
+ return provider.toString().trim().startsWith("class");
25
+ }
26
+ return true;
27
+ }
28
+ export function isPrimitive(provider) {
29
+ return (typeof provider === "string" ||
30
+ typeof provider === "number" ||
31
+ typeof provider === "boolean" ||
32
+ typeof provider === "symbol" ||
33
+ typeof provider === "bigint");
34
+ }
35
+ export function isPlainFunction(provider) {
36
+ return (typeof provider === "function" &&
37
+ (!provider.prototype || Object.keys(provider.prototype).length === 0));
38
+ }
39
+ export function isForwardRef(value) {
40
+ return (typeof value === "object" &&
41
+ value !== null &&
42
+ "__forward_ref__" in value &&
43
+ value.__forward_ref__ === true &&
44
+ "resolve" in value &&
45
+ typeof value.resolve === "function");
46
+ }
47
+ export function isPromiseLike(value) {
48
+ return (typeof value === "object" &&
49
+ value !== null &&
50
+ "then" in value &&
51
+ typeof value.then === "function");
52
+ }
53
+ export function isEagerProvider(provider) {
54
+ return (typeof provider === "object" &&
55
+ provider !== null &&
56
+ "eager" in provider &&
57
+ provider.eager === true);
58
+ }
59
+ export function hasProviderInit(value) {
60
+ return (typeof value === "object" &&
61
+ value !== null &&
62
+ "init" in value &&
63
+ typeof value.init === "function");
64
+ }
65
+ export function hasProviderPostInit(value) {
66
+ return (typeof value === "object" &&
67
+ value !== null &&
68
+ "postInit" in value &&
69
+ typeof value.postInit === "function");
70
+ }
71
+ export function hasInitAfter(provider) {
72
+ return (typeof provider === "object" &&
73
+ provider !== null &&
74
+ "initAfter" in provider &&
75
+ Array.isArray(provider.initAfter));
76
+ }
77
+ export function isResultLike(value) {
78
+ return (typeof value === "object" &&
79
+ value !== null &&
80
+ "ok" in value &&
81
+ (value.ok === true
82
+ ? "value" in value
83
+ : value.ok === false && "error" in value));
84
+ }
@@ -0,0 +1,33 @@
1
+ import type { DecoratorState, DecoratorToken } from "../decorators/decorator-state.types.js";
2
+ import { type HttpVerb } from "./http-verbs.js";
3
+ import type { RouteSchema } from "./openapi-builder.js";
4
+ type MiddlewareParameter = any[] | any;
5
+ type ControllerOptions = string | string[] | {
6
+ path: string | string[];
7
+ };
8
+ interface RootState {
9
+ paths: string[];
10
+ beforeMiddleware: any[];
11
+ afterMiddleware: any[];
12
+ }
13
+ interface MethodState {
14
+ paths: string[];
15
+ beforeMiddleware: any[];
16
+ afterMiddleware: any[];
17
+ verbs: HttpVerb[];
18
+ schema: RouteSchema;
19
+ }
20
+ export interface HttpDecoratorState extends DecoratorState<MethodState, RootState> {
21
+ }
22
+ export declare const HTTP_DECORATOR_STATE_TOKEN: DecoratorToken<HttpDecoratorState>;
23
+ export declare const GET: (path?: string) => (target: any, context: ClassMethodDecoratorContext) => any;
24
+ export declare const POST: (path?: string) => (target: any, context: ClassMethodDecoratorContext) => any;
25
+ export declare const PUT: (path?: string) => (target: any, context: ClassMethodDecoratorContext) => any;
26
+ export declare const DELETE: (path?: string) => (target: any, context: ClassMethodDecoratorContext) => any;
27
+ export declare const PATCH: (path?: string) => (target: any, context: ClassMethodDecoratorContext) => any;
28
+ export declare function controller(options?: ControllerOptions): (target: any, context: ClassDecoratorContext) => any;
29
+ export declare function before(middleware: MiddlewareParameter): (target: any, context: ClassMethodDecoratorContext | ClassDecoratorContext) => any;
30
+ export declare function after(middleware: MiddlewareParameter): (target: any, context: ClassMethodDecoratorContext | ClassDecoratorContext) => any;
31
+ export declare function schema(schema: RouteSchema): (target: any, context: ClassMethodDecoratorContext) => any;
32
+ export declare function rollUpHttpDecoratorState(root: RootState, method: MethodState): MethodState;
33
+ export {};
@@ -0,0 +1,151 @@
1
+ import { createDecoratorStateUpdater } from "../decorators/decorator-state.js";
2
+ import { HttpVerbs } from "./http-verbs.js";
3
+ const { update, token } = createDecoratorStateUpdater("http-decorator-state", {
4
+ method: () => ({
5
+ paths: [],
6
+ beforeMiddleware: [],
7
+ afterMiddleware: [],
8
+ verbs: [],
9
+ schema: {},
10
+ }),
11
+ root: () => ({
12
+ paths: [],
13
+ beforeMiddleware: [],
14
+ afterMiddleware: [],
15
+ }),
16
+ });
17
+ export const HTTP_DECORATOR_STATE_TOKEN = token;
18
+ function createRouteDecorator(httpVerb) {
19
+ return (path = "/") => (target, context) => {
20
+ update(context, {
21
+ method: (previous) => {
22
+ return {
23
+ ...previous,
24
+ verbs: uniq([...previous.verbs, httpVerb]),
25
+ paths: uniq([...previous.paths, path]),
26
+ };
27
+ },
28
+ });
29
+ return target;
30
+ };
31
+ }
32
+ export const GET = createRouteDecorator(HttpVerbs.GET);
33
+ export const POST = createRouteDecorator(HttpVerbs.POST);
34
+ export const PUT = createRouteDecorator(HttpVerbs.PUT);
35
+ export const DELETE = createRouteDecorator(HttpVerbs.DELETE);
36
+ export const PATCH = createRouteDecorator(HttpVerbs.PATCH);
37
+ export function controller(options) {
38
+ return (target, context) => {
39
+ if (!options)
40
+ return target;
41
+ update(context, {
42
+ root: (previous) => {
43
+ return {
44
+ ...previous,
45
+ paths: uniq([...previous.paths, ...normalizePaths(options)]),
46
+ };
47
+ },
48
+ });
49
+ return target;
50
+ };
51
+ }
52
+ export function before(middleware) {
53
+ return (target, context) => {
54
+ if (context.kind === "class") {
55
+ update(context, {
56
+ root: (previous) => {
57
+ return {
58
+ ...previous,
59
+ beforeMiddleware: appendMiddleware(previous.beforeMiddleware, middleware),
60
+ };
61
+ },
62
+ });
63
+ return target;
64
+ }
65
+ update(context, {
66
+ method: (previous) => {
67
+ return {
68
+ ...previous,
69
+ beforeMiddleware: appendMiddleware(previous.beforeMiddleware, middleware),
70
+ };
71
+ },
72
+ });
73
+ return target;
74
+ };
75
+ }
76
+ export function after(middleware) {
77
+ return (target, context) => {
78
+ if (context.kind === "class") {
79
+ update(context, {
80
+ root: (previous) => {
81
+ return {
82
+ ...previous,
83
+ afterMiddleware: appendMiddleware(previous.afterMiddleware, middleware),
84
+ };
85
+ },
86
+ });
87
+ return target;
88
+ }
89
+ update(context, {
90
+ method: (previous) => {
91
+ return {
92
+ ...previous,
93
+ afterMiddleware: appendMiddleware(previous.afterMiddleware, middleware),
94
+ };
95
+ },
96
+ });
97
+ return target;
98
+ };
99
+ }
100
+ export function schema(schema) {
101
+ return (target, context) => {
102
+ update(context, {
103
+ method: (previous) => {
104
+ return {
105
+ ...previous,
106
+ schema,
107
+ };
108
+ },
109
+ });
110
+ return target;
111
+ };
112
+ }
113
+ function uniq(items) {
114
+ const result = [];
115
+ for (const item of items) {
116
+ if (!result.includes(item))
117
+ result.push(item);
118
+ }
119
+ return result;
120
+ }
121
+ function normalizePaths(options) {
122
+ if (typeof options === "string")
123
+ return [options];
124
+ if (Array.isArray(options))
125
+ return options;
126
+ return Array.isArray(options.path) ? options.path : [options.path];
127
+ }
128
+ function appendMiddleware(target, incoming) {
129
+ return Array.isArray(incoming)
130
+ ? [...target, ...incoming]
131
+ : [...target, incoming];
132
+ }
133
+ export function rollUpHttpDecoratorState(root, method) {
134
+ return {
135
+ ...method,
136
+ paths: concatPaths(root.paths, method.paths),
137
+ beforeMiddleware: [...root.beforeMiddleware, ...method.beforeMiddleware],
138
+ afterMiddleware: [...method.afterMiddleware, ...root.afterMiddleware],
139
+ };
140
+ }
141
+ function concatPaths(rootPaths, methodPaths) {
142
+ if (rootPaths.length === 0)
143
+ return [...methodPaths];
144
+ const result = [];
145
+ rootPaths.forEach((rootPath) => {
146
+ methodPaths.forEach((methodPath) => {
147
+ result.push(rootPath + methodPath);
148
+ });
149
+ });
150
+ return result;
151
+ }
@@ -0,0 +1,58 @@
1
+ export declare enum HttpStatus {
2
+ OK = 200,
3
+ CREATED = 201,
4
+ BAD_REQUEST = 400,
5
+ UNAUTHORIZED = 401,
6
+ FORBIDDEN = 403,
7
+ NOT_FOUND = 404,
8
+ METHOD_NOT_ALLOWED = 405,
9
+ NOT_ACCEPTABLE = 406,
10
+ REQUEST_TIMEOUT = 408,
11
+ CONFLICT = 409,
12
+ GONE = 410,
13
+ PRECONDITION_FAILED = 412,
14
+ PAYLOAD_TOO_LARGE = 413,
15
+ UNSUPPORTED_MEDIA_TYPE = 415,
16
+ I_AM_A_TEAPOT = 418,
17
+ UNPROCESSABLE_ENTITY = 422,
18
+ INTERNAL_SERVER_ERROR = 500,
19
+ NOT_IMPLEMENTED = 501,
20
+ BAD_GATEWAY = 502,
21
+ SERVICE_UNAVAILABLE = 503,
22
+ GATEWAY_TIMEOUT = 504,
23
+ HTTP_VERSION_NOT_SUPPORTED = 505
24
+ }
25
+ export declare class HttpException<TMessage extends string, TStatus extends HttpStatus = HttpStatus> extends Error {
26
+ readonly message: TMessage;
27
+ readonly statusCode: TStatus;
28
+ readonly response?: (Record<string, any> | null) | undefined;
29
+ constructor(message: TMessage, statusCode: TStatus, response?: (Record<string, any> | null) | undefined);
30
+ getResponse(): {
31
+ message: TMessage;
32
+ statusCode: TStatus;
33
+ [key: string]: any;
34
+ };
35
+ getStatus(): TStatus;
36
+ }
37
+ export declare const httpException: {
38
+ badRequest: <M extends string = "Bad Request">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Bad Request" : M, HttpStatus.BAD_REQUEST>;
39
+ unauthorized: <M extends string = "Unauthorized">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Unauthorized" : M, HttpStatus.UNAUTHORIZED>;
40
+ forbidden: <M extends string = "Forbidden">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Forbidden" : M, HttpStatus.FORBIDDEN>;
41
+ notFound: <M extends string = "Not Found">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Not Found" : M, HttpStatus.NOT_FOUND>;
42
+ methodNotAllowed: <M extends string = "Method Not Allowed">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Method Not Allowed" : M, HttpStatus.METHOD_NOT_ALLOWED>;
43
+ notAcceptable: <M extends string = "Not Acceptable">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Not Acceptable" : M, HttpStatus.NOT_ACCEPTABLE>;
44
+ requestTimeout: <M extends string = "Request Timeout">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Request Timeout" : M, HttpStatus.REQUEST_TIMEOUT>;
45
+ conflict: <M extends string = "Conflict">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Conflict" : M, HttpStatus.CONFLICT>;
46
+ gone: <M extends string = "Gone">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Gone" : M, HttpStatus.GONE>;
47
+ preconditionFailed: <M extends string = "Precondition Failed">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Precondition Failed" : M, HttpStatus.PRECONDITION_FAILED>;
48
+ payloadTooLarge: <M extends string = "Payload Too Large">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Payload Too Large" : M, HttpStatus.PAYLOAD_TOO_LARGE>;
49
+ unsupportedMediaType: <M extends string = "Unsupported Media Type">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Unsupported Media Type" : M, HttpStatus.UNSUPPORTED_MEDIA_TYPE>;
50
+ imATeapot: <M extends string = "I'm a teapot">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "I'm a teapot" : M, HttpStatus.I_AM_A_TEAPOT>;
51
+ unprocessableEntity: <M extends string = "Unprocessable Entity">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Unprocessable Entity" : M, HttpStatus.UNPROCESSABLE_ENTITY>;
52
+ internalServerError: <M extends string = "Internal Server Error">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Internal Server Error" : M, HttpStatus.INTERNAL_SERVER_ERROR>;
53
+ notImplemented: <M extends string = "Not Implemented">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Not Implemented" : M, HttpStatus.NOT_IMPLEMENTED>;
54
+ badGateway: <M extends string = "Bad Gateway">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Bad Gateway" : M, HttpStatus.BAD_GATEWAY>;
55
+ serviceUnavailable: <M extends string = "Service Unavailable">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Service Unavailable" : M, HttpStatus.SERVICE_UNAVAILABLE>;
56
+ gatewayTimeout: <M extends string = "Gateway Timeout">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "Gateway Timeout" : M, HttpStatus.GATEWAY_TIMEOUT>;
57
+ httpVersionNotSupported: <M extends string = "HTTP Version Not Supported">(message?: M | undefined, response?: Record<string, any>) => HttpException<M extends undefined ? "HTTP Version Not Supported" : M, HttpStatus.HTTP_VERSION_NOT_SUPPORTED>;
58
+ };
@@ -0,0 +1,71 @@
1
+ export var HttpStatus;
2
+ (function (HttpStatus) {
3
+ HttpStatus[HttpStatus["OK"] = 200] = "OK";
4
+ HttpStatus[HttpStatus["CREATED"] = 201] = "CREATED";
5
+ HttpStatus[HttpStatus["BAD_REQUEST"] = 400] = "BAD_REQUEST";
6
+ HttpStatus[HttpStatus["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
7
+ HttpStatus[HttpStatus["FORBIDDEN"] = 403] = "FORBIDDEN";
8
+ HttpStatus[HttpStatus["NOT_FOUND"] = 404] = "NOT_FOUND";
9
+ HttpStatus[HttpStatus["METHOD_NOT_ALLOWED"] = 405] = "METHOD_NOT_ALLOWED";
10
+ HttpStatus[HttpStatus["NOT_ACCEPTABLE"] = 406] = "NOT_ACCEPTABLE";
11
+ HttpStatus[HttpStatus["REQUEST_TIMEOUT"] = 408] = "REQUEST_TIMEOUT";
12
+ HttpStatus[HttpStatus["CONFLICT"] = 409] = "CONFLICT";
13
+ HttpStatus[HttpStatus["GONE"] = 410] = "GONE";
14
+ HttpStatus[HttpStatus["PRECONDITION_FAILED"] = 412] = "PRECONDITION_FAILED";
15
+ HttpStatus[HttpStatus["PAYLOAD_TOO_LARGE"] = 413] = "PAYLOAD_TOO_LARGE";
16
+ HttpStatus[HttpStatus["UNSUPPORTED_MEDIA_TYPE"] = 415] = "UNSUPPORTED_MEDIA_TYPE";
17
+ HttpStatus[HttpStatus["I_AM_A_TEAPOT"] = 418] = "I_AM_A_TEAPOT";
18
+ HttpStatus[HttpStatus["UNPROCESSABLE_ENTITY"] = 422] = "UNPROCESSABLE_ENTITY";
19
+ HttpStatus[HttpStatus["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
20
+ HttpStatus[HttpStatus["NOT_IMPLEMENTED"] = 501] = "NOT_IMPLEMENTED";
21
+ HttpStatus[HttpStatus["BAD_GATEWAY"] = 502] = "BAD_GATEWAY";
22
+ HttpStatus[HttpStatus["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
23
+ HttpStatus[HttpStatus["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
24
+ HttpStatus[HttpStatus["HTTP_VERSION_NOT_SUPPORTED"] = 505] = "HTTP_VERSION_NOT_SUPPORTED";
25
+ })(HttpStatus || (HttpStatus = {}));
26
+ export class HttpException extends Error {
27
+ message;
28
+ statusCode;
29
+ response;
30
+ constructor(message, statusCode, response) {
31
+ super(message);
32
+ this.message = message;
33
+ this.statusCode = statusCode;
34
+ this.response = response;
35
+ this.name = this.constructor.name;
36
+ Error.captureStackTrace(this, this.constructor);
37
+ }
38
+ getResponse() {
39
+ return {
40
+ message: this.message,
41
+ statusCode: this.statusCode,
42
+ ...this.response,
43
+ };
44
+ }
45
+ getStatus() {
46
+ return this.statusCode;
47
+ }
48
+ }
49
+ const createFactory = (defaultMessage, status) => (message, response) => new HttpException((message ?? defaultMessage), status, response);
50
+ export const httpException = {
51
+ badRequest: createFactory("Bad Request", HttpStatus.BAD_REQUEST),
52
+ unauthorized: createFactory("Unauthorized", HttpStatus.UNAUTHORIZED),
53
+ forbidden: createFactory("Forbidden", HttpStatus.FORBIDDEN),
54
+ notFound: createFactory("Not Found", HttpStatus.NOT_FOUND),
55
+ methodNotAllowed: createFactory("Method Not Allowed", HttpStatus.METHOD_NOT_ALLOWED),
56
+ notAcceptable: createFactory("Not Acceptable", HttpStatus.NOT_ACCEPTABLE),
57
+ requestTimeout: createFactory("Request Timeout", HttpStatus.REQUEST_TIMEOUT),
58
+ conflict: createFactory("Conflict", HttpStatus.CONFLICT),
59
+ gone: createFactory("Gone", HttpStatus.GONE),
60
+ preconditionFailed: createFactory("Precondition Failed", HttpStatus.PRECONDITION_FAILED),
61
+ payloadTooLarge: createFactory("Payload Too Large", HttpStatus.PAYLOAD_TOO_LARGE),
62
+ unsupportedMediaType: createFactory("Unsupported Media Type", HttpStatus.UNSUPPORTED_MEDIA_TYPE),
63
+ imATeapot: createFactory("I'm a teapot", HttpStatus.I_AM_A_TEAPOT),
64
+ unprocessableEntity: createFactory("Unprocessable Entity", HttpStatus.UNPROCESSABLE_ENTITY),
65
+ internalServerError: createFactory("Internal Server Error", HttpStatus.INTERNAL_SERVER_ERROR),
66
+ notImplemented: createFactory("Not Implemented", HttpStatus.NOT_IMPLEMENTED),
67
+ badGateway: createFactory("Bad Gateway", HttpStatus.BAD_GATEWAY),
68
+ serviceUnavailable: createFactory("Service Unavailable", HttpStatus.SERVICE_UNAVAILABLE),
69
+ gatewayTimeout: createFactory("Gateway Timeout", HttpStatus.GATEWAY_TIMEOUT),
70
+ httpVersionNotSupported: createFactory("HTTP Version Not Supported", HttpStatus.HTTP_VERSION_NOT_SUPPORTED),
71
+ };
@@ -0,0 +1,12 @@
1
+ export type HttpVerb = "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "CONNECT" | "OPTIONS" | "PATCH" | "*";
2
+ export declare const HttpVerbs: {
3
+ readonly GET: "GET";
4
+ readonly HEAD: "HEAD";
5
+ readonly POST: "POST";
6
+ readonly PUT: "PUT";
7
+ readonly DELETE: "DELETE";
8
+ readonly CONNECT: "CONNECT";
9
+ readonly OPTIONS: "OPTIONS";
10
+ readonly PATCH: "PATCH";
11
+ readonly ALL: "*";
12
+ };
@@ -0,0 +1,11 @@
1
+ export const HttpVerbs = {
2
+ GET: "GET",
3
+ HEAD: "HEAD",
4
+ POST: "POST",
5
+ PUT: "PUT",
6
+ DELETE: "DELETE",
7
+ CONNECT: "CONNECT",
8
+ OPTIONS: "OPTIONS",
9
+ PATCH: "PATCH",
10
+ ALL: "*",
11
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./decorators.js";
2
+ export * from "./exceptions.js";
3
+ export * from "./openapi-builder.js";
@@ -0,0 +1,3 @@
1
+ export * from "./decorators.js";
2
+ export * from "./exceptions.js";
3
+ export * from "./openapi-builder.js";