@hvedinich/utils 0.0.70 → 0.0.71

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hvedinich/utils",
3
- "version": "0.0.70",
3
+ "version": "0.0.71",
4
4
  "description": "utils module",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,6 +20,7 @@
20
20
  "apollo-server-express": "2.17.0",
21
21
  "axios": "^0.27.2",
22
22
  "express": "4.17.1",
23
+ "cors": "2.8.5",
23
24
  "graphql-playground-middleware-express": "1.7.20",
24
25
  "jsonwebtoken": "8.5.1",
25
26
  "pino": "^9.5.0"
@@ -2,6 +2,7 @@ const express = require('express');
2
2
  const expressPlayground = require('graphql-playground-middleware-express').default;
3
3
  const axios = require('axios');
4
4
  const { ApolloServer, ApolloError } = require('apollo-server-express');
5
+ const cors = require('cors');
5
6
  const {
6
7
  guestCtx,
7
8
  token: { EXPIRED, UNAUTHORIZED },
@@ -48,11 +49,24 @@ const defaultContext = async ({ req }) => {
48
49
  };
49
50
 
50
51
  class Server {
51
- constructor({ config, healthCheck, context }) {
52
+ constructor({ config, healthCheck, context, originDomains }) {
52
53
  this.config = config;
54
+ this.originDomains = originDomains;
53
55
  this.app = express();
54
56
  this.router = null;
55
57
  this.app.get('/_status', healthCheck || defaultCHealthCheck);
58
+ if (this.originDomains?.length) {
59
+ this.app.use((req, res, next) => {
60
+ const authHeader = req.headers['access-control-request-headers'];
61
+ const wantsCredentials = authHeader ? authHeader.includes('x-with-credentials') : false;
62
+ const corsOptions = {
63
+ origin: wantsCredentials ? this.originDomains : '*',
64
+ credentials: wantsCredentials,
65
+ };
66
+
67
+ cors(corsOptions)(req, res, next);
68
+ });
69
+ }
56
70
  this.app.use((req, res, next) => {
57
71
  req.ctx = context({ req, res });
58
72
  next();
@@ -102,7 +116,7 @@ class Server {
102
116
  const server = new ApolloServer(serverOptions);
103
117
 
104
118
  const bodyParserConfig = this.jsonLimit ? { limit: this.jsonLimit } : true;
105
- server.applyMiddleware({ app: this.app, path: '/graphql', bodyParserConfig });
119
+ server.applyMiddleware({ app: this.app, path: '/graphql', bodyParserConfig, cors: !this.originDomains?.length });
106
120
 
107
121
  if (this.config.playground) {
108
122
  this.app.get('/playground', expressPlayground({ endpoint: '/graphql', subscriptionEndpoint: '/subscriptions' }));