nuxt-graphql-middleware 2.0.1 → 2.1.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/README.md CHANGED
@@ -246,6 +246,11 @@ export default (pluginContext) => {
246
246
  You have access to the context via the first parameter. The second parameter
247
247
  provides the fetch options, which you have to return.
248
248
 
249
+ It's also possible to return a Promise, useful if you need to handle things
250
+ like a token refresh. Be aware that this method is called before every query or
251
+ mutation request, so make sure it doesn't take too much time.
252
+
253
+
249
254
  ### Integrate with nuxt-auth
250
255
 
251
256
  Add a `beforeRequest` method in a custom plugin:
@@ -0,0 +1,325 @@
1
+ import path from 'path';
2
+ import fs from 'fs';
3
+ import mkdirp from 'mkdirp';
4
+ import chokidar from 'chokidar';
5
+ import consola from 'consola';
6
+ import express from 'express';
7
+ import { GraphQLClient } from 'graphql-request';
8
+ import { generate } from '@graphql-codegen/cli';
9
+ import * as PluginTypescript from '@graphql-codegen/typescript';
10
+ import * as PluginTypescriptOperations from '@graphql-codegen/typescript-operations';
11
+ import * as PluginSchemaAst from '@graphql-codegen/schema-ast';
12
+
13
+ // -- Unbuild CommonJS Shims --
14
+ import __cjs_url__ from 'url';
15
+ import __cjs_path__ from 'path';
16
+ import __cjs_mod__ from 'module';
17
+ const __filename = __cjs_url__.fileURLToPath(import.meta.url);
18
+ const __dirname = __cjs_path__.dirname(__filename);
19
+ const require = __cjs_mod__.createRequire(import.meta.url);
20
+
21
+
22
+ function getVariables(vars) {
23
+ try {
24
+ return JSON.parse(vars);
25
+ } catch (error) {
26
+ return {};
27
+ }
28
+ }
29
+ function buildHeaders(req, name, type, config) {
30
+ if (config?.buildHeaders) {
31
+ return config.buildHeaders(req, name, type);
32
+ }
33
+ if (config?.fetchOptions?.headers) {
34
+ return config.fetchOptions.headers;
35
+ }
36
+ return {};
37
+ }
38
+ function createServerMiddleware(graphqlServer, queries, mutations, config) {
39
+ const app = express();
40
+ app.use(express.json());
41
+ const clients = new Map();
42
+ function getClient(endpoint) {
43
+ if (!clients.has(endpoint)) {
44
+ const client = new GraphQLClient(endpoint);
45
+ clients.set(endpoint, client);
46
+ }
47
+ return clients.get(endpoint);
48
+ }
49
+ function getEndpoint(req) {
50
+ if (config?.buildEndpoint) {
51
+ return config.buildEndpoint(req);
52
+ }
53
+ return graphqlServer;
54
+ }
55
+ if (config?.middleware) {
56
+ app.use(config.middleware);
57
+ }
58
+ async function query(req, res) {
59
+ const name = req.query.name;
60
+ if (!name || !queries.has(name)) {
61
+ res.status(404).send();
62
+ return;
63
+ }
64
+ try {
65
+ const headers = buildHeaders(req, name, "query", config);
66
+ const variables = getVariables(req.query.variables);
67
+ const query2 = queries.get(name);
68
+ const endpoint = getEndpoint(req);
69
+ const client = getClient(endpoint);
70
+ const response = await client.rawRequest(query2, variables, headers);
71
+ if (config?.onQueryResponse) {
72
+ return config.onQueryResponse(response, req, res);
73
+ }
74
+ return res.json(response.data);
75
+ } catch (e) {
76
+ if (config?.onQueryError) {
77
+ return config.onQueryError(e, req, res);
78
+ }
79
+ return res.status(500).send();
80
+ }
81
+ }
82
+ async function mutate(req, res) {
83
+ const name = req.query.name;
84
+ if (!name || !mutations.has(name)) {
85
+ res.status(404).send();
86
+ return;
87
+ }
88
+ const mutation = mutations.get(name);
89
+ try {
90
+ const headers = buildHeaders(req, name, "mutation", config);
91
+ const endpoint = getEndpoint(req);
92
+ const client = getClient(endpoint);
93
+ const response = await client.request(mutation, req.body, headers);
94
+ if (config?.onMutationResponse) {
95
+ return config.onMutationResponse(response, req, res);
96
+ }
97
+ return res.json(response);
98
+ } catch (error) {
99
+ if (config?.onMutationError) {
100
+ return config.onMutationError(error, req, res);
101
+ }
102
+ return res.status(500).send();
103
+ }
104
+ }
105
+ app.get("/query", query);
106
+ app.post("/mutate", mutate);
107
+ return app;
108
+ }
109
+
110
+ const fragmentImport = require("@graphql-fragment-import/lib/inline-imports");
111
+ function graphqlImport(path, resolver) {
112
+ return fragmentImport(path, {
113
+ resolveImport(identifier) {
114
+ return resolver(identifier);
115
+ },
116
+ resolveOptions: {
117
+ basedir: "./"
118
+ }
119
+ });
120
+ }
121
+
122
+ const typescriptConfig = {
123
+ exportFragmentSpreadSubTypes: true,
124
+ preResolveTypes: true,
125
+ skipTypeNameForRoot: true
126
+ };
127
+ function pluginLoader(name) {
128
+ if (name === "@graphql-codegen/typescript") {
129
+ return Promise.resolve(PluginTypescript);
130
+ } else if (name === "@graphql-codegen/typescript-operations") {
131
+ return Promise.resolve(PluginTypescriptOperations);
132
+ } else {
133
+ return Promise.resolve(PluginSchemaAst);
134
+ }
135
+ }
136
+ function codegen(graphqlServer, options) {
137
+ const schemaPath = path.resolve(options.schemaOutputPath, "schema.graphql");
138
+ function generateSchema() {
139
+ const schema = options.skipSchemaDownload ? schemaPath : { [graphqlServer]: options.schemaOptions };
140
+ const configSchemaAst = { ...typescriptConfig, sort: true };
141
+ return generate({
142
+ schema,
143
+ pluginLoader,
144
+ generates: {
145
+ [schemaPath]: {
146
+ plugins: [{ "schema-ast": configSchemaAst }],
147
+ config: configSchemaAst
148
+ },
149
+ [path.resolve(options.typesOutputPath, "graphql-schema.ts")]: {
150
+ plugins: [{ typescript: typescriptConfig }],
151
+ config: typescriptConfig
152
+ }
153
+ }
154
+ }, true);
155
+ }
156
+ function generateTypes() {
157
+ const config = {
158
+ ...typescriptConfig,
159
+ onlyOperationTypes: true,
160
+ ...options.schemaOptions || {}
161
+ };
162
+ return generate({
163
+ schema: schemaPath,
164
+ pluginLoader,
165
+ documents: path.resolve(options.resolvedQueriesPath, "./*.graphql"),
166
+ generates: {
167
+ [path.resolve(options.typesOutputPath, "graphql-operations.ts")]: {
168
+ plugins: ["typescript", { "typescript-operations": config }],
169
+ config
170
+ }
171
+ }
172
+ }, true);
173
+ }
174
+ return { generateSchema, generateTypes };
175
+ }
176
+
177
+ const logger = consola.withTag("nuxt-graphql-middleware");
178
+ const PLUGIN_PATH = path.resolve(__dirname, "../dist/plugin.mjs");
179
+ var FileType;
180
+ (function(FileType2) {
181
+ FileType2["Query"] = "query";
182
+ FileType2["Mutation"] = "mutation";
183
+ })(FileType || (FileType = {}));
184
+ function resolveGraphqlFile(file, resolver) {
185
+ return fs.promises.readFile(file).then((buffer) => buffer.toString()).then((source) => graphqlImport(source, resolver));
186
+ }
187
+ function writeSource(dest, type, name, source) {
188
+ const fileName = `${type}.${name}.graphql`;
189
+ const out = path.resolve(dest, fileName);
190
+ return fs.promises.writeFile(out, source);
191
+ }
192
+ function resolveGraphql(files, map, resolver, filesMap, type, outputPath) {
193
+ return Promise.all(Object.keys(files).map((name) => {
194
+ const filePath = files[name];
195
+ const file = resolver(filePath);
196
+ return resolveGraphqlFile(file, resolver).then((source) => {
197
+ map.set(name, source);
198
+ if (outputPath) {
199
+ writeSource(outputPath, type, name, source);
200
+ }
201
+ filesMap.set(file, {
202
+ type,
203
+ name,
204
+ file: filePath
205
+ });
206
+ });
207
+ }));
208
+ }
209
+ const graphqlMiddleware = async function() {
210
+ const resolver = this.nuxt.resolver.resolveAlias;
211
+ const options = this.options;
212
+ const PORT = this.options?.server?.port || 3e3;
213
+ const provided = this.options.graphqlMiddleware || {};
214
+ const config = {
215
+ graphqlServer: provided.graphqlServer || "",
216
+ typescript: {
217
+ enabled: !!provided.typescript?.enabled,
218
+ schemaOptions: provided.typescript?.schemaOptions,
219
+ resolvedQueriesPath: provided.typescript?.resolvedQueriesPath || provided.outputPath || "",
220
+ schemaOutputPath: provided.typescript?.schemaOutputPath || "~/schema",
221
+ typesOutputPath: provided.typescript?.typesOutputPath || "~/types",
222
+ skipSchemaDownload: !!provided.typescript?.skipSchemaDownload
223
+ },
224
+ endpointNamespace: provided.endpointNamespace || "/__graphql_middleware",
225
+ debug: provided.debug || options.dev,
226
+ queries: provided.queries || {},
227
+ mutations: provided.mutations || {},
228
+ outputPath: provided.outputPath || "",
229
+ server: provided.server,
230
+ plugin: {
231
+ enabled: !!provided.plugin?.enabled,
232
+ port: 4e3,
233
+ cacheInBrowser: !!provided.plugin?.cacheInBrowser,
234
+ cacheInServer: !!provided.plugin?.cacheInServer
235
+ }
236
+ };
237
+ if (config.plugin?.enabled) {
238
+ this.addPlugin({
239
+ filename: "graphqlMiddleware.js",
240
+ src: PLUGIN_PATH,
241
+ options: {
242
+ namespace: config.endpointNamespace,
243
+ port: PORT,
244
+ cacheInBrowser: config.plugin?.cacheInBrowser ? "true" : "false",
245
+ cacheInServer: config.plugin?.cacheInServer ? "true" : "false"
246
+ }
247
+ });
248
+ }
249
+ const fileMap = new Map();
250
+ const queries = new Map();
251
+ const mutations = new Map();
252
+ const outputPath = config.outputPath ? resolver(config.outputPath) : "";
253
+ await mkdirp(outputPath);
254
+ const schemaOutputPath = resolver(config.typescript?.schemaOutputPath);
255
+ const typesOutputPath = resolver(config.typescript?.typesOutputPath);
256
+ const { generateSchema, generateTypes } = codegen(config.graphqlServer, {
257
+ resolvedQueriesPath: config.outputPath,
258
+ schemaOptions: config.typescript?.schemaOptions,
259
+ skipSchemaDownload: config.typescript?.skipSchemaDownload,
260
+ schemaOutputPath,
261
+ typesOutputPath
262
+ });
263
+ if (config.typescript?.enabled) {
264
+ if (!outputPath) {
265
+ throw new Error("TypeScript enabled, but no outputPath given.");
266
+ }
267
+ await mkdirp(schemaOutputPath);
268
+ await generateSchema();
269
+ }
270
+ function build() {
271
+ logger.log("Building GraphQL files...");
272
+ return Promise.all([
273
+ resolveGraphql(config.queries, queries, resolver, fileMap, FileType.Query, outputPath),
274
+ resolveGraphql(config.mutations, mutations, resolver, fileMap, FileType.Mutation, outputPath)
275
+ ]).then(() => {
276
+ logger.success("Finished building GraphQL files");
277
+ if (config.typescript?.enabled) {
278
+ return generateTypes().then(() => {
279
+ logger.success("Finished generating GraphQL TypeScript files.");
280
+ });
281
+ }
282
+ });
283
+ }
284
+ function watchFiles() {
285
+ const ignored = ["node_modules", ".nuxt"];
286
+ if (config.outputPath) {
287
+ ignored.push(config.outputPath);
288
+ }
289
+ const filesWatcher = chokidar.watch("./**/*.graphql", {
290
+ ignoreInitial: true,
291
+ ignored
292
+ });
293
+ if (filesWatcher) {
294
+ logger.info("Watching for query changes");
295
+ filesWatcher.on("change", () => {
296
+ build();
297
+ });
298
+ }
299
+ return filesWatcher;
300
+ }
301
+ let watcher;
302
+ if (this.nuxt.options.dev) {
303
+ this.nuxt.hook("build:done", () => {
304
+ watcher = watchFiles();
305
+ });
306
+ this.nuxt.hook("close", () => {
307
+ if (watcher) {
308
+ watcher.close();
309
+ watcher = void 0;
310
+ }
311
+ });
312
+ }
313
+ build().then(() => {
314
+ if (options.debug) {
315
+ logger.info("Available queries and mutations:");
316
+ console.table(Array.from(fileMap.entries()).map(([_key, value]) => value));
317
+ }
318
+ });
319
+ this.addServerMiddleware({
320
+ path: config.endpointNamespace,
321
+ handler: createServerMiddleware(config.graphqlServer, queries, mutations, config.server)
322
+ });
323
+ };
324
+
325
+ export { graphqlMiddleware as default };
@@ -4,7 +4,7 @@ function log(action, path, message) {
4
4
  console.log(`[API - ${action}] ${message}: ${path}`);
5
5
  }
6
6
  }
7
- export class GraphqlMiddlewarePlugin {
7
+ class GraphqlMiddlewarePlugin {
8
8
  constructor(baseURL, headers, useCache, context) {
9
9
  this.baseURL = baseURL;
10
10
  this.headers = headers || {};
@@ -21,6 +21,11 @@ export class GraphqlMiddlewarePlugin {
21
21
  beforeRequest(fn) {
22
22
  this.beforeRequestFn = fn;
23
23
  }
24
+ clearCache() {
25
+ if (this.cache) {
26
+ this.cache.clear();
27
+ }
28
+ }
24
29
  query(name, variables, headers = {}) {
25
30
  const params = new URLSearchParams({
26
31
  name,
@@ -32,7 +37,7 @@ export class GraphqlMiddlewarePlugin {
32
37
  return Promise.resolve(this.cache.get(url));
33
38
  }
34
39
  log("query", url, "Fetching");
35
- let fetchOptions = {
40
+ const defaultOptions = {
36
41
  method: "GET",
37
42
  credentials: "include",
38
43
  headers: {
@@ -42,23 +47,28 @@ export class GraphqlMiddlewarePlugin {
42
47
  ...this.getPluginHeaderValue()
43
48
  }
44
49
  };
45
- if (this.beforeRequestFn) {
46
- fetchOptions = this.beforeRequestFn(this.context, fetchOptions);
47
- }
48
- return fetch(url, fetchOptions).then((response) => {
49
- if (response.ok) {
50
- return response.json();
51
- }
52
- throw new Error("Server Error");
53
- }).then((data) => {
54
- if (this.cache && this.cache.size > 30) {
55
- const key = this.cache.keys().next().value;
56
- this.cache.delete(key);
57
- }
58
- this.cache?.set(url, data);
59
- return data;
50
+ return this.doBeforeRequest(defaultOptions).then((fetchOptions) => {
51
+ return fetch(url, fetchOptions).then((response) => {
52
+ if (response.ok) {
53
+ return response.json();
54
+ }
55
+ throw new Error("Server Error");
56
+ }).then((data) => {
57
+ if (this.cache && this.cache.size > 30) {
58
+ const key = this.cache.keys().next().value;
59
+ this.cache.delete(key);
60
+ }
61
+ this.cache?.set(url, data);
62
+ return data;
63
+ });
60
64
  });
61
65
  }
66
+ doBeforeRequest(fetchOptions) {
67
+ if (!this.beforeRequestFn) {
68
+ return Promise.resolve(fetchOptions);
69
+ }
70
+ return Promise.resolve(this.beforeRequestFn(this.context, fetchOptions));
71
+ }
62
72
  mutate(name, variables, headers = {}) {
63
73
  const params = new URLSearchParams({
64
74
  name
@@ -80,3 +90,18 @@ export class GraphqlMiddlewarePlugin {
80
90
  return fetch(this.baseURL + "/mutate?" + params.toString(), fetchOptions).then((response) => response.json());
81
91
  }
82
92
  }
93
+ const graphqlMiddlewarePlugin = (context, inject) => {
94
+ const namespace = "<%= options.namespace || '' %>";
95
+ const port = process?.env?.NUXT_PORT || "<%= options.port %>";
96
+ const cacheInBrowser = false;
97
+ const cacheInServer = false;
98
+ let baseURL = namespace;
99
+ if (process.server) {
100
+ baseURL = "http://localhost:" + port + namespace;
101
+ }
102
+ const useCache = process.server && cacheInServer || process.client && cacheInBrowser;
103
+ const plugin = new GraphqlMiddlewarePlugin(baseURL, context.req?.headers, useCache, context);
104
+ inject("graphql", plugin);
105
+ };
106
+
107
+ export { GraphqlMiddlewarePlugin, graphqlMiddlewarePlugin as default };
@@ -0,0 +1,13 @@
1
+ export interface GraphqlMiddlewareCodegenConfig {
2
+ enabled?: boolean;
3
+ skipSchemaDownload?: boolean;
4
+ resolvedQueriesPath: string;
5
+ schemaOutputPath: string;
6
+ typesOutputPath: string;
7
+ schemaOptions: any;
8
+ }
9
+ export default function (graphqlServer: string, options: GraphqlMiddlewareCodegenConfig): {
10
+ generateSchema: () => Promise<any>;
11
+ generateTypes: () => Promise<any>;
12
+ };
13
+ //# sourceMappingURL=codegen.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../../src/codegen.ts"],"names":[],"mappings":"AAsBA,MAAM,WAAW,8BAA8B;IAC7C,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAC5B,mBAAmB,EAAE,MAAM,CAAA;IAC3B,gBAAgB,EAAE,MAAM,CAAA;IACxB,eAAe,EAAE,MAAM,CAAA;IACvB,aAAa,EAAE,GAAG,CAAA;CACnB;AAED,MAAM,CAAC,OAAO,WACZ,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,8BAA8B;;;EAoDxC"}
@@ -0,0 +1,2 @@
1
+ export default function (path: string, resolver: any): any;
2
+ //# sourceMappingURL=graphqlImport.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"graphqlImport.d.ts","sourceRoot":"","sources":["../../src/graphqlImport.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,WAAW,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,OASnD"}
@@ -0,0 +1,2 @@
1
+ export * from './module';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAA"}
@@ -0,0 +1,18 @@
1
+ import { Module } from '@nuxt/types';
2
+ import { GraphqlMiddlewarePluginConfig } from './templates/plugin';
3
+ import { GraphqlServerMiddlewareConfig } from './serverMiddleware';
4
+ import { GraphqlMiddlewareCodegenConfig } from './codegen';
5
+ export interface GraphqlMiddlewareConfig {
6
+ graphqlServer: string;
7
+ typescript?: GraphqlMiddlewareCodegenConfig;
8
+ endpointNamespace?: string;
9
+ debug: boolean;
10
+ queries: Record<string, string>;
11
+ mutations: Record<string, string>;
12
+ outputPath: string;
13
+ plugin?: GraphqlMiddlewarePluginConfig;
14
+ server?: GraphqlServerMiddlewareConfig;
15
+ }
16
+ declare const graphqlMiddleware: Module;
17
+ export default graphqlMiddleware;
18
+ //# sourceMappingURL=module.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"module.d.ts","sourceRoot":"","sources":["../../src/module.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAEpC,OAAO,EAAE,6BAA6B,EAAE,MAAM,oBAAoB,CAAA;AAClE,OAAyB,EACvB,6BAA6B,EAC9B,MAAM,oBAAoB,CAAA;AAE3B,OAAgB,EAAE,8BAA8B,EAAE,MAAM,WAAW,CAAA;AAMnE,MAAM,WAAW,uBAAuB;IACtC,aAAa,EAAE,MAAM,CAAA;IACrB,UAAU,CAAC,EAAE,8BAA8B,CAAA;IAC3C,iBAAiB,CAAC,EAAE,MAAM,CAAA;IAC1B,KAAK,EAAE,OAAO,CAAA;IACd,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC/B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjC,UAAU,EAAE,MAAM,CAAA;IAClB,MAAM,CAAC,EAAE,6BAA6B,CAAA;IACtC,MAAM,CAAC,EAAE,6BAA6B,CAAA;CACvC;AAiED,QAAA,MAAM,iBAAiB,EAAE,MAgKxB,CAAA;AAED,eAAe,iBAAiB,CAAA"}
@@ -0,0 +1,13 @@
1
+ import { Request, RequestHandler } from 'express';
2
+ export interface GraphqlServerMiddlewareConfig {
3
+ middleware?: RequestHandler;
4
+ fetchOptions?: any;
5
+ buildHeaders?: (req: Request, name: string, type: string) => any;
6
+ buildEndpoint?: (req: Request) => string;
7
+ onQueryResponse?: any;
8
+ onQueryError?: any;
9
+ onMutationResponse?: any;
10
+ onMutationError?: any;
11
+ }
12
+ export default function createServerMiddleware(graphqlServer: string, queries: Map<string, any>, mutations: Map<string, any>, config?: GraphqlServerMiddlewareConfig): import("express-serve-static-core").Express;
13
+ //# sourceMappingURL=serverMiddleware.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"serverMiddleware.d.ts","sourceRoot":"","sources":["../../src/serverMiddleware.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAE,OAAO,EAAE,cAAc,EAAY,MAAM,SAAS,CAAA;AAcpE,MAAM,WAAW,6BAA6B;IAC5C,UAAU,CAAC,EAAE,cAAc,CAAA;IAC3B,YAAY,CAAC,EAAE,GAAG,CAAA;IAClB,YAAY,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,GAAG,CAAA;IAChE,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,MAAM,CAAA;IACxC,eAAe,CAAC,EAAE,GAAG,CAAA;IACrB,YAAY,CAAC,EAAE,GAAG,CAAA;IAClB,kBAAkB,CAAC,EAAE,GAAG,CAAA;IACxB,eAAe,CAAC,EAAE,GAAG,CAAA;CACtB;AAkBD,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAC5C,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,EACzB,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,EAC3B,MAAM,CAAC,EAAE,6BAA6B,+CA0FvC"}
@@ -1,6 +1,7 @@
1
- import { Context } from '@nuxt/types';
1
+ import { Context, Plugin } from '@nuxt/types';
2
2
  export interface GraphqlMiddlewarePluginConfig {
3
3
  enabled?: boolean;
4
+ port?: number;
4
5
  cacheInBrowser?: boolean;
5
6
  cacheInServer?: boolean;
6
7
  }
@@ -15,12 +16,11 @@ export declare class GraphqlMiddlewarePlugin {
15
16
  'Nuxt-Graphql-Middleware-Route': any;
16
17
  };
17
18
  beforeRequest(fn: Function): void;
18
- /**
19
- * Perform a GraphQL query via the middleware.
20
- */
19
+ clearCache(): void;
21
20
  query(name: string, variables?: any, headers?: any): Promise<any>;
22
- /**
23
- * Perform a GraphQL mutation via the middleware.
24
- */
21
+ doBeforeRequest(fetchOptions: Record<string, any>): Promise<any>;
25
22
  mutate(name: string, variables?: any, headers?: any): Promise<any>;
26
23
  }
24
+ declare const graphqlMiddlewarePlugin: Plugin;
25
+ export default graphqlMiddlewarePlugin;
26
+ //# sourceMappingURL=plugin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../../src/templates/plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAW7C,MAAM,WAAW,6BAA6B;IAC5C,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,aAAa,CAAC,EAAE,OAAO,CAAA;CACxB;AAED,qBAAa,uBAAuB;IAClC,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,GAAG,CAAA;IACZ,eAAe,EAAE,QAAQ,GAAG,SAAS,CAAA;IACrC,KAAK,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACxB,OAAO,EAAE,GAAG,CAAA;gBAGV,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,GAAG,EACZ,QAAQ,EAAE,OAAO,EACjB,OAAO,EAAE,OAAO;IAUlB,oBAAoB;;;IAMpB,aAAa,CAAC,EAAE,EAAE,QAAQ;IAI1B,UAAU;IASV,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,GAAG,EAAE,OAAO,GAAE,GAAQ;IA+CtD,eAAe,CAAC,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;IAWjD,MAAM,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,GAAG,EAAE,OAAO,GAAE,GAAQ;CAuBxD;AAED,QAAA,MAAM,uBAAuB,EAAE,MAyB9B,CAAA;AAED,eAAe,uBAAuB,CAAA"}
package/module.cjs ADDED
@@ -0,0 +1,6 @@
1
+ // CommonJS proxy to bypass jiti transforms from nuxt 2 and using native ESM
2
+ module.exports = function(...args) {
3
+ return import('./dist/module.mjs').then(m => m.default.call(this, ...args))
4
+ }
5
+
6
+ module.exports.meta = require('./package.json')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "2.0.1",
3
+ "version": "2.1.1",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,32 +12,44 @@
12
12
  "email": "me@dulnan.net",
13
13
  "url": "https://dulnan.net"
14
14
  },
15
- "main": "dist/module.js",
16
- "types": "dist/types.d.ts",
17
- "typings": "./index.d.ts",
15
+ "exports": {
16
+ ".": {
17
+ "import": "./dist/module.mjs",
18
+ "require": "./module.cjs"
19
+ },
20
+ "./dist/*": "./dist/*"
21
+ },
22
+ "main": "./module.cjs",
23
+ "types": "./types/index.d.ts",
18
24
  "files": [
19
- "dist/",
20
- "templates/"
25
+ "types/*",
26
+ "module.cjs",
27
+ "dist/*.js",
28
+ "dist/*.mjs",
29
+ "dist/*.d.ts",
30
+ "dist/types/*"
21
31
  ],
22
32
  "scripts": {
23
- "build": "siroc build && mkdist --src src/runtime --dist dist/runtime -d --ext js",
24
- "dev": "siroc build --watch"
33
+ "build": "unbuild && npm run generate-types",
34
+ "dev": "unbuild --stub=true",
35
+ "generate-types": "tsc -p tsconfig.json --emitDeclarationOnly",
36
+ "plain-ts": "tsc -p tsconfig.json && tsc -p tsconfig-cjs.json"
25
37
  },
26
38
  "dependencies": {
27
- "@graphql-codegen/cli": "^2.2.0",
28
- "@graphql-codegen/schema-ast": "^2.2.0",
29
- "@graphql-codegen/typescript": "^2.2.2",
30
- "@graphql-codegen/typescript-operations": "^2.1.4",
39
+ "@graphql-codegen/cli": "^2.3.0",
40
+ "@graphql-codegen/schema-ast": "^2.4.0",
41
+ "@graphql-codegen/typescript": "^2.4.1",
42
+ "@graphql-codegen/typescript-operations": "^2.2.1",
31
43
  "@graphql-fragment-import/lib": "^1.2.0",
32
44
  "express": "^4.17.1",
33
- "graphql": "^15.6.0",
34
- "graphql-request": "^3.5.0"
45
+ "graphql": "^16.0.1",
46
+ "graphql-request": "^3.6.1"
35
47
  },
36
48
  "devDependencies": {
37
49
  "@nuxt/types": "^2.15.8",
38
50
  "@nuxtjs/eslint-config-typescript": "latest",
39
51
  "@types/mkdirp": "^1.0.2",
40
- "@types/node": "^16.9.6",
52
+ "@types/node": "^16.11.10",
41
53
  "consola": "^2.15.3",
42
54
  "eslint": "latest",
43
55
  "eslint-config-prettier": "latest",
@@ -45,9 +57,9 @@
45
57
  "mkdirp": "^1.0.4",
46
58
  "mkdist": "latest",
47
59
  "nuxt": "^2.15.8",
48
- "prettier": "^2.4.1",
49
- "siroc": "^0.16.0",
50
- "typescript": "^4.4.3",
60
+ "prettier": "^2.5.0",
61
+ "typescript": "^4.5.2",
62
+ "unbuild": "^0.5.13",
51
63
  "vue": "^2.6.14"
52
64
  }
53
65
  }
@@ -0,0 +1,32 @@
1
+ import { GraphqlMiddlewarePlugin } from '../dist/types/templates/plugin'
2
+ import { GraphqlMiddlewareConfig } from '../dist/types/module'
3
+
4
+ declare module 'vue/types/vue' {
5
+ interface Vue {
6
+ readonly $graphql: GraphqlMiddlewarePlugin
7
+ }
8
+ }
9
+
10
+ declare module 'vuex/types/index' {
11
+ interface Store<S> {
12
+ readonly $graphql: GraphqlMiddlewarePlugin
13
+ }
14
+ }
15
+
16
+ declare module '@nuxt/types' {
17
+ interface NuxtAppOptions {
18
+ readonly $graphql: GraphqlMiddlewarePlugin
19
+ }
20
+ interface Context {
21
+ readonly $graphql: GraphqlMiddlewarePlugin
22
+ }
23
+ interface Configuration {
24
+ graphqlMiddleware?: GraphqlMiddlewareConfig
25
+ }
26
+ }
27
+
28
+ declare module '@nuxt/schema' {
29
+ interface NuxtConfig {
30
+ graphqlMiddleware?: GraphqlMiddlewareConfig
31
+ }
32
+ }