@keq-request/nestjs 5.0.0-beta.1 → 5.0.0-beta.10

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.
@@ -1,10 +1,10 @@
1
1
 
2
- > @keq-request/nestjs@5.0.0-beta.1 build /home/runner/work/keq/keq/packages/nestjs
2
+ > @keq-request/nestjs@5.0.0-beta.10 build /home/runner/work/keq/keq/packages/nestjs
3
3
  > tsdown
4
4
 
5
5
  ℹ tsdown v0.21.10 powered by rolldown v1.0.0-rc.17
6
6
  ℹ config file: /home/runner/work/keq/keq/packages/nestjs/tsdown.config.ts
7
- (node:2773) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/keq/keq/packages/nestjs/tsdown.config.ts?no-cache=119757d8-e689-4e58-9a81-69c968dc25b8 is not specified and it doesn't parse as CommonJS.
7
+ (node:2742) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///home/runner/work/keq/keq/packages/nestjs/tsdown.config.ts?no-cache=8189b2cf-8eb7-4535-86e4-76dbf1cebae5 is not specified and it doesn't parse as CommonJS.
8
8
  Reparsing as ES module because module syntax was detected. This incurs a performance overhead.
9
9
  To eliminate this warning, add "type": "module" to /home/runner/work/keq/keq/packages/nestjs/package.json.
10
10
  (Use `node --trace-warnings ...` to show where the warning was created)
@@ -12,25 +12,25 @@ To eliminate this warning, add "type": "module" to /home/runner/work/keq/keq/pac
12
12
  ℹ target: node20
13
13
  ℹ tsconfig: tsconfig.lib.json
14
14
  ℹ Build start
15
- ℹ [CJS] dist/index.js 2.35 kB │ gzip: 1.00 kB
16
- ℹ [CJS] dist/index.js.map 3.54 kB │ gzip: 1.35 kB
17
- ℹ [CJS] 2 files, total: 5.89 kB
15
+ ℹ [CJS] dist/index.js 5.83 kB │ gzip: 2.01 kB
16
+ ℹ [CJS] dist/index.js.map 7.28 kB │ gzip: 2.38 kB
17
+ ℹ [CJS] 2 files, total: 13.11 kB
18
18
  [PLUGIN_TIMINGS] Warning: Your build spent significant time in plugin `tsdown:deps`. See https://rolldown.rs/options/checks#plugintimings for more details.
19
19
 
20
- ℹ [CJS] dist/index.d.ts.map 0.54 kB │ gzip: 0.30 kB
21
- ℹ [CJS] dist/index.d.ts 2.17 kB │ gzip: 0.89 kB
22
- ℹ [CJS] 2 files, total: 2.71 kB
20
+ ℹ [ESM] dist/index.mjs 4.54 kB │ gzip: 1.57 kB
21
+ ℹ [ESM] dist/index.mjs.map 7.21 kB │ gzip: 2.36 kB
22
+ ℹ [ESM] dist/index.d.mts.map 0.99 kB │ gzip: 0.43 kB
23
+ ℹ [ESM] dist/index.d.mts 1.86 kB │ gzip: 0.66 kB
24
+ ℹ [ESM] 4 files, total: 14.60 kB
25
+ [PLUGIN_TIMINGS] Warning: Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
26
+
27
+ ✔ Build complete in 5031ms
28
+ ℹ [CJS] dist/index.d.ts.map 0.99 kB │ gzip: 0.43 kB
29
+ ℹ [CJS] dist/index.d.ts 1.86 kB │ gzip: 0.65 kB
30
+ ℹ [CJS] 2 files, total: 2.85 kB
31
+ ✔ Build complete in 5034ms
23
32
  [PLUGIN_TIMINGS] Warning: Your build spent significant time in plugins. Here is a breakdown:
24
- - rolldown-plugin-dts:generate (65%)
25
- - tsdown:deps (32%)
33
+ - rolldown-plugin-dts:generate (46%)
34
+ - tsdown:deps (44%)
26
35
  See https://rolldown.rs/options/checks#plugintimings for more details.
27
36
 
28
- ✔ Build complete in 4279ms
29
- ℹ [ESM] dist/index.mjs 2.17 kB │ gzip: 0.92 kB
30
- ℹ [ESM] dist/index.mjs.map 3.49 kB │ gzip: 1.34 kB
31
- ℹ [ESM] dist/index.d.mts.map 0.54 kB │ gzip: 0.30 kB
32
- ℹ [ESM] dist/index.d.mts 2.17 kB │ gzip: 0.89 kB
33
- ℹ [ESM] 4 files, total: 8.37 kB
34
- [PLUGIN_TIMINGS] Warning: Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
35
-
36
- ✔ Build complete in 4280ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,64 @@
1
1
  # @keq-request/nestjs
2
2
 
3
+ ## 5.0.0-beta.10
4
+
5
+ ### Minor Changes
6
+
7
+ - 2e13e75: **BREAKING CHANGE:** Remove `request` option from `KeqModuleOptions`, use fork-based instance isolation instead.
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [95908fd]
12
+ - keq@5.0.0-beta.10
13
+
14
+ ## 5.0.0-beta.9
15
+
16
+ ### Patch Changes
17
+
18
+ - keq@5.0.0-beta.9
19
+
20
+ ## 5.0.0-beta.8
21
+
22
+ ### Patch Changes
23
+
24
+ - keq@5.0.0-beta.8
25
+
26
+ ## 5.0.0-beta.7
27
+
28
+ ### Patch Changes
29
+
30
+ - keq@5.0.0-beta.7
31
+
32
+ ## 5.0.0-beta.6
33
+
34
+ ### Patch Changes
35
+
36
+ - keq@5.0.0-beta.6
37
+
38
+ ## 5.0.0-beta.5
39
+
40
+ ### Patch Changes
41
+
42
+ - keq@5.0.0-beta.5
43
+
44
+ ## 5.0.0-beta.4
45
+
46
+ ### Patch Changes
47
+
48
+ - keq@5.0.0-beta.4
49
+
50
+ ## 5.0.0-beta.3
51
+
52
+ ### Patch Changes
53
+
54
+ - keq@5.0.0-beta.3
55
+
56
+ ## 5.0.0-beta.2
57
+
58
+ ### Patch Changes
59
+
60
+ - keq@5.0.0-beta.2
61
+
3
62
  ## 5.0.0-beta.1
4
63
 
5
64
  ### Minor Changes
package/dist/index.d.mts CHANGED
@@ -1,62 +1,52 @@
1
- import { KeqMiddleware } from "keq";
2
- import * as _$_nestjs_common0 from "@nestjs/common";
3
- import { DynamicModule } from "@nestjs/common";
1
+ import { OnModuleInit, Type } from "@nestjs/common";
2
+ import { ModuleRef, ModulesContainer } from "@nestjs/core";
3
+ import { KeqExecutionContext, KeqMiddleware, KeqNext, KeqRequest } from "keq";
4
4
 
5
- //#region src/types/keq-module-options.d.ts
6
- interface KeqModuleOptions {
7
- middlewares?: KeqMiddleware[];
5
+ //#region src/keq.module.d.ts
6
+ declare class KeqModule implements OnModuleInit {
7
+ private readonly modulesContainer;
8
+ private readonly moduleRef;
9
+ private readonly keqRequest;
10
+ constructor(modulesContainer: ModulesContainer, moduleRef: ModuleRef, keqRequest: KeqRequest);
11
+ onModuleInit(): void;
8
12
  }
9
13
  //#endregion
10
- //#region src/keq.module-definition.d.ts
11
- declare const ConfigurableModuleClass: _$_nestjs_common0.ConfigurableModuleCls<KeqModuleOptions, "register", "create", {}>, MODULE_OPTIONS_TOKEN: string | symbol, OPTIONS_TYPE: KeqModuleOptions & Partial<{}>, ASYNC_OPTIONS_TYPE: _$_nestjs_common0.ConfigurableModuleAsyncOptions<KeqModuleOptions, "create"> & Partial<{}>;
14
+ //#region src/constants.d.ts
15
+ declare const KEQ_ROUTES: {
16
+ readonly ALL: symbol;
17
+ };
12
18
  //#endregion
13
- //#region src/keq.module.d.ts
14
- /**
15
- * KeqModule - Dynamic module for integrating Keq HTTP client with NestJS
16
- *
17
- * This module is global, so KeqRequest can be injected anywhere in your application
18
- * without needing to import the module in every feature module.
19
- *
20
- * @example
21
- * ```typescript
22
- * // Synchronous registration
23
- * KeqModule.register({
24
- * middlewares: [...]
25
- * })
26
- *
27
- * // Asynchronous registration
28
- * KeqModule.registerAsync({
29
- * imports: [ConfigModule],
30
- * useFactory: (config: ConfigService) => ({
31
- * middlewares: [...]
32
- * }),
33
- * inject: [ConfigService]
34
- * })
35
- *
36
- * // Inject in service
37
- * class UserService {
38
- * constructor(private readonly keqRequest: KeqRequest) {}
39
- * async getUser(id: string) {
40
- * return await this.keqRequest.get(`/users/${id}`).resolveWith('json')
41
- * }
42
- * }
43
- * ```
44
- */
45
- declare class KeqModule extends ConfigurableModuleClass {
46
- /**
47
- * Register the module with synchronous options
48
- * @param options - Configuration options including middlewares
49
- * @returns DynamicModule with KeqRequest provider
50
- */
51
- static register(options: typeof OPTIONS_TYPE): DynamicModule;
52
- /**
53
- * Register the module with asynchronous options
54
- * Useful when you need to inject dependencies like ConfigService
55
- * @param options - Async configuration options
56
- * @returns DynamicModule with KeqRequest provider
57
- */
58
- static registerAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule;
19
+ //#region src/interfaces/keq-nest-middleware.interface.d.ts
20
+ interface KeqNestMiddleware {
21
+ use(ctx: KeqExecutionContext, next: KeqNext): void | PromiseLike<void>;
22
+ }
23
+ //#endregion
24
+ //#region src/interfaces/keq-middleware-consumer.interface.d.ts
25
+ interface KeqRouteInfo {
26
+ host?: string;
27
+ method?: string;
28
+ pathname?: string;
29
+ }
30
+ interface KeqModuleClass {
31
+ readonly KEQ_REQUEST: symbol;
32
+ }
33
+ interface KeqMiddlewareConfigProxy {
34
+ forRoutes(...routes: Array<typeof KEQ_ROUTES.ALL | KeqRouteInfo | KeqModuleClass>): KeqMiddlewareConsumer;
35
+ }
36
+ interface KeqMiddlewareConsumer {
37
+ apply(...middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>): KeqMiddlewareConfigProxy;
38
+ }
39
+ //#endregion
40
+ //#region src/interfaces/keq-middleware-module.interface.d.ts
41
+ interface KeqMiddlewareModule {
42
+ configureKeqMiddleware(consumer: KeqMiddlewareConsumer): void;
43
+ }
44
+ //#endregion
45
+ //#region src/interfaces/keq-module-options.interface.d.ts
46
+ interface KeqModuleOptions {
47
+ middlewares?: KeqMiddleware[];
48
+ isolate?: boolean;
59
49
  }
60
50
  //#endregion
61
- export { KeqModule, KeqModuleOptions };
51
+ export { KEQ_ROUTES, type KeqMiddlewareConfigProxy, type KeqMiddlewareConsumer, type KeqMiddlewareModule, KeqModule, type KeqModuleClass, type KeqModuleOptions, type KeqNestMiddleware, type KeqRouteInfo };
62
52
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/types/keq-module-options.ts","../src/keq.module-definition.ts","../src/keq.module.ts"],"mappings":";;;;;UAGiB,gBAAA;EACf,WAAA,GAAc,aAAA;AAAA;;;cCEd,uBAAA,EAAuB,iBAAA,CAAA,qBAAA,CAAA,gBAAA,6BAAA,oBAAA,mBAAA,YAAA,EAEX,gBAAA,GAAA,OAAA,MAAA,kBAAA,EACM,iBAAA,CAAA,8BAAA,CAAA,gBAAA,cAAA,OAAA;;;;;;ADNpB;;;;;;;;ACEA;;;;;;;;;;;;;;;;;;;;cCsDa,SAAA,SAAkB,uBAAA;EDnD7B;;;;;EAAA,OCyDO,QAAA,CAAS,OAAA,SAAgB,YAAA,GAAe,aAAA;EAN1B;;;;;;EAAA,OAoBd,aAAA,CAAc,OAAA,SAAgB,kBAAA,GAAqB,aAAA;AAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/keq.module.ts","../src/constants.ts","../src/interfaces/keq-nest-middleware.interface.ts","../src/interfaces/keq-middleware-consumer.interface.ts","../src/interfaces/keq-middleware-module.interface.ts","../src/interfaces/keq-module-options.interface.ts"],"mappings":";;;;;cAYa,SAAA,YAAqB,YAAA;EAAA,iBAEb,gBAAA;EAAA,iBACA,SAAA;EAAA,iBACA,UAAA;cAFA,gBAAA,EAAkB,gBAAA,EAClB,SAAA,EAAW,SAAA,EACX,UAAA,EAAY,UAAA;EAG/B,YAAA,CAAA;AAAA;;;cCnBW,UAAA;EAAA,SAEH,GAAA;AAAA;;;UCCO,iBAAA;EACf,GAAA,CAAI,GAAA,EAAK,mBAAA,EAAqB,IAAA,EAAM,OAAA,UAAiB,WAAA;AAAA;;;UCEtC,YAAA;EACf,IAAA;EACA,MAAA;EACA,QAAA;AAAA;AAAA,UAGe,cAAA;EAAA,SACN,WAAA;AAAA;AAAA,UAGM,wBAAA;EACf,SAAA,IAAa,MAAA,EAAQ,KAAA,QAAa,UAAA,CAAW,GAAA,GAAM,YAAA,GAAe,cAAA,IAAkB,qBAAA;AAAA;AAAA,UAGrE,qBAAA;EACf,KAAA,IAAS,WAAA,EAAa,KAAA,CAAM,IAAA,CAAK,iBAAA,IAAqB,aAAA,IAAiB,wBAAA;AAAA;;;UClBxD,mBAAA;EACf,sBAAA,CAAuB,QAAA,EAAU,qBAAA;AAAA;;;UCDlB,gBAAA;EACf,WAAA,GAAc,aAAA;EACd,OAAA;AAAA"}
package/dist/index.d.ts CHANGED
@@ -1,62 +1,52 @@
1
- import * as _$_nestjs_common0 from "@nestjs/common";
2
- import { DynamicModule } from "@nestjs/common";
3
- import { KeqMiddleware } from "keq";
1
+ import { OnModuleInit, Type } from "@nestjs/common";
2
+ import { ModuleRef, ModulesContainer } from "@nestjs/core";
3
+ import { KeqExecutionContext, KeqMiddleware, KeqNext, KeqRequest } from "keq";
4
4
 
5
- //#region src/types/keq-module-options.d.ts
6
- interface KeqModuleOptions {
7
- middlewares?: KeqMiddleware[];
5
+ //#region src/keq.module.d.ts
6
+ declare class KeqModule implements OnModuleInit {
7
+ private readonly modulesContainer;
8
+ private readonly moduleRef;
9
+ private readonly keqRequest;
10
+ constructor(modulesContainer: ModulesContainer, moduleRef: ModuleRef, keqRequest: KeqRequest);
11
+ onModuleInit(): void;
8
12
  }
9
13
  //#endregion
10
- //#region src/keq.module-definition.d.ts
11
- declare const ConfigurableModuleClass: _$_nestjs_common0.ConfigurableModuleCls<KeqModuleOptions, "register", "create", {}>, MODULE_OPTIONS_TOKEN: string | symbol, OPTIONS_TYPE: KeqModuleOptions & Partial<{}>, ASYNC_OPTIONS_TYPE: _$_nestjs_common0.ConfigurableModuleAsyncOptions<KeqModuleOptions, "create"> & Partial<{}>;
14
+ //#region src/constants.d.ts
15
+ declare const KEQ_ROUTES: {
16
+ readonly ALL: symbol;
17
+ };
12
18
  //#endregion
13
- //#region src/keq.module.d.ts
14
- /**
15
- * KeqModule - Dynamic module for integrating Keq HTTP client with NestJS
16
- *
17
- * This module is global, so KeqRequest can be injected anywhere in your application
18
- * without needing to import the module in every feature module.
19
- *
20
- * @example
21
- * ```typescript
22
- * // Synchronous registration
23
- * KeqModule.register({
24
- * middlewares: [...]
25
- * })
26
- *
27
- * // Asynchronous registration
28
- * KeqModule.registerAsync({
29
- * imports: [ConfigModule],
30
- * useFactory: (config: ConfigService) => ({
31
- * middlewares: [...]
32
- * }),
33
- * inject: [ConfigService]
34
- * })
35
- *
36
- * // Inject in service
37
- * class UserService {
38
- * constructor(private readonly keqRequest: KeqRequest) {}
39
- * async getUser(id: string) {
40
- * return await this.keqRequest.get(`/users/${id}`).resolveWith('json')
41
- * }
42
- * }
43
- * ```
44
- */
45
- declare class KeqModule extends ConfigurableModuleClass {
46
- /**
47
- * Register the module with synchronous options
48
- * @param options - Configuration options including middlewares
49
- * @returns DynamicModule with KeqRequest provider
50
- */
51
- static register(options: typeof OPTIONS_TYPE): DynamicModule;
52
- /**
53
- * Register the module with asynchronous options
54
- * Useful when you need to inject dependencies like ConfigService
55
- * @param options - Async configuration options
56
- * @returns DynamicModule with KeqRequest provider
57
- */
58
- static registerAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule;
19
+ //#region src/interfaces/keq-nest-middleware.interface.d.ts
20
+ interface KeqNestMiddleware {
21
+ use(ctx: KeqExecutionContext, next: KeqNext): void | PromiseLike<void>;
22
+ }
23
+ //#endregion
24
+ //#region src/interfaces/keq-middleware-consumer.interface.d.ts
25
+ interface KeqRouteInfo {
26
+ host?: string;
27
+ method?: string;
28
+ pathname?: string;
29
+ }
30
+ interface KeqModuleClass {
31
+ readonly KEQ_REQUEST: symbol;
32
+ }
33
+ interface KeqMiddlewareConfigProxy {
34
+ forRoutes(...routes: Array<typeof KEQ_ROUTES.ALL | KeqRouteInfo | KeqModuleClass>): KeqMiddlewareConsumer;
35
+ }
36
+ interface KeqMiddlewareConsumer {
37
+ apply(...middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>): KeqMiddlewareConfigProxy;
38
+ }
39
+ //#endregion
40
+ //#region src/interfaces/keq-middleware-module.interface.d.ts
41
+ interface KeqMiddlewareModule {
42
+ configureKeqMiddleware(consumer: KeqMiddlewareConsumer): void;
43
+ }
44
+ //#endregion
45
+ //#region src/interfaces/keq-module-options.interface.d.ts
46
+ interface KeqModuleOptions {
47
+ middlewares?: KeqMiddleware[];
48
+ isolate?: boolean;
59
49
  }
60
50
  //#endregion
61
- export { KeqModule, KeqModuleOptions };
51
+ export { KEQ_ROUTES, type KeqMiddlewareConfigProxy, type KeqMiddlewareConsumer, type KeqMiddlewareModule, KeqModule, type KeqModuleClass, type KeqModuleOptions, type KeqNestMiddleware, type KeqRouteInfo };
62
52
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../src/types/keq-module-options.ts","../src/keq.module-definition.ts","../src/keq.module.ts"],"mappings":";;;;;UAGiB,gBAAA;EACf,WAAA,GAAc,aAAA;AAAA;;;cCEd,uBAAA,EAAuB,iBAAA,CAAA,qBAAA,CAAA,gBAAA,6BAAA,oBAAA,mBAAA,YAAA,EAEX,gBAAA,GAAA,OAAA,MAAA,kBAAA,EACM,iBAAA,CAAA,8BAAA,CAAA,gBAAA,cAAA,OAAA;;;;;;ADNpB;;;;;;;;ACEA;;;;;;;;;;;;;;;;;;;;cCsDa,SAAA,SAAkB,uBAAA;EDnD7B;;;;;EAAA,OCyDO,QAAA,CAAS,OAAA,SAAgB,YAAA,GAAe,aAAA;EAN1B;;;;;;EAAA,OAoBd,aAAA,CAAc,OAAA,SAAgB,kBAAA,GAAqB,aAAA;AAAA"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/keq.module.ts","../src/constants.ts","../src/interfaces/keq-nest-middleware.interface.ts","../src/interfaces/keq-middleware-consumer.interface.ts","../src/interfaces/keq-middleware-module.interface.ts","../src/interfaces/keq-module-options.interface.ts"],"mappings":";;;;;cAYa,SAAA,YAAqB,YAAA;EAAA,iBAEb,gBAAA;EAAA,iBACA,SAAA;EAAA,iBACA,UAAA;cAFA,gBAAA,EAAkB,gBAAA,EAClB,SAAA,EAAW,SAAA,EACX,UAAA,EAAY,UAAA;EAG/B,YAAA,CAAA;AAAA;;;cCnBW,UAAA;EAAA,SAEH,GAAA;AAAA;;;UCCO,iBAAA;EACf,GAAA,CAAI,GAAA,EAAK,mBAAA,EAAqB,IAAA,EAAM,OAAA,UAAiB,WAAA;AAAA;;;UCEtC,YAAA;EACf,IAAA;EACA,MAAA;EACA,QAAA;AAAA;AAAA,UAGe,cAAA;EAAA,SACN,WAAA;AAAA;AAAA,UAGM,wBAAA;EACf,SAAA,IAAa,MAAA,EAAQ,KAAA,QAAa,UAAA,CAAW,GAAA,GAAM,YAAA,GAAe,cAAA,IAAkB,qBAAA;AAAA;AAAA,UAGrE,qBAAA;EACf,KAAA,IAAS,WAAA,EAAa,KAAA,CAAM,IAAA,CAAK,iBAAA,IAAqB,aAAA,IAAiB,wBAAA;AAAA;;;UClBxD,mBAAA;EACf,sBAAA,CAAuB,QAAA,EAAU,qBAAA;AAAA;;;UCDlB,gBAAA;EACf,WAAA,GAAc,aAAA;EACd,OAAA;AAAA"}
package/dist/index.js CHANGED
@@ -1,8 +1,107 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- let keq = require("keq");
2
+ //#region \0rolldown/runtime.js
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
13
+ get: ((k) => from[k]).bind(null, key),
14
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
15
+ });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
20
+ value: mod,
21
+ enumerable: true
22
+ }) : target, mod));
23
+ //#endregion
3
24
  let _nestjs_common = require("@nestjs/common");
4
- //#region src/keq.module-definition.ts
5
- const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, OPTIONS_TYPE, ASYNC_OPTIONS_TYPE } = new _nestjs_common.ConfigurableModuleBuilder().build();
25
+ let _nestjs_core = require("@nestjs/core");
26
+ let keq = require("keq");
27
+ let picomatch = require("picomatch");
28
+ picomatch = __toESM(picomatch);
29
+ //#region src/constants.ts
30
+ const KEQ_ROUTES = { ALL: Symbol("KEQ_ROUTES.ALL") };
31
+ //#endregion
32
+ //#region src/keq-middleware-consumer.ts
33
+ var KeqMiddlewareConsumerImpl = class {
34
+ entries = [];
35
+ apply(...middlewares) {
36
+ const entry = {
37
+ middlewares,
38
+ routes: []
39
+ };
40
+ this.entries.push(entry);
41
+ return { forRoutes: (...routes) => {
42
+ entry.routes = routes;
43
+ return this;
44
+ } };
45
+ }
46
+ applyTo(keqRequest, moduleRef) {
47
+ for (const entry of this.entries) {
48
+ const resolved = entry.middlewares.map((m) => this.resolveMiddleware(m, moduleRef));
49
+ if (entry.routes.length === 0 || entry.routes.some((r) => r === KEQ_ROUTES.ALL)) for (const mw of resolved) keqRequest.use(mw);
50
+ else for (const routeTarget of entry.routes) {
51
+ if (routeTarget === KEQ_ROUTES.ALL) continue;
52
+ if (this.isKeqModuleClass(routeTarget)) {
53
+ const targetRequest = moduleRef.get(routeTarget.KEQ_REQUEST, { strict: false });
54
+ if (targetRequest) for (const mw of resolved) targetRequest.use(mw);
55
+ } else {
56
+ const route = this.buildRoute(routeTarget);
57
+ keqRequest.useRouter().route(route, resolved);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ isKeqModuleClass(route) {
63
+ return typeof route === "function" && "KEQ_REQUEST" in route && typeof route.KEQ_REQUEST === "symbol";
64
+ }
65
+ isNestMiddlewareClass(m) {
66
+ return typeof m === "function" && m.prototype !== void 0 && "use" in m.prototype;
67
+ }
68
+ resolveMiddleware(m, moduleRef) {
69
+ if (this.isNestMiddlewareClass(m)) {
70
+ const instance = moduleRef.get(m, { strict: false });
71
+ return async (ctx, next) => {
72
+ await instance.use(ctx, next);
73
+ };
74
+ }
75
+ return m;
76
+ }
77
+ buildRoute(info) {
78
+ const routes = [];
79
+ if (info.host) {
80
+ const host = info.host;
81
+ routes.push((ctx) => ctx.request.url.host === host);
82
+ }
83
+ if (info.method) {
84
+ const method = info.method.toLowerCase();
85
+ routes.push((ctx) => ctx.request.method.toLowerCase() === method);
86
+ }
87
+ if (info.pathname) {
88
+ const isMatch = (0, picomatch.default)(info.pathname);
89
+ routes.push((ctx) => isMatch(ctx.request.url.pathname));
90
+ }
91
+ if (routes.length === 1) return routes[0];
92
+ return (0, keq.composeRoute)(routes);
93
+ }
94
+ };
95
+ //#endregion
96
+ //#region src/utils/has-configure-keq-middleware.ts
97
+ function hasConfigureKeqMiddleware(instance) {
98
+ return instance !== null && instance !== void 0 && typeof instance.configureKeqMiddleware === "function";
99
+ }
100
+ //#endregion
101
+ //#region \0@oxc-project+runtime@0.127.0/helpers/decorateMetadata.js
102
+ function __decorateMetadata(k, v) {
103
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
104
+ }
6
105
  //#endregion
7
106
  //#region \0@oxc-project+runtime@0.127.0/helpers/decorate.js
8
107
  function __decorate(decorators, target, key, desc) {
@@ -13,46 +112,41 @@ function __decorate(decorators, target, key, desc) {
13
112
  }
14
113
  //#endregion
15
114
  //#region src/keq.module.ts
16
- /**
17
- * Provider that creates and configures a KeqRequest instance
18
- * Applies any middlewares defined in the module options
19
- */
20
- const requestProvider = {
21
- provide: keq.KeqRequest,
22
- useFactory: (config) => {
23
- const request = new keq.KeqRequest();
24
- if (config.middlewares) for (const middleware of config.middlewares) request.use(middleware);
25
- return request;
26
- },
27
- inject: [MODULE_OPTIONS_TOKEN]
28
- };
29
- let KeqModule = class KeqModule extends ConfigurableModuleClass {
30
- /**
31
- * Register the module with synchronous options
32
- * @param options - Configuration options including middlewares
33
- * @returns DynamicModule with KeqRequest provider
34
- */
35
- static register(options) {
36
- const mo = super.register(options);
37
- mo.providers.push(requestProvider);
38
- mo.global = true;
39
- return mo;
40
- }
41
- /**
42
- * Register the module with asynchronous options
43
- * Useful when you need to inject dependencies like ConfigService
44
- * @param options - Async configuration options
45
- * @returns DynamicModule with KeqRequest provider
46
- */
47
- static registerAsync(options) {
48
- const mo = super.registerAsync(options);
49
- mo.providers.push(requestProvider);
50
- mo.global = true;
51
- return mo;
115
+ var _ref, _ref2, _ref3;
116
+ let KeqModule = class KeqModule {
117
+ constructor(modulesContainer, moduleRef, keqRequest) {
118
+ this.modulesContainer = modulesContainer;
119
+ this.moduleRef = moduleRef;
120
+ this.keqRequest = keqRequest;
121
+ }
122
+ onModuleInit() {
123
+ for (const [, moduleWrapper] of this.modulesContainer) {
124
+ const instance = moduleWrapper.instance;
125
+ if (hasConfigureKeqMiddleware(instance)) {
126
+ const consumer = new KeqMiddlewareConsumerImpl();
127
+ instance.configureKeqMiddleware(consumer);
128
+ consumer.applyTo(this.keqRequest, this.moduleRef);
129
+ }
130
+ }
52
131
  }
53
132
  };
54
- KeqModule = __decorate([(0, _nestjs_common.Global)()], KeqModule);
133
+ KeqModule = __decorate([
134
+ (0, _nestjs_common.Global)(),
135
+ (0, _nestjs_common.Module)({
136
+ providers: [{
137
+ provide: keq.KeqRequest,
138
+ useFactory: () => new keq.KeqRequest()
139
+ }],
140
+ exports: [keq.KeqRequest]
141
+ }),
142
+ __decorateMetadata("design:paramtypes", [
143
+ typeof (_ref = typeof _nestjs_core.ModulesContainer !== "undefined" && _nestjs_core.ModulesContainer) === "function" ? _ref : Object,
144
+ typeof (_ref2 = typeof _nestjs_core.ModuleRef !== "undefined" && _nestjs_core.ModuleRef) === "function" ? _ref2 : Object,
145
+ typeof (_ref3 = typeof keq.KeqRequest !== "undefined" && keq.KeqRequest) === "function" ? _ref3 : Object
146
+ ])
147
+ ], KeqModule);
55
148
  //#endregion
149
+ exports.KEQ_ROUTES = KEQ_ROUTES;
56
150
  Object.defineProperty(exports, "KeqModule", {
57
151
  enumerable: true,
58
152
  get: function() {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["ConfigurableModuleBuilder","KeqRequest"],"sources":["../src/keq.module-definition.ts","../src/keq.module.ts"],"sourcesContent":["\nimport { ConfigurableModuleBuilder } from '@nestjs/common'\nimport { KeqModuleOptions } from './types/keq-module-options'\n\n\nexport const {\n ConfigurableModuleClass,\n MODULE_OPTIONS_TOKEN,\n OPTIONS_TYPE,\n ASYNC_OPTIONS_TYPE,\n} = new ConfigurableModuleBuilder<KeqModuleOptions>().build()\n","\nimport { KeqRequest } from 'keq'\nimport { DynamicModule, Global, Provider } from '@nestjs/common'\nimport { ASYNC_OPTIONS_TYPE, ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, OPTIONS_TYPE } from './keq.module-definition.js'\nimport { KeqModuleOptions } from './types/index.js'\n\n\n/**\n * Provider that creates and configures a KeqRequest instance\n * Applies any middlewares defined in the module options\n */\nconst requestProvider: Provider = {\n provide: KeqRequest,\n useFactory: (config: KeqModuleOptions) => {\n const request = new KeqRequest()\n\n if (config.middlewares) {\n for (const middleware of config.middlewares) {\n request.use(middleware)\n }\n }\n\n return request\n },\n inject: [MODULE_OPTIONS_TOKEN],\n}\n\n/**\n * KeqModule - Dynamic module for integrating Keq HTTP client with NestJS\n *\n * This module is global, so KeqRequest can be injected anywhere in your application\n * without needing to import the module in every feature module.\n *\n * @example\n * ```typescript\n * // Synchronous registration\n * KeqModule.register({\n * middlewares: [...]\n * })\n *\n * // Asynchronous registration\n * KeqModule.registerAsync({\n * imports: [ConfigModule],\n * useFactory: (config: ConfigService) => ({\n * middlewares: [...]\n * }),\n * inject: [ConfigService]\n * })\n *\n * // Inject in service\n * class UserService {\n * constructor(private readonly keqRequest: KeqRequest) {}\n * async getUser(id: string) {\n * return await this.keqRequest.get(`/users/${id}`).resolveWith('json')\n * }\n * }\n * ```\n */\n@Global()\nexport class KeqModule extends ConfigurableModuleClass {\n /**\n * Register the module with synchronous options\n * @param options - Configuration options including middlewares\n * @returns DynamicModule with KeqRequest provider\n */\n static register(options: typeof OPTIONS_TYPE): DynamicModule {\n const mo = super.register(options)\n mo.providers!.push(requestProvider)\n mo.global = true\n\n return mo\n }\n\n /**\n * Register the module with asynchronous options\n * Useful when you need to inject dependencies like ConfigService\n * @param options - Async configuration options\n * @returns DynamicModule with KeqRequest provider\n */\n static registerAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule {\n const mo = super.registerAsync(options)\n mo.providers!.push(requestProvider)\n mo.global = true\n\n return mo\n }\n}\n"],"mappings":";;;;AAKA,MAAa,EACX,yBACA,sBACA,cACA,uBACE,IAAIA,eAAAA,2BAA6C,CAAC,OAAO;;;;;;;;;;;;;;;ACC7D,MAAM,kBAA4B;CAChC,SAASC,IAAAA;CACT,aAAa,WAA6B;EACxC,MAAM,UAAU,IAAIA,IAAAA,YAAY;AAEhC,MAAI,OAAO,YACT,MAAK,MAAM,cAAc,OAAO,YAC9B,SAAQ,IAAI,WAAW;AAI3B,SAAO;;CAET,QAAQ,CAAC,qBAAqB;CAC/B;AAkCM,IAAA,YAAA,MAAM,kBAAkB,wBAAwB;;;;;;CAMrD,OAAO,SAAS,SAA6C;EAC3D,MAAM,KAAK,MAAM,SAAS,QAAQ;AAClC,KAAG,UAAW,KAAK,gBAAgB;AACnC,KAAG,SAAS;AAEZ,SAAO;;;;;;;;CAST,OAAO,cAAc,SAAmD;EACtE,MAAM,KAAK,MAAM,cAAc,QAAQ;AACvC,KAAG,UAAW,KAAK,gBAAgB;AACnC,KAAG,SAAS;AAEZ,SAAO;;;oDA1BF,CAAA,EAAA,UAAA"}
1
+ {"version":3,"file":"index.js","names":["KeqRequest"],"sources":["../src/constants.ts","../src/keq-middleware-consumer.ts","../src/utils/has-configure-keq-middleware.ts","../src/keq.module.ts"],"sourcesContent":["export const KEQ_ROUTES = {\n ALL: Symbol('KEQ_ROUTES.ALL'),\n} as const\n","import type { Type } from '@nestjs/common'\nimport type { ModuleRef } from '@nestjs/core'\nimport type { KeqMiddleware, KeqRoute } from 'keq'\nimport { composeRoute, KeqRequest } from 'keq'\nimport picomatch from 'picomatch'\nimport { KEQ_ROUTES } from './constants.js'\nimport type { KeqMiddlewareConfigProxy, KeqMiddlewareConsumer, KeqModuleClass, KeqRouteInfo } from './interfaces/keq-middleware-consumer.interface.js'\nimport type { KeqNestMiddleware } from './interfaces/keq-nest-middleware.interface.js'\n\n\ninterface MiddlewareEntry {\n middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>\n routes: Array<typeof KEQ_ROUTES.ALL | KeqRouteInfo | KeqModuleClass>\n}\n\nexport class KeqMiddlewareConsumerImpl implements KeqMiddlewareConsumer {\n private entries: MiddlewareEntry[] = []\n\n apply(...middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>): KeqMiddlewareConfigProxy {\n const entry: MiddlewareEntry = { middlewares, routes: [] }\n this.entries.push(entry)\n return {\n forRoutes: (...routes) => {\n entry.routes = routes\n return this\n },\n }\n }\n\n applyTo(keqRequest: KeqRequest, moduleRef: ModuleRef): void {\n for (const entry of this.entries) {\n const resolved = entry.middlewares.map((m) => this.resolveMiddleware(m, moduleRef))\n const isGlobal = entry.routes.length === 0\n || entry.routes.some((r) => r === KEQ_ROUTES.ALL)\n\n if (isGlobal) {\n for (const mw of resolved) keqRequest.use(mw)\n } else {\n for (const routeTarget of entry.routes) {\n if (routeTarget === KEQ_ROUTES.ALL) continue\n\n if (this.isKeqModuleClass(routeTarget)) {\n const targetRequest = moduleRef.get<KeqRequest>(routeTarget.KEQ_REQUEST, { strict: false })\n if (targetRequest) {\n for (const mw of resolved) targetRequest.use(mw)\n }\n } else {\n const route = this.buildRoute(routeTarget as KeqRouteInfo)\n keqRequest.useRouter().route(route, resolved)\n }\n }\n }\n }\n }\n\n private isKeqModuleClass(route: unknown): route is KeqModuleClass {\n return (\n typeof route === 'function'\n && 'KEQ_REQUEST' in route\n && typeof (route as Record<string, unknown>).KEQ_REQUEST === 'symbol'\n )\n }\n\n private isNestMiddlewareClass(m: Type<KeqNestMiddleware> | KeqMiddleware): m is Type<KeqNestMiddleware> {\n return typeof m === 'function' && m.prototype !== undefined && 'use' in m.prototype\n }\n\n private resolveMiddleware(m: Type<KeqNestMiddleware> | KeqMiddleware, moduleRef: ModuleRef): KeqMiddleware {\n if (this.isNestMiddlewareClass(m)) {\n const instance = moduleRef.get<KeqNestMiddleware>(m, { strict: false })\n return async (ctx, next) => {\n await instance.use(ctx, next)\n }\n }\n return m\n }\n\n private buildRoute(info: KeqRouteInfo): KeqRoute {\n const routes: KeqRoute[] = []\n if (info.host) {\n const host = info.host\n routes.push((ctx) => ctx.request.url.host === host)\n }\n if (info.method) {\n const method = info.method.toLowerCase()\n routes.push((ctx) => ctx.request.method.toLowerCase() === method)\n }\n if (info.pathname) {\n const isMatch: (test: string) => boolean = picomatch(info.pathname)\n routes.push((ctx) => isMatch(ctx.request.url.pathname))\n }\n if (routes.length === 1) return routes[0]\n return composeRoute(routes)\n }\n}\n","import type { KeqMiddlewareModule } from '../interfaces/keq-middleware-module.interface.js'\n\n\nexport function hasConfigureKeqMiddleware(instance: unknown): instance is KeqMiddlewareModule {\n return (\n instance !== null\n && instance !== undefined\n && typeof (instance as Record<string, unknown>).configureKeqMiddleware === 'function'\n )\n}\n","import { Global, Module, OnModuleInit } from '@nestjs/common'\nimport { ModulesContainer, ModuleRef } from '@nestjs/core'\nimport { KeqRequest } from 'keq'\nimport { KeqMiddlewareConsumerImpl } from './keq-middleware-consumer.js'\nimport { hasConfigureKeqMiddleware } from './utils/has-configure-keq-middleware.js'\n\n\n@Global()\n@Module({\n providers: [{ provide: KeqRequest, useFactory: () => new KeqRequest() }],\n exports: [KeqRequest],\n})\nexport class KeqModule implements OnModuleInit {\n constructor(\n private readonly modulesContainer: ModulesContainer,\n private readonly moduleRef: ModuleRef,\n private readonly keqRequest: KeqRequest,\n ) {}\n\n onModuleInit(): void {\n for (const [, moduleWrapper] of this.modulesContainer) {\n const instance = moduleWrapper.instance\n if (hasConfigureKeqMiddleware(instance)) {\n const consumer = new KeqMiddlewareConsumerImpl()\n instance.configureKeqMiddleware(consumer)\n consumer.applyTo(this.keqRequest, this.moduleRef)\n }\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,MAAa,aAAa,EACxB,KAAK,OAAO,iBAAiB,EAC9B;;;ACaD,IAAa,4BAAb,MAAwE;CACtE,UAAqC,EAAE;CAEvC,MAAM,GAAG,aAAuF;EAC9F,MAAM,QAAyB;GAAE;GAAa,QAAQ,EAAE;GAAE;AAC1D,OAAK,QAAQ,KAAK,MAAM;AACxB,SAAO,EACL,YAAY,GAAG,WAAW;AACxB,SAAM,SAAS;AACf,UAAO;KAEV;;CAGH,QAAQ,YAAwB,WAA4B;AAC1D,OAAK,MAAM,SAAS,KAAK,SAAS;GAChC,MAAM,WAAW,MAAM,YAAY,KAAK,MAAM,KAAK,kBAAkB,GAAG,UAAU,CAAC;AAInF,OAHiB,MAAM,OAAO,WAAW,KACpC,MAAM,OAAO,MAAM,MAAM,MAAM,WAAW,IAAI,CAGjD,MAAK,MAAM,MAAM,SAAU,YAAW,IAAI,GAAG;OAE7C,MAAK,MAAM,eAAe,MAAM,QAAQ;AACtC,QAAI,gBAAgB,WAAW,IAAK;AAEpC,QAAI,KAAK,iBAAiB,YAAY,EAAE;KACtC,MAAM,gBAAgB,UAAU,IAAgB,YAAY,aAAa,EAAE,QAAQ,OAAO,CAAC;AAC3F,SAAI,cACF,MAAK,MAAM,MAAM,SAAU,eAAc,IAAI,GAAG;WAE7C;KACL,MAAM,QAAQ,KAAK,WAAW,YAA4B;AAC1D,gBAAW,WAAW,CAAC,MAAM,OAAO,SAAS;;;;;CAOvD,iBAAyB,OAAyC;AAChE,SACE,OAAO,UAAU,cACd,iBAAiB,SACjB,OAAQ,MAAkC,gBAAgB;;CAIjE,sBAA8B,GAA0E;AACtG,SAAO,OAAO,MAAM,cAAc,EAAE,cAAc,KAAA,KAAa,SAAS,EAAE;;CAG5E,kBAA0B,GAA4C,WAAqC;AACzG,MAAI,KAAK,sBAAsB,EAAE,EAAE;GACjC,MAAM,WAAW,UAAU,IAAuB,GAAG,EAAE,QAAQ,OAAO,CAAC;AACvE,UAAO,OAAO,KAAK,SAAS;AAC1B,UAAM,SAAS,IAAI,KAAK,KAAK;;;AAGjC,SAAO;;CAGT,WAAmB,MAA8B;EAC/C,MAAM,SAAqB,EAAE;AAC7B,MAAI,KAAK,MAAM;GACb,MAAM,OAAO,KAAK;AAClB,UAAO,MAAM,QAAQ,IAAI,QAAQ,IAAI,SAAS,KAAK;;AAErD,MAAI,KAAK,QAAQ;GACf,MAAM,SAAS,KAAK,OAAO,aAAa;AACxC,UAAO,MAAM,QAAQ,IAAI,QAAQ,OAAO,aAAa,KAAK,OAAO;;AAEnE,MAAI,KAAK,UAAU;GACjB,MAAM,WAAA,GAAA,UAAA,SAA+C,KAAK,SAAS;AACnE,UAAO,MAAM,QAAQ,QAAQ,IAAI,QAAQ,IAAI,SAAS,CAAC;;AAEzD,MAAI,OAAO,WAAW,EAAG,QAAO,OAAO;AACvC,UAAA,GAAA,IAAA,cAAoB,OAAO;;;;;ACzF/B,SAAgB,0BAA0B,UAAoD;AAC5F,QACE,aAAa,QACV,aAAa,KAAA,KACb,OAAQ,SAAqC,2BAA2B;;;;;;;;;;;;;;;;;;ACKxE,IAAA,YAAA,MAAM,UAAkC;CAC7C,YACE,kBACA,WACA,YACA;AAHiB,OAAA,mBAAA;AACA,OAAA,YAAA;AACA,OAAA,aAAA;;CAGnB,eAAqB;AACnB,OAAK,MAAM,GAAG,kBAAkB,KAAK,kBAAkB;GACrD,MAAM,WAAW,cAAc;AAC/B,OAAI,0BAA0B,SAAS,EAAE;IACvC,MAAM,WAAW,IAAI,2BAA2B;AAChD,aAAS,uBAAuB,SAAS;AACzC,aAAS,QAAQ,KAAK,YAAY,KAAK,UAAU;;;;;;6BAlBhD;4BACD;EACN,WAAW,CAAC;GAAE,SAASA,IAAAA;GAAY,kBAAkB,IAAIA,IAAAA,YAAY;GAAE,CAAC;EACxE,SAAS,CAACA,IAAAA,WAAW;EACtB,CAAC"}
package/dist/index.mjs CHANGED
@@ -1,7 +1,83 @@
1
- import { KeqRequest } from "keq";
2
- import { ConfigurableModuleBuilder, Global } from "@nestjs/common";
3
- //#region src/keq.module-definition.ts
4
- const { ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, OPTIONS_TYPE, ASYNC_OPTIONS_TYPE } = new ConfigurableModuleBuilder().build();
1
+ import { Global, Module } from "@nestjs/common";
2
+ import { ModuleRef, ModulesContainer } from "@nestjs/core";
3
+ import { KeqRequest, composeRoute } from "keq";
4
+ import picomatch from "picomatch";
5
+ //#region src/constants.ts
6
+ const KEQ_ROUTES = { ALL: Symbol("KEQ_ROUTES.ALL") };
7
+ //#endregion
8
+ //#region src/keq-middleware-consumer.ts
9
+ var KeqMiddlewareConsumerImpl = class {
10
+ entries = [];
11
+ apply(...middlewares) {
12
+ const entry = {
13
+ middlewares,
14
+ routes: []
15
+ };
16
+ this.entries.push(entry);
17
+ return { forRoutes: (...routes) => {
18
+ entry.routes = routes;
19
+ return this;
20
+ } };
21
+ }
22
+ applyTo(keqRequest, moduleRef) {
23
+ for (const entry of this.entries) {
24
+ const resolved = entry.middlewares.map((m) => this.resolveMiddleware(m, moduleRef));
25
+ if (entry.routes.length === 0 || entry.routes.some((r) => r === KEQ_ROUTES.ALL)) for (const mw of resolved) keqRequest.use(mw);
26
+ else for (const routeTarget of entry.routes) {
27
+ if (routeTarget === KEQ_ROUTES.ALL) continue;
28
+ if (this.isKeqModuleClass(routeTarget)) {
29
+ const targetRequest = moduleRef.get(routeTarget.KEQ_REQUEST, { strict: false });
30
+ if (targetRequest) for (const mw of resolved) targetRequest.use(mw);
31
+ } else {
32
+ const route = this.buildRoute(routeTarget);
33
+ keqRequest.useRouter().route(route, resolved);
34
+ }
35
+ }
36
+ }
37
+ }
38
+ isKeqModuleClass(route) {
39
+ return typeof route === "function" && "KEQ_REQUEST" in route && typeof route.KEQ_REQUEST === "symbol";
40
+ }
41
+ isNestMiddlewareClass(m) {
42
+ return typeof m === "function" && m.prototype !== void 0 && "use" in m.prototype;
43
+ }
44
+ resolveMiddleware(m, moduleRef) {
45
+ if (this.isNestMiddlewareClass(m)) {
46
+ const instance = moduleRef.get(m, { strict: false });
47
+ return async (ctx, next) => {
48
+ await instance.use(ctx, next);
49
+ };
50
+ }
51
+ return m;
52
+ }
53
+ buildRoute(info) {
54
+ const routes = [];
55
+ if (info.host) {
56
+ const host = info.host;
57
+ routes.push((ctx) => ctx.request.url.host === host);
58
+ }
59
+ if (info.method) {
60
+ const method = info.method.toLowerCase();
61
+ routes.push((ctx) => ctx.request.method.toLowerCase() === method);
62
+ }
63
+ if (info.pathname) {
64
+ const isMatch = picomatch(info.pathname);
65
+ routes.push((ctx) => isMatch(ctx.request.url.pathname));
66
+ }
67
+ if (routes.length === 1) return routes[0];
68
+ return composeRoute(routes);
69
+ }
70
+ };
71
+ //#endregion
72
+ //#region src/utils/has-configure-keq-middleware.ts
73
+ function hasConfigureKeqMiddleware(instance) {
74
+ return instance !== null && instance !== void 0 && typeof instance.configureKeqMiddleware === "function";
75
+ }
76
+ //#endregion
77
+ //#region \0@oxc-project+runtime@0.127.0/helpers/decorateMetadata.js
78
+ function __decorateMetadata(k, v) {
79
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
80
+ }
5
81
  //#endregion
6
82
  //#region \0@oxc-project+runtime@0.127.0/helpers/decorate.js
7
83
  function __decorate(decorators, target, key, desc) {
@@ -12,46 +88,40 @@ function __decorate(decorators, target, key, desc) {
12
88
  }
13
89
  //#endregion
14
90
  //#region src/keq.module.ts
15
- /**
16
- * Provider that creates and configures a KeqRequest instance
17
- * Applies any middlewares defined in the module options
18
- */
19
- const requestProvider = {
20
- provide: KeqRequest,
21
- useFactory: (config) => {
22
- const request = new KeqRequest();
23
- if (config.middlewares) for (const middleware of config.middlewares) request.use(middleware);
24
- return request;
25
- },
26
- inject: [MODULE_OPTIONS_TOKEN]
27
- };
28
- let KeqModule = class KeqModule extends ConfigurableModuleClass {
29
- /**
30
- * Register the module with synchronous options
31
- * @param options - Configuration options including middlewares
32
- * @returns DynamicModule with KeqRequest provider
33
- */
34
- static register(options) {
35
- const mo = super.register(options);
36
- mo.providers.push(requestProvider);
37
- mo.global = true;
38
- return mo;
39
- }
40
- /**
41
- * Register the module with asynchronous options
42
- * Useful when you need to inject dependencies like ConfigService
43
- * @param options - Async configuration options
44
- * @returns DynamicModule with KeqRequest provider
45
- */
46
- static registerAsync(options) {
47
- const mo = super.registerAsync(options);
48
- mo.providers.push(requestProvider);
49
- mo.global = true;
50
- return mo;
91
+ var _ref, _ref2, _ref3;
92
+ let KeqModule = class KeqModule {
93
+ constructor(modulesContainer, moduleRef, keqRequest) {
94
+ this.modulesContainer = modulesContainer;
95
+ this.moduleRef = moduleRef;
96
+ this.keqRequest = keqRequest;
97
+ }
98
+ onModuleInit() {
99
+ for (const [, moduleWrapper] of this.modulesContainer) {
100
+ const instance = moduleWrapper.instance;
101
+ if (hasConfigureKeqMiddleware(instance)) {
102
+ const consumer = new KeqMiddlewareConsumerImpl();
103
+ instance.configureKeqMiddleware(consumer);
104
+ consumer.applyTo(this.keqRequest, this.moduleRef);
105
+ }
106
+ }
51
107
  }
52
108
  };
53
- KeqModule = __decorate([Global()], KeqModule);
109
+ KeqModule = __decorate([
110
+ Global(),
111
+ Module({
112
+ providers: [{
113
+ provide: KeqRequest,
114
+ useFactory: () => new KeqRequest()
115
+ }],
116
+ exports: [KeqRequest]
117
+ }),
118
+ __decorateMetadata("design:paramtypes", [
119
+ typeof (_ref = typeof ModulesContainer !== "undefined" && ModulesContainer) === "function" ? _ref : Object,
120
+ typeof (_ref2 = typeof ModuleRef !== "undefined" && ModuleRef) === "function" ? _ref2 : Object,
121
+ typeof (_ref3 = typeof KeqRequest !== "undefined" && KeqRequest) === "function" ? _ref3 : Object
122
+ ])
123
+ ], KeqModule);
54
124
  //#endregion
55
- export { KeqModule };
125
+ export { KEQ_ROUTES, KeqModule };
56
126
 
57
127
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/keq.module-definition.ts","../src/keq.module.ts"],"sourcesContent":["\nimport { ConfigurableModuleBuilder } from '@nestjs/common'\nimport { KeqModuleOptions } from './types/keq-module-options'\n\n\nexport const {\n ConfigurableModuleClass,\n MODULE_OPTIONS_TOKEN,\n OPTIONS_TYPE,\n ASYNC_OPTIONS_TYPE,\n} = new ConfigurableModuleBuilder<KeqModuleOptions>().build()\n","\nimport { KeqRequest } from 'keq'\nimport { DynamicModule, Global, Provider } from '@nestjs/common'\nimport { ASYNC_OPTIONS_TYPE, ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, OPTIONS_TYPE } from './keq.module-definition.js'\nimport { KeqModuleOptions } from './types/index.js'\n\n\n/**\n * Provider that creates and configures a KeqRequest instance\n * Applies any middlewares defined in the module options\n */\nconst requestProvider: Provider = {\n provide: KeqRequest,\n useFactory: (config: KeqModuleOptions) => {\n const request = new KeqRequest()\n\n if (config.middlewares) {\n for (const middleware of config.middlewares) {\n request.use(middleware)\n }\n }\n\n return request\n },\n inject: [MODULE_OPTIONS_TOKEN],\n}\n\n/**\n * KeqModule - Dynamic module for integrating Keq HTTP client with NestJS\n *\n * This module is global, so KeqRequest can be injected anywhere in your application\n * without needing to import the module in every feature module.\n *\n * @example\n * ```typescript\n * // Synchronous registration\n * KeqModule.register({\n * middlewares: [...]\n * })\n *\n * // Asynchronous registration\n * KeqModule.registerAsync({\n * imports: [ConfigModule],\n * useFactory: (config: ConfigService) => ({\n * middlewares: [...]\n * }),\n * inject: [ConfigService]\n * })\n *\n * // Inject in service\n * class UserService {\n * constructor(private readonly keqRequest: KeqRequest) {}\n * async getUser(id: string) {\n * return await this.keqRequest.get(`/users/${id}`).resolveWith('json')\n * }\n * }\n * ```\n */\n@Global()\nexport class KeqModule extends ConfigurableModuleClass {\n /**\n * Register the module with synchronous options\n * @param options - Configuration options including middlewares\n * @returns DynamicModule with KeqRequest provider\n */\n static register(options: typeof OPTIONS_TYPE): DynamicModule {\n const mo = super.register(options)\n mo.providers!.push(requestProvider)\n mo.global = true\n\n return mo\n }\n\n /**\n * Register the module with asynchronous options\n * Useful when you need to inject dependencies like ConfigService\n * @param options - Async configuration options\n * @returns DynamicModule with KeqRequest provider\n */\n static registerAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule {\n const mo = super.registerAsync(options)\n mo.providers!.push(requestProvider)\n mo.global = true\n\n return mo\n }\n}\n"],"mappings":";;;AAKA,MAAa,EACX,yBACA,sBACA,cACA,uBACE,IAAI,2BAA6C,CAAC,OAAO;;;;;;;;;;;;;;;ACC7D,MAAM,kBAA4B;CAChC,SAAS;CACT,aAAa,WAA6B;EACxC,MAAM,UAAU,IAAI,YAAY;AAEhC,MAAI,OAAO,YACT,MAAK,MAAM,cAAc,OAAO,YAC9B,SAAQ,IAAI,WAAW;AAI3B,SAAO;;CAET,QAAQ,CAAC,qBAAqB;CAC/B;AAkCM,IAAA,YAAA,MAAM,kBAAkB,wBAAwB;;;;;;CAMrD,OAAO,SAAS,SAA6C;EAC3D,MAAM,KAAK,MAAM,SAAS,QAAQ;AAClC,KAAG,UAAW,KAAK,gBAAgB;AACnC,KAAG,SAAS;AAEZ,SAAO;;;;;;;;CAST,OAAO,cAAc,SAAmD;EACtE,MAAM,KAAK,MAAM,cAAc,QAAQ;AACvC,KAAG,UAAW,KAAK,gBAAgB;AACnC,KAAG,SAAS;AAEZ,SAAO;;;wBA1BV,QAAQ,CAAA,EAAA,UAAA"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/constants.ts","../src/keq-middleware-consumer.ts","../src/utils/has-configure-keq-middleware.ts","../src/keq.module.ts"],"sourcesContent":["export const KEQ_ROUTES = {\n ALL: Symbol('KEQ_ROUTES.ALL'),\n} as const\n","import type { Type } from '@nestjs/common'\nimport type { ModuleRef } from '@nestjs/core'\nimport type { KeqMiddleware, KeqRoute } from 'keq'\nimport { composeRoute, KeqRequest } from 'keq'\nimport picomatch from 'picomatch'\nimport { KEQ_ROUTES } from './constants.js'\nimport type { KeqMiddlewareConfigProxy, KeqMiddlewareConsumer, KeqModuleClass, KeqRouteInfo } from './interfaces/keq-middleware-consumer.interface.js'\nimport type { KeqNestMiddleware } from './interfaces/keq-nest-middleware.interface.js'\n\n\ninterface MiddlewareEntry {\n middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>\n routes: Array<typeof KEQ_ROUTES.ALL | KeqRouteInfo | KeqModuleClass>\n}\n\nexport class KeqMiddlewareConsumerImpl implements KeqMiddlewareConsumer {\n private entries: MiddlewareEntry[] = []\n\n apply(...middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>): KeqMiddlewareConfigProxy {\n const entry: MiddlewareEntry = { middlewares, routes: [] }\n this.entries.push(entry)\n return {\n forRoutes: (...routes) => {\n entry.routes = routes\n return this\n },\n }\n }\n\n applyTo(keqRequest: KeqRequest, moduleRef: ModuleRef): void {\n for (const entry of this.entries) {\n const resolved = entry.middlewares.map((m) => this.resolveMiddleware(m, moduleRef))\n const isGlobal = entry.routes.length === 0\n || entry.routes.some((r) => r === KEQ_ROUTES.ALL)\n\n if (isGlobal) {\n for (const mw of resolved) keqRequest.use(mw)\n } else {\n for (const routeTarget of entry.routes) {\n if (routeTarget === KEQ_ROUTES.ALL) continue\n\n if (this.isKeqModuleClass(routeTarget)) {\n const targetRequest = moduleRef.get<KeqRequest>(routeTarget.KEQ_REQUEST, { strict: false })\n if (targetRequest) {\n for (const mw of resolved) targetRequest.use(mw)\n }\n } else {\n const route = this.buildRoute(routeTarget as KeqRouteInfo)\n keqRequest.useRouter().route(route, resolved)\n }\n }\n }\n }\n }\n\n private isKeqModuleClass(route: unknown): route is KeqModuleClass {\n return (\n typeof route === 'function'\n && 'KEQ_REQUEST' in route\n && typeof (route as Record<string, unknown>).KEQ_REQUEST === 'symbol'\n )\n }\n\n private isNestMiddlewareClass(m: Type<KeqNestMiddleware> | KeqMiddleware): m is Type<KeqNestMiddleware> {\n return typeof m === 'function' && m.prototype !== undefined && 'use' in m.prototype\n }\n\n private resolveMiddleware(m: Type<KeqNestMiddleware> | KeqMiddleware, moduleRef: ModuleRef): KeqMiddleware {\n if (this.isNestMiddlewareClass(m)) {\n const instance = moduleRef.get<KeqNestMiddleware>(m, { strict: false })\n return async (ctx, next) => {\n await instance.use(ctx, next)\n }\n }\n return m\n }\n\n private buildRoute(info: KeqRouteInfo): KeqRoute {\n const routes: KeqRoute[] = []\n if (info.host) {\n const host = info.host\n routes.push((ctx) => ctx.request.url.host === host)\n }\n if (info.method) {\n const method = info.method.toLowerCase()\n routes.push((ctx) => ctx.request.method.toLowerCase() === method)\n }\n if (info.pathname) {\n const isMatch: (test: string) => boolean = picomatch(info.pathname)\n routes.push((ctx) => isMatch(ctx.request.url.pathname))\n }\n if (routes.length === 1) return routes[0]\n return composeRoute(routes)\n }\n}\n","import type { KeqMiddlewareModule } from '../interfaces/keq-middleware-module.interface.js'\n\n\nexport function hasConfigureKeqMiddleware(instance: unknown): instance is KeqMiddlewareModule {\n return (\n instance !== null\n && instance !== undefined\n && typeof (instance as Record<string, unknown>).configureKeqMiddleware === 'function'\n )\n}\n","import { Global, Module, OnModuleInit } from '@nestjs/common'\nimport { ModulesContainer, ModuleRef } from '@nestjs/core'\nimport { KeqRequest } from 'keq'\nimport { KeqMiddlewareConsumerImpl } from './keq-middleware-consumer.js'\nimport { hasConfigureKeqMiddleware } from './utils/has-configure-keq-middleware.js'\n\n\n@Global()\n@Module({\n providers: [{ provide: KeqRequest, useFactory: () => new KeqRequest() }],\n exports: [KeqRequest],\n})\nexport class KeqModule implements OnModuleInit {\n constructor(\n private readonly modulesContainer: ModulesContainer,\n private readonly moduleRef: ModuleRef,\n private readonly keqRequest: KeqRequest,\n ) {}\n\n onModuleInit(): void {\n for (const [, moduleWrapper] of this.modulesContainer) {\n const instance = moduleWrapper.instance\n if (hasConfigureKeqMiddleware(instance)) {\n const consumer = new KeqMiddlewareConsumerImpl()\n instance.configureKeqMiddleware(consumer)\n consumer.applyTo(this.keqRequest, this.moduleRef)\n }\n }\n }\n}\n"],"mappings":";;;;;AAAA,MAAa,aAAa,EACxB,KAAK,OAAO,iBAAiB,EAC9B;;;ACaD,IAAa,4BAAb,MAAwE;CACtE,UAAqC,EAAE;CAEvC,MAAM,GAAG,aAAuF;EAC9F,MAAM,QAAyB;GAAE;GAAa,QAAQ,EAAE;GAAE;AAC1D,OAAK,QAAQ,KAAK,MAAM;AACxB,SAAO,EACL,YAAY,GAAG,WAAW;AACxB,SAAM,SAAS;AACf,UAAO;KAEV;;CAGH,QAAQ,YAAwB,WAA4B;AAC1D,OAAK,MAAM,SAAS,KAAK,SAAS;GAChC,MAAM,WAAW,MAAM,YAAY,KAAK,MAAM,KAAK,kBAAkB,GAAG,UAAU,CAAC;AAInF,OAHiB,MAAM,OAAO,WAAW,KACpC,MAAM,OAAO,MAAM,MAAM,MAAM,WAAW,IAAI,CAGjD,MAAK,MAAM,MAAM,SAAU,YAAW,IAAI,GAAG;OAE7C,MAAK,MAAM,eAAe,MAAM,QAAQ;AACtC,QAAI,gBAAgB,WAAW,IAAK;AAEpC,QAAI,KAAK,iBAAiB,YAAY,EAAE;KACtC,MAAM,gBAAgB,UAAU,IAAgB,YAAY,aAAa,EAAE,QAAQ,OAAO,CAAC;AAC3F,SAAI,cACF,MAAK,MAAM,MAAM,SAAU,eAAc,IAAI,GAAG;WAE7C;KACL,MAAM,QAAQ,KAAK,WAAW,YAA4B;AAC1D,gBAAW,WAAW,CAAC,MAAM,OAAO,SAAS;;;;;CAOvD,iBAAyB,OAAyC;AAChE,SACE,OAAO,UAAU,cACd,iBAAiB,SACjB,OAAQ,MAAkC,gBAAgB;;CAIjE,sBAA8B,GAA0E;AACtG,SAAO,OAAO,MAAM,cAAc,EAAE,cAAc,KAAA,KAAa,SAAS,EAAE;;CAG5E,kBAA0B,GAA4C,WAAqC;AACzG,MAAI,KAAK,sBAAsB,EAAE,EAAE;GACjC,MAAM,WAAW,UAAU,IAAuB,GAAG,EAAE,QAAQ,OAAO,CAAC;AACvE,UAAO,OAAO,KAAK,SAAS;AAC1B,UAAM,SAAS,IAAI,KAAK,KAAK;;;AAGjC,SAAO;;CAGT,WAAmB,MAA8B;EAC/C,MAAM,SAAqB,EAAE;AAC7B,MAAI,KAAK,MAAM;GACb,MAAM,OAAO,KAAK;AAClB,UAAO,MAAM,QAAQ,IAAI,QAAQ,IAAI,SAAS,KAAK;;AAErD,MAAI,KAAK,QAAQ;GACf,MAAM,SAAS,KAAK,OAAO,aAAa;AACxC,UAAO,MAAM,QAAQ,IAAI,QAAQ,OAAO,aAAa,KAAK,OAAO;;AAEnE,MAAI,KAAK,UAAU;GACjB,MAAM,UAAqC,UAAU,KAAK,SAAS;AACnE,UAAO,MAAM,QAAQ,QAAQ,IAAI,QAAQ,IAAI,SAAS,CAAC;;AAEzD,MAAI,OAAO,WAAW,EAAG,QAAO,OAAO;AACvC,SAAO,aAAa,OAAO;;;;;ACzF/B,SAAgB,0BAA0B,UAAoD;AAC5F,QACE,aAAa,QACV,aAAa,KAAA,KACb,OAAQ,SAAqC,2BAA2B;;;;;;;;;;;;;;;;;;ACKxE,IAAA,YAAA,MAAM,UAAkC;CAC7C,YACE,kBACA,WACA,YACA;AAHiB,OAAA,mBAAA;AACA,OAAA,YAAA;AACA,OAAA,aAAA;;CAGnB,eAAqB;AACnB,OAAK,MAAM,GAAG,kBAAkB,KAAK,kBAAkB;GACrD,MAAM,WAAW,cAAc;AAC/B,OAAI,0BAA0B,SAAS,EAAE;IACvC,MAAM,WAAW,IAAI,2BAA2B;AAChD,aAAS,uBAAuB,SAAS;AACzC,aAAS,QAAQ,KAAK,YAAY,KAAK,UAAU;;;;;;CAlBxD,QAAQ;CACR,OAAO;EACN,WAAW,CAAC;GAAE,SAAS;GAAY,kBAAkB,IAAI,YAAY;GAAE,CAAC;EACxE,SAAS,CAAC,WAAW;EACtB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@keq-request/nestjs",
3
- "version": "5.0.0-beta.1",
3
+ "version": "5.0.0-beta.10",
4
4
  "description": "Request API write by Typescript for flexibility, readability, and a low learning curve.",
5
5
  "keywords": [
6
6
  "request",
@@ -33,14 +33,20 @@
33
33
  "type": "git",
34
34
  "url": "git+https://github.com/keq-request/keq.git"
35
35
  },
36
+ "dependencies": {
37
+ "picomatch": "^4.0.0"
38
+ },
36
39
  "devDependencies": {
37
40
  "@nestjs/common": "^11.1.19",
41
+ "@nestjs/core": "^11.1.19",
38
42
  "@types/node": "^20.19.39",
39
- "keq": "5.0.0-beta.1"
43
+ "@types/picomatch": "^4.0.0",
44
+ "keq": "5.0.0-beta.10"
40
45
  },
41
46
  "peerDependencies": {
42
47
  "@nestjs/common": "^11.0.0",
43
- "keq": "^5.0.0-beta.1"
48
+ "@nestjs/core": "^11.0.0",
49
+ "keq": "^5.0.0-beta.10"
44
50
  },
45
51
  "engines": {
46
52
  "node": ">=20.0.0"
@@ -0,0 +1,3 @@
1
+ export const KEQ_ROUTES = {
2
+ ALL: Symbol('KEQ_ROUTES.ALL'),
3
+ } as const
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './keq.module.js'
2
- export * from './types/index.js'
2
+ export * from './constants.js'
3
+ export * from './interfaces/index.js'
@@ -0,0 +1,4 @@
1
+ export type { KeqNestMiddleware } from './keq-nest-middleware.interface.js'
2
+ export type { KeqMiddlewareModule } from './keq-middleware-module.interface.js'
3
+ export type { KeqMiddlewareConsumer, KeqMiddlewareConfigProxy, KeqRouteInfo, KeqModuleClass } from './keq-middleware-consumer.interface.js'
4
+ export type { KeqModuleOptions } from './keq-module-options.interface.js'
@@ -0,0 +1,23 @@
1
+ import type { Type } from '@nestjs/common'
2
+ import type { KeqMiddleware } from 'keq'
3
+ import type { KEQ_ROUTES } from '../constants.js'
4
+ import type { KeqNestMiddleware } from './keq-nest-middleware.interface.js'
5
+
6
+
7
+ export interface KeqRouteInfo {
8
+ host?: string
9
+ method?: string
10
+ pathname?: string
11
+ }
12
+
13
+ export interface KeqModuleClass {
14
+ readonly KEQ_REQUEST: symbol
15
+ }
16
+
17
+ export interface KeqMiddlewareConfigProxy {
18
+ forRoutes(...routes: Array<typeof KEQ_ROUTES.ALL | KeqRouteInfo | KeqModuleClass>): KeqMiddlewareConsumer
19
+ }
20
+
21
+ export interface KeqMiddlewareConsumer {
22
+ apply(...middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>): KeqMiddlewareConfigProxy
23
+ }
@@ -0,0 +1,6 @@
1
+ import type { KeqMiddlewareConsumer } from './keq-middleware-consumer.interface.js'
2
+
3
+
4
+ export interface KeqMiddlewareModule {
5
+ configureKeqMiddleware(consumer: KeqMiddlewareConsumer): void
6
+ }
@@ -1,6 +1,7 @@
1
- import { KeqMiddleware } from 'keq'
1
+ import type { KeqMiddleware } from 'keq'
2
2
 
3
3
 
4
4
  export interface KeqModuleOptions {
5
5
  middlewares?: KeqMiddleware[]
6
+ isolate?: boolean
6
7
  }
@@ -0,0 +1,6 @@
1
+ import type { KeqExecutionContext, KeqNext } from 'keq'
2
+
3
+
4
+ export interface KeqNestMiddleware {
5
+ use(ctx: KeqExecutionContext, next: KeqNext): void | PromiseLike<void>
6
+ }
@@ -0,0 +1,95 @@
1
+ import type { Type } from '@nestjs/common'
2
+ import type { ModuleRef } from '@nestjs/core'
3
+ import type { KeqMiddleware, KeqRoute } from 'keq'
4
+ import { composeRoute, KeqRequest } from 'keq'
5
+ import picomatch from 'picomatch'
6
+ import { KEQ_ROUTES } from './constants.js'
7
+ import type { KeqMiddlewareConfigProxy, KeqMiddlewareConsumer, KeqModuleClass, KeqRouteInfo } from './interfaces/keq-middleware-consumer.interface.js'
8
+ import type { KeqNestMiddleware } from './interfaces/keq-nest-middleware.interface.js'
9
+
10
+
11
+ interface MiddlewareEntry {
12
+ middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>
13
+ routes: Array<typeof KEQ_ROUTES.ALL | KeqRouteInfo | KeqModuleClass>
14
+ }
15
+
16
+ export class KeqMiddlewareConsumerImpl implements KeqMiddlewareConsumer {
17
+ private entries: MiddlewareEntry[] = []
18
+
19
+ apply(...middlewares: Array<Type<KeqNestMiddleware> | KeqMiddleware>): KeqMiddlewareConfigProxy {
20
+ const entry: MiddlewareEntry = { middlewares, routes: [] }
21
+ this.entries.push(entry)
22
+ return {
23
+ forRoutes: (...routes) => {
24
+ entry.routes = routes
25
+ return this
26
+ },
27
+ }
28
+ }
29
+
30
+ applyTo(keqRequest: KeqRequest, moduleRef: ModuleRef): void {
31
+ for (const entry of this.entries) {
32
+ const resolved = entry.middlewares.map((m) => this.resolveMiddleware(m, moduleRef))
33
+ const isGlobal = entry.routes.length === 0
34
+ || entry.routes.some((r) => r === KEQ_ROUTES.ALL)
35
+
36
+ if (isGlobal) {
37
+ for (const mw of resolved) keqRequest.use(mw)
38
+ } else {
39
+ for (const routeTarget of entry.routes) {
40
+ if (routeTarget === KEQ_ROUTES.ALL) continue
41
+
42
+ if (this.isKeqModuleClass(routeTarget)) {
43
+ const targetRequest = moduleRef.get<KeqRequest>(routeTarget.KEQ_REQUEST, { strict: false })
44
+ if (targetRequest) {
45
+ for (const mw of resolved) targetRequest.use(mw)
46
+ }
47
+ } else {
48
+ const route = this.buildRoute(routeTarget as KeqRouteInfo)
49
+ keqRequest.useRouter().route(route, resolved)
50
+ }
51
+ }
52
+ }
53
+ }
54
+ }
55
+
56
+ private isKeqModuleClass(route: unknown): route is KeqModuleClass {
57
+ return (
58
+ typeof route === 'function'
59
+ && 'KEQ_REQUEST' in route
60
+ && typeof (route as Record<string, unknown>).KEQ_REQUEST === 'symbol'
61
+ )
62
+ }
63
+
64
+ private isNestMiddlewareClass(m: Type<KeqNestMiddleware> | KeqMiddleware): m is Type<KeqNestMiddleware> {
65
+ return typeof m === 'function' && m.prototype !== undefined && 'use' in m.prototype
66
+ }
67
+
68
+ private resolveMiddleware(m: Type<KeqNestMiddleware> | KeqMiddleware, moduleRef: ModuleRef): KeqMiddleware {
69
+ if (this.isNestMiddlewareClass(m)) {
70
+ const instance = moduleRef.get<KeqNestMiddleware>(m, { strict: false })
71
+ return async (ctx, next) => {
72
+ await instance.use(ctx, next)
73
+ }
74
+ }
75
+ return m
76
+ }
77
+
78
+ private buildRoute(info: KeqRouteInfo): KeqRoute {
79
+ const routes: KeqRoute[] = []
80
+ if (info.host) {
81
+ const host = info.host
82
+ routes.push((ctx) => ctx.request.url.host === host)
83
+ }
84
+ if (info.method) {
85
+ const method = info.method.toLowerCase()
86
+ routes.push((ctx) => ctx.request.method.toLowerCase() === method)
87
+ }
88
+ if (info.pathname) {
89
+ const isMatch: (test: string) => boolean = picomatch(info.pathname)
90
+ routes.push((ctx) => isMatch(ctx.request.url.pathname))
91
+ }
92
+ if (routes.length === 1) return routes[0]
93
+ return composeRoute(routes)
94
+ }
95
+ }
package/src/keq.module.ts CHANGED
@@ -1,87 +1,30 @@
1
-
1
+ import { Global, Module, OnModuleInit } from '@nestjs/common'
2
+ import { ModulesContainer, ModuleRef } from '@nestjs/core'
2
3
  import { KeqRequest } from 'keq'
3
- import { DynamicModule, Global, Provider } from '@nestjs/common'
4
- import { ASYNC_OPTIONS_TYPE, ConfigurableModuleClass, MODULE_OPTIONS_TOKEN, OPTIONS_TYPE } from './keq.module-definition.js'
5
- import { KeqModuleOptions } from './types/index.js'
6
-
4
+ import { KeqMiddlewareConsumerImpl } from './keq-middleware-consumer.js'
5
+ import { hasConfigureKeqMiddleware } from './utils/has-configure-keq-middleware.js'
7
6
 
8
- /**
9
- * Provider that creates and configures a KeqRequest instance
10
- * Applies any middlewares defined in the module options
11
- */
12
- const requestProvider: Provider = {
13
- provide: KeqRequest,
14
- useFactory: (config: KeqModuleOptions) => {
15
- const request = new KeqRequest()
16
7
 
17
- if (config.middlewares) {
18
- for (const middleware of config.middlewares) {
19
- request.use(middleware)
8
+ @Global()
9
+ @Module({
10
+ providers: [{ provide: KeqRequest, useFactory: () => new KeqRequest() }],
11
+ exports: [KeqRequest],
12
+ })
13
+ export class KeqModule implements OnModuleInit {
14
+ constructor(
15
+ private readonly modulesContainer: ModulesContainer,
16
+ private readonly moduleRef: ModuleRef,
17
+ private readonly keqRequest: KeqRequest,
18
+ ) {}
19
+
20
+ onModuleInit(): void {
21
+ for (const [, moduleWrapper] of this.modulesContainer) {
22
+ const instance = moduleWrapper.instance
23
+ if (hasConfigureKeqMiddleware(instance)) {
24
+ const consumer = new KeqMiddlewareConsumerImpl()
25
+ instance.configureKeqMiddleware(consumer)
26
+ consumer.applyTo(this.keqRequest, this.moduleRef)
20
27
  }
21
28
  }
22
-
23
- return request
24
- },
25
- inject: [MODULE_OPTIONS_TOKEN],
26
- }
27
-
28
- /**
29
- * KeqModule - Dynamic module for integrating Keq HTTP client with NestJS
30
- *
31
- * This module is global, so KeqRequest can be injected anywhere in your application
32
- * without needing to import the module in every feature module.
33
- *
34
- * @example
35
- * ```typescript
36
- * // Synchronous registration
37
- * KeqModule.register({
38
- * middlewares: [...]
39
- * })
40
- *
41
- * // Asynchronous registration
42
- * KeqModule.registerAsync({
43
- * imports: [ConfigModule],
44
- * useFactory: (config: ConfigService) => ({
45
- * middlewares: [...]
46
- * }),
47
- * inject: [ConfigService]
48
- * })
49
- *
50
- * // Inject in service
51
- * class UserService {
52
- * constructor(private readonly keqRequest: KeqRequest) {}
53
- * async getUser(id: string) {
54
- * return await this.keqRequest.get(`/users/${id}`).resolveWith('json')
55
- * }
56
- * }
57
- * ```
58
- */
59
- @Global()
60
- export class KeqModule extends ConfigurableModuleClass {
61
- /**
62
- * Register the module with synchronous options
63
- * @param options - Configuration options including middlewares
64
- * @returns DynamicModule with KeqRequest provider
65
- */
66
- static register(options: typeof OPTIONS_TYPE): DynamicModule {
67
- const mo = super.register(options)
68
- mo.providers!.push(requestProvider)
69
- mo.global = true
70
-
71
- return mo
72
- }
73
-
74
- /**
75
- * Register the module with asynchronous options
76
- * Useful when you need to inject dependencies like ConfigService
77
- * @param options - Async configuration options
78
- * @returns DynamicModule with KeqRequest provider
79
- */
80
- static registerAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule {
81
- const mo = super.registerAsync(options)
82
- mo.providers!.push(requestProvider)
83
- mo.global = true
84
-
85
- return mo
86
29
  }
87
30
  }
@@ -0,0 +1,10 @@
1
+ import type { KeqMiddlewareModule } from '../interfaces/keq-middleware-module.interface.js'
2
+
3
+
4
+ export function hasConfigureKeqMiddleware(instance: unknown): instance is KeqMiddlewareModule {
5
+ return (
6
+ instance !== null
7
+ && instance !== undefined
8
+ && typeof (instance as Record<string, unknown>).configureKeqMiddleware === 'function'
9
+ )
10
+ }
@@ -1,11 +0,0 @@
1
-
2
- import { ConfigurableModuleBuilder } from '@nestjs/common'
3
- import { KeqModuleOptions } from './types/keq-module-options'
4
-
5
-
6
- export const {
7
- ConfigurableModuleClass,
8
- MODULE_OPTIONS_TOKEN,
9
- OPTIONS_TYPE,
10
- ASYNC_OPTIONS_TYPE,
11
- } = new ConfigurableModuleBuilder<KeqModuleOptions>().build()
@@ -1 +0,0 @@
1
- export * from './keq-module-options.js'