golevelup/nestjs
repository·master·Indexed 25 days ago
https://github.com/golevelup/nestjsA collection of NestJS integrations and utilities for third-party services including RabbitMQ, Google Cloud PubSub, Stripe, Hasura, and GraphQL. It provides specialized packages such as @golevelup/nestjs-common for utility functions and mixins, @golevelup/nestjs-discovery for querying application providers and controllers, and @golevelup/nestjs-rabbitmq for RPC and Pub/Sub message handling.
What's inside golevelup/nestjs
- GoLevelUp provides a collection of specialized modules and utilities designed to extend and enhance NestJS applications. The library includes integrations for messaging (RabbitMQ, Google Cloud Pub/Sub), database/API tools (Hasura, GraphQL Request, Graphile Worker), and third-party services (Stripe, Webhooks), as well as core utilities for discovery and testing.
Handle Hasura Events in NestJS Interceptors, Guards, and Filters
masterHasura event handlers run in a special NestJS context called
hasura_event. If you use global Guards, Interceptors, or Filters, they will also apply to Hasura events. To prevent this, check the context type usingcontext.getType()and skip logic if the type is'hasura_event'.@Injectable() class ExampleInterceptor implements NestInterceptor { intercept(context: ExecutionContext, next: CallHandler<any>) { const contextType = context.getType<'http' | 'hasura_event'>(); // Do nothing if this is a Hasura event if (contextType === 'hasura_event') { return next.handle(); } // Execute custom interceptor logic for HTTP request/response return next.handle(); } }Install @golevelup/nestjs-graphile-worker
masterInstall the package using your preferred package manager to integrate Graphile Worker into your NestJS application.
npm install --save @golevelup/nestjs-graphile-worker # or yarn add @golevelup/nestjs-graphile-worker # or pnpm add @golevelup/nestjs-graphile-workerInstall dependencies and register git hooks
masterBefore writing code, runpnpm iat the root of the repository. This ensures all packages are installed and all git hooks (such as CommitLint) are correctly registered to validate your changes during the commit process.pnpm iConfigure StripeModule
masterImport
StripeModuleinto your module (e.g.,AppModule). You must provide anapiKey. If you intend to consume webhooks, you must also providewebhookConfigcontaining yourstripeSecrets.Snapshot Event Secrets:
account: Webhook secret for your account.accountTest: Webhook secret for test mode.connect: Webhook secret for Connected accounts.connectTest: Webhook secret for Connected accounts in test mode.
Thin Event Secrets (Optional):
stripeThinSecrets: Separate secrets for Stripe's v2 thin events (required if using@StripeThinWebhookHandler).
import { StripeModule } from '@golevelup/nestjs-stripe'; @Module({ imports: [ StripeModule.forRoot({ apiKey: 'sk_***', webhookConfig: { // Snapshot event secrets stripeSecrets: { account: 'whsec_***', accountTest: 'whsec_***', connect: 'whsec_***', connectTest: 'whsec_***', }, // Thin event secrets (optional) stripeThinSecrets: { account: 'whsec_***', accountTest: 'whsec_***', connect: 'whsec_***', connectTest: 'whsec_***', }, }, }), ], }) export class AppModule {}Configure GraphileWorkerModule
masterImport and add
GraphileWorkerModuleto your NestJS module imports. You can useforRootfor static configuration orforRootAsyncfor dynamic configuration (e.g., usingConfigService).Note: The library is fully ESM. CommonJS (
require) is not supported. Your project must support ESM (Node.js 16+ recommended).import { Module } from '@nestjs/common'; import { GraphileWorkerModule } from '@golevelup/nestjs-graphile-worker'; @Module({ imports: [ GraphileWorkerModule.forRoot({ connectionString: process.env.DATABASE_URL, // ...other options }), ], }) export class AppModule {}Install @golevelup/nestjs-graphql-request
masterInstall the package using your preferred package manager to enable easy initialization of
GraphQLClientfor NestJS Dependency Injection.npm install ---save @golevelup/nestjs-graphql-request # or yarn add @golevelup/nestjs-graphql-request # or pnpm add @golevelup/nestjs-graphql-requestConfigure GraphQLRequestModule
masterImport and add
GraphQLRequestModuleto theimportsarray of your NestJS module. Use the.forRoot()method to provide configuration options, which are based on thegraphql-requestpackage (such asendpointandoptionsfor headers).import { GraphQLRequestModule } from '@golevelup/nestjs-graphql-request'; @Module({ imports: [ GraphQLRequestModule.forRoot({ // Exposes configuration options based on the graphql-request package endpoint: config.get('endpoint'), options: { headers: { 'content-type': 'application/json', 'x-hasura-admin-secret': config.get('secret'), }, }, }), ], }) export class AppModule {}Prevent RabbitMQ connection during tests
masterTo avoid attempting to connect to a real RabbitMQ broker during unit or integration tests, pass
undefinedtoRabbitMQModule.forRoot. This causes the connection process to be ignored.Common pattern: Use a conditional check on
process.env.NODE_ENVto provide the configuration object only when not in atestenvironment.import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq'; @Module({ imports: [ RabbitMQModule.forRoot( RabbitMQModule, /** Not sending config object makes the connection process to be ignored */ process.env.NODE_ENV !== 'test' ? { exchanges: [ { name: 'exchange1', type: 'topic', }, ], uri: 'amqp://rabbitmq:rabbitmq@localhost:5672', connectionInitOptions: { wait: false }, } : undefined, ), ], }) export class RabbitExampleModule {}Configure Simple Raw Body Parsing for Webhooks
masterTo validate webhooks, you often need access to the unmodified raw request body. Because NestJS enables JSON parsing by default, you must follow these two steps to preserve the raw body on specific routes:
1. Disable global body parsing
In your application bootstrap function (usually
main.ts), setbodyParser: falsewhen creating the Nest application.2. Apply raw body parsing to specific routes
Use the
applyRawBodyOnlyToutility function within yourAppModule(implementingNestModule) to specify which routes should receive the raw body. This utility automatically applies JSON parsing to all other routes while excluding the routes you specify.// Step 1: In main.ts const app = await NestFactory.create(AppModule, { bodyParser: false, }); // Step 2: In AppModule import { applyRawBodyOnlyTo } from '@golevelup/nestjs-webhooks'; import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common'; @Module({}) class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { applyRawBodyOnlyTo(consumer, { method: RequestMethod.ALL, path: 'webhook', }); } }Mock AmqpConnection for testing
masterWhen testing modules that depend on
AmqpConnection, you can mock the connection to satisfy publishers without requiring a real RabbitMQ broker.- Create a mock module that provides a mocked instance of
AmqpConnectionand exports it. - Use NestJS
Test.createTestingModuleto override your real RabbitMQ module with the mock module using.overrideModule().useModule().
// In your test setup... import { Module } from '@nestjs/common'; import { AmqpConnection } from '@golevelup/nestjs-rabbitmq'; import { createMock } from '@golevelup/ts-jest'; import { Test } from '@nestjs/testing'; import { AppModule } from 'where your root module is located'; import { RabbitExampleModule } from 'where your rabbitmq module is located'; // Create a valid mock module @Module({ providers: [ { provide: AmqpConnection, useValue: createMock<AmqpConnection>(), }, ], exports: [AmqpConnection], }) class MockRabbitExampleModule {} // Then override the real `RabbitMqModule` with the mocked one beforeAll(async () => { const moduleFixture = await Test.createTestingModule({ imports: [AppModule], }) .overrideModule(RabbitExampleModule) .useModule(MockRabbitExampleModule) .compile(); const app = moduleFixture.createNestApplication(); await app.init(); });- Create a mock module that provides a mocked instance of
Install @golevelup/nestjs-common
masterInstall the
@golevelup/nestjs-commonpackage using your preferred package manager to access utility functions and low-level reusable modules for the NestJS ecosystem.npm install ---save @golevelup/nestjs-common # or yarn add @golevelup/nestjs-common # or pnpm add @golevelup/nestjs-common