@chillicream/nitro-express-middleware 19.0.0-insider.1

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/LICENSE ADDED
@@ -0,0 +1,95 @@
1
+ Copyright (c) ChilliCream, Inc.
2
+
3
+ Source code in this repository is covered by the ChilliCream License 1.0 license as designated by a licensing file in a subdirectory or file header. The default throughout the repository is a license under the ChilliCream License 1.0, unless a file header or a licensing file in a subdirectory specifies another license.
4
+
5
+ ---
6
+
7
+ # ChilliCream License 1.0
8
+
9
+ URL: https://chillicream.com/licensing/chillicream-license
10
+
11
+ ## Acceptance
12
+
13
+ By using the software, you agree to all of the terms and conditions below.
14
+
15
+ ## Copyright License
16
+
17
+ The licensor grants you a non-exclusive, royalty-free, worldwide,
18
+ non-sublicensable, non-transferable license to use, copy, distribute, make
19
+ available, and prepare derivative works of the software, in each case subject to
20
+ the limitations and conditions below.
21
+
22
+ ## Limitations
23
+
24
+ You may not move, change, disable, or circumvent the license key functionality
25
+ in the software, and you may not remove or obscure any functionality in the
26
+ software that is protected by the license key.
27
+
28
+ You may not alter, remove, or obscure any licensing, copyright, or other notices
29
+ of the licensor in the software. Any use of the licensor’s trademarks is subject
30
+ to applicable law.
31
+
32
+ ## Patents
33
+
34
+ The licensor grants you a license, under any patent claims the licensor can
35
+ license, or becomes able to license, to make, have made, use, sell, offer for
36
+ sale, import and have imported the software, in each case subject to the
37
+ limitations and conditions in this license. This license does not cover any
38
+ patent claims that you cause to be infringed by modifications or additions to
39
+ the software. If you or your company make any written claim that the software
40
+ infringes or contributes to infringement of any patent, your patent license for
41
+ the software granted under these terms ends immediately. If your company makes
42
+ such a claim, your patent license ends immediately for work on behalf of your
43
+ company.
44
+
45
+ ## Notices
46
+
47
+ You must ensure that anyone who gets a copy of any part of the software from you
48
+ also gets a copy of these terms.
49
+
50
+ If you modify the software, you must include in any modified copies of the
51
+ software prominent notices stating that you have modified the software.
52
+
53
+ ## No Other Rights
54
+
55
+ These terms do not imply any licenses other than those expressly granted in
56
+ these terms.
57
+
58
+ ## Termination
59
+
60
+ If you use the software in violation of these terms, such use is not licensed,
61
+ and your licenses will automatically terminate. If the licensor provides you
62
+ with a notice of your violation, and you cease all violation of this license no
63
+ later than 30 days after you receive that notice, your licenses will be
64
+ reinstated retroactively. However, if you violate these terms after such
65
+ reinstatement, any additional violation of these terms will cause your licenses
66
+ to terminate automatically and permanently.
67
+
68
+ ## No Liability
69
+
70
+ _As far as the law allows, the software comes as is, without any warranty or
71
+ condition, and the licensor will not be liable to you for any damages arising
72
+ out of these terms or the use or nature of the software, under any kind of
73
+ legal claim._
74
+
75
+ ## Definitions
76
+
77
+ The **licensor** is the entity offering these terms, and the **software** is the
78
+ software the licensor makes available under these terms, including any portion
79
+ of it.
80
+
81
+ **you** refers to the individual or entity agreeing to these terms.
82
+
83
+ **your company** is any legal entity, sole proprietorship, or other kind of
84
+ organization that you work for, plus all organizations that have control over,
85
+ are under the control of, or are under common control with that
86
+ organization. **control** means ownership of substantially all the assets of an
87
+ entity, or the power to direct its management and policies by vote, contract, or
88
+ otherwise. Control can be direct or indirect.
89
+
90
+ **your licenses** are all the licenses granted to you for the software under
91
+ these terms.
92
+
93
+ **use** means anything you do with the software requiring one of your licenses.
94
+
95
+ **trademark** means trademarks, service marks, and similar rights.
package/README.md ADDED
@@ -0,0 +1,312 @@
1
+ # Nitro Express Middleware
2
+
3
+ _Express middleware for Nitro GraphQL IDE_
4
+
5
+ ## Description
6
+
7
+ This middleware allows to setup _Nitro GraphQL IDE_ for your GraphQL server.
8
+
9
+ You can use a **cdn** hosted version of the app or a **self** hosted version using the ad hoc package.
10
+
11
+ ## Installation
12
+
13
+ Install this package and the required `peerDependencies` in your project:
14
+
15
+ ```sh
16
+ npm install @chillicream/nitro-express-middleware --save-dev
17
+ # or
18
+ yarn add @chillicream/nitro-express-middleware --dev
19
+ # or
20
+ pnpm add @chillicream/nitro-express-middleware --save-dev
21
+ ```
22
+
23
+ **Note**: `@chillicream/nitro-embedded` is optional and only needed if you prefer to **self** host the app.
24
+
25
+ ## Usage
26
+
27
+ Add the middleware to your app.
28
+
29
+ ```javascript
30
+ import express from "express";
31
+ import nitroMiddleware from "@chillicream/nitro-express-middleware";
32
+
33
+ // ...
34
+
35
+ const app = express();
36
+
37
+ app.use(
38
+ "/graphql",
39
+
40
+ // for `cdn` hosted version
41
+ nitroMiddleware({ mode: "cdn" })
42
+
43
+ // for `embedded` version
44
+ // nitroMiddleware({ mode: "embedded" }),
45
+
46
+ // place here your graphql middleware and others
47
+ );
48
+
49
+ app.listen(3000, () => {
50
+ console.log(`GraphQL on http://localhost:3000/graphql`);
51
+ });
52
+ ```
53
+
54
+ ### Extended configuration
55
+
56
+ - To pin a specific version instead of using "latest":
57
+
58
+ ```javascript
59
+ nitroMiddleware({
60
+ mode: "cdn",
61
+ target: { version: "3.0.0" },
62
+ });
63
+ ```
64
+
65
+ - To use your own infrastructure:
66
+
67
+ ```javascript
68
+ nitroMiddleware({
69
+ mode: "cdn",
70
+ target: "https://mycompany.com/nitro",
71
+ });
72
+ ```
73
+
74
+ ### Custom options
75
+
76
+ - To pass `options` supported by _Nitro GraphQL IDE_:
77
+
78
+ ```javascript
79
+ nitroMiddleware({
80
+ mode: "cdn",
81
+ options: {
82
+ title: "nitro",
83
+ },
84
+ });
85
+ ```
86
+
87
+ ## Recipes
88
+
89
+ <details id="graphql-http">
90
+ <summary><a href="#graphql-http"><sub><sup>🔗</sup></sub></a> With <a href="https://github.com/enisdenjo/graphql-http">graphql-http</a></summary>
91
+ <hr>
92
+
93
+ ```javascript
94
+ import express from "express";
95
+ import { createHandler } from "graphql-http";
96
+ import { GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";
97
+
98
+ import nitroMiddleware from "@chillicream/nitro-express-middleware";
99
+
100
+ const schema = new GraphQLSchema({
101
+ query: new GraphQLObjectType({
102
+ name: "Query",
103
+ fields: {
104
+ greeting: {
105
+ type: GraphQLString,
106
+ resolve(_parent, _args) {
107
+ return "Hello, World!";
108
+ },
109
+ },
110
+ },
111
+ }),
112
+ });
113
+
114
+ const app = express();
115
+
116
+ const handler = createHandler({ schema });
117
+
118
+ app.use(
119
+ "/graphql",
120
+
121
+ // for `cdn` hosted version
122
+ nitroMiddleware({ mode: "cdn" }),
123
+
124
+ // for `embedded` version
125
+ // nitroMiddleware({ mode: "embedded" }),
126
+
127
+ async (req, res) => {
128
+ try {
129
+ const [body, init] = await handler({
130
+ url: req.url,
131
+ method: req.method,
132
+ headers: req.headers,
133
+ body: () =>
134
+ new Promise((resolve) => {
135
+ let body = "";
136
+ req.on("data", (chunk) => (body += chunk));
137
+ req.on("end", () => resolve(body));
138
+ }),
139
+ raw: req,
140
+ });
141
+ res.writeHead(init.status, init.statusText, init.headers).end(body);
142
+ } catch (err) {
143
+ res.writeHead(500).end(err.message);
144
+ }
145
+ }
146
+ );
147
+
148
+ app.listen(3000, () => {
149
+ console.log(`GraphQL on http://localhost:3000/graphql`);
150
+ });
151
+ ```
152
+
153
+ <hr>
154
+ </details>
155
+
156
+ <details id="graphql-yoga">
157
+ <summary><a href="#graphql-yoga"><sub><sup>🔗</sup></sub></a> With <a href="https://the-guild.dev/graphql/yoga-server">graphql-yoga</a></summary>
158
+ <hr>
159
+
160
+ ```javascript
161
+ import express from "express";
162
+ import { createYoga, createSchema } from "graphql-yoga";
163
+
164
+ import nitroMiddleware from "@chillicream/nitro-express-middleware";
165
+
166
+ const graphQLServer = createYoga({
167
+ schema: createSchema({
168
+ typeDefs: /* GraphQL */ `
169
+ type Query {
170
+ greeting: String
171
+ }
172
+ `,
173
+ resolvers: {
174
+ Query: {
175
+ greeting: () => "Hello, World!",
176
+ },
177
+ },
178
+ }),
179
+ logging: false,
180
+ });
181
+
182
+ const app = express();
183
+
184
+ app.use(
185
+ "/graphql",
186
+
187
+ // for `cdn` hosted version
188
+ nitroMiddleware({ mode: "cdn" }),
189
+
190
+ // for `embedded` version
191
+ // nitroMiddleware({ mode: "embedded" }),
192
+
193
+ graphQLServer
194
+ );
195
+
196
+ app.listen(3000, () => {
197
+ console.log(`GraphQL on http://localhost:3000/graphql`);
198
+ });
199
+ ```
200
+
201
+ <hr>
202
+ </details>
203
+
204
+ <details id="express-graphql">
205
+ <summary><a href="#express-graphql"><sub><sup>🔗</sup></sub></a> With <a href="https://github.com/graphql/express-graphql">express-graphql</a></summary>
206
+ <hr>
207
+
208
+ ```javascript
209
+ import express from "express";
210
+ import { graphqlHTTP } from "express-graphql";
211
+ import { GraphQLObjectType, GraphQLSchema, GraphQLString } from "graphql";
212
+
213
+ import nitroMiddleware from "@chillicream/nitro-express-middleware";
214
+
215
+ const schema = new GraphQLSchema({
216
+ query: new GraphQLObjectType({
217
+ name: "Query",
218
+ fields: {
219
+ greeting: {
220
+ type: GraphQLString,
221
+ resolve(_parent, _args) {
222
+ return "Hello, World!";
223
+ },
224
+ },
225
+ },
226
+ }),
227
+ });
228
+
229
+ const app = express();
230
+
231
+ app.use(
232
+ "/graphql",
233
+
234
+ // for `cdn` hosted version
235
+ nitroMiddleware({ mode: "cdn" }),
236
+
237
+ // for `embedded` version
238
+ // nitroMiddleware({ mode: "embedded" }),
239
+
240
+ graphqlHTTP({
241
+ schema,
242
+ graphiql: false,
243
+ })
244
+ );
245
+
246
+ app.listen(3000, () => {
247
+ console.log(`GraphQL on http://localhost:3000/graphql`);
248
+ });
249
+ ```
250
+
251
+ <hr>
252
+ </details>
253
+
254
+ <details id="apollo-server">
255
+ <summary><a href="#apollo-server"><sub><sup>🔗</sup></sub></a> With <a href="https://www.apollographql.com/docs/apollo-server/">apollo-server-</a></summary>
256
+ <hr>
257
+
258
+ ```javascript
259
+ import { ApolloServer } from "@apollo/server";
260
+ import { expressMiddleware } from "@apollo/server/express4";
261
+ import { ApolloServerPluginDrainHttpServer } from "@apollo/server/plugin/drainHttpServer";
262
+ import express from "express";
263
+ import http from "http";
264
+ import cors from "cors";
265
+ import bodyParser from "body-parser";
266
+
267
+ import nitroMiddleware from "@chillicream/nitro-express-middleware";
268
+
269
+ const typeDefs = `#graphql
270
+ type Query {
271
+ greeting: String
272
+ }
273
+ `;
274
+
275
+ const resolvers = {
276
+ Query: {
277
+ greeting: () => "Hello, World!",
278
+ },
279
+ };
280
+
281
+ const app = express();
282
+ const httpServer = http.createServer(app);
283
+ const server = new ApolloServer({
284
+ typeDefs,
285
+ resolvers,
286
+ plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
287
+ });
288
+ await server.start();
289
+
290
+ app.use(
291
+ "/graphql",
292
+
293
+ // for `cdn` hosted version
294
+ nitroMiddleware({ mode: "cdn" }),
295
+
296
+ // for `embedded` version
297
+ // nitroMiddleware({ mode: "embedded" }),
298
+
299
+ cors(),
300
+ bodyParser.json(),
301
+ expressMiddleware(server, {
302
+ context: async ({ req }) => ({ token: req.headers.token }),
303
+ })
304
+ );
305
+
306
+ httpServer.listen({ port: 3000 }, () => {
307
+ console.log(`GraphQL on http://localhost:3000/graphql`);
308
+ });
309
+ ```
310
+
311
+ <hr>
312
+ </details>
package/index.cjs ADDED
@@ -0,0 +1,119 @@
1
+ /*!
2
+ * @license ChilliCream License 1.0
3
+ *
4
+ * Copyright (c) ChilliCream, Inc.
5
+ *
6
+ * This source code is licensed under the ChilliCream License 1.0 found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+ 'use strict';
10
+
11
+ Object.defineProperty(exports, '__esModule', { value: true });
12
+
13
+ var express = require('express');
14
+ var httpProxyMiddleware = require('http-proxy-middleware');
15
+ var module$1 = require('node:module');
16
+ var path = require('node:path');
17
+
18
+ const Defaults = {
19
+ CDN_URL: "https://cdn.chillicream.com/web",
20
+ VERSION: "latest",
21
+ EMBEDDED_ID: "@chillicream/nitro-embedded",
22
+ CONFIG: {
23
+ mode: "cdn",
24
+ },
25
+ OPTIONS: {
26
+ useBrowserUrlAsEndpoint: true,
27
+ },
28
+ };
29
+ const Paths = {
30
+ ROOT: "/",
31
+ INDEX: "/index.html",
32
+ CONFIG: "/nitro-config.json",
33
+ };
34
+ function resolveUrl(target) {
35
+ return typeof target === "string"
36
+ ? target
37
+ : `${target?.baseUrl || Defaults.CDN_URL}/${target?.version || Defaults.VERSION}`;
38
+ }
39
+ function resolvePath(target) {
40
+ const require$1 = module$1.createRequire(require('url').pathToFileURL(__filename).toString());
41
+ return path.dirname(require$1.resolve(target || Defaults.EMBEDDED_ID));
42
+ }
43
+ function resolveOptions(options) {
44
+ return Object.assign({}, Defaults.OPTIONS, options);
45
+ }
46
+
47
+ function combineMiddlewares(...middlewares) {
48
+ return middlewares.reduce((a, b) => (req, res, next) => {
49
+ a(req, res, (err) => {
50
+ if (err) {
51
+ next(err);
52
+ }
53
+ else {
54
+ b(req, res, next);
55
+ }
56
+ });
57
+ });
58
+ }
59
+ function createRootMiddleware() {
60
+ return function rootMiddleware(req, res, next) {
61
+ if (req.method === "GET" || req.method === "HEAD") {
62
+ if (req.path === Paths.ROOT && req.accepts("html")) {
63
+ const path = req.originalUrl.replace(/\?.*/, "");
64
+ if (!path.endsWith("/")) {
65
+ const query = req.originalUrl.slice(path.length);
66
+ return res.redirect(301, path + "/" + query);
67
+ }
68
+ }
69
+ }
70
+ next();
71
+ };
72
+ }
73
+ function createConfigMiddleware(options) {
74
+ return function configMiddleware(req, res, next) {
75
+ if (req.method === "GET" || req.method === "HEAD") {
76
+ if (req.path === Paths.CONFIG && req.accepts("json")) {
77
+ if (req.method === "GET") {
78
+ return res.json(resolveOptions(options));
79
+ }
80
+ else {
81
+ return res.json();
82
+ }
83
+ }
84
+ }
85
+ next();
86
+ };
87
+ }
88
+ function cdnMiddleware(target, options) {
89
+ return combineMiddlewares(createRootMiddleware(), createConfigMiddleware(options), httpProxyMiddleware.createProxyMiddleware((pathname, req) => {
90
+ return (pathname !== req.baseUrl &&
91
+ (req.method === "GET" || req.method === "HEAD"));
92
+ }, {
93
+ target,
94
+ pathRewrite(path, req) {
95
+ return path.replace(req.baseUrl, "");
96
+ },
97
+ followRedirects: true,
98
+ changeOrigin: true,
99
+ logLevel: "silent",
100
+ }));
101
+ }
102
+ function selfMiddleware(target, options) {
103
+ return combineMiddlewares(createRootMiddleware(), createConfigMiddleware(options), express.static(target, {
104
+ redirect: true,
105
+ fallthrough: true,
106
+ }));
107
+ }
108
+ function nitroMiddleware({ mode, target, options, }) {
109
+ return mode === "cdn"
110
+ ? cdnMiddleware(resolveUrl(target), options)
111
+ : selfMiddleware(resolvePath(target), options);
112
+ }
113
+
114
+ exports.cdnMiddleware = cdnMiddleware;
115
+ exports.combineMiddlewares = combineMiddlewares;
116
+ exports.createConfigMiddleware = createConfigMiddleware;
117
+ exports.createRootMiddleware = createRootMiddleware;
118
+ exports.default = nitroMiddleware;
119
+ exports.selfMiddleware = selfMiddleware;
package/index.d.cts ADDED
@@ -0,0 +1,40 @@
1
+ /// <reference types="express" />
2
+ interface HttpHeaderDictionary {
3
+ readonly [key: string]: string | string[];
4
+ }
5
+ type SubscriptionProtocol = "auto" | "graphql-transport-ws" | "graphql-ws" | "graphql-sse";
6
+ interface Options {
7
+ readonly title?: string;
8
+ readonly disableTelemetry?: boolean;
9
+ readonly gaTrackingId?: string;
10
+ readonly useBrowserUrlAsEndpoint: boolean;
11
+ readonly includeCookies?: boolean;
12
+ readonly useGet?: boolean;
13
+ readonly subscriptionProtocol?: SubscriptionProtocol;
14
+ readonly sdlEndpoint?: string;
15
+ readonly endpoint?: string;
16
+ readonly httpHeaders?: HttpHeaderDictionary;
17
+ readonly graphQLDocument?: string;
18
+ readonly variables?: Record<string, any>;
19
+ }
20
+ type Config = {
21
+ readonly mode: "cdn";
22
+ /** Endpoint **url** for the `cdn` hosted version. */
23
+ readonly target?: {
24
+ baseUrl: string;
25
+ version?: string;
26
+ } | string;
27
+ readonly options?: Options;
28
+ } | {
29
+ readonly mode: "embedded";
30
+ /** Package **id** or **path** for the `embedded` version. */
31
+ readonly target?: string;
32
+ readonly options?: Options;
33
+ };
34
+ declare function combineMiddlewares(...middlewares: Handler[]): Handler;
35
+ declare function createRootMiddleware(): Handler;
36
+ declare function createConfigMiddleware(options?: Options): Handler;
37
+ declare function cdnMiddleware(target: string, options?: Options): Handler;
38
+ declare function selfMiddleware(target: string, options?: Options): Handler;
39
+ declare function nitroMiddleware({ mode, target, options, }: Config): Handler;
40
+ export { combineMiddlewares, createRootMiddleware, createConfigMiddleware, cdnMiddleware, selfMiddleware, nitroMiddleware as default };
package/index.d.mts ADDED
@@ -0,0 +1,40 @@
1
+ /// <reference types="express" />
2
+ interface HttpHeaderDictionary {
3
+ readonly [key: string]: string | string[];
4
+ }
5
+ type SubscriptionProtocol = "auto" | "graphql-transport-ws" | "graphql-ws" | "graphql-sse";
6
+ interface Options {
7
+ readonly title?: string;
8
+ readonly disableTelemetry?: boolean;
9
+ readonly gaTrackingId?: string;
10
+ readonly useBrowserUrlAsEndpoint: boolean;
11
+ readonly includeCookies?: boolean;
12
+ readonly useGet?: boolean;
13
+ readonly subscriptionProtocol?: SubscriptionProtocol;
14
+ readonly sdlEndpoint?: string;
15
+ readonly endpoint?: string;
16
+ readonly httpHeaders?: HttpHeaderDictionary;
17
+ readonly graphQLDocument?: string;
18
+ readonly variables?: Record<string, any>;
19
+ }
20
+ type Config = {
21
+ readonly mode: "cdn";
22
+ /** Endpoint **url** for the `cdn` hosted version. */
23
+ readonly target?: {
24
+ baseUrl: string;
25
+ version?: string;
26
+ } | string;
27
+ readonly options?: Options;
28
+ } | {
29
+ readonly mode: "embedded";
30
+ /** Package **id** or **path** for the `embedded` version. */
31
+ readonly target?: string;
32
+ readonly options?: Options;
33
+ };
34
+ declare function combineMiddlewares(...middlewares: Handler[]): Handler;
35
+ declare function createRootMiddleware(): Handler;
36
+ declare function createConfigMiddleware(options?: Options): Handler;
37
+ declare function cdnMiddleware(target: string, options?: Options): Handler;
38
+ declare function selfMiddleware(target: string, options?: Options): Handler;
39
+ declare function nitroMiddleware({ mode, target, options, }: Config): Handler;
40
+ export { combineMiddlewares, createRootMiddleware, createConfigMiddleware, cdnMiddleware, selfMiddleware, nitroMiddleware as default };
package/index.mjs ADDED
@@ -0,0 +1,110 @@
1
+ /*!
2
+ * @license ChilliCream License 1.0
3
+ *
4
+ * Copyright (c) ChilliCream, Inc.
5
+ *
6
+ * This source code is licensed under the ChilliCream License 1.0 found in the
7
+ * LICENSE file in the root directory of this source tree.
8
+ */
9
+ import express from 'express';
10
+ import { createProxyMiddleware } from 'http-proxy-middleware';
11
+ import module from 'node:module';
12
+ import path from 'node:path';
13
+
14
+ const Defaults = {
15
+ CDN_URL: "https://cdn.chillicream.com/web",
16
+ VERSION: "latest",
17
+ EMBEDDED_ID: "@chillicream/nitro-embedded",
18
+ CONFIG: {
19
+ mode: "cdn",
20
+ },
21
+ OPTIONS: {
22
+ useBrowserUrlAsEndpoint: true,
23
+ },
24
+ };
25
+ const Paths = {
26
+ ROOT: "/",
27
+ INDEX: "/index.html",
28
+ CONFIG: "/nitro-config.json",
29
+ };
30
+ function resolveUrl(target) {
31
+ return typeof target === "string"
32
+ ? target
33
+ : `${target?.baseUrl || Defaults.CDN_URL}/${target?.version || Defaults.VERSION}`;
34
+ }
35
+ function resolvePath(target) {
36
+ const require = module.createRequire(import.meta.url);
37
+ return path.dirname(require.resolve(target || Defaults.EMBEDDED_ID));
38
+ }
39
+ function resolveOptions(options) {
40
+ return Object.assign({}, Defaults.OPTIONS, options);
41
+ }
42
+
43
+ function combineMiddlewares(...middlewares) {
44
+ return middlewares.reduce((a, b) => (req, res, next) => {
45
+ a(req, res, (err) => {
46
+ if (err) {
47
+ next(err);
48
+ }
49
+ else {
50
+ b(req, res, next);
51
+ }
52
+ });
53
+ });
54
+ }
55
+ function createRootMiddleware() {
56
+ return function rootMiddleware(req, res, next) {
57
+ if (req.method === "GET" || req.method === "HEAD") {
58
+ if (req.path === Paths.ROOT && req.accepts("html")) {
59
+ const path = req.originalUrl.replace(/\?.*/, "");
60
+ if (!path.endsWith("/")) {
61
+ const query = req.originalUrl.slice(path.length);
62
+ return res.redirect(301, path + "/" + query);
63
+ }
64
+ }
65
+ }
66
+ next();
67
+ };
68
+ }
69
+ function createConfigMiddleware(options) {
70
+ return function configMiddleware(req, res, next) {
71
+ if (req.method === "GET" || req.method === "HEAD") {
72
+ if (req.path === Paths.CONFIG && req.accepts("json")) {
73
+ if (req.method === "GET") {
74
+ return res.json(resolveOptions(options));
75
+ }
76
+ else {
77
+ return res.json();
78
+ }
79
+ }
80
+ }
81
+ next();
82
+ };
83
+ }
84
+ function cdnMiddleware(target, options) {
85
+ return combineMiddlewares(createRootMiddleware(), createConfigMiddleware(options), createProxyMiddleware((pathname, req) => {
86
+ return (pathname !== req.baseUrl &&
87
+ (req.method === "GET" || req.method === "HEAD"));
88
+ }, {
89
+ target,
90
+ pathRewrite(path, req) {
91
+ return path.replace(req.baseUrl, "");
92
+ },
93
+ followRedirects: true,
94
+ changeOrigin: true,
95
+ logLevel: "silent",
96
+ }));
97
+ }
98
+ function selfMiddleware(target, options) {
99
+ return combineMiddlewares(createRootMiddleware(), createConfigMiddleware(options), express.static(target, {
100
+ redirect: true,
101
+ fallthrough: true,
102
+ }));
103
+ }
104
+ function nitroMiddleware({ mode, target, options, }) {
105
+ return mode === "cdn"
106
+ ? cdnMiddleware(resolveUrl(target), options)
107
+ : selfMiddleware(resolvePath(target), options);
108
+ }
109
+
110
+ export { cdnMiddleware, combineMiddlewares, createConfigMiddleware, createRootMiddleware, nitroMiddleware as default, selfMiddleware };
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@chillicream/nitro-express-middleware",
3
+ "version": "19.0.0-insider.1",
4
+ "description": "Express middleware for Nitro GraphQL IDE",
5
+ "homepage": "https://chillicream.com",
6
+ "repository": "github:ChilliCream/graphql-platform",
7
+ "keywords": [
8
+ "apollo",
9
+ "apollo-server",
10
+ "banana",
11
+ "bananacakepop",
12
+ "bcp",
13
+ "express",
14
+ "express-graphql",
15
+ "graphql",
16
+ "graphql-http",
17
+ "graphql-ide",
18
+ "graphql-yoga",
19
+ "middleware",
20
+ "editor",
21
+ "ide"
22
+ ],
23
+ "author": {
24
+ "name": "ChilliCream",
25
+ "url": "https://chillicream.com",
26
+ "email": "info@chillicream.com"
27
+ },
28
+ "license": "SEE LICENSE FILE",
29
+ "engines": {
30
+ "node": ">=12"
31
+ },
32
+ "type": "module",
33
+ "types": "index.d.mts",
34
+ "module": "index.mjs",
35
+ "main": "index.cjs",
36
+ "exports": {
37
+ ".": {
38
+ "import": {
39
+ "types": "./index.d.mts",
40
+ "default": "./index.mjs"
41
+ },
42
+ "require": {
43
+ "types": "./index.d.cts",
44
+ "default": "./index.cjs"
45
+ },
46
+ "default": {
47
+ "types": "./index.d.mts",
48
+ "default": "./index.mjs"
49
+ }
50
+ },
51
+ "./package.json": "./package.json"
52
+ },
53
+ "peerDependencies": {
54
+ "@chillicream/nitro-embedded": "*",
55
+ "express": "*",
56
+ "http-proxy-middleware": "*"
57
+ },
58
+ "peerDependenciesMeta": {
59
+ "@chillicream/nitro-embedded": {
60
+ "optional": true
61
+ },
62
+ "http-proxy-middleware": {
63
+ "optional": true
64
+ }
65
+ },
66
+ "stableVersion": "0.0.0"
67
+ }