nuxt-graphql-middleware 2.1.0 → 2.1.3

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
@@ -90,7 +90,7 @@ function createServerMiddleware(graphqlServer, queries, mutations, config) {
90
90
  const headers = buildHeaders(req, name, "mutation", config);
91
91
  const endpoint = getEndpoint(req);
92
92
  const client = getClient(endpoint);
93
- const response = await client.request(mutation, req.body, headers);
93
+ const response = await client.rawRequest(mutation, req.body, headers);
94
94
  if (config?.onMutationResponse) {
95
95
  return config.onMutationResponse(response, req, res);
96
96
  }
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,23 +47,28 @@ 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
@@ -16,7 +16,9 @@ export declare class GraphqlMiddlewarePlugin {
16
16
  'Nuxt-Graphql-Middleware-Route': any;
17
17
  };
18
18
  beforeRequest(fn: Function): void;
19
+ clearCache(): void;
19
20
  query(name: string, variables?: any, headers?: any): Promise<any>;
21
+ doBeforeRequest(fetchOptions: Record<string, any>): Promise<any>;
20
22
  mutate(name: string, variables?: any, headers?: any): Promise<any>;
21
23
  }
22
24
  declare const graphqlMiddlewarePlugin: Plugin;
@@ -1 +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,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O,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.1.0",
3
+ "version": "2.1.3",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",