nuxt-graphql-middleware 3.1.0-beta.0 → 3.1.0-beta.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/dist/module.d.ts CHANGED
@@ -1,13 +1,110 @@
1
- import { NuxtModule } from '@nuxt/schema';
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+ import { TypeScriptDocumentsPluginConfig } from '@graphql-codegen/typescript-operations';
2
3
  import { H3Event } from 'h3';
3
4
  import { FetchOptions, FetchResponse, FetchError } from 'ofetch';
4
- import { TypeScriptDocumentsPluginConfig } from '@graphql-codegen/typescript-operations';
5
5
 
6
6
  type GraphqlMiddlewareGraphqlEndpointMethod = (event?: H3Event, operation?: string, operationName?: string) => string | Promise<string> | void;
7
7
  type GraphqlMiddlewareServerFetchOptionsMethod = (event?: H3Event, operation?: string, operationName?: string) => FetchOptions | Promise<FetchOptions>;
8
8
  type GraphqlMiddlewareOnServerResponseMethod = (event: H3Event, response: FetchResponse<any>, operation?: string, operationName?: string) => any | Promise<any>;
9
9
  type GraphqlMiddlewareOnServerErrorMethod = (event: H3Event, error: FetchError, operation?: string, operationName?: string) => any | Promise<any>;
10
- interface GraphqlMiddlewareConfig {
10
+ /**
11
+ * Configuration options during runtime.
12
+ */
13
+ type GraphqlMiddlewareServerOptions = {
14
+ /**
15
+ * Custom callback to return the GraphQL endpoint per request.
16
+ *
17
+ * @default undefined
18
+ *
19
+ * @example
20
+ * ```ts
21
+ * function graphqlEndpoint(event, operation, operationName) {
22
+ * const language = getLanguageFromRequest(event)
23
+ * return `https://api.example.com/${language}/graphql`
24
+ * }
25
+ * ```
26
+ */
27
+ graphqlEndpoint?: GraphqlMiddlewareGraphqlEndpointMethod;
28
+ /**
29
+ * Provide the options for the ofetch request to the GraphQL server.
30
+ *
31
+ * @default undefined
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { getHeader } from 'h3'
36
+ *
37
+ * // Pass the cookie from the client request to the GraphQL request.
38
+ * function serverFetchOptions(event, operation, operationName) {
39
+ * return {
40
+ * headers: {
41
+ * Cookie: getHeader(event, 'cookie')
42
+ * }
43
+ * }
44
+ * }
45
+ * ```
46
+ */
47
+ serverFetchOptions?: GraphqlMiddlewareServerFetchOptionsMethod;
48
+ /**
49
+ * Handle the response from the GraphQL server.
50
+ *
51
+ * You can alter the response, add additional properties to the data, get
52
+ * and set headers, etc.
53
+ *
54
+ * ```ts
55
+ * import type { H3Event } from 'h3'
56
+ * import type { FetchResponse } from 'ofetch'
57
+ *
58
+ * function onServerResponse(event: H3Event, graphqlResponse: FetchResponse) {
59
+ * // Set a static header.
60
+ * event.node.res.setHeader('x-nuxt-custom-header', 'A custom header value')
61
+ *
62
+ * // Pass the set-cookie header from the GraphQL response to the client.
63
+ * const setCookie = graphqlResponse.headers.get('set-cookie')
64
+ * if (setCookie) {
65
+ * event.node.res.setHeader('set-cookie', setCookie)
66
+ * }
67
+ *
68
+ * // Add additional properties to the response.
69
+ * graphqlResponse._data.__customProperty = ['My', 'values']
70
+ *
71
+ * // Return the GraphQL response.
72
+ * return graphqlResponse._data
73
+ * }
74
+ * ```
75
+ */
76
+ onServerResponse?: GraphqlMiddlewareOnServerResponseMethod;
77
+ /**
78
+ * Handle a fetch error from the GraphQL request.
79
+ *
80
+ * Note that errors are only thrown for responses that are not status
81
+ * 200-299. See https://github.com/unjs/ofetch#%EF%B8%8F-handling-errors for
82
+ * more information.
83
+ *
84
+ * ```ts
85
+ * import { createError } from 'h3'
86
+ * import type { H3Event } from 'h3'
87
+ * import type { FetchError } from 'ofetch'
88
+ *
89
+ * function onServerError(
90
+ * event: H3Event,
91
+ * error: FetchError,
92
+ * operation: string,
93
+ * operationName: string,
94
+ * ) {
95
+ * // Throw a h3 error.
96
+ * throw createError({
97
+ * statusCode: 500,
98
+ * statusMessage: `Couldn't execute GraphQL ${operation} "${operationName}".`,
99
+ * data: error.message
100
+ * })
101
+ * }
102
+ * ```
103
+ */
104
+ onServerError?: GraphqlMiddlewareOnServerErrorMethod;
105
+ };
106
+
107
+ interface ModuleOptions {
11
108
  /**
12
109
  * File glob patterns for the auto import feature.
13
110
  *
@@ -116,107 +213,7 @@ interface GraphqlMiddlewareConfig {
116
213
  */
117
214
  codegenConfig?: TypeScriptDocumentsPluginConfig;
118
215
  }
119
- /**
120
- * Configuration options during runtime.
121
- */
122
- type GraphqlMiddlewareServerOptions = {
123
- /**
124
- * Custom callback to return the GraphQL endpoint per request.
125
- *
126
- * @default undefined
127
- *
128
- * @example
129
- * ```ts
130
- * function graphqlEndpoint(event, operation, operationName) {
131
- * const language = getLanguageFromRequest(event)
132
- * return `https://api.example.com/${language}/graphql`
133
- * }
134
- * ```
135
- */
136
- graphqlEndpoint?: GraphqlMiddlewareGraphqlEndpointMethod;
137
- /**
138
- * Provide the options for the ofetch request to the GraphQL server.
139
- *
140
- * @default undefined
141
- *
142
- * @example
143
- * ```ts
144
- * import { getHeader } from 'h3'
145
- *
146
- * // Pass the cookie from the client request to the GraphQL request.
147
- * function serverFetchOptions(event, operation, operationName) {
148
- * return {
149
- * headers: {
150
- * Cookie: getHeader(event, 'cookie')
151
- * }
152
- * }
153
- * }
154
- * ```
155
- */
156
- serverFetchOptions?: GraphqlMiddlewareServerFetchOptionsMethod;
157
- /**
158
- * Handle the response from the GraphQL server.
159
- *
160
- * You can alter the response, add additional properties to the data, get
161
- * and set headers, etc.
162
- *
163
- * ```ts
164
- * import type { H3Event } from 'h3'
165
- * import type { FetchResponse } from 'ofetch'
166
- *
167
- * function onServerResponse(event: H3Event, graphqlResponse: FetchResponse) {
168
- * // Set a static header.
169
- * event.node.res.setHeader('x-nuxt-custom-header', 'A custom header value')
170
- *
171
- * // Pass the set-cookie header from the GraphQL response to the client.
172
- * const setCookie = graphqlResponse.headers.get('set-cookie')
173
- * if (setCookie) {
174
- * event.node.res.setHeader('set-cookie', setCookie)
175
- * }
176
- *
177
- * // Add additional properties to the response.
178
- * graphqlResponse._data.__customProperty = ['My', 'values']
179
- *
180
- * // Return the GraphQL response.
181
- * return graphqlResponse._data
182
- * }
183
- * ```
184
- */
185
- onServerResponse?: GraphqlMiddlewareOnServerResponseMethod;
186
- /**
187
- * Handle a fetch error from the GraphQL request.
188
- *
189
- * Note that errors are only thrown for responses that are not status
190
- * 200-299. See https://github.com/unjs/ofetch#%EF%B8%8F-handling-errors for
191
- * more information.
192
- *
193
- * ```ts
194
- * import { createError } from 'h3'
195
- * import type { H3Event } from 'h3'
196
- * import type { FetchError } from 'ofetch'
197
- *
198
- * function onServerError(
199
- * event: H3Event,
200
- * error: FetchError,
201
- * operation: string,
202
- * operationName: string,
203
- * ) {
204
- * // Throw a h3 error.
205
- * throw createError({
206
- * statusCode: 500,
207
- * statusMessage: `Couldn't execute GraphQL ${operation} "${operationName}".`,
208
- * data: error.message
209
- * })
210
- * }
211
- * ```
212
- */
213
- onServerError?: GraphqlMiddlewareOnServerErrorMethod;
214
- };
215
-
216
- type ModuleOptions = GraphqlMiddlewareConfig;
217
216
  type ModuleHooks = {};
218
- declare const _default: NuxtModule<GraphqlMiddlewareConfig>;
219
-
220
- declare function defineGraphqlServerOptions(options: GraphqlMiddlewareServerOptions): GraphqlMiddlewareServerOptions;
217
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
221
218
 
222
- export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, _default as default, defineGraphqlServerOptions };
219
+ export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, _default as default };
package/dist/module.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
3
  "configKey": "graphqlMiddleware",
4
- "version": "3.1.0-beta.0",
4
+ "version": "3.1.0-beta.2",
5
5
  "compatibility": {
6
- "nuxt": "^3.0.0"
6
+ "nuxt": "^3.1.0"
7
7
  }
8
8
  }
package/dist/module.mjs CHANGED
@@ -17,7 +17,7 @@ import { oldVisit } from '@graphql-codegen/plugin-helpers';
17
17
  import { pascalCase } from 'change-case-all';
18
18
 
19
19
  const name = "nuxt-graphql-middleware";
20
- const version = "3.1.0-beta.0";
20
+ const version = "3.1.0-beta.2";
21
21
 
22
22
  var GraphqlMiddlewareTemplate = /* @__PURE__ */ ((GraphqlMiddlewareTemplate2) => {
23
23
  GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations.d.ts";
@@ -255,7 +255,7 @@ function validateDeprecated(options) {
255
255
  );
256
256
  if (key === "graphqlEndpoint") {
257
257
  logger.info(`
258
- import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware'
258
+ import { defineGraphqlServerOptions } from '#graphql-server-options'
259
259
  import { getHeader } from 'h3'
260
260
  import acceptLanguageParser from 'accept-language-parser';
261
261
 
@@ -269,11 +269,11 @@ export default defineGraphqlServerOptions({
269
269
  const language = languages[0]?.code || 'en'
270
270
  return \`https://api.example.com/\${language}/graphql\`
271
271
  }
272
- }`);
272
+ })`);
273
273
  }
274
274
  if (key === "serverFetchOptions") {
275
275
  logger.info(`
276
- import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware'
276
+ import { defineGraphqlServerOptions } from '#graphql-server-options'
277
277
  import { getHeader } from 'h3'
278
278
 
279
279
  // Pass the cookie from the client request to the GraphQL request.
@@ -285,11 +285,11 @@ export default defineGraphqlServerOptions({
285
285
  }
286
286
  }
287
287
  }
288
- }`);
288
+ })`);
289
289
  }
290
290
  if (key === "onServerResponse") {
291
291
  logger.info(`
292
- import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware'
292
+ import { defineGraphqlServerOptions } from '#graphql-server-options'
293
293
  import type { H3Event } from 'h3'
294
294
  import type { FetchResponse } from 'ofetch'
295
295
 
@@ -310,11 +310,11 @@ export default defineGraphqlServerOptions({
310
310
  // Return the GraphQL response.
311
311
  return graphqlResponse._data
312
312
  }
313
- }`);
313
+ })`);
314
314
  }
315
315
  if (key === "onServerError") {
316
316
  logger.info(`
317
- import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware'
317
+ import { defineGraphqlServerOptions } from '#graphql-server-options'
318
318
  import type { H3Event } from 'h3'
319
319
  import type { FetchError } from 'ofetch'
320
320
 
@@ -326,8 +326,7 @@ export default defineGraphqlServerOptions({
326
326
  errors: [error.message]
327
327
  }
328
328
  }
329
- })
330
- `);
329
+ })`);
331
330
  }
332
331
  throw new TypeError("Invalid configuration for graphqlMiddleware." + key);
333
332
  }
@@ -495,7 +494,7 @@ const module = defineNuxtModule({
495
494
  configKey: "graphqlMiddleware",
496
495
  version,
497
496
  compatibility: {
498
- nuxt: "^3.0.0"
497
+ nuxt: "^3.1.0"
499
498
  }
500
499
  },
501
500
  defaults: defaultOptions,
@@ -610,6 +609,9 @@ declare module '#graphql-documents' {
610
609
  `;
611
610
  }
612
611
  });
612
+ nuxt.options.alias["#graphql-server-options"] = moduleResolver(
613
+ "runtime/serverOptions"
614
+ );
613
615
  const extensions = ["js", "mjs", "ts"];
614
616
  const resolvedPath = "~/app/graphqlMiddleware.serverOptions".replace(/^(~~|@@)/, nuxt.options.rootDir).replace(/^(~|@)/, nuxt.options.srcDir);
615
617
  const template = (() => {
@@ -631,7 +633,7 @@ declare module '#graphql-documents' {
631
633
  nuxt.options.nitro.externals = nuxt.options.nitro.externals || {};
632
634
  nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || [];
633
635
  nuxt.options.nitro.externals.inline.push(template.dst);
634
- nuxt.options.alias["#graphql-middleware-server-options"] = template.dst;
636
+ nuxt.options.alias["#graphql-middleware-server-options-build"] = template.dst;
635
637
  addServerHandler({
636
638
  handler: moduleResolver("./runtime/serverHandler/index"),
637
639
  route: options.serverApiPrefix + "/:operation/:name"
@@ -665,8 +667,5 @@ declare module '#graphql-documents' {
665
667
  }
666
668
  }
667
669
  });
668
- function defineGraphqlServerOptions(options) {
669
- return options;
670
- }
671
670
 
672
- export { module as default, defineGraphqlServerOptions };
671
+ export { module as default };
@@ -18,6 +18,7 @@ export function useGraphqlQuery(...args) {
18
18
  const state2 = useGraphqlState();
19
19
  return $fetch(getEndpoint("query", name), {
20
20
  params: buildRequestParams(args[1]),
21
+ // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
21
22
  ...state2.fetchOptions
22
23
  });
23
24
  }
@@ -29,6 +30,7 @@ export function useGraphqlMutation(...args) {
29
30
  return Promise.reject(new Error("Invalid mutation name"));
30
31
  }
31
32
  return $fetch(getEndpoint("mutation", name), {
33
+ // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
32
34
  method: "post",
33
35
  body,
34
36
  ...state2.fetchOptions
@@ -10,7 +10,7 @@ import {
10
10
  import { GraphqlMiddlewareOperation } from "./../settings/index.mjs";
11
11
  import { documents } from "#graphql-documents";
12
12
  import { useRuntimeConfig } from "#imports";
13
- import serverOptions from "#graphql-middleware-server-options";
13
+ import serverOptions from "#graphql-middleware-server-options-build";
14
14
  export default defineEventHandler(async (event) => {
15
15
  const method = getMethod(event);
16
16
  const operation = event.context.params.operation;
@@ -33,6 +33,7 @@ export default defineEventHandler(async (event) => {
33
33
  );
34
34
  const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(getQuery(event)) : await readBody(event);
35
35
  return $fetch.raw(endpoint, {
36
+ // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
36
37
  method: "POST",
37
38
  body: {
38
39
  query,
@@ -0,0 +1,2 @@
1
+ import { GraphqlMiddlewareServerOptions } from './../../types';
2
+ export declare function defineGraphqlServerOptions(options: GraphqlMiddlewareServerOptions): GraphqlMiddlewareServerOptions;
@@ -0,0 +1,3 @@
1
+ export function defineGraphqlServerOptions(options) {
2
+ return options;
3
+ }
@@ -0,0 +1,2 @@
1
+ import { defineGraphqlServerOptions } from './defineGraphqlServerOptions';
2
+ export { defineGraphqlServerOptions };
@@ -0,0 +1,2 @@
1
+ import { defineGraphqlServerOptions } from "./defineGraphqlServerOptions.mjs";
2
+ export { defineGraphqlServerOptions };
package/dist/types.d.ts CHANGED
@@ -8,4 +8,4 @@ declare module '@nuxt/schema' {
8
8
  }
9
9
 
10
10
 
11
- export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, default, defineGraphqlServerOptions } from './module'
11
+ export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, default } from './module'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "3.1.0-beta.0",
3
+ "version": "3.1.0-beta.2",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -35,11 +35,11 @@
35
35
  "test:coverage": "vitest run --coverage"
36
36
  },
37
37
  "dependencies": {
38
- "@graphql-codegen/cli": "^2.15.0",
39
- "@graphql-codegen/schema-ast": "^2.5.1",
40
- "@graphql-codegen/typescript": "^2.8.3",
38
+ "@graphql-codegen/cli": "^2.16.4",
39
+ "@graphql-codegen/schema-ast": "^2.6.1",
40
+ "@graphql-codegen/typescript": "^2.8.7",
41
41
  "@graphql-codegen/typescript-generic-sdk": "^3.0.4",
42
- "@graphql-codegen/typescript-operations": "^2.5.8",
42
+ "@graphql-codegen/typescript-operations": "^2.5.12",
43
43
  "@graphql-fragment-import/lib": "^2.0.0",
44
44
  "@graphql-tools/utils": "^9.1.1",
45
45
  "@nuxt/kit": "^3.0.0",
@@ -49,21 +49,21 @@
49
49
  "devDependencies": {
50
50
  "@nuxt/module-builder": "^0.2.1",
51
51
  "@nuxt/schema": "^3.0.0",
52
- "@nuxt/test-utils": "^3.0.0",
52
+ "@nuxt/test-utils": "^3.1.1",
53
53
  "@nuxtjs/eslint-config-typescript": "^12.0.0",
54
54
  "@types/capture-console": "^1.0.1",
55
55
  "@types/cli-table": "^0.3.1",
56
56
  "@types/inquirer": "^9.0.3",
57
- "@vitest/coverage-c8": "^0.25.3",
57
+ "@vitest/coverage-c8": "^0.25.8",
58
58
  "cypress": "^11.2.0",
59
- "eslint": "^8.29.0",
60
- "eslint-config-prettier": "^8.5.0",
59
+ "eslint": "^8.33.0",
60
+ "eslint-config-prettier": "^8.6.0",
61
61
  "eslint-plugin-prettier": "^4.2.1",
62
62
  "jsdoc-to-markdown": "^8.0.0",
63
- "nuxt": "^3.0.0",
64
- "prettier": "^2.8.0",
63
+ "nuxt": "^3.1.1",
64
+ "prettier": "^2.8.3",
65
65
  "strip-ansi": "^7.0.1",
66
- "typedoc": "^0.23.21",
66
+ "typedoc": "^0.23.24",
67
67
  "typedoc-plugin-markdown": "^3.14.0",
68
68
  "vitepress": "^1.0.0-alpha.30",
69
69
  "vitest": "^0.25.3"