diff --git a/examples/webhook-signing/nestjs/app.controller.ts b/examples/webhook-signing/nestjs/app.controller.ts index aa6ef92729..43e974b4f3 100644 --- a/examples/webhook-signing/nestjs/app.controller.ts +++ b/examples/webhook-signing/nestjs/app.controller.ts @@ -8,28 +8,26 @@ import { RawBodyRequest, Req, Res, -} from '@nestjs/common' -import { Request,Response } from 'express' -import Stripe from 'stripe' + Injectable, + Inject, +} from '@nestjs/common'; +import {Request, Response} from 'express'; +import Stripe from 'stripe'; +import {ConfigService} from '@nestjs/config'; @Controller() export class AppController { private readonly client: Stripe; - constructor( - private config: ConfigService - ) { - this.client = new Stripe( - config.get('Stripe.secret_key'), - { - apiVersion: '2022-11-15', - typescript: true, - } - ); + constructor(@Inject(ConfigService) private readonly config: ConfigService) { + this.client = new Stripe(this.config.get('Stripe.secret_key'), { + apiVersion: '2022-11-15', + typescript: true, + }); } @Get('/') async index(): Promise { - return 'ok' + return 'ok'; } @Post('/webhooks') @@ -38,14 +36,14 @@ export class AppController { @Req() req: RawBodyRequest, @Res() res: Response ) { - let event: Stripe.Event; try { event = this.client.webhooks.constructEvent( - req.rawBody, - sig, - this.config.get('Stripe.webhook_secret')); + req.rawBody, + sig, + this.config.get('Stripe.webhook_secret') + ); } catch (err) { // On error, log and return the error message console.log(`❌ Error message: ${err.message}`); @@ -69,6 +67,6 @@ export class AppController { } // Return a response to acknowledge receipt of the event - res.json({received: true}); + res.status(200).json({received: true}); } } diff --git a/examples/webhook-signing/nestjs/app.module.ts b/examples/webhook-signing/nestjs/app.module.ts index f33b403d63..e98643f832 100644 --- a/examples/webhook-signing/nestjs/app.module.ts +++ b/examples/webhook-signing/nestjs/app.module.ts @@ -1,21 +1,16 @@ -import { Module } from "@nestjs/common"; -import { ConfigModule } from '@nestjs/config' -import { Config } from "./config"; -import { AppController } from "./app.controller"; - -export const configuration = async (): Promise => { - const { config } = <{ config: Config }> await import(`${__dirname}/config`); - return config; -}; +import {Module} from '@nestjs/common'; +import {ConfigModule} from '@nestjs/config'; +import {config} from './config'; +import {AppController} from './app.controller'; @Module({ controllers: [AppController], imports: [ ConfigModule.forRoot({ isGlobal: true, - load: [configuration], + load: [config], envFilePath: `.env`, - }) + }), ], }) export class AppModule {} diff --git a/examples/webhook-signing/nestjs/config.ts b/examples/webhook-signing/nestjs/config.ts index 09a6b26990..4a4fa65cf6 100644 --- a/examples/webhook-signing/nestjs/config.ts +++ b/examples/webhook-signing/nestjs/config.ts @@ -1,16 +1,13 @@ -export const config: { +type Config = { Stripe: { - publishable_key: string - secret_key: string - webhook_secret: string - } -} = { + secret_key: string; + webhook_secret: string; + }; +}; + +export const config = (): Config => ({ Stripe: { - publishable_key: process.env.STRIPE_PUBLISHABLE_KEY || '', secret_key: process.env.STRIPE_SECRET_KEY || '', webhook_secret: process.env.STRIPE_WEBHOOK_SECRET || '', - } -} -export type Config = typeof config; - - + }, +}); diff --git a/examples/webhook-signing/nestjs/main.ts b/examples/webhook-signing/nestjs/main.ts old mode 100644 new mode 100755 index 1fc5e62191..186d16df70 --- a/examples/webhook-signing/nestjs/main.ts +++ b/examples/webhook-signing/nestjs/main.ts @@ -1,16 +1,18 @@ #!/usr/bin/env -S npm run-script run -import { NestFactory } from "@nestjs/core"; -import { INestApplication } from "@nestjs/common"; -import { AppModule } from "./app.module"; +import {NestFactory} from '@nestjs/core'; +import {INestApplication} from '@nestjs/common'; +import {AppModule} from './app.module'; async function bootstrap() { - const app = await NestFactory.create(AppModule, { rawBody: true }); + const app = await NestFactory.create(AppModule, { + rawBody: true, + }); app.enableCors({ - origin: "*", + origin: '*', }); await app.listen(0); - console.log(`Webhook endpoint available at ${await app.getUrl()}/webhook`); + console.log(`Webhook endpoint available at ${await app.getUrl()}/webhooks`); } bootstrap(); diff --git a/examples/webhook-signing/tsconfig.json b/examples/webhook-signing/tsconfig.json index 4f085eba27..7ad9dfdfe8 100644 --- a/examples/webhook-signing/tsconfig.json +++ b/examples/webhook-signing/tsconfig.json @@ -10,6 +10,8 @@ /* Advanced Options */ /* Disallow inconsistently-cased references to the same file. */ - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + + "experimentalDecorators": true, } } diff --git a/test/Integration.spec.ts b/test/Integration.spec.ts index 384998a710..3bde503608 100644 --- a/test/Integration.spec.ts +++ b/test/Integration.spec.ts @@ -107,4 +107,6 @@ describe('Integration test', function() { }); it('Webhook sample deno', () => runWebhookTest('deno')); + + it('Webhook sample nestjs', () => runWebhookTest('nestjs')); });