From ec4be17250bd428588fcb04b22fc1db4ea10aee5 Mon Sep 17 00:00:00 2001 From: arsaizdihar Date: Mon, 25 Mar 2024 21:34:26 +0700 Subject: [PATCH] feat: add cors --- .env.example | 3 ++- src/configs/env.config.ts | 5 +++++ src/controllers/hello.controller.ts | 4 ++-- src/index.ts | 9 +++++++++ src/routes/hello.route.ts | 5 +++-- 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.env.example b/.env.example index 3c062d7..ee1c7fb 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ PORT=5000 -DATABASE_URL=postgres://postgres:postgres@localhost:5432/hmifapp \ No newline at end of file +DATABASE_URL=postgres://postgres:postgres@localhost:5432/hmifapp +ALLOWED_ORIGINS="['http://localhost:5173']" \ No newline at end of file diff --git a/src/configs/env.config.ts b/src/configs/env.config.ts index 45a2e38..a54ef10 100644 --- a/src/configs/env.config.ts +++ b/src/configs/env.config.ts @@ -4,6 +4,11 @@ import 'dotenv/config'; const EnvSchema = z.object({ PORT: z.coerce.number().default(5000), DATABASE_URL: z.string().url(), + ALLOWED_ORIGINS: z + .string() + .default('["http://localhost:5173"]') + .transform((value) => JSON.parse(value)) + .pipe(z.array(z.string().url())), }); const result = EnvSchema.safeParse(process.env); diff --git a/src/controllers/hello.controller.ts b/src/controllers/hello.controller.ts index fa7e9a0..3354fed 100644 --- a/src/controllers/hello.controller.ts +++ b/src/controllers/hello.controller.ts @@ -1,9 +1,9 @@ import { OpenAPIHono } from '@hono/zod-openapi'; -import { getUsersRoute } from '../routes/hello.route'; +import { getUserRoute } from '../routes/hello.route'; export const helloRouter = new OpenAPIHono(); -helloRouter.openapi(getUsersRoute, (c) => { +helloRouter.openapi(getUserRoute, (c) => { const { id } = c.req.valid('param'); return c.json({ id, diff --git a/src/index.ts b/src/index.ts index 27cea05..1c3d30a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { serve } from '@hono/node-server'; import { swaggerUI } from '@hono/swagger-ui'; import { OpenAPIHono } from '@hono/zod-openapi'; +import { cors } from 'hono/cors'; import { logger } from 'hono/logger'; import packageJson from '../package.json'; import { env } from './configs/env.config'; @@ -9,6 +10,13 @@ import { apiRouter } from './controllers/api.controller'; const app = new OpenAPIHono(); app.use(logger()); +app.use( + '/api/*', + cors({ + credentials: true, + origin: env.ALLOWED_ORIGINS, + }), +); app.route('/api', apiRouter); app.doc('/doc', { openapi: '3.1.0', @@ -16,6 +24,7 @@ app.doc('/doc', { version: packageJson.version, title: packageJson.displayName, }, + tags: [{ name: 'hello', description: 'Hello API' }], }); app.get('/swagger', swaggerUI({ url: '/doc' })); diff --git a/src/routes/hello.route.ts b/src/routes/hello.route.ts index 857402a..8e048d4 100644 --- a/src/routes/hello.route.ts +++ b/src/routes/hello.route.ts @@ -1,7 +1,9 @@ import { createRoute } from '@hono/zod-openapi'; import { ParamsSchema, UserSchema } from '../types/hello.types'; -export const getUsersRoute = createRoute({ +export const getUserRoute = createRoute({ + operationId: 'getUser', + tags: ['hello'], method: 'get', path: '/users/{id}', request: { @@ -17,5 +19,4 @@ export const getUsersRoute = createRoute({ description: 'Retrieve the user', }, }, - tags: ['hello'], });