nuxt-graphql-middleware 1.2.1 → 2.0.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.
@@ -1,91 +0,0 @@
1
- const IS_DEV = process.env.NODE_ENV === 'development';
2
- function log(action, path, message) {
3
- if (IS_DEV) {
4
- console.log(`[API - ${action}] ${message}: ${path}`);
5
- }
6
- }
7
- export class GraphqlMiddlewarePlugin {
8
- constructor(baseURL, headers, useCache, context) {
9
- this.baseURL = baseURL;
10
- this.headers = headers || {};
11
- this.context = context;
12
- if (useCache) {
13
- this.cache = new Map();
14
- }
15
- }
16
- getPluginHeaderValue() {
17
- var _a, _b;
18
- return {
19
- 'Nuxt-Graphql-Middleware-Route': ((_b = (_a = this.context) === null || _a === void 0 ? void 0 : _a.route) === null || _b === void 0 ? void 0 : _b.fullPath) || '',
20
- };
21
- }
22
- beforeRequest(fn) {
23
- this.beforeRequestFn = fn;
24
- }
25
- query(name, variables, headers = {}) {
26
- var _a;
27
- const params = new URLSearchParams({
28
- name,
29
- variables: JSON.stringify(variables || {}),
30
- });
31
- const url = this.baseURL + '/query?' + params.toString();
32
- if ((_a = this.cache) === null || _a === void 0 ? void 0 : _a.has(url)) {
33
- log('query', url, 'Loading from cache');
34
- return Promise.resolve(this.cache.get(url));
35
- }
36
- log('query', url, 'Fetching');
37
- let fetchOptions = {
38
- method: 'GET',
39
- credentials: 'include',
40
- headers: Object.assign(Object.assign(Object.assign({ 'Content-Type': 'application/json' }, headers), this.headers), this.getPluginHeaderValue()),
41
- };
42
- if (this.beforeRequestFn) {
43
- fetchOptions = this.beforeRequestFn(this.context, fetchOptions);
44
- }
45
- return fetch(url, fetchOptions)
46
- .then((response) => {
47
- if (response.ok) {
48
- return response.json();
49
- }
50
- throw new Error('Server Error');
51
- })
52
- .then((data) => {
53
- var _a;
54
- if (this.cache && this.cache.size > 30) {
55
- const key = this.cache.keys().next().value;
56
- this.cache.delete(key);
57
- }
58
- (_a = this.cache) === null || _a === void 0 ? void 0 : _a.set(url, data);
59
- return data;
60
- });
61
- }
62
- mutate(name, variables, headers = {}) {
63
- const params = new URLSearchParams({
64
- name,
65
- });
66
- let fetchOptions = {
67
- method: 'POST',
68
- credentials: 'include',
69
- headers: Object.assign(Object.assign(Object.assign({ 'Content-Type': 'application/json' }, headers), this.headers), this.getPluginHeaderValue()),
70
- body: JSON.stringify(variables),
71
- };
72
- if (this.beforeRequestFn) {
73
- fetchOptions = this.beforeRequestFn(this.context, fetchOptions);
74
- }
75
- return fetch(this.baseURL + '/mutate?' + params.toString(), fetchOptions).then((response) => response.json());
76
- }
77
- }
78
- const graphqlMiddlewarePlugin = (context, inject) => {
79
- var _a, _b;
80
- const namespace = "<%= options.namespace || '' %>";
81
- const port = ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.NUXT_PORT) || '<%= options.port %>';
82
- const cacheInBrowser = "<%= options.cacheInBrowser || '' %>" === 'true';
83
- const cacheInServer = "<%= options.cacheInServer || '' %>" === 'true';
84
- let baseURL = namespace;
85
- if (process.server) {
86
- baseURL = 'http://0.0.0.0:' + port + namespace;
87
- }
88
- const useCache = (process.server && cacheInServer) || (process.client && cacheInBrowser);
89
- inject('graphql', new GraphqlMiddlewarePlugin(baseURL, (_b = context.req) === null || _b === void 0 ? void 0 : _b.headers, useCache, context));
90
- };
91
- export default graphqlMiddlewarePlugin;
@@ -1,107 +0,0 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
- import express from 'express';
11
- import { GraphQLClient } from 'graphql-request';
12
- function getVariables(vars) {
13
- try {
14
- const variables = JSON.parse(vars);
15
- return variables;
16
- }
17
- catch (error) {
18
- return {};
19
- }
20
- }
21
- function buildHeaders(req, name, type, config) {
22
- var _a;
23
- if (config === null || config === void 0 ? void 0 : config.buildHeaders) {
24
- return config.buildHeaders(req, name, type);
25
- }
26
- if ((_a = config === null || config === void 0 ? void 0 : config.fetchOptions) === null || _a === void 0 ? void 0 : _a.headers) {
27
- return config.fetchOptions.headers;
28
- }
29
- return {};
30
- }
31
- export default function createServerMiddleware(graphqlServer, queries, mutations, config) {
32
- const app = express();
33
- app.use(express.json());
34
- const clients = new Map();
35
- function getClient(endpoint) {
36
- if (!clients.has(endpoint)) {
37
- const client = new GraphQLClient(endpoint);
38
- clients.set(endpoint, client);
39
- }
40
- return clients.get(endpoint);
41
- }
42
- function getEndpoint(req) {
43
- if (config === null || config === void 0 ? void 0 : config.buildEndpoint) {
44
- return config.buildEndpoint(req);
45
- }
46
- return graphqlServer;
47
- }
48
- if (config === null || config === void 0 ? void 0 : config.middleware) {
49
- app.use(config.middleware);
50
- }
51
- function query(req, res) {
52
- return __awaiter(this, void 0, void 0, function* () {
53
- const name = req.query.name;
54
- if (!name || !queries.has(name)) {
55
- res.status(404).send();
56
- return;
57
- }
58
- try {
59
- const headers = buildHeaders(req, name, 'query', config);
60
- const variables = getVariables(req.query.variables);
61
- const query = queries.get(name);
62
- const endpoint = getEndpoint(req);
63
- const client = getClient(endpoint);
64
- const response = yield client.rawRequest(query, variables, headers);
65
- if (config === null || config === void 0 ? void 0 : config.onQueryResponse) {
66
- return config.onQueryResponse(response, req, res);
67
- }
68
- return res.json(response.data);
69
- }
70
- catch (e) {
71
- if (config === null || config === void 0 ? void 0 : config.onQueryError) {
72
- return config.onQueryError(e, req, res);
73
- }
74
- return res.status(500).send();
75
- }
76
- });
77
- }
78
- function mutate(req, res) {
79
- return __awaiter(this, void 0, void 0, function* () {
80
- const name = req.query.name;
81
- if (!name || !mutations.has(name)) {
82
- res.status(404).send();
83
- return;
84
- }
85
- const mutation = mutations.get(name);
86
- try {
87
- const headers = buildHeaders(req, name, 'mutation', config);
88
- const endpoint = getEndpoint(req);
89
- const client = getClient(endpoint);
90
- const response = yield client.request(mutation, req.body, headers);
91
- if (config === null || config === void 0 ? void 0 : config.onMutationResponse) {
92
- return config.onMutationResponse(response, req, res);
93
- }
94
- return res.json(response);
95
- }
96
- catch (error) {
97
- if (config === null || config === void 0 ? void 0 : config.onMutationError) {
98
- return config.onMutationError(error, req, res);
99
- }
100
- return res.status(500).send();
101
- }
102
- });
103
- }
104
- app.get('/query', query);
105
- app.post('/mutate', mutate);
106
- return app;
107
- }
@@ -1,2 +0,0 @@
1
- export { graphqlMiddleware as default } from './module';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,IAAI,OAAO,EAAE,MAAM,UAAU,CAAA"}
@@ -1,13 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"codegen.d.ts","sourceRoot":"","sources":["../../../src/module/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;;;EAmDxC"}
@@ -1,2 +0,0 @@
1
- export default function (path: string, resolver: any): any;
2
- //# sourceMappingURL=graphqlImport.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"graphqlImport.d.ts","sourceRoot":"","sources":["../../../src/module/graphqlImport.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,OAAO,WAAW,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,OASnD"}
@@ -1,17 +0,0 @@
1
- import { Module } from '@nuxt/types';
2
- import { GraphqlServerMiddlewareConfig } from './../serverMiddleware';
3
- import { GraphqlMiddlewarePluginConfig } from './../plugin';
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
- export declare const graphqlMiddleware: Module;
17
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/module/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAW,MAAM,EAAE,MAAM,aAAa,CAAA;AAE7C,OAAyB,EACvB,6BAA6B,EAC9B,MAAM,uBAAuB,CAAA;AAE9B,OAAO,EAAE,6BAA6B,EAAE,MAAM,aAAa,CAAA;AAC3D,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,eAAO,MAAM,iBAAiB,EAAE,MA8J/B,CAAA"}
@@ -1,23 +0,0 @@
1
- import { Context, Plugin } from '@nuxt/types';
2
- export interface GraphqlMiddlewarePluginConfig {
3
- enabled?: boolean;
4
- cacheInBrowser?: boolean;
5
- cacheInServer?: boolean;
6
- }
7
- export declare class GraphqlMiddlewarePlugin {
8
- baseURL: string;
9
- headers: any;
10
- beforeRequestFn: Function | undefined;
11
- cache?: Map<string, any>;
12
- context: any;
13
- constructor(baseURL: string, headers: any, useCache: boolean, context: Context);
14
- getPluginHeaderValue(): {
15
- 'Nuxt-Graphql-Middleware-Route': any;
16
- };
17
- beforeRequest(fn: Function): void;
18
- query(name: string, variables?: any, headers?: any): Promise<any>;
19
- mutate(name: string, variables?: any, headers?: any): Promise<any>;
20
- }
21
- declare const graphqlMiddlewarePlugin: Plugin;
22
- export default graphqlMiddlewarePlugin;
23
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/plugin/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAW7C,MAAM,WAAW,6BAA6B;IAC5C,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,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;IAO1B,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,GAAG,EAAE,OAAO,GAAE,GAAQ;IAgDtD,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"}
@@ -1,13 +0,0 @@
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=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/serverMiddleware/index.ts"],"names":[],"mappings":"AAAA,OAAgB,EAAE,OAAO,EAAE,cAAc,EAAY,MAAM,SAAS,CAAA;AAepE,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"}