|
1 | 1 | import type { IMethodExecuteOptions, IRestfulConnect, IRestMethodOperator } from '@unioc/web'
|
2 | 2 | import type { INestJSMethodParamMetadata, NestJSMethodWrapper } from './method-wrapper'
|
| 3 | +import type { NestJSRestfulScanner } from './restful-scanner' |
3 | 4 | import { UnauthorizedException } from '@nestjs/common'
|
4 | 5 | import { ExecutionContextBuilder } from '../execution-context-builder'
|
5 | 6 | import { EndingHandler } from './ending-handler'
|
6 | 7 | import { NestJSMethodOperator } from './method-operator'
|
7 | 8 |
|
8 | 9 | export class NestJSRestfulHandler implements IRestfulConnect.Handler {
|
| 10 | + constructor(private readonly _restfulScanner: NestJSRestfulScanner) {} |
| 11 | + |
| 12 | + getRestfulScanner(): NestJSRestfulScanner { |
| 13 | + return this._restfulScanner |
| 14 | + } |
| 15 | + |
9 | 16 | private _getParamValue(currentMetadata: INestJSMethodParamMetadata, ctx: IRestfulConnect.WebContext): unknown {
|
10 | 17 | switch (currentMetadata.paramType) {
|
11 | 18 | case 'body':
|
@@ -151,23 +158,35 @@ export class NestJSRestfulHandler implements IRestfulConnect.Handler {
|
151 | 158 | throw new Error('Method operator is not a NestJSMethodOperator')
|
152 | 159 |
|
153 | 160 | const methodWrapper = methodOperator.getMethodWrapper()
|
| 161 | + let methodArguments: unknown[] = [] |
| 162 | + const extraOptions = { |
| 163 | + webContext: ctx, |
| 164 | + adapterType: 'connect', |
| 165 | + handlerType: 'nestjs', |
| 166 | + } as const |
154 | 167 |
|
155 | 168 | try {
|
156 | 169 | // 1. Build params with pipes
|
157 |
| - const params = await this.buildParams(methodWrapper, ctx) |
| 170 | + methodArguments = await this.buildParams(methodWrapper, ctx) |
158 | 171 | // 2. Execute guards
|
159 |
| - await this.executeGuards(methodWrapper, params) |
| 172 | + await this.executeGuards(methodWrapper, methodArguments) |
160 | 173 | // 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) |
168 | 178 | }
|
169 | 179 | 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, { |
171 | 190 | type: 'error',
|
172 | 191 | error,
|
173 | 192 | })
|
|
0 commit comments