(procedure: AnyProcedure, options: ProcedureHandlerOptions<any, any, any, any>)
| 224 | } |
| 225 | |
| 226 | async function executeProcedureInternal(procedure: AnyProcedure, options: ProcedureHandlerOptions<any, any, any, any>): Promise<any> { |
| 227 | const middlewares = procedure['~orpc'].middlewares |
| 228 | const inputValidationIndex = Math.min(Math.max(0, procedure['~orpc'].inputValidationIndex), middlewares.length) |
| 229 | const outputValidationIndex = Math.min(Math.max(0, procedure['~orpc'].outputValidationIndex), middlewares.length) |
| 230 | |
| 231 | const next = async (index: number, context: Context, input: unknown): Promise<unknown> => { |
| 232 | let currentInput = input |
| 233 | |
| 234 | if (index === inputValidationIndex) { |
| 235 | currentInput = await validateInput(procedure, currentInput) |
| 236 | } |
| 237 | |
| 238 | const mid = middlewares[index] |
| 239 | |
| 240 | const output = mid |
| 241 | ? await runWithSpan( |
| 242 | { name: `middleware.${mid.name}`, signal: options.signal }, |
| 243 | async (span) => { |
| 244 | span?.setAttribute('middleware.index', index) |
| 245 | span?.setAttribute('middleware.name', mid.name) |
| 246 | |
| 247 | const result = await mid({ |
| 248 | ...options, |
| 249 | context, |
| 250 | next: async (...[nextOptions]) => { |
| 251 | const nextContext: Context = nextOptions?.context ?? {} |
| 252 | |
| 253 | return { |
| 254 | output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput), |
| 255 | context: nextContext, |
| 256 | } |
| 257 | }, |
| 258 | }, currentInput, middlewareOutputFn) |
| 259 | |
| 260 | return result.output |
| 261 | }, |
| 262 | ) |
| 263 | : await runWithSpan( |
| 264 | { name: 'handler', signal: options.signal }, |
| 265 | () => procedure['~orpc'].handler({ ...options, context, input: currentInput }), |
| 266 | ) |
| 267 | |
| 268 | if (index === outputValidationIndex) { |
| 269 | return await validateOutput(procedure, output) |
| 270 | } |
| 271 | |
| 272 | return output |
| 273 | } |
| 274 | |
| 275 | return next(0, options.context, options.input) |
| 276 | } |
no test coverage detected