import type { DynamicModule, ExecutionContext } from '@nestjs/common' import { Module } from '@nestjs/common' import type { OpenAPIHandlerCodecCoreOptions } from '@orpc/openapi/standard' import type { DefaultInitialContext } from '@orpc/server' import type { StandardHandlerOptions } from '@orpc/server/standard' import type { Promisable, Value } from '@orpc/shared' import type { StandardLazyRequest } from '@standard-server/core' import type { ToEventStreamOptions } from '@standard-server/node' import { ORPC_MODULE_CONFIG_SYMBOL } from './constants' import { ImplementInterceptor } from './implement' /** * A standard lazy request extended with NestJS route parameters, * used as the request representation inside the `@orpc/nest` integration. * * @see {@link https://orpc.dev/docs/integrations/nest#toneststandardlazyrequest-option | Implement oRPC contract with NestJS - toNestStandardLazyRequest option} */ export interface NestStandardLazyRequest extends StandardLazyRequest { /** * Route parameters extracted from the request path. */ params?: Record | undefined } export type ORPCModuleConfig = OpenAPIHandlerCodecCoreOptions & StandardHandlerOptions & ({} extends DefaultInitialContext ? { context?: Value, [ctx: ExecutionContext]> | undefined } : { context: Value, [ctx: ExecutionContext]> }) & { /** * Customize how to convert NestJS `req` and `res` to {@link NestStandardLazyRequest}. * You might need to define this if you are not using the express, fastify or node adapters. */ toNestStandardLazyRequest?: ((req: any, res: any) => NestStandardLazyRequest) | undefined /** * Options for how to convert the Standard Response to a Nest Response (returning value), like event stream options, etc. */ toNestResponse?: | { /** * Options for the event stream, like keep-alive settings, initial comment, etc. */ eventStream?: ToEventStreamOptions | undefined } | undefined } /** * NestJS module that configures the `@orpc/nest` integration via `forRoot` * or `forRootAsync`, providing options such as initial context, plugins, * and interceptors to `@Implement` handlers. * * @see {@link https://orpc.dev/docs/integrations/nest#configuration | Implement oRPC contract with NestJS - Configuration} */ export @Module({}) // oxlint-disable-next-line typescript/no-extraneous-class class ORPCModule { static forRoot(config: ORPCModuleConfig): DynamicModule { return { module: ORPCModule, providers: [ { provide: ORPC_MODULE_CONFIG_SYMBOL, useValue: config, }, ImplementInterceptor, ], exports: [ORPC_MODULE_CONFIG_SYMBOL, ImplementInterceptor], global: true, } } static forRootAsync(options: { imports?: any[] | undefined useFactory: (...args: any[]) => Promise | ORPCModuleConfig inject?: any[] | undefined }): DynamicModule { return { module: ORPCModule, imports: options.imports, providers: [ { provide: ORPC_MODULE_CONFIG_SYMBOL, useFactory: options.useFactory, inject: options.inject, }, ImplementInterceptor, ], exports: [ORPC_MODULE_CONFIG_SYMBOL, ImplementInterceptor], global: true, } } }