@avantstay/graphql-ts-client 11.0.0-beta.1 → 12.0.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.
- package/README.md +4 -4
- package/dist/chunk-I4QOADOI.mjs +27 -0
- package/dist/endpoint.d.ts +1 -1
- package/dist/endpoint.js +8 -3
- package/dist/endpoint.mjs +275 -0
- package/dist/index.d.ts +4 -7
- package/dist/index.js +73 -56
- package/dist/index.mjs +530 -0
- package/dist/{types-4ada662b.d.ts → types-23d2d6aa.d.ts} +6 -1
- package/package.json +44 -43
- package/src/__snapshots__/generateTypescriptClient.test.ts.snap +55 -2
- package/src/__testClient.d.ts +4 -16
- package/src/__testClient.js +1 -10
- package/src/__testClient.mjs +87 -0
- package/src/endpoint.ts +8 -9
- package/src/generateTypescriptClient.test.ts +3 -3
- package/src/generateTypescriptClient.ts +37 -22
- package/src/graphqlRequest.test.ts +3 -4
- package/src/jsonToGraphQLQuery.ts +11 -8
- package/src/types.ts +29 -22
package/README.md
CHANGED
|
@@ -33,10 +33,10 @@ import { myAwesomeApi, AssetType, Granularity, OnBoardingStage } from './myAweso
|
|
|
33
33
|
async function somewhereOverTheRainbow() {
|
|
34
34
|
// Set an specific header if needed
|
|
35
35
|
myAwesomeApi.setHeader('Authorization', 'Bearer 010101010101')
|
|
36
|
-
|
|
36
|
+
|
|
37
37
|
// You can also change the API url
|
|
38
38
|
myAwesomeApi.setUrl('https://my-runtime-url.com/graphql')
|
|
39
|
-
|
|
39
|
+
|
|
40
40
|
// And configure how retrials should work
|
|
41
41
|
myAwesomeApi.setRetryConfig({
|
|
42
42
|
max: 3,
|
|
@@ -44,12 +44,12 @@ async function somewhereOverTheRainbow() {
|
|
|
44
44
|
// do something before retrying
|
|
45
45
|
},
|
|
46
46
|
})
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
// Adding response listeners is also possible
|
|
49
49
|
myAwesomeApi.addResponseListener(({ queryName, query, variables, response }) => {
|
|
50
50
|
// do something whenever a request is responded
|
|
51
51
|
})
|
|
52
|
-
|
|
52
|
+
|
|
53
53
|
const response = await myAwesomeApi.queries.globalIndicators({
|
|
54
54
|
// Optionally you can define an alias for this request
|
|
55
55
|
__alias: 'myCustomGlobalIndicators',
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
+
}) : x)(function(x) {
|
|
4
|
+
if (typeof require !== "undefined")
|
|
5
|
+
return require.apply(this, arguments);
|
|
6
|
+
throw new Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/types.ts
|
|
10
|
+
var GraphQLClientError = class extends Error {
|
|
11
|
+
constructor(responseData) {
|
|
12
|
+
super();
|
|
13
|
+
this.responseData = responseData;
|
|
14
|
+
Object.setPrototypeOf(this, GraphQLClientError.prototype);
|
|
15
|
+
}
|
|
16
|
+
get message() {
|
|
17
|
+
return this.response.errors.map((it) => it.message).join(";\n");
|
|
18
|
+
}
|
|
19
|
+
get response() {
|
|
20
|
+
return this.responseData;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export {
|
|
25
|
+
__require,
|
|
26
|
+
GraphQLClientError
|
|
27
|
+
};
|
package/dist/endpoint.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { C as ClientConfig, I as IResponseListener, E as Endpoint } from './types-
|
|
1
|
+
import { C as ClientConfig, I as IResponseListener, E as Endpoint } from './types-23d2d6aa.js';
|
|
2
2
|
|
|
3
3
|
declare const getApiEndpointCreator: (apiConfig: {
|
|
4
4
|
getClient: () => ClientConfig;
|
package/dist/endpoint.js
CHANGED
|
@@ -603,7 +603,8 @@ function jsonToGraphQLQuery(param) {
|
|
|
603
603
|
var alias = jsonQuery.__alias;
|
|
604
604
|
var newJsonQuery = cloneDeep((0, import_omit.default)(jsonQuery, [
|
|
605
605
|
"__alias",
|
|
606
|
-
"__headers"
|
|
606
|
+
"__headers",
|
|
607
|
+
"__url"
|
|
607
608
|
]));
|
|
608
609
|
extractVariables({
|
|
609
610
|
jsonQuery: _defineProperty({}, queryName, newJsonQuery),
|
|
@@ -723,11 +724,13 @@ var getApiEndpointCreator = function(apiConfig) {
|
|
|
723
724
|
return function(kind, queryName) {
|
|
724
725
|
var rawEndpoint = function() {
|
|
725
726
|
var _ref = _asyncToGenerator(function(failureMode, jsonQuery) {
|
|
726
|
-
var _jsonQuery___alias, alias, _jsonQuery___retry, shouldRetry, _jsonQuery___headers, requestHeaders, _jsonToGraphQLQuery, query, variables, start, logOptions, responseListenerData, _ref, data, errors, warnings, headers, status, response, error;
|
|
727
|
+
var clientConfig, _jsonQuery___alias, alias, _jsonQuery___url, url, _jsonQuery___retry, shouldRetry, _jsonQuery___headers, requestHeaders, _jsonToGraphQLQuery, query, variables, start, logOptions, responseListenerData, _ref, data, errors, warnings, headers, status, response, error;
|
|
727
728
|
return __generator(this, function(_state) {
|
|
728
729
|
switch(_state.label){
|
|
729
730
|
case 0:
|
|
731
|
+
clientConfig = apiConfig.getClient();
|
|
730
732
|
alias = (_jsonQuery___alias = jsonQuery === null || jsonQuery === void 0 ? void 0 : jsonQuery.__alias) !== null && _jsonQuery___alias !== void 0 ? _jsonQuery___alias : queryName;
|
|
733
|
+
url = (_jsonQuery___url = jsonQuery === null || jsonQuery === void 0 ? void 0 : jsonQuery.__url) !== null && _jsonQuery___url !== void 0 ? _jsonQuery___url : clientConfig.url;
|
|
731
734
|
shouldRetry = (_jsonQuery___retry = jsonQuery === null || jsonQuery === void 0 ? void 0 : jsonQuery.__retry) !== null && _jsonQuery___retry !== void 0 ? _jsonQuery___retry : true;
|
|
732
735
|
requestHeaders = (_jsonQuery___headers = jsonQuery === null || jsonQuery === void 0 ? void 0 : jsonQuery.__headers) !== null && _jsonQuery___headers !== void 0 ? _jsonQuery___headers : {};
|
|
733
736
|
_jsonToGraphQLQuery = jsonToGraphQLQuery({
|
|
@@ -764,7 +767,9 @@ var getApiEndpointCreator = function(apiConfig) {
|
|
|
764
767
|
shouldRetry: shouldRetry,
|
|
765
768
|
failureMode: failureMode,
|
|
766
769
|
queryName: alias,
|
|
767
|
-
client:
|
|
770
|
+
client: _objectSpreadProps(_objectSpread({}, clientConfig), {
|
|
771
|
+
url: url
|
|
772
|
+
}),
|
|
768
773
|
requestHeaders: requestHeaders,
|
|
769
774
|
query: query,
|
|
770
775
|
variables: variables,
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GraphQLClientError,
|
|
3
|
+
__require
|
|
4
|
+
} from "./chunk-I4QOADOI.mjs";
|
|
5
|
+
|
|
6
|
+
// src/endpoint.ts
|
|
7
|
+
import memoize from "moize";
|
|
8
|
+
|
|
9
|
+
// src/graphqlRequest.ts
|
|
10
|
+
import _axios from "axios";
|
|
11
|
+
var sleep = (ms = 0) => new Promise((resolve) => {
|
|
12
|
+
setTimeout(() => resolve(), ms);
|
|
13
|
+
});
|
|
14
|
+
async function graphqlRequest({
|
|
15
|
+
shouldRetry = true,
|
|
16
|
+
axios = _axios,
|
|
17
|
+
queryName,
|
|
18
|
+
client,
|
|
19
|
+
query,
|
|
20
|
+
requestHeaders = {},
|
|
21
|
+
variables,
|
|
22
|
+
failureMode,
|
|
23
|
+
errorsParser
|
|
24
|
+
}) {
|
|
25
|
+
let lastResponse;
|
|
26
|
+
const maxRetrials = shouldRetry ? client.retryConfig.max : 0;
|
|
27
|
+
for (let trial = 0; true; trial++) {
|
|
28
|
+
const infoParams = {
|
|
29
|
+
_q: queryName,
|
|
30
|
+
...trial > 0 ? { _retrial: trial + 1 } : {}
|
|
31
|
+
};
|
|
32
|
+
const {
|
|
33
|
+
data: responseData = {},
|
|
34
|
+
headers,
|
|
35
|
+
status
|
|
36
|
+
} = await axios.post(
|
|
37
|
+
client.url,
|
|
38
|
+
{ query, variables, operationName: queryName },
|
|
39
|
+
{
|
|
40
|
+
params: infoParams,
|
|
41
|
+
headers: {
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
...client.headers,
|
|
44
|
+
...requestHeaders
|
|
45
|
+
},
|
|
46
|
+
validateStatus: () => true
|
|
47
|
+
}
|
|
48
|
+
);
|
|
49
|
+
let { data, errors, warnings } = responseData;
|
|
50
|
+
if (status >= 400 && !errors?.length) {
|
|
51
|
+
errors = [{ message: `Request "${queryName}" failed with status ${status}` }];
|
|
52
|
+
}
|
|
53
|
+
lastResponse = {
|
|
54
|
+
errors: errorsParser ? errorsParser(errors) : errors,
|
|
55
|
+
data,
|
|
56
|
+
warnings,
|
|
57
|
+
headers,
|
|
58
|
+
status
|
|
59
|
+
};
|
|
60
|
+
if (!errors?.length) {
|
|
61
|
+
break;
|
|
62
|
+
} else if (trial < maxRetrials && typeof client.retryConfig?.before === "function") {
|
|
63
|
+
await client.retryConfig?.before({
|
|
64
|
+
queryName,
|
|
65
|
+
query,
|
|
66
|
+
variables,
|
|
67
|
+
response: lastResponse
|
|
68
|
+
});
|
|
69
|
+
if (client.retryConfig.waitBeforeRetry) {
|
|
70
|
+
await sleep(client.retryConfig.waitBeforeRetry);
|
|
71
|
+
}
|
|
72
|
+
} else if (trial >= maxRetrials) {
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (failureMode === "loud" && lastResponse.errors && lastResponse.errors?.length) {
|
|
77
|
+
throw new GraphQLClientError(lastResponse);
|
|
78
|
+
}
|
|
79
|
+
return lastResponse;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/jsonToGraphQLQuery.ts
|
|
83
|
+
import omit from "lodash/omit";
|
|
84
|
+
var VAR_PREFIX = "@@VAR@@";
|
|
85
|
+
var VAR_PREFIX_LENGTH = VAR_PREFIX.length;
|
|
86
|
+
var fromEntries = __require("lodash/fromPairs");
|
|
87
|
+
var entries = __require("lodash/toPairs");
|
|
88
|
+
var cloneDeep = __require("lodash/cloneDeep");
|
|
89
|
+
function jsonToGraphQLQuery({
|
|
90
|
+
kind,
|
|
91
|
+
queryName,
|
|
92
|
+
jsonQuery = {},
|
|
93
|
+
typesTree
|
|
94
|
+
}) {
|
|
95
|
+
const variablesData = {};
|
|
96
|
+
const alias = jsonQuery.__alias;
|
|
97
|
+
const newJsonQuery = cloneDeep(omit(jsonQuery, ["__alias", "__headers", "__url"]));
|
|
98
|
+
extractVariables({
|
|
99
|
+
jsonQuery: { [queryName]: newJsonQuery },
|
|
100
|
+
variables: variablesData,
|
|
101
|
+
parentType: kind === "query" ? typesTree.Query : typesTree.Mutation
|
|
102
|
+
});
|
|
103
|
+
const variableItems = Object.values(variablesData).reduce(
|
|
104
|
+
(variablesObj, variables2) => {
|
|
105
|
+
variables2.forEach((variable, index) => {
|
|
106
|
+
const name = variable.update(variables2.length > 1 ? index : void 0);
|
|
107
|
+
variablesObj[name] = { type: variable.type, value: variable.value };
|
|
108
|
+
});
|
|
109
|
+
return variablesObj;
|
|
110
|
+
},
|
|
111
|
+
{}
|
|
112
|
+
);
|
|
113
|
+
const variablesQuery = Object.keys(variableItems).length ? `(${entries(variableItems).map(([queryName2, { type }]) => `$${queryName2}: ${type}`).join(", ")})` : "";
|
|
114
|
+
const query = `${kind} ${alias || queryName}${variablesQuery} { ${alias ? `${alias}:` : ""}${queryName}${toGraphql(
|
|
115
|
+
newJsonQuery
|
|
116
|
+
)} }`;
|
|
117
|
+
const variables = fromEntries(entries(variableItems).map(([k, v]) => [k, v.value]));
|
|
118
|
+
return {
|
|
119
|
+
query,
|
|
120
|
+
variables
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function extractVariables({
|
|
124
|
+
jsonQuery,
|
|
125
|
+
variables,
|
|
126
|
+
parentType
|
|
127
|
+
}) {
|
|
128
|
+
if (!parentType)
|
|
129
|
+
return;
|
|
130
|
+
if (jsonQuery.__args) {
|
|
131
|
+
Object.keys(jsonQuery.__args).forEach((k) => {
|
|
132
|
+
if (typeof jsonQuery.__args[k] === "string" && jsonQuery.__args[k].startsWith(VAR_PREFIX))
|
|
133
|
+
return;
|
|
134
|
+
if (jsonQuery.__args[k] === void 0)
|
|
135
|
+
return;
|
|
136
|
+
const variableName = k;
|
|
137
|
+
if (!variables[variableName]) {
|
|
138
|
+
variables[variableName] = [];
|
|
139
|
+
}
|
|
140
|
+
variables[variableName].push({
|
|
141
|
+
name: variableName,
|
|
142
|
+
type: parentType.__args[k],
|
|
143
|
+
value: jsonQuery.__args[k],
|
|
144
|
+
update: (index) => {
|
|
145
|
+
const name = `${variableName}${index !== void 0 ? `_${index}` : ""}`;
|
|
146
|
+
jsonQuery.__args[k] = `${VAR_PREFIX}$${name}`;
|
|
147
|
+
return name;
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
jsonQuery.__args[k] = VAR_PREFIX;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
Object.keys(jsonQuery).filter((k) => k !== "__args" && typeof jsonQuery[k] === "object").forEach(
|
|
154
|
+
(k) => extractVariables({
|
|
155
|
+
jsonQuery: jsonQuery[k],
|
|
156
|
+
variables,
|
|
157
|
+
parentType: parentType.hasOwnProperty(k) ? parentType[k] : parentType.__fields ? parentType.__fields[k] : void 0
|
|
158
|
+
})
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
function toGraphql(jsonQuery) {
|
|
162
|
+
const fields = entries(jsonQuery).filter(([k, v]) => k !== "__args" && v !== false && v !== void 0).map(([k, v]) => typeof v === "object" ? `${k}${toGraphql(v)}` : k).join(" ");
|
|
163
|
+
const validArgs = jsonQuery.__args ? entries(jsonQuery.__args).filter(([_, v]) => v !== void 0) : [];
|
|
164
|
+
const argsQuery = validArgs.length ? `(${validArgs.map(([k, v]) => `${k}:${v.substr(VAR_PREFIX_LENGTH)}`).join(",")})` : "";
|
|
165
|
+
return `${argsQuery} ${fields ? `{ ${fields} }` : ""}`;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// src/logging.ts
|
|
169
|
+
function logRequest(logInfo) {
|
|
170
|
+
let identifier = `%c#graphql-ts-client ${logInfo.kind} ${logInfo.queryName}`;
|
|
171
|
+
let identifierStyles = "color: transparent; font-size: 0px";
|
|
172
|
+
console.groupCollapsed(
|
|
173
|
+
`%c#graphql-ts-client %c${logInfo.kind} %c${logInfo.queryName} %c(${logInfo.duration.toFixed(2)}ms)`,
|
|
174
|
+
"color: #f90",
|
|
175
|
+
"color: #999",
|
|
176
|
+
`color: ${logInfo.response ? "unset" : "#f00"}; font-weight: bold`,
|
|
177
|
+
"color: #999"
|
|
178
|
+
);
|
|
179
|
+
console.groupCollapsed(`%cQuery ${identifier}`, "color: #999", identifierStyles);
|
|
180
|
+
console.log(logInfo.formatGraphQL(logInfo.query) + identifier, identifierStyles);
|
|
181
|
+
console.groupEnd();
|
|
182
|
+
console.groupCollapsed(`%cVariables ${identifier}`, "color: #999", identifierStyles);
|
|
183
|
+
console.log(JSON.stringify(logInfo.variables, null, " ") + identifier, identifierStyles);
|
|
184
|
+
console.groupEnd();
|
|
185
|
+
console.groupCollapsed(`%cTrace ${identifier}`, "color: #999", identifierStyles);
|
|
186
|
+
console.trace(identifier, identifierStyles);
|
|
187
|
+
console.groupEnd();
|
|
188
|
+
if (logInfo.response) {
|
|
189
|
+
console.log("%cResponse".padEnd(15, " ") + identifier, "color: #999", identifierStyles, logInfo.response);
|
|
190
|
+
}
|
|
191
|
+
if (logInfo.error) {
|
|
192
|
+
console.log("%cError".padEnd(15, " ") + identifier, "color: #999", identifierStyles, logInfo.error);
|
|
193
|
+
}
|
|
194
|
+
console.groupEnd();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/endpoint.ts
|
|
198
|
+
var executeListeners = (listeners, data) => setTimeout(() => listeners.forEach((runResponseListener) => runResponseListener(data)));
|
|
199
|
+
var getApiEndpointCreator = (apiConfig) => (kind, queryName) => {
|
|
200
|
+
const rawEndpoint = async (failureMode, jsonQuery) => {
|
|
201
|
+
const clientConfig = apiConfig.getClient();
|
|
202
|
+
const alias = jsonQuery?.__alias ?? queryName;
|
|
203
|
+
const url = jsonQuery?.__url ?? clientConfig.url;
|
|
204
|
+
const shouldRetry = jsonQuery?.__retry ?? true;
|
|
205
|
+
const requestHeaders = jsonQuery?.__headers ?? {};
|
|
206
|
+
const { query, variables } = jsonToGraphQLQuery({ kind, queryName, jsonQuery, typesTree: apiConfig.typesTree });
|
|
207
|
+
const start = +new Date();
|
|
208
|
+
const logOptions = {
|
|
209
|
+
kind,
|
|
210
|
+
queryName: alias,
|
|
211
|
+
formatGraphQL: apiConfig.formatGraphQL,
|
|
212
|
+
requestHeaders,
|
|
213
|
+
query,
|
|
214
|
+
variables
|
|
215
|
+
};
|
|
216
|
+
const responseListenerData = {
|
|
217
|
+
queryName: alias,
|
|
218
|
+
query: apiConfig.formatGraphQL(query),
|
|
219
|
+
variables
|
|
220
|
+
};
|
|
221
|
+
try {
|
|
222
|
+
const { data, errors, warnings, headers, status } = await graphqlRequest({
|
|
223
|
+
shouldRetry,
|
|
224
|
+
failureMode,
|
|
225
|
+
queryName: alias,
|
|
226
|
+
client: { ...clientConfig, url },
|
|
227
|
+
requestHeaders,
|
|
228
|
+
query,
|
|
229
|
+
variables,
|
|
230
|
+
errorsParser: apiConfig.errorsParser
|
|
231
|
+
});
|
|
232
|
+
const response = { data, warnings, headers, status, errors };
|
|
233
|
+
if (apiConfig.verbose && globalThis.document) {
|
|
234
|
+
logRequest({
|
|
235
|
+
...logOptions,
|
|
236
|
+
response,
|
|
237
|
+
duration: +new Date() - start
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
executeListeners(apiConfig.responseListeners, {
|
|
241
|
+
...responseListenerData,
|
|
242
|
+
response
|
|
243
|
+
});
|
|
244
|
+
return { data: data?.[alias], errors, warnings, headers, status };
|
|
245
|
+
} catch (error) {
|
|
246
|
+
if (apiConfig.verbose && globalThis.document) {
|
|
247
|
+
logRequest({
|
|
248
|
+
...logOptions,
|
|
249
|
+
error,
|
|
250
|
+
duration: +new Date() - start
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
executeListeners(apiConfig.responseListeners, {
|
|
254
|
+
...responseListenerData,
|
|
255
|
+
response: error.response
|
|
256
|
+
});
|
|
257
|
+
throw error;
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const endpoint = async (jsonQuery) => {
|
|
261
|
+
const { data } = await rawEndpoint("loud", jsonQuery);
|
|
262
|
+
return data;
|
|
263
|
+
};
|
|
264
|
+
const memoizeeOptions = {
|
|
265
|
+
maxAge: apiConfig.maxAge,
|
|
266
|
+
isSerialized: true
|
|
267
|
+
};
|
|
268
|
+
endpoint.raw = rawEndpoint.bind(null, "silent");
|
|
269
|
+
endpoint.memo = memoize(endpoint, memoizeeOptions);
|
|
270
|
+
endpoint.memoRaw = memoize(endpoint.raw, memoizeeOptions);
|
|
271
|
+
return endpoint;
|
|
272
|
+
};
|
|
273
|
+
export {
|
|
274
|
+
getApiEndpointCreator
|
|
275
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { PathLike } from 'fs';
|
|
2
|
-
|
|
2
|
+
import { T as TypescriptClientOutput } from './types-23d2d6aa.js';
|
|
3
|
+
export { C as ClientConfig, c as DeepReplace, D as Defined, E as Endpoint, G as GraphQLClientError, I as IResponseListener, J as JsonOutput, L as LogInfo, M as Maybe, P as Projection, d as RawEndpoint, b as Replacement, R as ResponseData, a as ResponseListenerInfo, T as TypescriptClientOutput, U as Unpacked } from './types-23d2d6aa.js';
|
|
3
4
|
|
|
4
5
|
type IClientOptions = {
|
|
5
6
|
output?: PathLike;
|
|
@@ -14,11 +15,7 @@ type IClientOptions = {
|
|
|
14
15
|
skipCache?: boolean;
|
|
15
16
|
errorsParser?: (errors: any[]) => any;
|
|
16
17
|
};
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
js: string;
|
|
20
|
-
};
|
|
21
|
-
declare function generateTypescriptClient({ introspectionEndpoint, ...options }: IClientOptions): Promise<Client>;
|
|
22
|
-
declare function generateTypescriptClientFromSDL(SDL: string, options: IClientOptions): Client;
|
|
18
|
+
declare function generateTypescriptClient({ introspectionEndpoint, ...options }: IClientOptions): Promise<TypescriptClientOutput>;
|
|
19
|
+
declare function generateTypescriptClientFromSDL(SDL: string, options: IClientOptions): TypescriptClientOutput;
|
|
23
20
|
|
|
24
21
|
export { generateTypescriptClient, generateTypescriptClientFromSDL };
|
package/dist/index.js
CHANGED
|
@@ -477,7 +477,7 @@ var import_case = __toESM(require("case"));
|
|
|
477
477
|
var esbuild = __toESM(require("esbuild"));
|
|
478
478
|
var fs = __toESM(require("fs"));
|
|
479
479
|
var import_graphql = require("graphql");
|
|
480
|
-
var
|
|
480
|
+
var import_kebabCase = __toESM(require("lodash/kebabCase"));
|
|
481
481
|
var import_orderBy = __toESM(require("lodash/orderBy"));
|
|
482
482
|
var import_set = __toESM(require("lodash/set"));
|
|
483
483
|
var import_md5 = __toESM(require("md5"));
|
|
@@ -487,65 +487,52 @@ var prettier = __toESM(require("prettier"));
|
|
|
487
487
|
// package.json
|
|
488
488
|
var package_default = {
|
|
489
489
|
name: "@avantstay/graphql-ts-client",
|
|
490
|
-
version: "
|
|
490
|
+
version: "12.0.0",
|
|
491
491
|
description: "GraphQL Typescript Client Generator",
|
|
492
|
-
|
|
493
|
-
license: "MIT",
|
|
494
|
-
main: "dist/index.js",
|
|
495
|
-
typings: "dist/index.d.ts",
|
|
496
|
-
scripts: {
|
|
497
|
-
build: "tsup src/index.ts src/endpoint.ts --dts",
|
|
498
|
-
test: "cross-env GQL_CLIENT_DIST_PATH='.' jest --watch",
|
|
499
|
-
prepublishOnly: "yarn build"
|
|
500
|
-
},
|
|
501
|
-
files: [
|
|
502
|
-
"dist",
|
|
503
|
-
"src"
|
|
504
|
-
],
|
|
505
|
-
engines: {
|
|
506
|
-
node: ">=12"
|
|
507
|
-
},
|
|
492
|
+
homepage: "https://github.com/avantstay/graphql-ts-client",
|
|
508
493
|
bugs: {
|
|
509
494
|
url: "https://github.com/avantstay/graphql-ts-client/issues"
|
|
510
495
|
},
|
|
511
|
-
homepage: "https://github.com/avantstay/graphql-ts-client#readme",
|
|
512
496
|
repository: {
|
|
513
497
|
type: "git",
|
|
514
498
|
url: "git+https://github.com/avantstay/graphql-ts-client.git"
|
|
515
499
|
},
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
endOfLine: "lf",
|
|
527
|
-
printWidth: 130,
|
|
528
|
-
htmlWhitespaceSensitivity: "ignore",
|
|
529
|
-
jsxSingleQuote: false
|
|
530
|
-
},
|
|
531
|
-
module: "dist/graphql-ts-client.esm.js",
|
|
532
|
-
"size-limit": [
|
|
533
|
-
{
|
|
534
|
-
path: "dist/graphql-ts-client.cjs.production.min.js",
|
|
535
|
-
limit: "10 KB"
|
|
500
|
+
license: "MIT",
|
|
501
|
+
author: "Wellington Guimaraes",
|
|
502
|
+
contributors: [
|
|
503
|
+
"Felipe Pinheiro <felipe.pinheiro.90@hotmail.com>"
|
|
504
|
+
],
|
|
505
|
+
exports: {
|
|
506
|
+
".": {
|
|
507
|
+
import: "./dist/index.mjs",
|
|
508
|
+
require: "./dist/index.js",
|
|
509
|
+
types: "./dist/index.d.ts"
|
|
536
510
|
},
|
|
537
|
-
{
|
|
538
|
-
|
|
539
|
-
|
|
511
|
+
"./dist/endpoint": {
|
|
512
|
+
import: "./dist/endpoint.mjs",
|
|
513
|
+
require: "./dist/endpoint.js",
|
|
514
|
+
types: "./dist/endpoint.d.ts"
|
|
540
515
|
}
|
|
516
|
+
},
|
|
517
|
+
main: "dist/index.js",
|
|
518
|
+
module: "dist/index.mjs",
|
|
519
|
+
typings: "dist/index.d.ts",
|
|
520
|
+
files: [
|
|
521
|
+
"dist",
|
|
522
|
+
"src"
|
|
541
523
|
],
|
|
524
|
+
scripts: {
|
|
525
|
+
build: "yarn tsup src/index.ts src/endpoint.ts --dts --format cjs,esm",
|
|
526
|
+
prepublishOnly: "yarn build",
|
|
527
|
+
test: "cross-env GQL_CLIENT_DIST_PATH='.' jest --watch"
|
|
528
|
+
},
|
|
542
529
|
dependencies: {
|
|
543
530
|
"@types/md5": "^2.3.2",
|
|
544
531
|
axios: "^0.24.0",
|
|
545
532
|
"axios-retry": "^3.2.4",
|
|
546
533
|
case: "^1.6.3",
|
|
547
534
|
esbuild: "^0.13.14",
|
|
548
|
-
graphql: "^
|
|
535
|
+
graphql: "^16.13.2",
|
|
549
536
|
lodash: "^4.17.21",
|
|
550
537
|
md5: "^2.3.0",
|
|
551
538
|
moize: "^6.1.0",
|
|
@@ -574,9 +561,23 @@ var package_default = {
|
|
|
574
561
|
tsup: "^6.5.0",
|
|
575
562
|
typescript: "^4.9.4"
|
|
576
563
|
},
|
|
564
|
+
peerDependencies: {},
|
|
565
|
+
engines: {
|
|
566
|
+
node: ">=12"
|
|
567
|
+
},
|
|
577
568
|
publishConfig: {
|
|
578
569
|
"@avantstay:registry": "https://registry.npmjs.org/"
|
|
579
|
-
}
|
|
570
|
+
},
|
|
571
|
+
"size-limit": [
|
|
572
|
+
{
|
|
573
|
+
path: "dist/graphql-ts-client.cjs.production.min.js",
|
|
574
|
+
limit: "10 KB"
|
|
575
|
+
},
|
|
576
|
+
{
|
|
577
|
+
path: "dist/graphql-ts-client.esm.js",
|
|
578
|
+
limit: "10 KB"
|
|
579
|
+
}
|
|
580
|
+
]
|
|
580
581
|
};
|
|
581
582
|
// src/generateTypescriptClient.ts
|
|
582
583
|
var tempDir = fs.realpathSync(import_os.default.tmpdir());
|
|
@@ -639,7 +640,7 @@ function gqlFieldToTypescript(field, param) {
|
|
|
639
640
|
selection: false
|
|
640
641
|
});
|
|
641
642
|
});
|
|
642
|
-
fieldTypeDefinition = "{ __headers?: {[key: string]: string}; __retry?: boolean; __alias?: string; __args".concat(fieldsOnArgs.every(function(arg) {
|
|
643
|
+
fieldTypeDefinition = "{ __headers?: {[key: string]: string}; __retry?: boolean; __alias?: string; __url?: string; __args".concat(fieldsOnArgs.every(function(arg) {
|
|
643
644
|
return arg.isOptional;
|
|
644
645
|
}) ? "?" : "", ": { ").concat(fieldsOnArgs.map(function(arg) {
|
|
645
646
|
return arg.code;
|
|
@@ -679,7 +680,7 @@ function gqlEndpointToCode(kind, endpoint, codeOutputType) {
|
|
|
679
680
|
selection: true
|
|
680
681
|
});
|
|
681
682
|
var argsType = endpoint.args && endpoint.args.length ? getArgsType(endpoint) : null;
|
|
682
|
-
var inputType = "{\n __headers?: {[key: string]: string};\n __retry?: boolean;\n __alias?: string;\n ".concat(argsType ? "__args".concat(argsType.optional ? "?" : "", ": ").concat(argsType.alias) : "", "\n }").concat(selectionType ? " & ".concat(selectionType) : "");
|
|
683
|
+
var inputType = "{\n __headers?: {[key: string]: string};\n __retry?: boolean;\n __alias?: string;\n __url?: string;\n ".concat(argsType ? "__args".concat(argsType.optional ? "?" : "", ": ").concat(argsType.alias) : "", "\n }").concat(selectionType ? " & ".concat(selectionType) : "");
|
|
683
684
|
var outputType = gqlTypeToTypescript(endpoint.type, {
|
|
684
685
|
required: true
|
|
685
686
|
});
|
|
@@ -773,9 +774,12 @@ function generateClientCode(types, options) {
|
|
|
773
774
|
var clientCacheFileName = "gql-ts-client__client__".concat(typesHash, "__").concat(package_default.version, ".json");
|
|
774
775
|
var clientCacheFilePath = import_path.default.resolve(tempDir, clientCacheFileName);
|
|
775
776
|
if (!options.skipCache && fs.existsSync(clientCacheFilePath)) {
|
|
776
|
-
|
|
777
|
+
var output2 = JSON.parse(fs.readFileSync(clientCacheFilePath, {
|
|
777
778
|
encoding: "utf8"
|
|
778
779
|
}));
|
|
780
|
+
if (output2.js && output2.mjs && output2.typings) {
|
|
781
|
+
return output2;
|
|
782
|
+
}
|
|
779
783
|
}
|
|
780
784
|
var queries = ((_types_find = types.find(function(it) {
|
|
781
785
|
return it.name === "Query";
|
|
@@ -848,6 +852,10 @@ function generateClientCode(types, options) {
|
|
|
848
852
|
format: "cjs",
|
|
849
853
|
loader: "js"
|
|
850
854
|
}).code,
|
|
855
|
+
mjs: esbuild.transformSync(jsCode, {
|
|
856
|
+
format: "esm",
|
|
857
|
+
loader: "js"
|
|
858
|
+
}).code,
|
|
851
859
|
typings: prettier.format(typingsCode, {
|
|
852
860
|
semi: false,
|
|
853
861
|
parser: "typescript"
|
|
@@ -866,7 +874,7 @@ function _fetchIntrospection() {
|
|
|
866
874
|
switch(_state.label){
|
|
867
875
|
case 0:
|
|
868
876
|
endpoint = param.endpoint, headers = param.headers;
|
|
869
|
-
introspectionCacheFileName = "gql-ts-client__introspection__".concat((0,
|
|
877
|
+
introspectionCacheFileName = "gql-ts-client__introspection__".concat((0, import_kebabCase.default)(endpoint), ".json");
|
|
870
878
|
introspectionCacheFilePath = import_path.default.resolve(tempDir, introspectionCacheFileName);
|
|
871
879
|
loadedFromCache = false;
|
|
872
880
|
return [
|
|
@@ -876,20 +884,21 @@ function _fetchIntrospection() {
|
|
|
876
884
|
}, {
|
|
877
885
|
headers: _objectSpread({
|
|
878
886
|
"Content-Type": "application/json"
|
|
879
|
-
}, headers)
|
|
880
|
-
|
|
881
|
-
|
|
887
|
+
}, headers),
|
|
888
|
+
timeout: 5e3
|
|
889
|
+
}).catch(function(e) {
|
|
882
890
|
if (fs.existsSync(introspectionCacheFilePath)) {
|
|
883
891
|
var cachedSchema = JSON.parse(fs.readFileSync(introspectionCacheFilePath, {
|
|
884
892
|
encoding: "utf8"
|
|
885
893
|
}));
|
|
886
894
|
loadedFromCache = true;
|
|
887
|
-
console.warn("Successfully restored from local cache.");
|
|
895
|
+
console.warn("Successfully restored (".concat(endpoint, ") from local cache."));
|
|
888
896
|
return {
|
|
889
897
|
data: cachedSchema
|
|
890
898
|
};
|
|
891
899
|
} else {
|
|
892
|
-
|
|
900
|
+
console.error(e);
|
|
901
|
+
return Promise.reject("The GraphQL introspection request failed (".concat(endpoint, ")"));
|
|
893
902
|
}
|
|
894
903
|
})
|
|
895
904
|
];
|
|
@@ -915,7 +924,7 @@ function generateClient(introspectionTypes, _param) {
|
|
|
915
924
|
var output = _param.output, restOptions = _objectWithoutProperties(_param, [
|
|
916
925
|
"output"
|
|
917
926
|
]);
|
|
918
|
-
var _generateClientCode = generateClientCode(introspectionTypes, restOptions), js = _generateClientCode.js, typings = _generateClientCode.typings;
|
|
927
|
+
var _generateClientCode = generateClientCode(introspectionTypes, restOptions), js = _generateClientCode.js, mjs = _generateClientCode.mjs, typings = _generateClientCode.typings;
|
|
919
928
|
if (output && typeof output === "string") {
|
|
920
929
|
var outputDir = import_path.default.dirname(output);
|
|
921
930
|
if (!fs.existsSync(outputDir)) {
|
|
@@ -929,9 +938,13 @@ function generateClient(introspectionTypes, _param) {
|
|
|
929
938
|
fs.writeFileSync(output.replace(/(\.(ts|js))?$/, ".js"), js, {
|
|
930
939
|
encoding: "utf8"
|
|
931
940
|
});
|
|
941
|
+
fs.writeFileSync(output.replace(/(\.(ts|js))?$/, ".mjs"), mjs, {
|
|
942
|
+
encoding: "utf8"
|
|
943
|
+
});
|
|
932
944
|
}
|
|
933
945
|
return {
|
|
934
946
|
js: js,
|
|
947
|
+
mjs: mjs,
|
|
935
948
|
typings: typings
|
|
936
949
|
};
|
|
937
950
|
}
|
|
@@ -972,11 +985,15 @@ function _generateTypescriptClient() {
|
|
|
972
985
|
return _generateTypescriptClient.apply(this, arguments);
|
|
973
986
|
}
|
|
974
987
|
function generateTypescriptClientFromSDL(SDL, options) {
|
|
975
|
-
var
|
|
988
|
+
var _introspectionResult_data;
|
|
976
989
|
var _options_clientName;
|
|
977
990
|
console.log("Generating TypeScript client from SDL (name: ".concat((_options_clientName = options.clientName) !== null && _options_clientName !== void 0 ? _options_clientName : "n/a", ")"));
|
|
978
991
|
var graphqlSchemaObj = (0, import_graphql.buildSchema)(SDL);
|
|
979
|
-
var
|
|
992
|
+
var introspectionResult = (0, import_graphql.graphqlSync)({
|
|
993
|
+
schema: graphqlSchemaObj,
|
|
994
|
+
source: (0, import_graphql.getIntrospectionQuery)()
|
|
995
|
+
});
|
|
996
|
+
var introspectionTypes = (_introspectionResult_data = introspectionResult.data) === null || _introspectionResult_data === void 0 ? void 0 : _introspectionResult_data.__schema.types;
|
|
980
997
|
return generateClient(introspectionTypes, options);
|
|
981
998
|
}
|
|
982
999
|
// src/types.ts
|