Skip to content

Commit 3f6dcdf

Browse files
Merge pull request #84 from uniocjs/dev-groupguanfang
feat(core,nestjs): expose extraOptions for IPluginContext.handle in core; finishing the guards/pipes error handle (#82,#24)
2 parents e19161e + 255fbf5 commit 3f6dcdf

File tree

7 files changed

+45
-18
lines changed

7 files changed

+45
-18
lines changed

packages/adapter/adapter-nestjs/src/arguments-host-builder.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class ArgumentsHostBuilder implements ArgumentsHost {
99

1010
private _httpArgumentsHost: HttpArgumentsHost = {} as any
1111

12-
getArgs<T extends Array<any> = any[]>(): T {
12+
getArgs<T extends Array<unknown> = unknown[]>(): T {
1313
return this._args as T
1414
}
1515

packages/adapter/adapter-nestjs/src/restful/ending-handler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class EndingHandler {
1111
* @param ctx - The web context of the request.
1212
* @param result - The result of the request.
1313
*/
14-
async sendConnectEndingResponse(ctx: IRestfulConnect.WebContext, result: IResult): Promise<void> {
14+
async sendConnectEndingIfNotSent(ctx: IRestfulConnect.WebContext, result: IResult): Promise<void> {
1515
if (ctx.response.writableEnded || ctx.response.writableFinished)
1616
return
1717

packages/adapter/adapter-nestjs/src/restful/restful-handler.ts

+29-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
import type { IMethodExecuteOptions, IRestfulConnect, IRestMethodOperator } from '@unioc/web'
22
import type { INestJSMethodParamMetadata, NestJSMethodWrapper } from './method-wrapper'
3+
import type { NestJSRestfulScanner } from './restful-scanner'
34
import { UnauthorizedException } from '@nestjs/common'
45
import { ExecutionContextBuilder } from '../execution-context-builder'
56
import { EndingHandler } from './ending-handler'
67
import { NestJSMethodOperator } from './method-operator'
78

89
export class NestJSRestfulHandler implements IRestfulConnect.Handler {
10+
constructor(private readonly _restfulScanner: NestJSRestfulScanner) {}
11+
12+
getRestfulScanner(): NestJSRestfulScanner {
13+
return this._restfulScanner
14+
}
15+
916
private _getParamValue(currentMetadata: INestJSMethodParamMetadata, ctx: IRestfulConnect.WebContext): unknown {
1017
switch (currentMetadata.paramType) {
1118
case 'body':
@@ -151,23 +158,35 @@ export class NestJSRestfulHandler implements IRestfulConnect.Handler {
151158
throw new Error('Method operator is not a NestJSMethodOperator')
152159

153160
const methodWrapper = methodOperator.getMethodWrapper()
161+
let methodArguments: unknown[] = []
162+
const extraOptions = {
163+
webContext: ctx,
164+
adapterType: 'connect',
165+
handlerType: 'nestjs',
166+
} as const
154167

155168
try {
156169
// 1. Build params with pipes
157-
const params = await this.buildParams(methodWrapper, ctx)
170+
methodArguments = await this.buildParams(methodWrapper, ctx)
158171
// 2. Execute guards
159-
await this.executeGuards(methodWrapper, params)
172+
await this.executeGuards(methodWrapper, methodArguments)
160173
// 3. Execute the controller method
161-
const result = await methodWrapper.execute(params, {
162-
webContext: ctx,
163-
adapterType: 'connect',
164-
handlerType: 'nestjs',
165-
})
166-
// 4. Send the ending response
167-
await this._endingHandler.sendConnectEndingResponse(ctx, result)
174+
const result = await methodWrapper.execute(methodArguments, extraOptions)
175+
// 4. TODO: Execute the interceptors
176+
// 5. Send the ending response
177+
await this._endingHandler.sendConnectEndingIfNotSent(ctx, result)
168178
}
169179
catch (error) {
170-
await this._endingHandler.sendConnectEndingResponse(ctx, {
180+
await this.getRestfulScanner().getPluginContext().handle({
181+
classWrapper: methodWrapper.getControllerOperator()
182+
.getControllerWrapper()
183+
.getClassWrapper(),
184+
propertyKey: methodWrapper.getPropertyKey(),
185+
methodArguments,
186+
error,
187+
extraOptions,
188+
})
189+
await this._endingHandler.sendConnectEndingIfNotSent(ctx, {
171190
type: 'error',
172191
error,
173192
})

packages/adapter/adapter-nestjs/src/restful/restful-scanner.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ArgumentMetadata, ArgumentsHost, CanActivate, ExceptionFilter, ExecutionContext, PipeTransform } from '@nestjs/common'
2-
import type { IArgument } from '@unioc/core'
2+
import type { IArgument, IPluginContext } from '@unioc/core'
33
import type { IClass } from '@unioc/shared'
44
import type { IHttpParam, IRestfulConnect, IRestfulScanner } from '@unioc/web'
55
import type { NestJSMethodOperator } from './method-operator'
@@ -12,6 +12,10 @@ export interface INestJSPipeArgument extends IArgument, ArgumentMetadata, Partia
1212
export type INestJSFilterCatchType = 'done' | 'no-match'
1313

1414
export class NestJSRestfulScanner extends RestfulScanner implements IRestfulScanner {
15+
override getPluginContext(): IPluginContext {
16+
return super.getPluginContext()
17+
}
18+
1519
protected override scanAllRestControllerWrappers(): void {
1620
const allClassWrappers = this.getControllerWrappers()
1721

@@ -131,6 +135,6 @@ export class NestJSRestfulScanner extends RestfulScanner implements IRestfulScan
131135
}
132136

133137
resolveConnectHandler(): IRestfulConnect.Handler {
134-
return new NestJSRestfulHandler()
138+
return new NestJSRestfulHandler(this)
135139
}
136140
}

packages/adapter/adapter-nestjs/test/guard.test.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ it('should use guard with filter', async () => {
7676
.get('/test-guard-with-filter')
7777
.expect(401)
7878
.expect('Content-Type', /application\/json/)
79-
// It is not working, TODO: fix it
80-
// .expect({
81-
// status: 'Not Authorized!',
82-
// })
79+
.expect({
80+
status: 'Not Authorized!',
81+
})
8382
})

packages/core/core/src/contexts/plugins/plugin-context.ts

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class PluginContext extends AbstractContext implements IPluginContext {
5454
options.methodArguments,
5555
options.error,
5656
'error',
57+
options.extraOptions,
5758
)
5859
}
5960
}

packages/core/core/src/types/plugin.ts

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export interface IHandleExecutorOptions {
2323
* The current method arguments.
2424
*/
2525
methodArguments: unknown[]
26+
/**
27+
* The extra options.
28+
*/
29+
extraOptions?: Record<string, unknown>
2630
}
2731

2832
export interface IPluginContext extends IContext, IInternalLogger, IBootstrapDeriver {

0 commit comments

Comments
 (0)