Skip to content

Commit

Permalink
feat: add cors
Browse files Browse the repository at this point in the history
  • Loading branch information
arsaizdihar committed Mar 25, 2024
1 parent 5a398a6 commit ec4be17
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
PORT=5000
DATABASE_URL=postgres://postgres:postgres@localhost:5432/hmifapp
DATABASE_URL=postgres://postgres:postgres@localhost:5432/hmifapp
ALLOWED_ORIGINS="['http://localhost:5173']"
5 changes: 5 additions & 0 deletions src/configs/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/hello.controller.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,13 +10,21 @@ 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',
info: {
version: packageJson.version,
title: packageJson.displayName,
},
tags: [{ name: 'hello', description: 'Hello API' }],
});
app.get('/swagger', swaggerUI({ url: '/doc' }));

Expand Down
5 changes: 3 additions & 2 deletions src/routes/hello.route.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -17,5 +19,4 @@ export const getUsersRoute = createRoute({
description: 'Retrieve the user',
},
},
tags: ['hello'],
});

0 comments on commit ec4be17

Please sign in to comment.