Skip to content

Commit

Permalink
fix(api): Explicitly generate metadata for nest build script (#6329)
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont authored Aug 15, 2024
1 parent cfcaa75 commit fa7f9ad
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 98 deletions.
3 changes: 3 additions & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,6 @@ fabric.properties
newrelic_agent.log

backups/

# Nest.js auto-generated metadata (https://docs.nestjs.com/recipes/swc#monorepo-and-cli-plugins)
src/metadata.ts
3 changes: 2 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"license": "MIT",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build",
"build": "pnpm build:metadata && nest build",
"format": "prettier --write \"src/**/*.ts\"",
"docker:build": "pnpm --silent --workspace-root pnpm-context -- apps/api/Dockerfile | BULL_MQ_PRO_NPM_TOKEN=${BULL_MQ_PRO_NPM_TOKEN} docker buildx build --load -t novu-api --secret id=BULL_MQ_PRO_NPM_TOKEN --build-arg PACKAGE_PATH=apps/api - $DOCKER_BUILD_ARGUMENTS",
"docker:build:depot": "pnpm --silent --workspace-root pnpm-context -- apps/api/Dockerfile | depot build --build-arg PACKAGE_PATH=apps/api - -t novu-api --load",
Expand All @@ -16,6 +16,7 @@
"start:test": "cross-env NODE_ENV=test PORT=1336 nest start --watch",
"start:debug": "nest start --debug",
"start:prod": "node dist/main.js",
"build:metadata": "cross-env ts-node scripts/generate-metadata.ts",
"lint": "eslint src",
"lint:fix": "pnpm lint -- --fix",
"lint:openapi": "spectral lint http://127.0.0.1:${PORT:-3000}/openapi.yaml",
Expand Down
32 changes: 32 additions & 0 deletions apps/api/scripts/generate-metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* This file is responsible for generating Nest.js metadata for the API.
* Metadata generation is required when using SWC with Nest.js due to SWC
* not natively supporting Typescript, which is required to use the `reflect-metadata`
* API and in turn, resolve types for the OpenAPI specification.
*
* @see https://docs.nestjs.com/recipes/swc#monorepo-and-cli-plugins
*/
import fs from 'node:fs';
import path from 'node:path';
import { PluginMetadataGenerator } from '@nestjs/cli/lib/compiler/plugins';
import { ReadonlyVisitor } from '@nestjs/swagger/dist/plugin';

const tsconfigPath = 'tsconfig.build.json';
const srcPath = path.join(__dirname, '..', 'src');
const metadataPath = path.join(srcPath, 'metadata.ts');

/*
* We create an empty metadata file to ensure that files importing `metadata.ts`
* will compile successfully before the metadata generation occurs.
*/
const defaultContent = `export default async () => { return {}; };`;

fs.writeFileSync(metadataPath, defaultContent, 'utf8');
console.log('metadata.ts file has been generated with default content.');

const generator = new PluginMetadataGenerator();
generator.generate({
visitors: [new ReadonlyVisitor({ introspectComments: true, pathToSource: srcPath })],
outputDir: srcPath,
tsconfigPath,
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { injectDocumentComponents } from './injection';
import { API_KEY_SWAGGER_SECURITY_NAME } from '@novu/application-generic';
import { SecuritySchemeObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';
import { removeEndpointsWithoutApiKey, transformDocument } from './open.api.manipulation.component';
// Necessary to resolve ESlint errors that surface before metadata generation
// eslint-disable-next-line import/extensions
import metadata from '../../../../metadata';

export const API_KEY_SECURITY_DEFINITIONS: SecuritySchemeObject = {
Expand Down
97 changes: 0 additions & 97 deletions apps/api/src/metadata.ts

This file was deleted.

0 comments on commit fa7f9ad

Please sign in to comment.