nuxt-graphql-middleware 2.0.3 → 2.1.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.
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:
package/dist/module.mjs CHANGED
@@ -146,7 +146,7 @@ function codegen(graphqlServer, options) {
146
146
  plugins: [{ "schema-ast": configSchemaAst }],
147
147
  config: configSchemaAst
148
148
  },
149
- [path.resolve(options.typesOutputPath, "graphql-schema.d.ts")]: {
149
+ [path.resolve(options.typesOutputPath, "graphql-schema.ts")]: {
150
150
  plugins: [{ typescript: typescriptConfig }],
151
151
  config: typescriptConfig
152
152
  }
@@ -156,14 +156,15 @@ function codegen(graphqlServer, options) {
156
156
  function generateTypes() {
157
157
  const config = {
158
158
  ...typescriptConfig,
159
- onlyOperationTypes: true
159
+ onlyOperationTypes: true,
160
+ ...options.schemaOptions || {}
160
161
  };
161
162
  return generate({
162
163
  schema: schemaPath,
163
164
  pluginLoader,
164
165
  documents: path.resolve(options.resolvedQueriesPath, "./*.graphql"),
165
166
  generates: {
166
- [path.resolve(options.typesOutputPath, "graphql-operations.d.ts")]: {
167
+ [path.resolve(options.typesOutputPath, "graphql-operations.ts")]: {
167
168
  plugins: ["typescript", { "typescript-operations": config }],
168
169
  config
169
170
  }
package/dist/plugin.mjs CHANGED
@@ -21,6 +21,11 @@ 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 @@ 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,28 +47,33 @@ 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
65
75
  });
66
- let fetchOptions = {
76
+ let defaultOptions = {
67
77
  method: "POST",
68
78
  credentials: "include",
69
79
  headers: {
@@ -74,10 +84,9 @@ class GraphqlMiddlewarePlugin {
74
84
  },
75
85
  body: JSON.stringify(variables)
76
86
  };
77
- if (this.beforeRequestFn) {
78
- fetchOptions = this.beforeRequestFn(this.context, fetchOptions);
79
- }
80
- return fetch(this.baseURL + "/mutate?" + params.toString(), fetchOptions).then((response) => response.json());
87
+ return this.doBeforeRequest(defaultOptions).then((fetchOptions) => {
88
+ return fetch(this.baseURL + "/mutate?" + params.toString(), fetchOptions).then((response) => response.json());
89
+ });
81
90
  }
82
91
  }
83
92
  const graphqlMiddlewarePlugin = (context, inject) => {
@@ -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"}
@@ -0,0 +1,26 @@
1
+ import { Context, Plugin } from '@nuxt/types';
2
+ export interface GraphqlMiddlewarePluginConfig {
3
+ enabled?: boolean;
4
+ port?: number;
5
+ cacheInBrowser?: boolean;
6
+ cacheInServer?: boolean;
7
+ }
8
+ export declare class GraphqlMiddlewarePlugin {
9
+ baseURL: string;
10
+ headers: any;
11
+ beforeRequestFn: Function | undefined;
12
+ cache?: Map<string, any>;
13
+ context: any;
14
+ constructor(baseURL: string, headers: any, useCache: boolean, context: Context);
15
+ getPluginHeaderValue(): {
16
+ 'Nuxt-Graphql-Middleware-Route': any;
17
+ };
18
+ beforeRequest(fn: Function): void;
19
+ clearCache(): void;
20
+ query(name: string, variables?: any, headers?: any): Promise<any>;
21
+ doBeforeRequest(fetchOptions: Record<string, any>): Promise<any>;
22
+ mutate(name: string, variables?: any, headers?: any): Promise<any>;
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "2.0.3",
3
+ "version": "2.1.2",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,7 +26,8 @@
26
26
  "module.cjs",
27
27
  "dist/*.js",
28
28
  "dist/*.mjs",
29
- "dist/*.d.ts"
29
+ "dist/*.d.ts",
30
+ "dist/types/*"
30
31
  ],
31
32
  "scripts": {
32
33
  "build": "unbuild && npm run generate-types",
@@ -41,14 +42,14 @@
41
42
  "@graphql-codegen/typescript-operations": "^2.2.1",
42
43
  "@graphql-fragment-import/lib": "^1.2.0",
43
44
  "express": "^4.17.1",
44
- "graphql": "^15.6.0",
45
+ "graphql": "^16.0.1",
45
46
  "graphql-request": "^3.6.1"
46
47
  },
47
48
  "devDependencies": {
48
49
  "@nuxt/types": "^2.15.8",
49
50
  "@nuxtjs/eslint-config-typescript": "latest",
50
51
  "@types/mkdirp": "^1.0.2",
51
- "@types/node": "^16.11.9",
52
+ "@types/node": "^16.11.10",
52
53
  "consola": "^2.15.3",
53
54
  "eslint": "latest",
54
55
  "eslint-config-prettier": "latest",
@@ -56,7 +57,7 @@
56
57
  "mkdirp": "^1.0.4",
57
58
  "mkdist": "latest",
58
59
  "nuxt": "^2.15.8",
59
- "prettier": "^2.4.1",
60
+ "prettier": "^2.5.0",
60
61
  "typescript": "^4.5.2",
61
62
  "unbuild": "^0.5.13",
62
63
  "vue": "^2.6.14"
package/types/index.d.ts CHANGED
@@ -24,3 +24,9 @@ declare module '@nuxt/types' {
24
24
  graphqlMiddleware?: GraphqlMiddlewareConfig
25
25
  }
26
26
  }
27
+
28
+ declare module '@nuxt/schema' {
29
+ interface NuxtConfig {
30
+ graphqlMiddleware?: GraphqlMiddlewareConfig
31
+ }
32
+ }