nuxt-graphql-middleware 4.0.0 → 4.1.0

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.
Files changed (70) hide show
  1. package/README.md +10 -2
  2. package/dist/client/200.html +10 -12
  3. package/dist/client/404.html +10 -12
  4. package/dist/client/_nuxt/Bk31qVW4.js +1 -0
  5. package/dist/client/_nuxt/CHM3Rz8p.js +1 -0
  6. package/dist/client/_nuxt/CUKk0hSk.js +24 -0
  7. package/dist/client/_nuxt/Ca6k-hEN.js +2 -0
  8. package/dist/client/_nuxt/KmIjpfWJ.js +1 -0
  9. package/dist/client/_nuxt/builds/latest.json +1 -1
  10. package/dist/client/_nuxt/builds/meta/debd7d31-13de-43c8-a23f-9d618b1b6aee.json +1 -0
  11. package/dist/client/_nuxt/entry.BMcZ91J5.css +1 -0
  12. package/dist/client/_nuxt/error-404.CjTTbIxB.css +1 -0
  13. package/dist/client/_nuxt/error-500.B4KzowuE.css +1 -0
  14. package/dist/client/_nuxt/index.DCCKx2Zk.css +1 -0
  15. package/dist/client/index.html +10 -12
  16. package/dist/module.d.mts +71 -10
  17. package/dist/module.d.ts +71 -10
  18. package/dist/module.json +6 -2
  19. package/dist/module.mjs +239 -155
  20. package/dist/runtime/composables/nuxtApp.d.ts +9 -4
  21. package/dist/runtime/composables/nuxtApp.js +47 -0
  22. package/dist/runtime/composables/server.d.ts +4 -3
  23. package/dist/runtime/composables/{server.mjs → server.js} +2 -2
  24. package/dist/runtime/composables/shared.d.ts +12 -7
  25. package/dist/runtime/composables/useAsyncGraphqlQuery.d.ts +22 -0
  26. package/dist/runtime/composables/useAsyncGraphqlQuery.js +43 -0
  27. package/dist/runtime/composables/useGraphqlMutation.d.ts +3 -2
  28. package/dist/runtime/composables/{useGraphqlMutation.mjs → useGraphqlMutation.js} +2 -2
  29. package/dist/runtime/composables/useGraphqlQuery.d.ts +3 -2
  30. package/dist/runtime/composables/useGraphqlQuery.js +20 -0
  31. package/dist/runtime/composables/useGraphqlState.d.ts +1 -1
  32. package/dist/runtime/composables/{useGraphqlState.mjs → useGraphqlState.js} +1 -1
  33. package/dist/runtime/composables/useGraphqlUploadMutation.d.ts +7 -0
  34. package/dist/runtime/composables/useGraphqlUploadMutation.js +57 -0
  35. package/dist/runtime/helpers/ClientCache.d.ts +15 -0
  36. package/dist/runtime/helpers/ClientCache.js +40 -0
  37. package/dist/runtime/helpers/{index.mjs → index.js} +1 -1
  38. package/dist/runtime/plugins/provideState.d.ts +1 -1
  39. package/dist/runtime/plugins/provideState.js +17 -0
  40. package/dist/runtime/serverHandler/helpers/index.d.ts +5 -3
  41. package/dist/runtime/serverHandler/helpers/{index.mjs → index.js} +2 -2
  42. package/dist/runtime/serverHandler/{index.mjs → index.js} +3 -3
  43. package/dist/runtime/serverHandler/upload.d.ts +2 -0
  44. package/dist/runtime/serverHandler/upload.js +101 -0
  45. package/dist/runtime/serverOptions/defineGraphqlServerOptions.d.ts +2 -2
  46. package/dist/runtime/serverOptions/index.d.ts +1 -1
  47. package/dist/runtime/serverOptions/{index.mjs → index.js} +1 -1
  48. package/dist/runtime/types.d.ts +24 -0
  49. package/dist/runtime/types.js +0 -0
  50. package/dist/types.d.mts +2 -9
  51. package/dist/types.d.ts +2 -9
  52. package/package.json +41 -30
  53. package/dist/client/_nuxt/_plugin-vue_export-helper.x3n3nnut.js +0 -1
  54. package/dist/client/_nuxt/builds/meta/71f1ba68-6be9-464d-87d1-1b4b3ac786b2.json +0 -1
  55. package/dist/client/_nuxt/entry.N5CnhLMX.css +0 -1
  56. package/dist/client/_nuxt/entry.sn4ItzJP.js +0 -29
  57. package/dist/client/_nuxt/error-404._g063Njv.js +0 -1
  58. package/dist/client/_nuxt/error-404.gOl2ep2B.css +0 -1
  59. package/dist/client/_nuxt/error-500.iP22ayZD.js +0 -1
  60. package/dist/client/_nuxt/error-500.z8GAN5B3.css +0 -1
  61. package/dist/client/_nuxt/index.KH5Eug0w.css +0 -1
  62. package/dist/client/_nuxt/index.wowlKkKL.js +0 -2
  63. package/dist/client/_nuxt/vue.f36acd1f.H09MX4qF.js +0 -1
  64. package/dist/runtime/composables/nuxtApp.mjs +0 -15
  65. package/dist/runtime/composables/useGraphqlQuery.mjs +0 -9
  66. package/dist/runtime/plugins/provideState.mjs +0 -7
  67. /package/dist/runtime/composables/{shared.mjs → shared.js} +0 -0
  68. /package/dist/runtime/serverHandler/{debug.mjs → debug.js} +0 -0
  69. /package/dist/runtime/serverOptions/{defineGraphqlServerOptions.mjs → defineGraphqlServerOptions.js} +0 -0
  70. /package/dist/runtime/settings/{index.mjs → index.js} +0 -0
@@ -1,10 +1,11 @@
1
- import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult, type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared';
1
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
2
+ import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult, type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared.js';
2
3
  import type { GraphqlMiddlewareQuery, GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
3
4
  /**
4
5
  * Performs a GraphQL query.
5
6
  */
6
- export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery> | [QueryObjectArgs<T, GraphqlMiddlewareQuery>]): Promise<GetQueryResult<T, GraphqlMiddlewareQuery>>;
7
+ export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName, R extends GetQueryResult<T, GraphqlMiddlewareQuery>>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery> | [QueryObjectArgs<T, GraphqlMiddlewareQuery>]): Promise<GraphqlResponse<R>>;
7
8
  /**
8
9
  * Performs a GraphQL mutation.
9
10
  */
10
- export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GetMutationResult<T, GraphqlMiddlewareMutation>>;
11
+ export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName, R extends GetMutationResult<T, GraphqlMiddlewareMutation>>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GraphqlResponse<R>>;
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  getEndpoint
3
- } from "./shared.mjs";
4
- import { buildRequestParams } from "./../helpers/index.mjs";
3
+ } from "./shared.js";
4
+ import { buildRequestParams } from "./../helpers/index.js";
5
5
  function performRequest(operation, operationName, method, options) {
6
6
  return $fetch(getEndpoint(operation, operationName), {
7
7
  ...options,
@@ -1,24 +1,27 @@
1
1
  import type { FetchOptions } from 'ofetch';
2
2
  import type { GraphqlMiddlewareQuery, GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
3
+ import type { RequestCacheOptions } from '#graphql-middleware/types';
3
4
  export type GraphqlMiddlewareQueryName = keyof GraphqlMiddlewareQuery;
4
5
  export type GraphqlMiddlewareMutationName = keyof GraphqlMiddlewareMutation;
5
- export type GetQueryArgs<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T][0] extends null ? [T] : M[T][1] extends false ? [T, M[T][0]] : [T, M[T][0]?];
6
- export type GetMutationArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? [T] : M[T][1] extends false ? [T, M[T][0]] : [T, M[T][0]?];
7
- export type GraphqlResponse<T> = {
8
- data: T;
9
- errors: any[];
6
+ export type GraphqlComposableOptions = {
7
+ fetchOptions?: FetchOptions;
8
+ graphqlCaching?: RequestCacheOptions;
10
9
  };
11
- export type GetQueryResult<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T] extends undefined ? undefined : GraphqlResponse<M[T][2]>;
12
- export type GetMutationResult<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T] extends undefined ? undefined : GraphqlResponse<M[T][2]>;
10
+ export type GetQueryArgs<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T][0] extends null ? [T, (null | undefined)?, GraphqlComposableOptions?] : M[T][1] extends false ? [T, M[T][0], GraphqlComposableOptions?] : [T, M[T][0]?, GraphqlComposableOptions?];
11
+ export type GetMutationArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? [T, (null | undefined)?, GraphqlComposableOptions?] : M[T][1] extends false ? [T, M[T][0], GraphqlComposableOptions?] : [T, M[T][0]?, GraphqlComposableOptions?];
12
+ export type GetQueryResult<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T] extends undefined ? undefined : M[T][2];
13
+ export type GetMutationResult<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T] extends undefined ? undefined : M[T][2];
13
14
  export declare function getEndpoint(operation: string, operationName: string): string;
14
15
  export type QueryObjectArgs<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T][0] extends null ? {
15
16
  name: T;
16
17
  fetchOptions?: FetchOptions;
18
+ graphqlCaching?: RequestCacheOptions;
17
19
  variables?: null;
18
20
  } : {
19
21
  name: T;
20
22
  variables: M[T][0];
21
23
  fetchOptions?: FetchOptions;
24
+ graphqlCaching?: RequestCacheOptions;
22
25
  };
23
26
  export type MutationObjectArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? {
24
27
  name: T;
@@ -29,3 +32,5 @@ export type MutationObjectArgs<T extends GraphqlMiddlewareMutationName, M extend
29
32
  variables: M[T][0];
30
33
  fetchOptions?: FetchOptions;
31
34
  };
35
+ export type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick<T, K[number]> : T;
36
+ export type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>;
@@ -0,0 +1,22 @@
1
+ import { type GraphqlMiddlewareQueryName, type KeysOf, type PickFrom } from './shared.js';
2
+ import type { FetchOptions } from 'ofetch';
3
+ import { type Ref } from 'vue';
4
+ import type { GraphqlMiddlewareQuery } from '#build/nuxt-graphql-middleware';
5
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
6
+ import type { RequestCacheOptions } from '#graphql-middleware/types';
7
+ import type { AsyncData, AsyncDataOptions, NuxtError } from '#app';
8
+ type AsyncGraphqlQueryOptions<ResponseType, DefaultT, Keys extends KeysOf<DefaultT>, F> = AsyncDataOptions<ResponseType, DefaultT, Keys> & {
9
+ graphqlCaching?: RequestCacheOptions;
10
+ fetchOptions?: F;
11
+ };
12
+ /**
13
+ * Wrapper for useAsyncData to perform a single GraphQL query.
14
+ */
15
+ export declare function useAsyncGraphqlQuery<Name extends GraphqlMiddlewareQueryName, VarType extends GraphqlMiddlewareQuery[Name][0], VarsOptional extends GraphqlMiddlewareQuery[Name][1], ResponseType extends GraphqlResponse<GraphqlMiddlewareQuery[Name][2]>, F extends FetchOptions<'json'>, DefaultT = ResponseType, Keys extends KeysOf<DefaultT> = KeysOf<DefaultT>>(name: Name, ...args: VarsOptional extends true ? [
16
+ (undefined | null | Record<string, never> | VarType | Ref<VarType>)?,
17
+ AsyncGraphqlQueryOptions<ResponseType, DefaultT, Keys, F>?
18
+ ] : [
19
+ VarType | Ref<VarType>,
20
+ (undefined | null | AsyncGraphqlQueryOptions<ResponseType, DefaultT, Keys, F>)?
21
+ ]): AsyncData<PickFrom<DefaultT, KeysOf<DefaultT>> | null, NuxtError<unknown> | null>;
22
+ export {};
@@ -0,0 +1,43 @@
1
+ import { isRef, unref } from "vue";
2
+ import { buildRequestParams } from "./../helpers/index.js";
3
+ import { performRequest } from "./nuxtApp.js";
4
+ import { useAsyncData, useAppConfig, useNuxtApp } from "#imports";
5
+ import { hash } from "ohash";
6
+ export function useAsyncGraphqlQuery(name, ...args) {
7
+ const variables = args[0];
8
+ const asyncDataOptions = args[1] || {};
9
+ const fetchOptions = asyncDataOptions.fetchOptions;
10
+ const key = `graphql:${name}:${hash(unref(variables))}`;
11
+ const config = useAppConfig();
12
+ const app = useNuxtApp();
13
+ if (import.meta.client) {
14
+ if (variables && isRef(variables)) {
15
+ if (!asyncDataOptions.watch) {
16
+ asyncDataOptions.watch = [];
17
+ }
18
+ asyncDataOptions.watch.push(variables);
19
+ }
20
+ if (asyncDataOptions.graphqlCaching?.client && !asyncDataOptions.getCachedData) {
21
+ asyncDataOptions.getCachedData = function(key2) {
22
+ if (!app.isHydrating && !config.graphqlMiddleware.clientCacheEnabled) {
23
+ return;
24
+ }
25
+ return app.payload.data[key2];
26
+ };
27
+ }
28
+ }
29
+ return useAsyncData(
30
+ key,
31
+ () => performRequest(
32
+ "query",
33
+ name,
34
+ "get",
35
+ {
36
+ params: buildRequestParams(unref(variables)),
37
+ ...fetchOptions
38
+ },
39
+ asyncDataOptions.graphqlCaching
40
+ ),
41
+ asyncDataOptions
42
+ );
43
+ }
@@ -1,6 +1,7 @@
1
- import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared';
1
+ import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared.js';
2
2
  import type { GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
3
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
3
4
  /**
4
5
  * Performs a GraphQL mutation.
5
6
  */
6
- export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GetMutationResult<T, GraphqlMiddlewareMutation>>;
7
+ export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName, R extends GetMutationResult<T, GraphqlMiddlewareMutation>>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GraphqlResponse<R>>;
@@ -1,6 +1,6 @@
1
- import { performRequest } from "./nuxtApp.mjs";
1
+ import { performRequest } from "./nuxtApp.js";
2
2
  export function useGraphqlMutation(...args) {
3
- const [name, body, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1]] : [args[0].name, args[0].variables, args[0].fetchOptions];
3
+ const [name, body, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions] : [args[0].name, args[0].variables, args[0].fetchOptions];
4
4
  return performRequest("mutation", name, "post", {
5
5
  body,
6
6
  ...fetchOptions
@@ -1,6 +1,7 @@
1
- import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './shared';
1
+ import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './shared.js';
2
2
  import type { GraphqlMiddlewareQuery } from '#build/nuxt-graphql-middleware';
3
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
3
4
  /**
4
5
  * Performs a GraphQL query.
5
6
  */
6
- export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery> | [QueryObjectArgs<T, GraphqlMiddlewareQuery>]): Promise<GetQueryResult<T, GraphqlMiddlewareQuery>>;
7
+ export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName, R extends GetQueryResult<T, GraphqlMiddlewareQuery>>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery> | [QueryObjectArgs<T, GraphqlMiddlewareQuery>]): Promise<GraphqlResponse<R>>;
@@ -0,0 +1,20 @@
1
+ import { buildRequestParams } from "./../helpers/index.js";
2
+ import { performRequest } from "./nuxtApp.js";
3
+ export function useGraphqlQuery(...args) {
4
+ const [name, variables, fetchOptions = {}, graphqlCaching = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.graphqlCaching] : [
5
+ args[0].name,
6
+ args[0].variables,
7
+ args[0].fetchOptions,
8
+ args[0].graphqlCaching
9
+ ];
10
+ return performRequest(
11
+ "query",
12
+ name,
13
+ "get",
14
+ {
15
+ params: buildRequestParams(variables),
16
+ ...fetchOptions
17
+ },
18
+ graphqlCaching
19
+ );
20
+ }
@@ -1,2 +1,2 @@
1
- import { type GraphqlMiddlewareState } from './../../types';
1
+ import type { GraphqlMiddlewareState } from '#graphql-middleware/types';
2
2
  export declare const useGraphqlState: (providedApp?: any) => GraphqlMiddlewareState | null;
@@ -1,4 +1,4 @@
1
- import { useNuxtApp } from "#app";
1
+ import { useNuxtApp } from "#imports";
2
2
  export const useGraphqlState = function(providedApp) {
3
3
  try {
4
4
  const app = providedApp || useNuxtApp();
@@ -0,0 +1,7 @@
1
+ import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared.js';
2
+ import type { GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
3
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
4
+ /**
5
+ * Performs a GraphQL upload mutation.
6
+ */
7
+ export declare function useGraphqlUploadMutation<T extends GraphqlMiddlewareMutationName, R extends GetMutationResult<T, GraphqlMiddlewareMutation>>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GraphqlResponse<R>>;
@@ -0,0 +1,57 @@
1
+ import {
2
+ getEndpoint
3
+ } from "./shared.js";
4
+ import { useGraphqlState } from "#imports";
5
+ function createFormData(variables) {
6
+ const formData = new FormData();
7
+ formData.append("operations", "{}");
8
+ const map = {};
9
+ let fileIndex = 0;
10
+ const traverseAndBuildMap = (obj, path) => {
11
+ if (obj instanceof File) {
12
+ const fileKey = `${fileIndex++}`;
13
+ map[fileKey] = [path];
14
+ formData.append(fileKey, obj);
15
+ return null;
16
+ }
17
+ if (Array.isArray(obj)) {
18
+ return obj.map(
19
+ (item, index) => traverseAndBuildMap(item, `${path}.${index}`)
20
+ );
21
+ }
22
+ if (typeof obj === "object" && obj !== null) {
23
+ const newObj = {};
24
+ for (const key in obj) {
25
+ newObj[key] = traverseAndBuildMap(obj[key], `${path}.${key}`);
26
+ }
27
+ return newObj;
28
+ }
29
+ return obj;
30
+ };
31
+ const cleanedVariables = traverseAndBuildMap(variables, "variables");
32
+ formData.append("variables", JSON.stringify(cleanedVariables));
33
+ formData.append("map", JSON.stringify(map));
34
+ return formData;
35
+ }
36
+ export function useGraphqlUploadMutation(...args) {
37
+ const [name, variables, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1]] : [args[0].name, args[0].variables, args[0].fetchOptions];
38
+ if (!variables) {
39
+ throw new Error(
40
+ 'Using "useGraphqlUploadMutation" without variables is not supported.'
41
+ );
42
+ }
43
+ const state = useGraphqlState();
44
+ const formData = createFormData(variables);
45
+ return $fetch(getEndpoint("upload", name), {
46
+ ...state && state.fetchOptions ? state.fetchOptions : {},
47
+ ...fetchOptions || {},
48
+ method: "POST",
49
+ body: formData
50
+ }).then((v) => {
51
+ return {
52
+ ...v,
53
+ data: v?.data,
54
+ errors: v?.errors || []
55
+ };
56
+ });
57
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Simple LRU (least recently used) cache.
3
+ *
4
+ * Keeps the amount of cached items limited to the given max size.
5
+ * Once that number is reached, the cache item used the least is removed.
6
+ */
7
+ export declare class GraphqlMiddlewareCache {
8
+ cache: Record<string, unknown>;
9
+ keys: string[];
10
+ maxSize: number;
11
+ constructor(maxSize?: number);
12
+ set(key: string, value: unknown): void;
13
+ get<T>(key: string): T | undefined;
14
+ purge(): void;
15
+ }
@@ -0,0 +1,40 @@
1
+ export class GraphqlMiddlewareCache {
2
+ cache;
3
+ keys;
4
+ maxSize;
5
+ constructor(maxSize = 100) {
6
+ this.cache = {};
7
+ this.keys = [];
8
+ this.maxSize = maxSize;
9
+ }
10
+ set(key, value) {
11
+ if (Object.prototype.hasOwnProperty.call(this.cache, key)) {
12
+ const index = this.keys.indexOf(key);
13
+ if (index > -1) {
14
+ this.keys.splice(index, 1);
15
+ }
16
+ } else if (this.keys.length >= this.maxSize) {
17
+ const oldestKey = this.keys.shift();
18
+ if (oldestKey !== void 0) {
19
+ delete this.cache[oldestKey];
20
+ }
21
+ }
22
+ this.cache[key] = value;
23
+ this.keys.push(key);
24
+ }
25
+ get(key) {
26
+ const value = this.cache[key];
27
+ if (value !== void 0) {
28
+ const index = this.keys.indexOf(key);
29
+ if (index > -1) {
30
+ this.keys.splice(index, 1);
31
+ this.keys.push(key);
32
+ }
33
+ return value;
34
+ }
35
+ return void 0;
36
+ }
37
+ purge() {
38
+ this.cache = {};
39
+ }
40
+ }
@@ -2,7 +2,7 @@ export function falsy(value) {
2
2
  return value !== null && value !== void 0;
3
3
  }
4
4
  export function buildRequestParams(variables) {
5
- if (!variables) {
5
+ if (typeof variables !== "object" || !variables) {
6
6
  return {};
7
7
  }
8
8
  for (const key in variables) {
@@ -1,5 +1,5 @@
1
1
  /**
2
2
  * Create and provide the state singleton for the composables.
3
3
  */
4
- declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
4
+ declare const _default: any;
5
5
  export default _default;
@@ -0,0 +1,17 @@
1
+ import { defineNuxtPlugin } from "#imports";
2
+ export default defineNuxtPlugin({
3
+ name: "nuxt-graphql-middleware-provide-state",
4
+ // We can set a very low order to make sure this plugin runs as early as
5
+ // possible.
6
+ order: -9999,
7
+ setup() {
8
+ const graphqlState = {
9
+ fetchOptions: {}
10
+ };
11
+ return {
12
+ provide: {
13
+ graphqlState
14
+ }
15
+ };
16
+ }
17
+ });
@@ -1,8 +1,9 @@
1
1
  import { type QueryObject } from 'ufo';
2
2
  import type { H3Event } from 'h3';
3
3
  import type { FetchOptions, FetchResponse, FetchError } from 'ofetch';
4
- import type { GraphqlMiddlewareRuntimeConfig, GraphqlMiddlewareServerOptions } from './../../../types';
5
- import { GraphqlMiddlewareOperation } from './../../settings';
4
+ import type { GraphqlMiddlewareRuntimeConfig, GraphqlMiddlewareServerOptions } from './../../../types.js';
5
+ import { GraphqlMiddlewareOperation } from './../../settings.js';
6
+ import { type Documents } from '#graphql-documents';
6
7
  export declare function queryParamToVariables(query: QueryObject): any;
7
8
  /**
8
9
  * Get the URL of the GraphQL endpoint.
@@ -12,10 +13,11 @@ export declare function getEndpoint(config: GraphqlMiddlewareRuntimeConfig, serv
12
13
  * Get the options for the $fetch request to the GraphQL server.
13
14
  */
14
15
  export declare function getFetchOptions(serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string): FetchOptions | Promise<FetchOptions>;
16
+ export declare function throwError(statusMessage: string, statusCode?: number): never;
15
17
  /**
16
18
  * Assure that the request is valid.
17
19
  */
18
- export declare function validateRequest(method?: string, operation?: GraphqlMiddlewareOperation, name?: string, documents?: Record<string, Record<string, string>>): void;
20
+ export declare function validateRequest(method?: string, operation?: GraphqlMiddlewareOperation | string, name?: string, documents?: Documents): void;
19
21
  /**
20
22
  * Handle GraphQL server response.
21
23
  */
@@ -1,5 +1,5 @@
1
1
  import { createError } from "h3";
2
- import { GraphqlMiddlewareOperation } from "./../../settings/index.mjs";
2
+ import { GraphqlMiddlewareOperation } from "./../../settings/index.js";
3
3
  export function queryParamToVariables(query) {
4
4
  try {
5
5
  if (query.__variables && typeof query.__variables === "string") {
@@ -31,7 +31,7 @@ export function getFetchOptions(serverOptions, event, operation, operationName)
31
31
  }
32
32
  return {};
33
33
  }
34
- function throwError(statusMessage, statusCode = 400) {
34
+ export function throwError(statusMessage, statusCode = 400) {
35
35
  throw createError({
36
36
  statusCode,
37
37
  statusMessage
@@ -6,10 +6,10 @@ import {
6
6
  validateRequest,
7
7
  onServerResponse,
8
8
  onServerError
9
- } from "./helpers/index.mjs";
10
- import { GraphqlMiddlewareOperation } from "./../settings/index.mjs";
9
+ } from "./helpers/index.js";
10
+ import { GraphqlMiddlewareOperation } from "./../settings/index.js";
11
11
  import { documents } from "#graphql-documents";
12
- import serverOptions from "#graphql-middleware-server-options-build";
12
+ import { serverOptions } from "#graphql-middleware-server-options-build";
13
13
  import { useRuntimeConfig } from "#imports";
14
14
  export default defineEventHandler(async (event) => {
15
15
  const method = event.method;
@@ -0,0 +1,2 @@
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
2
+ export default _default;
@@ -0,0 +1,101 @@
1
+ import {
2
+ defineEventHandler,
3
+ readMultipartFormData
4
+ } from "h3";
5
+ import {
6
+ getEndpoint,
7
+ getFetchOptions,
8
+ validateRequest,
9
+ onServerResponse,
10
+ onServerError,
11
+ throwError
12
+ } from "./helpers/index.js";
13
+ import { GraphqlMiddlewareOperation } from "./../settings/index.js";
14
+ import { documents } from "#graphql-documents";
15
+ import { serverOptions } from "#graphql-middleware-server-options-build";
16
+ import { useRuntimeConfig } from "#imports";
17
+ function parseMultipart(data) {
18
+ const files = data.filter((v) => !!v.filename);
19
+ const variablesData = data.find((v) => v.name === "variables")?.data?.toString();
20
+ if (!variablesData) {
21
+ throw new Error("Missing variables in form data.");
22
+ }
23
+ const variables = JSON.parse(variablesData);
24
+ const map = data.find((v) => v.name === "map")?.data.toString() || "{}";
25
+ return { files, map, variables };
26
+ }
27
+ export default defineEventHandler(async (event) => {
28
+ const method = event.method;
29
+ const operation = GraphqlMiddlewareOperation.Mutation;
30
+ const operationName = event.context?.params?.name;
31
+ validateRequest(method, operation, operationName, documents);
32
+ const operationDocument = documents[operation][operationName];
33
+ const multiPartData = await readMultipartFormData(event);
34
+ if (!multiPartData) {
35
+ return throwError("Failed to read multi part data.");
36
+ }
37
+ const { variables, map, files } = parseMultipart(multiPartData);
38
+ const formData = new FormData();
39
+ formData.append(
40
+ "operations",
41
+ JSON.stringify({
42
+ query: operationDocument,
43
+ variables,
44
+ operationName
45
+ })
46
+ );
47
+ formData.append("map", map);
48
+ for (const file of files) {
49
+ if (!file.name) {
50
+ return throwError("Missing name for file: " + file.filename);
51
+ }
52
+ const blob = new Blob([file.data]);
53
+ formData.append(file.name, blob, file.filename);
54
+ }
55
+ if (serverOptions.doGraphqlRequest) {
56
+ return serverOptions.doGraphqlRequest({
57
+ event,
58
+ operation,
59
+ operationName,
60
+ operationDocument,
61
+ variables,
62
+ formData
63
+ });
64
+ }
65
+ const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
66
+ const endpoint = await getEndpoint(
67
+ runtimeConfig,
68
+ serverOptions,
69
+ event,
70
+ operation,
71
+ operationName
72
+ );
73
+ const fetchOptions = await getFetchOptions(
74
+ serverOptions,
75
+ event,
76
+ operation,
77
+ operationName
78
+ );
79
+ return $fetch.raw(endpoint, {
80
+ ...fetchOptions,
81
+ // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
82
+ method: "POST",
83
+ body: formData
84
+ }).then((response) => {
85
+ return onServerResponse(
86
+ serverOptions,
87
+ event,
88
+ response,
89
+ operation,
90
+ operationName
91
+ );
92
+ }).catch((error) => {
93
+ return onServerError(
94
+ serverOptions,
95
+ event,
96
+ error,
97
+ operation,
98
+ operationName
99
+ );
100
+ });
101
+ });
@@ -1,2 +1,2 @@
1
- import { type GraphqlMiddlewareServerOptions } from './../../types';
2
- export declare function defineGraphqlServerOptions(options: GraphqlMiddlewareServerOptions): GraphqlMiddlewareServerOptions;
1
+ import { type GraphqlMiddlewareServerOptions } from './../../types.js';
2
+ export declare function defineGraphqlServerOptions<T extends object>(options: GraphqlMiddlewareServerOptions<T>): GraphqlMiddlewareServerOptions<T>;
@@ -1,2 +1,2 @@
1
- import { defineGraphqlServerOptions } from './defineGraphqlServerOptions';
1
+ import { defineGraphqlServerOptions } from './defineGraphqlServerOptions.js';
2
2
  export { defineGraphqlServerOptions };
@@ -1,2 +1,2 @@
1
- import { defineGraphqlServerOptions } from "./defineGraphqlServerOptions.mjs";
1
+ import { defineGraphqlServerOptions } from "./defineGraphqlServerOptions.js";
2
2
  export { defineGraphqlServerOptions };
@@ -0,0 +1,24 @@
1
+ import type { FetchOptions, FetchContext } from 'ofetch';
2
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
3
+ import type { GraphqlMiddlewareResponseUnion } from '#build/nuxt-graphql-middleware';
4
+ export type GraphqlResponseErrorLocation = {
5
+ line: number;
6
+ column: number;
7
+ };
8
+ export type GraphqlResponseError = {
9
+ message: string;
10
+ locations: GraphqlResponseErrorLocation[];
11
+ path: string[];
12
+ };
13
+ export type GraphqlServerResponse<T> = {
14
+ data: T;
15
+ errors: GraphqlResponseError[];
16
+ };
17
+ export interface GraphqlMiddlewareState {
18
+ fetchOptions: Omit<FetchOptions<'json'>, 'onResponse'> & {
19
+ onResponse?: (context: FetchContext<GraphqlResponse<GraphqlMiddlewareResponseUnion>>) => void | Promise<void>;
20
+ };
21
+ }
22
+ export type RequestCacheOptions = {
23
+ client?: boolean;
24
+ };
File without changes
package/dist/types.d.mts CHANGED
@@ -1,18 +1,11 @@
1
-
2
- import type { ModuleOptions, ModuleHooks } from './module.js'
3
-
1
+ import type { ModuleHooks } from './module.js'
4
2
 
5
3
  declare module '@nuxt/schema' {
6
- interface NuxtConfig { ['graphqlMiddleware']?: Partial<ModuleOptions> }
7
- interface NuxtOptions { ['graphqlMiddleware']?: ModuleOptions }
8
4
  interface NuxtHooks extends ModuleHooks {}
9
5
  }
10
6
 
11
7
  declare module 'nuxt/schema' {
12
- interface NuxtConfig { ['graphqlMiddleware']?: Partial<ModuleOptions> }
13
- interface NuxtOptions { ['graphqlMiddleware']?: ModuleOptions }
14
8
  interface NuxtHooks extends ModuleHooks {}
15
9
  }
16
10
 
17
-
18
- export type { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, default } from './module.js'
11
+ export { type GraphqlMiddlewareServerOptions, type ModuleHooks, type ModuleOptions, default } from './module.js'
package/dist/types.d.ts CHANGED
@@ -1,18 +1,11 @@
1
-
2
- import type { ModuleOptions, ModuleHooks } from './module'
3
-
1
+ import type { ModuleHooks } from './module'
4
2
 
5
3
  declare module '@nuxt/schema' {
6
- interface NuxtConfig { ['graphqlMiddleware']?: Partial<ModuleOptions> }
7
- interface NuxtOptions { ['graphqlMiddleware']?: ModuleOptions }
8
4
  interface NuxtHooks extends ModuleHooks {}
9
5
  }
10
6
 
11
7
  declare module 'nuxt/schema' {
12
- interface NuxtConfig { ['graphqlMiddleware']?: Partial<ModuleOptions> }
13
- interface NuxtOptions { ['graphqlMiddleware']?: ModuleOptions }
14
8
  interface NuxtHooks extends ModuleHooks {}
15
9
  }
16
10
 
17
-
18
- export type { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, default } from './module'
11
+ export { type GraphqlMiddlewareServerOptions, type ModuleHooks, type ModuleOptions, default } from './module'