@hvedinich/utils 0.0.72 → 0.0.74
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 +1 -1
- package/src/server/index.js +18 -8
package/package.json
CHANGED
package/src/server/index.js
CHANGED
|
@@ -51,19 +51,29 @@ const defaultContext = async ({ req }) => {
|
|
|
51
51
|
class Server {
|
|
52
52
|
constructor({ config, healthCheck, context, originDomains }) {
|
|
53
53
|
this.config = config;
|
|
54
|
-
this.originDomains = originDomains;
|
|
55
54
|
this.app = express();
|
|
55
|
+
this.activateApolloCors = !originDomains?.length;
|
|
56
56
|
this.router = null;
|
|
57
57
|
this.app.get('/_status', healthCheck || defaultCHealthCheck);
|
|
58
|
-
if (
|
|
58
|
+
if (originDomains?.length) {
|
|
59
59
|
this.app.use((req, res, next) => {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
origin: wantsCredentials ? this.originDomains : '*',
|
|
64
|
-
credentials: wantsCredentials,
|
|
60
|
+
let corsOptions = {
|
|
61
|
+
origin: true,
|
|
62
|
+
credentials: true,
|
|
65
63
|
};
|
|
66
64
|
|
|
65
|
+
if (req.url === '/graphql') {
|
|
66
|
+
const authHeader = req.headers['access-control-request-headers'];
|
|
67
|
+
const wantsCredentials = authHeader
|
|
68
|
+
? authHeader.includes('x-with-credentials') || authHeader.includes('authorization')
|
|
69
|
+
: false;
|
|
70
|
+
|
|
71
|
+
corsOptions = {
|
|
72
|
+
origin: wantsCredentials ? originDomains : '*',
|
|
73
|
+
credentials: wantsCredentials,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
67
77
|
cors(corsOptions)(req, res, next);
|
|
68
78
|
});
|
|
69
79
|
}
|
|
@@ -116,7 +126,7 @@ class Server {
|
|
|
116
126
|
const server = new ApolloServer(serverOptions);
|
|
117
127
|
|
|
118
128
|
const bodyParserConfig = this.jsonLimit ? { limit: this.jsonLimit } : true;
|
|
119
|
-
server.applyMiddleware({ app: this.app, path: '/graphql', bodyParserConfig, cors:
|
|
129
|
+
server.applyMiddleware({ app: this.app, path: '/graphql', bodyParserConfig, cors: this.activateApolloCors });
|
|
120
130
|
|
|
121
131
|
if (this.config.playground) {
|
|
122
132
|
this.app.get('/playground', expressPlayground({ endpoint: '/graphql', subscriptionEndpoint: '/subscriptions' }));
|