@keq-request/nestjs 5.0.0-beta.12 → 5.0.0-beta.14
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 +17 -17
- package/CHANGELOG.md +31 -0
- package/__tests__/keq-module.spec.ts +203 -0
- package/dist/index.d.mts +75 -17
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +75 -17
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +112 -61
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +106 -63
- package/dist/index.mjs.map +1 -1
- package/jest.config.cts +12 -2
- package/package.json +5 -3
- package/src/decorators/inject-keq-consumer.decorator.ts +26 -0
- package/src/index.ts +3 -0
- package/src/interfaces/index.ts +7 -2
- package/src/interfaces/keq-middleware-consumer.interface.ts +12 -6
- package/src/keq-consumer.ts +34 -0
- package/src/keq-middleware-collector.ts +150 -0
- package/src/keq-middleware-consumer.ts +30 -126
- package/src/keq.module.ts +8 -24
- package/src/interfaces/keq-middleware-module.interface.ts +0 -6
- package/src/utils/has-configure-keq-middleware.ts +0 -10
package/src/interfaces/index.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
export type { KeqNestMiddleware } from './keq-nest-middleware.interface.js'
|
|
2
|
-
export type {
|
|
3
|
-
|
|
2
|
+
export type {
|
|
3
|
+
KeqMiddlewareConfigProxy,
|
|
4
|
+
KeqScopedConfigProxy,
|
|
5
|
+
KeqRouteInfo,
|
|
6
|
+
KeqRouteTarget,
|
|
7
|
+
KeqModuleClass,
|
|
8
|
+
} from './keq-middleware-consumer.interface.js'
|
|
4
9
|
export type { KeqModuleOptions } from './keq-module-options.interface.js'
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Type } from '@nestjs/common'
|
|
2
|
-
import type { KeqMiddleware } from 'keq'
|
|
3
2
|
import type { KEQ_ROUTES } from '../constants.js'
|
|
4
|
-
import type {
|
|
3
|
+
import type { KeqMiddlewareConsumer } from '../keq-middleware-consumer.js'
|
|
4
|
+
import type { KeqConsumer } from '../keq-consumer.js'
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
export interface KeqRouteInfo {
|
|
@@ -10,15 +10,21 @@ export interface KeqRouteInfo {
|
|
|
10
10
|
pathname?: string
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export interface KeqModuleClass {
|
|
13
|
+
export interface KeqModuleClass extends Type<any> {
|
|
14
14
|
readonly KEQ_REQUEST: symbol
|
|
15
|
+
readonly KEQ_CONSUMER: symbol
|
|
15
16
|
}
|
|
16
17
|
|
|
18
|
+
/** forRoutes() 接受的路由目标(不再包含 KeqModuleClass,模块绑定通过 @InjectKeqConsumer 实现) */
|
|
19
|
+
export type KeqRouteTarget = typeof KEQ_ROUTES.ALL | KeqRouteInfo
|
|
20
|
+
|
|
17
21
|
export interface KeqMiddlewareConfigProxy {
|
|
18
22
|
exclude(...routes: KeqRouteInfo[]): KeqMiddlewareConfigProxy
|
|
19
|
-
forRoutes(...routes:
|
|
23
|
+
forRoutes(...routes: KeqRouteTarget[]): KeqMiddlewareConsumer
|
|
20
24
|
}
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
/** 模块绑定 Consumer 的配置代理,forRoutes() 返回 KeqConsumer<T> */
|
|
27
|
+
export interface KeqScopedConfigProxy<T = KeqModuleClass> {
|
|
28
|
+
exclude(...routes: KeqRouteInfo[]): KeqScopedConfigProxy<T>
|
|
29
|
+
forRoutes(...routes: KeqRouteTarget[]): KeqConsumer<T>
|
|
24
30
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { KeqMiddleware } from 'keq'
|
|
2
|
+
import { KeqRequest } from 'keq'
|
|
3
|
+
import type { KeqModuleClass, KeqScopedConfigProxy } from './interfaces/keq-middleware-consumer.interface.js'
|
|
4
|
+
import type { KeqNestMiddleware } from './interfaces/keq-nest-middleware.interface.js'
|
|
5
|
+
import { KeqMiddlewareCollector } from './keq-middleware-collector.js'
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 为指定 Keq 模块按路由注册中间件。
|
|
10
|
+
*
|
|
11
|
+
* Keq 模块由 `@keq-request/cli` 生成,通过
|
|
12
|
+
* {@link InjectKeqConsumer} 装饰器注入本实例。
|
|
13
|
+
*
|
|
14
|
+
* @typeParam T - 目标模块类
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* constructor(
|
|
19
|
+
* \@InjectKeqConsumer(GeneratedModule) consumer: KeqConsumer<GeneratedModule>,
|
|
20
|
+
* ) {
|
|
21
|
+
* consumer.apply(authMw).forRoutes('*')
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export class KeqConsumer<T = KeqModuleClass> {
|
|
26
|
+
constructor(private readonly keqRequest: KeqRequest) {}
|
|
27
|
+
|
|
28
|
+
apply(
|
|
29
|
+
...middlewares: Array<KeqNestMiddleware | KeqMiddleware>
|
|
30
|
+
): KeqScopedConfigProxy<T> {
|
|
31
|
+
const collector = new KeqMiddlewareCollector(this.keqRequest, this)
|
|
32
|
+
return collector.apply(middlewares) as unknown as KeqScopedConfigProxy<T>
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import type { KeqExecutionContext, KeqMiddleware, KeqRoute } from 'keq'
|
|
2
|
+
import { composeRoute, KeqRequest } from 'keq'
|
|
3
|
+
import picomatch from 'picomatch'
|
|
4
|
+
import { KEQ_ROUTES } from './constants.js'
|
|
5
|
+
import type { KeqMiddlewareConfigProxy, KeqRouteTarget, KeqRouteInfo } from './interfaces/keq-middleware-consumer.interface.js'
|
|
6
|
+
import type { KeqNestMiddleware } from './interfaces/keq-nest-middleware.interface.js'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/** 单条中间件注册条目 */
|
|
10
|
+
interface MiddlewareEntry {
|
|
11
|
+
middlewares: Array<KeqNestMiddleware | KeqMiddleware>
|
|
12
|
+
routes: KeqRouteTarget[]
|
|
13
|
+
excludedRouteInfos?: KeqRouteInfo[]
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 中间件收集器,收集中间件配置条目并应用到指定的 {@link KeqRequest} 实例。
|
|
18
|
+
*
|
|
19
|
+
* 通过 `apply()` → `forRoutes()` 链式 API 收集多组中间件条目,
|
|
20
|
+
* 在 `forRoutes()` 被调用时立即将中间件绑定到目标请求。
|
|
21
|
+
*
|
|
22
|
+
* 注意:该类并非 `@Injectable()`,由 Consumer 内部手动创建。
|
|
23
|
+
*
|
|
24
|
+
* @typeParam TConsumer - Consumer 类型(`KeqMiddlewareConsumer` 或 `KeqConsumer<T>`)
|
|
25
|
+
*/
|
|
26
|
+
export class KeqMiddlewareCollector<TConsumer extends object = object> {
|
|
27
|
+
private entries: MiddlewareEntry[] = []
|
|
28
|
+
|
|
29
|
+
constructor(
|
|
30
|
+
private readonly keqRequest: KeqRequest,
|
|
31
|
+
private readonly consumer: TConsumer,
|
|
32
|
+
) {}
|
|
33
|
+
|
|
34
|
+
apply(
|
|
35
|
+
middlewares: Array<KeqNestMiddleware | KeqMiddleware>,
|
|
36
|
+
): KeqMiddlewareConfigProxy {
|
|
37
|
+
const entry: MiddlewareEntry = { middlewares, routes: [] }
|
|
38
|
+
const excludedRouteInfos: KeqRouteInfo[] = []
|
|
39
|
+
this.entries.push(entry)
|
|
40
|
+
|
|
41
|
+
const { keqRequest, consumer } = this
|
|
42
|
+
|
|
43
|
+
const proxy = {
|
|
44
|
+
exclude: (...routes: KeqRouteInfo[]) => {
|
|
45
|
+
excludedRouteInfos.push(...routes)
|
|
46
|
+
return proxy
|
|
47
|
+
},
|
|
48
|
+
forRoutes: (...routes: KeqRouteTarget[]) => {
|
|
49
|
+
entry.routes = routes
|
|
50
|
+
if (excludedRouteInfos.length > 0) {
|
|
51
|
+
entry.excludedRouteInfos = excludedRouteInfos
|
|
52
|
+
}
|
|
53
|
+
this.applyTo(keqRequest)
|
|
54
|
+
return consumer
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
return proxy as unknown as KeqMiddlewareConfigProxy
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* 将收集的所有中间件条目解析并应用到指定的 {@link KeqRequest} 实例。
|
|
62
|
+
*/
|
|
63
|
+
applyTo(keqRequest: KeqRequest): void {
|
|
64
|
+
for (const entry of this.entries) {
|
|
65
|
+
let resolved = entry.middlewares.map((m) => this.normalizeMiddleware(m))
|
|
66
|
+
|
|
67
|
+
if (entry.excludedRouteInfos && entry.excludedRouteInfos.length > 0) {
|
|
68
|
+
const excludedRoutes = entry.excludedRouteInfos.map((info) => this.buildRoute(info))
|
|
69
|
+
resolved = resolved.map((mw) => this.wrapWithExclusion(mw, excludedRoutes))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const isGlobal
|
|
73
|
+
= entry.routes.length === 0 || entry.routes.some((r) => r === KEQ_ROUTES.ALL)
|
|
74
|
+
|
|
75
|
+
if (isGlobal) {
|
|
76
|
+
for (const mw of resolved) keqRequest.use(mw)
|
|
77
|
+
} else {
|
|
78
|
+
for (const routeTarget of entry.routes) {
|
|
79
|
+
if (routeTarget === KEQ_ROUTES.ALL) continue
|
|
80
|
+
const route = this.buildRoute(routeTarget as KeqRouteInfo)
|
|
81
|
+
keqRequest.useRouter().route(route, resolved)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ─── 中间件标准化 ────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 标准化中间件:将不同形式的中间件统一为 {@link KeqMiddleware} 函数。
|
|
91
|
+
* - 若已是 {@link KeqMiddleware} 函数 → 直接返回
|
|
92
|
+
* - 若为 {@link KeqNestMiddleware} 实例对象 → 包装 `use` 方法为中间件函数
|
|
93
|
+
*/
|
|
94
|
+
private normalizeMiddleware(
|
|
95
|
+
m: KeqNestMiddleware | KeqMiddleware,
|
|
96
|
+
): KeqMiddleware {
|
|
97
|
+
if (this.isNestMiddlewareInstance(m)) {
|
|
98
|
+
return async (ctx, next) => {
|
|
99
|
+
await m.use(ctx, next)
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return m
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private isNestMiddlewareInstance(
|
|
106
|
+
m: KeqNestMiddleware | KeqMiddleware,
|
|
107
|
+
): m is KeqNestMiddleware {
|
|
108
|
+
return typeof m === 'object' && m !== null && 'use' in m
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ─── 路由构建 ─────────────────────────────────────────
|
|
112
|
+
|
|
113
|
+
private buildRoute(info: KeqRouteInfo): KeqRoute {
|
|
114
|
+
const routes: KeqRoute[] = []
|
|
115
|
+
if (info.host) {
|
|
116
|
+
const host = info.host
|
|
117
|
+
routes.push((ctx) => ctx.request.url.host === host)
|
|
118
|
+
}
|
|
119
|
+
if (info.method) {
|
|
120
|
+
const method = info.method.toLowerCase()
|
|
121
|
+
routes.push((ctx) => ctx.request.method.toLowerCase() === method)
|
|
122
|
+
}
|
|
123
|
+
if (info.pathname) {
|
|
124
|
+
const isMatch: (test: string) => boolean = picomatch(info.pathname)
|
|
125
|
+
routes.push((ctx) => isMatch(ctx.request.url.pathname))
|
|
126
|
+
}
|
|
127
|
+
if (routes.length === 1) return routes[0]
|
|
128
|
+
return composeRoute(routes)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private wrapWithExclusion(middleware: KeqMiddleware, excludedRoutes: KeqRoute[]): KeqMiddleware {
|
|
132
|
+
const excluded: KeqMiddleware = async (ctx, next) => {
|
|
133
|
+
if (await this.checkExcluded(ctx, excludedRoutes)) {
|
|
134
|
+
return next()
|
|
135
|
+
}
|
|
136
|
+
return middleware(ctx, next)
|
|
137
|
+
}
|
|
138
|
+
return excluded
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private async checkExcluded(
|
|
142
|
+
ctx: KeqExecutionContext,
|
|
143
|
+
excludedRoutes: KeqRoute[],
|
|
144
|
+
): Promise<boolean> {
|
|
145
|
+
for (const route of excludedRoutes) {
|
|
146
|
+
if (await route(ctx)) return true
|
|
147
|
+
}
|
|
148
|
+
return false
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -1,129 +1,33 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
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'
|
|
1
|
+
import { Injectable } from '@nestjs/common'
|
|
2
|
+
import type { KeqMiddleware } from 'keq'
|
|
3
|
+
import { KeqRequest } from 'keq'
|
|
4
|
+
import type { KeqMiddlewareConfigProxy } from './interfaces/keq-middleware-consumer.interface.js'
|
|
8
5
|
import type { KeqNestMiddleware } from './interfaces/keq-nest-middleware.interface.js'
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return this
|
|
36
|
-
},
|
|
37
|
-
}
|
|
38
|
-
return proxy
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
applyTo(keqRequest: KeqRequest, moduleRef: ModuleRef): void {
|
|
42
|
-
for (const entry of this.entries) {
|
|
43
|
-
let resolved = entry.middlewares.map((m) => this.resolveMiddleware(m, moduleRef))
|
|
44
|
-
|
|
45
|
-
if (entry.excludedRouteInfos && entry.excludedRouteInfos.length > 0) {
|
|
46
|
-
const excludedRoutes = entry.excludedRouteInfos.map((info) => this.buildRoute(info))
|
|
47
|
-
resolved = resolved.map((mw) => this.wrapWithExclusion(mw, excludedRoutes))
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
const isGlobal = entry.routes.length === 0
|
|
51
|
-
|| entry.routes.some((r) => r === KEQ_ROUTES.ALL)
|
|
52
|
-
|
|
53
|
-
if (isGlobal) {
|
|
54
|
-
for (const mw of resolved) keqRequest.use(mw)
|
|
55
|
-
} else {
|
|
56
|
-
for (const routeTarget of entry.routes) {
|
|
57
|
-
if (routeTarget === KEQ_ROUTES.ALL) continue
|
|
58
|
-
|
|
59
|
-
if (this.isKeqModuleClass(routeTarget)) {
|
|
60
|
-
const targetRequest = moduleRef.get<KeqRequest>(routeTarget.KEQ_REQUEST, { strict: false })
|
|
61
|
-
if (targetRequest) {
|
|
62
|
-
for (const mw of resolved) targetRequest.use(mw)
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
const route = this.buildRoute(routeTarget as KeqRouteInfo)
|
|
66
|
-
keqRequest.useRouter().route(route, resolved)
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
private isKeqModuleClass(route: unknown): route is KeqModuleClass {
|
|
74
|
-
return (
|
|
75
|
-
typeof route === 'function'
|
|
76
|
-
&& 'KEQ_REQUEST' in route
|
|
77
|
-
&& typeof (route as Record<string, unknown>).KEQ_REQUEST === 'symbol'
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
private isNestMiddlewareClass(m: Type<KeqNestMiddleware> | KeqMiddleware): m is Type<KeqNestMiddleware> {
|
|
82
|
-
return typeof m === 'function' && m.prototype !== undefined && 'use' in m.prototype
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
private resolveMiddleware(m: Type<KeqNestMiddleware> | KeqMiddleware, moduleRef: ModuleRef): KeqMiddleware {
|
|
86
|
-
if (this.isNestMiddlewareClass(m)) {
|
|
87
|
-
const instance = moduleRef.get<KeqNestMiddleware>(m, { strict: false })
|
|
88
|
-
return async (ctx, next) => {
|
|
89
|
-
await instance.use(ctx, next)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
return m
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
private buildRoute(info: KeqRouteInfo): KeqRoute {
|
|
96
|
-
const routes: KeqRoute[] = []
|
|
97
|
-
if (info.host) {
|
|
98
|
-
const host = info.host
|
|
99
|
-
routes.push((ctx) => ctx.request.url.host === host)
|
|
100
|
-
}
|
|
101
|
-
if (info.method) {
|
|
102
|
-
const method = info.method.toLowerCase()
|
|
103
|
-
routes.push((ctx) => ctx.request.method.toLowerCase() === method)
|
|
104
|
-
}
|
|
105
|
-
if (info.pathname) {
|
|
106
|
-
const isMatch: (test: string) => boolean = picomatch(info.pathname)
|
|
107
|
-
routes.push((ctx) => isMatch(ctx.request.url.pathname))
|
|
108
|
-
}
|
|
109
|
-
if (routes.length === 1) return routes[0]
|
|
110
|
-
return composeRoute(routes)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
private wrapWithExclusion(middleware: KeqMiddleware, excludedRoutes: KeqRoute[]): KeqMiddleware {
|
|
114
|
-
const excluded: KeqMiddleware = async (ctx, next) => {
|
|
115
|
-
if (await this.checkExcluded(ctx, excludedRoutes)) {
|
|
116
|
-
return next()
|
|
117
|
-
}
|
|
118
|
-
return middleware(ctx, next)
|
|
119
|
-
}
|
|
120
|
-
return excluded
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
private async checkExcluded(ctx: KeqExecutionContext, excludedRoutes: KeqRoute[]): Promise<boolean> {
|
|
124
|
-
for (const route of excludedRoutes) {
|
|
125
|
-
if (await route(ctx)) return true
|
|
126
|
-
}
|
|
127
|
-
return false
|
|
6
|
+
import { KeqMiddlewareCollector } from './keq-middleware-collector.js'
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 全局中间件消费者,提供 `.apply().forRoutes()` API。
|
|
11
|
+
*
|
|
12
|
+
* `forRoutes()` 返回 `KeqMiddlewareConsumer` 自身,支持链式注册:
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* consumer
|
|
16
|
+
* .apply(mw1).forRoutes(KEQ_ROUTES.ALL)
|
|
17
|
+
* .apply(mw2).forRoutes({ pathname: '/api/*' })
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* 模块级别的中间件请使用 `@InjectKeqConsumer(GeneratedModule)` 注入
|
|
21
|
+
* `KeqConsumer<GeneratedModule>`,其 `forRoutes()` 直接作用于目标模块。
|
|
22
|
+
*/
|
|
23
|
+
@Injectable()
|
|
24
|
+
export class KeqMiddlewareConsumer {
|
|
25
|
+
constructor(private readonly keqRequest: KeqRequest) {}
|
|
26
|
+
|
|
27
|
+
apply(
|
|
28
|
+
...middlewares: Array<KeqNestMiddleware | KeqMiddleware>
|
|
29
|
+
): KeqMiddlewareConfigProxy {
|
|
30
|
+
const collector = new KeqMiddlewareCollector(this.keqRequest, this)
|
|
31
|
+
return collector.apply(middlewares)
|
|
128
32
|
}
|
|
129
33
|
}
|
package/src/keq.module.ts
CHANGED
|
@@ -1,30 +1,14 @@
|
|
|
1
|
-
import { Global, Module
|
|
2
|
-
import { ModulesContainer, ModuleRef } from '@nestjs/core'
|
|
1
|
+
import { Global, Module } from '@nestjs/common'
|
|
3
2
|
import { KeqRequest } from 'keq'
|
|
4
|
-
import {
|
|
5
|
-
import { hasConfigureKeqMiddleware } from './utils/has-configure-keq-middleware.js'
|
|
3
|
+
import { KeqMiddlewareConsumer } from './keq-middleware-consumer.js'
|
|
6
4
|
|
|
7
5
|
|
|
8
6
|
@Global()
|
|
9
7
|
@Module({
|
|
10
|
-
providers: [
|
|
11
|
-
|
|
8
|
+
providers: [
|
|
9
|
+
{ provide: KeqRequest, useFactory: () => new KeqRequest() },
|
|
10
|
+
KeqMiddlewareConsumer,
|
|
11
|
+
],
|
|
12
|
+
exports: [KeqRequest, KeqMiddlewareConsumer],
|
|
12
13
|
})
|
|
13
|
-
export class KeqModule
|
|
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)
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
}
|
|
14
|
+
export class KeqModule {}
|
|
@@ -1,10 +0,0 @@
|
|
|
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
|
-
}
|