Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,23 @@ oRPC will use NestJS parsed body when it's available, and only use the oRPC pars
Configure the `@orpc/nest` module by importing `ORPCModule` in your NestJS application:

```ts
import { REQUEST } from '@nestjs/core'
import { onError, ORPCModule } from '@orpc/nest'
import { Request } from 'express' // if you use express adapter

@Module({
imports: [
ORPCModule.forRoot({
interceptors: [
onError((error) => {
console.error(error)
}),
],
eventIteratorKeepAliveInterval: 5000, // 5 seconds
ORPCModule.forRootAsync({ // or .forRoot
useFactory: (request: Request) => ({
interceptors: [
onError((error) => {
console.error(error)
}),
],
context: { request }, // oRPC context, accessible from middlewares, etc.
eventIteratorKeepAliveInterval: 5000, // 5 seconds
}),
inject: [REQUEST],
}),
],
})
Expand Down
50 changes: 50 additions & 0 deletions packages/nest/src/implement.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { NodeHttpRequest } from '@orpc/standard-server-node'
import type { Request } from 'express'
import { Controller, Req } from '@nestjs/common'
import { REQUEST } from '@nestjs/core'
import { FastifyAdapter } from '@nestjs/platform-fastify'
import { Test } from '@nestjs/testing'
import { oc, ORPCError } from '@orpc/contract'
Expand Down Expand Up @@ -412,4 +414,52 @@ describe('@Implement', async () => {
eventIteratorKeepAliveComment: '__TEST__',
}))
})

it('works with ORPCModule.forRootAsync', async () => {
const interceptor = vi.fn(({ next }) => next())
const moduleRef = await Test.createTestingModule({
imports: [
ORPCModule.forRootAsync({
useFactory: async (request: Request) => ({
interceptors: [interceptor],
eventIteratorKeepAliveComment: '__TEST__',
context: {
request,
},
}),
inject: [REQUEST],
}),
],
controllers: [ImplProcedureController],
}).compile()

const app = moduleRef.createNestApplication()
await app.init()

const httpServer = app.getHttpServer()

const res = await supertest(httpServer)
.post('/ping?param=value&param2[]=value2&param2[]=value3')
.set('x-custom', 'value')
.send({ hello: 'world' })

expect(res.statusCode).toEqual(200)
expect(res.body).toEqual('pong')

expect(interceptor).toHaveBeenCalledTimes(1)
expect(interceptor).toHaveBeenCalledWith(expect.objectContaining({
context: expect.objectContaining({
request: expect.objectContaining({
url: '/ping?param=value&param2[]=value2&param2[]=value3',
headers: expect.objectContaining({
'x-custom': 'value',
}),
}),
}),
}))
expect(sendStandardResponseSpy).toHaveBeenCalledTimes(1)
expect(sendStandardResponseSpy).toHaveBeenCalledWith(expect.anything(), expect.anything(), expect.objectContaining({
eventIteratorKeepAliveComment: '__TEST__',
}))
})
})
21 changes: 21 additions & 0 deletions packages/nest/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,25 @@ export class ORPCModule {
global: true,
}
}

static forRootAsync(options: {
imports?: any[]
useFactory: (...args: any[]) => Promise<ORPCModuleConfig> | ORPCModuleConfig
inject?: any[]
}): 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,
}
}
}