MCPcopy Create free account
hub / github.com/middleapi/orpc / intercept

Method intercept

packages/nest/src/implement.ts:123–185  ·  view source on GitHub ↗
(ctx: ExecutionContext, next: CallHandler<any>)

Source from the content-addressed store, hash-verified

121 }
122
123 intercept(ctx: ExecutionContext, next: CallHandler<any>): Observable<any> {
124 return next.handle().pipe(
125 mergeMap(async (impl: unknown) => {
126 const { default: procedure } = await unlazy(impl)
127
128 if (!isProcedure(procedure)) {
129 throw new Error(`
130 The return value of the @Implement controller handler must be a corresponding implemented router or procedure.
131 `)
132 }
133
134 const req: Request | FastifyRequest | HonoContext['req'] = ctx.switchToHttp().getRequest()
135 const res: Response | FastifyReply | HonoContext = ctx.switchToHttp().getResponse()
136
137 // Detect a Hono adapter response context by its Fetch response factory.
138 const isHono = 'finalized' in res && typeof (res as HonoContext).newResponse === 'function'
139 const isFastify = 'raw' in req && !isHono
140
141 const standardRequest = (() => {
142 if (isHono) {
143 return StandardServerFetch.toStandardLazyRequest((req as HonoContext['req']).raw)
144 }
145 if (isFastify) {
146 return StandardServerFastify.toStandardLazyRequest(req as FastifyRequest, res as FastifyReply)
147 }
148 return StandardServerNode.toStandardLazyRequest(req as Request, res as Response)
149 })()
150
151 const handler = new StandardHandler(procedure, {
152 init: () => {},
153 match: () => Promise.resolve({ path: toArray(this.config.path), procedure, params: flattenParams(req.params as NestParams | undefined) }),
154 }, this.codec, {
155 // Since plugins can modify options directly, so we need to clone to avoid affecting other handlers/requests
156 // TODO: improve plugins system to avoid this cloning
157 clientInterceptors: [...toArray(this.config.interceptors)],
158 plugins: [...toArray(this.config.plugins)],
159 })
160
161 const result = await handler.handle(standardRequest, {
162 context: this.config.context,
163 })
164
165 if (result.matched) {
166 return intercept(
167 toArray(this.config.sendResponseInterceptors),
168 { request: req, response: res, standardResponse: result.response },
169 async ({ response, standardResponse }) => {
170 if (isHono) {
171 const fetchResponse = StandardServerFetch.toFetchResponse(standardResponse, this.config)
172 return (response as HonoContext).newResponse(fetchResponse.body, fetchResponse)
173 }
174 else if (isFastify) {
175 await StandardServerFastify.sendStandardResponse(response as FastifyReply, standardResponse, this.config)
176 }
177 else {
178 await StandardServerNode.sendStandardResponse(response as Response, standardResponse, this.config)
179 }
180 },

Callers

nothing calls this directly

Calls 7

handleMethod · 0.95
unlazyFunction · 0.90
isProcedureFunction · 0.90
toArrayFunction · 0.90
interceptFunction · 0.90
flattenParamsFunction · 0.85
handleMethod · 0.45

Tested by

no test coverage detected