@hemia/core 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -261,6 +261,23 @@ class HemiaFactory {
261
261
  const app = express();
262
262
  app.use(express.json());
263
263
  app.use(express.urlencoded({ extended: true }));
264
+ app.use((req, res, next) => {
265
+ const defaultHeaders = {
266
+ 'Access-Control-Allow-Origin': req.headers.origin || '*',
267
+ 'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,PATCH,OPTIONS',
268
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Api-Key, Language, X-No-Cookies, x-session',
269
+ 'Access-Control-Allow-Credentials': 'true',
270
+ 'Access-Control-Expose-Headers': 'Authorization, X-Correlation-Id, X-Trace-Id, x-session',
271
+ };
272
+ const headers = { ...defaultHeaders, ...(options.corsHeaders || {}) };
273
+ Object.entries(headers).forEach(([key, value]) => {
274
+ res.header(key, value);
275
+ });
276
+ if (req.method === 'OPTIONS') {
277
+ return res.sendStatus(200);
278
+ }
279
+ next();
280
+ });
264
281
  if (!container.isBound(Reflector)) {
265
282
  container.bind(Reflector).toSelf().inSingletonScope();
266
283
  }
@@ -263,6 +263,23 @@ class HemiaFactory {
263
263
  const app = express();
264
264
  app.use(express.json());
265
265
  app.use(express.urlencoded({ extended: true }));
266
+ app.use((req, res, next) => {
267
+ const defaultHeaders = {
268
+ 'Access-Control-Allow-Origin': req.headers.origin || '*',
269
+ 'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,PATCH,OPTIONS',
270
+ 'Access-Control-Allow-Headers': 'Content-Type, Authorization, X-Api-Key, Language, X-No-Cookies, x-session',
271
+ 'Access-Control-Allow-Credentials': 'true',
272
+ 'Access-Control-Expose-Headers': 'Authorization, X-Correlation-Id, X-Trace-Id, x-session',
273
+ };
274
+ const headers = { ...defaultHeaders, ...(options.corsHeaders || {}) };
275
+ Object.entries(headers).forEach(([key, value]) => {
276
+ res.header(key, value);
277
+ });
278
+ if (req.method === 'OPTIONS') {
279
+ return res.sendStatus(200);
280
+ }
281
+ next();
282
+ });
266
283
  if (!container.isBound(exports.Reflector)) {
267
284
  container.bind(exports.Reflector).toSelf().inSingletonScope();
268
285
  }
@@ -5,6 +5,7 @@ import { Express } from "express";
5
5
  export interface HemiaFactoryOptions {
6
6
  onTraceFinish?: TraceFinishCallback;
7
7
  logger?: boolean;
8
+ corsHeaders?: Record<string, string>;
8
9
  }
9
10
  export declare class HemiaFactory {
10
11
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/core",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "Core utilities for Hemia projects",
5
5
  "main": "dist/hemia-core.js",
6
6
  "module": "dist/hemia-core.esm.js",