@keq-request/nestjs 5.0.0-alpha.36 → 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.
- package/.turbo/turbo-build.log +19 -19
- package/CHANGELOG.md +108 -0
- package/dist/index.d.mts +45 -55
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +45 -55
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +134 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +112 -42
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -3
- package/src/constants.ts +3 -0
- package/src/index.ts +2 -1
- package/src/interfaces/index.ts +4 -0
- package/src/interfaces/keq-middleware-consumer.interface.ts +23 -0
- package/src/interfaces/keq-middleware-module.interface.ts +6 -0
- package/src/{types/keq-module-options.ts → interfaces/keq-module-options.interface.ts} +2 -1
- package/src/interfaces/keq-nest-middleware.interface.ts +6 -0
- package/src/keq-middleware-consumer.ts +95 -0
- package/src/keq.module.ts +23 -80
- package/src/utils/has-configure-keq-middleware.ts +10 -0
- package/src/keq.module-definition.ts +0 -11
- package/src/types/index.ts +0 -1
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
|
|
2
|
-
> @keq-request/nestjs@5.0.0-
|
|
2
|
+
> @keq-request/nestjs@5.0.0-beta.10 build /home/runner/work/keq/keq/packages/nestjs
|
|
3
3
|
> tsdown
|
|
4
4
|
|
|
5
5
|
[34mℹ[39m [34mtsdown v0.21.10[39m powered by [38;2;255;126;23mrolldown v1.0.0-rc.17[39m
|
|
6
6
|
[34mℹ[39m config file: [4m/home/runner/work/keq/keq/packages/nestjs/tsdown.config.ts[24m
|
|
7
|
-
(node:
|
|
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
|
[34mℹ[39m target: [34mnode20[39m
|
|
13
13
|
[34mℹ[39m tsconfig: [34mtsconfig.lib.json[39m
|
|
14
14
|
[34mℹ[39m Build start
|
|
15
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.js[22m [
|
|
16
|
-
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.js.map [
|
|
17
|
-
[34mℹ[39m [33m[CJS][39m 2 files, total:
|
|
15
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[1mindex.js[22m [2m5.83 kB[22m [2m│ gzip: 2.01 kB[22m
|
|
16
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.js.map [2m7.28 kB[22m [2m│ gzip: 2.38 kB[22m
|
|
17
|
+
[34mℹ[39m [33m[CJS][39m 2 files, total: 13.11 kB
|
|
18
18
|
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugin `tsdown:deps`. See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
19
19
|
|
|
20
|
-
[34mℹ[39m [
|
|
21
|
-
[34mℹ[39m [
|
|
22
|
-
[34mℹ[39m [
|
|
20
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m4.54 kB[22m [2m│ gzip: 1.57 kB[22m
|
|
21
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [2m7.21 kB[22m [2m│ gzip: 2.36 kB[22m
|
|
22
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.d.mts.map [2m0.99 kB[22m [2m│ gzip: 0.43 kB[22m
|
|
23
|
+
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m1.86 kB[22m [2m│ gzip: 0.66 kB[22m
|
|
24
|
+
[34mℹ[39m [34m[ESM][39m 4 files, total: 14.60 kB
|
|
25
|
+
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
26
|
+
|
|
27
|
+
[32m✔[39m Build complete in [32m5031ms[39m
|
|
28
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22mindex.d.ts.map [2m0.99 kB[22m [2m│ gzip: 0.43 kB[22m
|
|
29
|
+
[34mℹ[39m [33m[CJS][39m [2mdist/[22m[32m[1mindex.d.ts[22m[39m [2m1.86 kB[22m [2m│ gzip: 0.65 kB[22m
|
|
30
|
+
[34mℹ[39m [33m[CJS][39m 2 files, total: 2.85 kB
|
|
31
|
+
[32m✔[39m Build complete in [32m5034ms[39m
|
|
23
32
|
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugins. Here is a breakdown:
|
|
24
|
-
- rolldown-plugin-dts:generate (
|
|
25
|
-
- tsdown:deps (
|
|
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
|
-
[32m✔[39m Build complete in [32m4402ms[39m
|
|
29
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[1mindex.mjs[22m [2m2.17 kB[22m [2m│ gzip: 0.92 kB[22m
|
|
30
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.mjs.map [2m3.49 kB[22m [2m│ gzip: 1.34 kB[22m
|
|
31
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22mindex.d.mts.map [2m0.54 kB[22m [2m│ gzip: 0.30 kB[22m
|
|
32
|
-
[34mℹ[39m [34m[ESM][39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m2.17 kB[22m [2m│ gzip: 0.89 kB[22m
|
|
33
|
-
[34mℹ[39m [34m[ESM][39m 4 files, total: 8.37 kB
|
|
34
|
-
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
35
|
-
|
|
36
|
-
[32m✔[39m Build complete in [32m4404ms[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,113 @@
|
|
|
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
|
+
|
|
62
|
+
## 5.0.0-beta.1
|
|
63
|
+
|
|
64
|
+
### Minor Changes
|
|
65
|
+
|
|
66
|
+
- 437fc0c: **Feat:** nestjs supported。
|
|
67
|
+
|
|
68
|
+
### Patch Changes
|
|
69
|
+
|
|
70
|
+
- Updated dependencies [cbc5d17]
|
|
71
|
+
- Updated dependencies [153244f]
|
|
72
|
+
- Updated dependencies [153244f]
|
|
73
|
+
- Updated dependencies [153244f]
|
|
74
|
+
- Updated dependencies [0a2eb2f]
|
|
75
|
+
- Updated dependencies [153244f]
|
|
76
|
+
- Updated dependencies [214ae66]
|
|
77
|
+
- Updated dependencies [c7ffd1f]
|
|
78
|
+
- Updated dependencies [90311b3]
|
|
79
|
+
- Updated dependencies [1f367c0]
|
|
80
|
+
- Updated dependencies [0c7db81]
|
|
81
|
+
- Updated dependencies [df114d1]
|
|
82
|
+
- Updated dependencies [2686b8d]
|
|
83
|
+
- Updated dependencies [842e555]
|
|
84
|
+
- Updated dependencies [153244f]
|
|
85
|
+
- Updated dependencies [2686b8d]
|
|
86
|
+
- Updated dependencies [9290139]
|
|
87
|
+
- Updated dependencies [7873a0a]
|
|
88
|
+
- Updated dependencies [e7eb9dc]
|
|
89
|
+
- Updated dependencies [7ff2162]
|
|
90
|
+
- Updated dependencies [22ce01a]
|
|
91
|
+
- Updated dependencies [0873c7e]
|
|
92
|
+
- Updated dependencies [153244f]
|
|
93
|
+
- Updated dependencies [a7a83da]
|
|
94
|
+
- Updated dependencies [f194c41]
|
|
95
|
+
- Updated dependencies [153244f]
|
|
96
|
+
- Updated dependencies [153244f]
|
|
97
|
+
- Updated dependencies [d076b76]
|
|
98
|
+
- Updated dependencies [f8abc63]
|
|
99
|
+
- Updated dependencies [153244f]
|
|
100
|
+
- Updated dependencies [ca6c879]
|
|
101
|
+
- Updated dependencies [63161c4]
|
|
102
|
+
- Updated dependencies [d472648]
|
|
103
|
+
- Updated dependencies [b8d02ca]
|
|
104
|
+
- Updated dependencies [0a04864]
|
|
105
|
+
- Updated dependencies [581815a]
|
|
106
|
+
- Updated dependencies [7343445]
|
|
107
|
+
- Updated dependencies [153244f]
|
|
108
|
+
- Updated dependencies [eed26f9]
|
|
109
|
+
- keq@5.0.0-beta.1
|
|
110
|
+
|
|
3
111
|
## 5.0.0-alpha.36
|
|
4
112
|
|
|
5
113
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -1,62 +1,52 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
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/
|
|
6
|
-
|
|
7
|
-
|
|
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/
|
|
11
|
-
declare const
|
|
14
|
+
//#region src/constants.d.ts
|
|
15
|
+
declare const KEQ_ROUTES: {
|
|
16
|
+
readonly ALL: symbol;
|
|
17
|
+
};
|
|
12
18
|
//#endregion
|
|
13
|
-
//#region src/keq.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/
|
|
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
|
|
2
|
-
import {
|
|
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/
|
|
6
|
-
|
|
7
|
-
|
|
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/
|
|
11
|
-
declare const
|
|
14
|
+
//#region src/constants.d.ts
|
|
15
|
+
declare const KEQ_ROUTES: {
|
|
16
|
+
readonly ALL: symbol;
|
|
17
|
+
};
|
|
12
18
|
//#endregion
|
|
13
|
-
//#region src/keq.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/
|
|
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
|
-
|
|
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
|
-
|
|
5
|
-
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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([
|
|
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":["
|
|
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 {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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([
|
|
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
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/keq.
|
|
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-
|
|
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
|
-
"
|
|
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
|
-
"
|
|
48
|
+
"@nestjs/core": "^11.0.0",
|
|
49
|
+
"keq": "^5.0.0-beta.10"
|
|
44
50
|
},
|
|
45
51
|
"engines": {
|
|
46
52
|
"node": ">=20.0.0"
|
package/src/constants.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -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,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 {
|
|
4
|
-
import {
|
|
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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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()
|
package/src/types/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './keq-module-options.js'
|