Skip to content

Commit

Permalink
Make cors config optional + move withCors to shared package
Browse files Browse the repository at this point in the history
  • Loading branch information
samuel committed Apr 3, 2024
1 parent 6a31273 commit db7a511
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 33 deletions.
2 changes: 2 additions & 0 deletions apps/policy-engine/.env.default
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ NODE_ENV=development

PORT=3010

CORS="http://localhost:4200"

POLICY_ENGINE_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/engine?schema=public"

ENGINE_UID="local-dev-engine-instance-1"
Expand Down
16 changes: 2 additions & 14 deletions apps/policy-engine/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ConfigService } from '@narval/config-module'
import { withSwagger } from '@narval/nestjs-shared'
import { withCors, withSwagger } from '@narval/nestjs-shared'
import { INestApplication, Logger, ValidationPipe } from '@nestjs/common'
import { NestFactory } from '@nestjs/core'
import { lastValueFrom, map, of, switchMap } from 'rxjs'
Expand Down Expand Up @@ -35,18 +35,6 @@ const withGlobalFilters =
return app
}

const withCors =
(configService: ConfigService<Config>) =>
(app: INestApplication): INestApplication => {
const origin = configService.get('cors')

if (origin.length > 0) {
app.enableCors({ origin })
}

return app
}

const provision = async () => {
const application = await NestFactory.createApplicationContext(ProvisionModule)

Expand Down Expand Up @@ -78,7 +66,7 @@ async function bootstrap() {
),
map(withGlobalPipes),
map(withGlobalFilters(configService)),
map(withCors(configService)),
map(withCors(configService.get('cors'))),
switchMap((app) => app.listen(port))
)
)
Expand Down
2 changes: 1 addition & 1 deletion apps/policy-engine/src/policy-engine.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export enum Env {
const configSchema = z.object({
env: z.nativeEnum(Env),
port: z.coerce.number(),
cors: z.array(z.string()),
cors: z.array(z.string()).optional(),
database: z.object({
url: z.string().startsWith('postgresql:')
}),
Expand Down
4 changes: 2 additions & 2 deletions apps/vault/.env.default
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
NODE_ENV=development

CORS="http://localhost:4200"

PORT=3011

CORS="http://localhost:4200"

APP_DATABASE_URL="postgresql://postgres:postgres@localhost:5432/vault?schema=public"

APP_UID="local-dev-vault-instance-1"
Expand Down
2 changes: 1 addition & 1 deletion apps/vault/src/main.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export enum Env {
const configSchema = z.object({
env: z.nativeEnum(Env),
port: z.coerce.number(),
cors: z.array(z.string()),
cors: z.array(z.string()).optional(),
database: z.object({
url: z.string().startsWith('postgresql:')
}),
Expand Down
17 changes: 2 additions & 15 deletions apps/vault/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { withSwagger } from '@narval/nestjs-shared'
import { withCors, withSwagger } from '@narval/nestjs-shared'
import { INestApplication, Logger, ValidationPipe } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { NestFactory } from '@nestjs/core'
import { lastValueFrom, map, of, switchMap } from 'rxjs'
import { Config } from './main.config'
import { MainModule } from './main.module'

/**
Expand All @@ -18,18 +17,6 @@ const withGlobalPipes = (app: INestApplication): INestApplication => {
return app
}

const withCors =
(configService: ConfigService<Config>) =>
(app: INestApplication): INestApplication => {
const origin = configService.get('cors')

if (origin.length > 0) {
app.enableCors({ origin })
}

return app
}

async function bootstrap() {
const logger = new Logger('AppBootstrap')
const application = await NestFactory.create(MainModule, { bodyParser: true })
Expand All @@ -50,7 +37,7 @@ async function bootstrap() {
})
),
map(withGlobalPipes),
map(withCors(configService)),
map(withCors(configService.get('cors'))),
switchMap((app) => app.listen(port))
)
)
Expand Down
1 change: 1 addition & 0 deletions packages/nestjs-shared/src/lib/util/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './with-cors.util'
export * from './with-swagger.util'
11 changes: 11 additions & 0 deletions packages/nestjs-shared/src/lib/util/with-cors.util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { INestApplication } from '@nestjs/common'

export const withCors =
(origin: string[] | undefined) =>
(app: INestApplication): INestApplication => {
if (origin && origin.length > 0) {
app.enableCors({ origin })
}

return app
}

0 comments on commit db7a511

Please sign in to comment.