@orval/swr 6.19.1 → 6.21.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/dist/index.js +300 -33
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,50 +1,291 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
builder: () => builder,
|
|
24
|
+
default: () => src_default,
|
|
25
|
+
generateSwr: () => generateSwr,
|
|
26
|
+
generateSwrHeader: () => generateSwrHeader,
|
|
27
|
+
getSwrDependencies: () => getSwrDependencies
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
var import_core = require("@orval/core");
|
|
31
|
+
var AXIOS_DEPENDENCIES = [
|
|
32
|
+
{
|
|
33
|
+
exports: [
|
|
34
|
+
{
|
|
35
|
+
name: "axios",
|
|
36
|
+
default: true,
|
|
37
|
+
values: true,
|
|
38
|
+
syntheticDefaultImport: true
|
|
39
|
+
},
|
|
40
|
+
{ name: "AxiosRequestConfig" },
|
|
41
|
+
{ name: "AxiosResponse" },
|
|
42
|
+
{ name: "AxiosError" }
|
|
43
|
+
],
|
|
44
|
+
dependency: "axios"
|
|
45
|
+
}
|
|
46
|
+
];
|
|
47
|
+
var PARAMS_SERIALIZER_DEPENDENCIES = [
|
|
48
|
+
{
|
|
49
|
+
exports: [
|
|
50
|
+
{
|
|
51
|
+
name: "qs",
|
|
52
|
+
default: true,
|
|
53
|
+
values: true,
|
|
54
|
+
syntheticDefaultImport: true
|
|
55
|
+
}
|
|
56
|
+
],
|
|
57
|
+
dependency: "qs"
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
var SWR_DEPENDENCIES = [
|
|
61
|
+
{
|
|
62
|
+
exports: [
|
|
63
|
+
{ name: "useSwr", values: true, default: true },
|
|
64
|
+
{ name: "SWRConfiguration" },
|
|
65
|
+
{ name: "Key" }
|
|
66
|
+
],
|
|
67
|
+
dependency: "swr"
|
|
68
|
+
}
|
|
69
|
+
];
|
|
70
|
+
var getSwrDependencies = (hasGlobalMutator, hasParamsSerializerOptions) => [
|
|
71
|
+
...!hasGlobalMutator ? AXIOS_DEPENDENCIES : [],
|
|
72
|
+
...hasParamsSerializerOptions ? PARAMS_SERIALIZER_DEPENDENCIES : [],
|
|
73
|
+
...SWR_DEPENDENCIES
|
|
74
|
+
];
|
|
75
|
+
var generateSwrRequestFunction = ({
|
|
76
|
+
headers,
|
|
77
|
+
queryParams,
|
|
78
|
+
operationName,
|
|
79
|
+
response,
|
|
80
|
+
mutator,
|
|
81
|
+
body,
|
|
82
|
+
props,
|
|
83
|
+
verb,
|
|
84
|
+
formData,
|
|
85
|
+
formUrlEncoded,
|
|
86
|
+
override,
|
|
87
|
+
paramsSerializer
|
|
88
|
+
}, { route, context }) => {
|
|
89
|
+
var _a, _b;
|
|
90
|
+
const isRequestOptions = (override == null ? void 0 : override.requestOptions) !== false;
|
|
91
|
+
const isFormData = (override == null ? void 0 : override.formData) !== false;
|
|
92
|
+
const isFormUrlEncoded = (override == null ? void 0 : override.formUrlEncoded) !== false;
|
|
93
|
+
const isExactOptionalPropertyTypes = !!((_b = (_a = context.tsconfig) == null ? void 0 : _a.compilerOptions) == null ? void 0 : _b.exactOptionalPropertyTypes);
|
|
94
|
+
const isBodyVerb = import_core.VERBS_WITH_BODY.includes(verb);
|
|
95
|
+
const isSyntheticDefaultImportsAllowed = (0, import_core.isSyntheticDefaultImportsAllow)(
|
|
96
|
+
context.tsconfig
|
|
97
|
+
);
|
|
98
|
+
const bodyForm = (0, import_core.generateFormDataAndUrlEncodedFunction)({
|
|
99
|
+
formData,
|
|
100
|
+
formUrlEncoded,
|
|
101
|
+
body,
|
|
102
|
+
isFormData,
|
|
103
|
+
isFormUrlEncoded
|
|
104
|
+
});
|
|
105
|
+
if (mutator) {
|
|
106
|
+
const mutatorConfig = (0, import_core.generateMutatorConfig)({
|
|
107
|
+
route,
|
|
108
|
+
body,
|
|
109
|
+
headers,
|
|
110
|
+
queryParams,
|
|
111
|
+
response,
|
|
112
|
+
verb,
|
|
113
|
+
isFormData,
|
|
114
|
+
isFormUrlEncoded,
|
|
115
|
+
hasSignal: false,
|
|
116
|
+
isBodyVerb,
|
|
117
|
+
isExactOptionalPropertyTypes
|
|
118
|
+
});
|
|
119
|
+
const propsImplementation = (mutator == null ? void 0 : mutator.bodyTypeName) && body.definition ? (0, import_core.toObjectString)(props, "implementation").replace(
|
|
120
|
+
new RegExp(`(\\w*):\\s?${body.definition}`),
|
|
121
|
+
`$1: ${mutator.bodyTypeName}<${body.definition}>`
|
|
122
|
+
) : (0, import_core.toObjectString)(props, "implementation");
|
|
123
|
+
const requestOptions = isRequestOptions ? (0, import_core.generateMutatorRequestOptions)(
|
|
124
|
+
override == null ? void 0 : override.requestOptions,
|
|
125
|
+
mutator.hasSecondArg
|
|
126
|
+
) : "";
|
|
127
|
+
return `export const ${operationName} = (
|
|
128
|
+
${propsImplementation}
|
|
129
|
+
${isRequestOptions && mutator.hasSecondArg ? `options?: SecondParameter<typeof ${mutator.name}>` : ""}) => {${bodyForm}
|
|
130
|
+
return ${mutator.name}<${response.definition.success || "unknown"}>(
|
|
131
|
+
${mutatorConfig},
|
|
132
|
+
${requestOptions});
|
|
7
133
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
134
|
+
`;
|
|
135
|
+
}
|
|
136
|
+
const options = (0, import_core.generateOptions)({
|
|
137
|
+
route,
|
|
138
|
+
body,
|
|
139
|
+
headers,
|
|
140
|
+
queryParams,
|
|
141
|
+
response,
|
|
142
|
+
verb,
|
|
143
|
+
requestOptions: override == null ? void 0 : override.requestOptions,
|
|
144
|
+
isFormData,
|
|
145
|
+
isFormUrlEncoded,
|
|
146
|
+
paramsSerializer,
|
|
147
|
+
paramsSerializerOptions: override == null ? void 0 : override.paramsSerializerOptions,
|
|
148
|
+
isExactOptionalPropertyTypes,
|
|
149
|
+
hasSignal: false
|
|
150
|
+
});
|
|
151
|
+
return `export const ${operationName} = (
|
|
152
|
+
${(0, import_core.toObjectString)(
|
|
153
|
+
props,
|
|
154
|
+
"implementation"
|
|
155
|
+
)} ${isRequestOptions ? `options?: AxiosRequestConfig
|
|
156
|
+
` : ""} ): Promise<AxiosResponse<${response.definition.success || "unknown"}>> => {${bodyForm}
|
|
157
|
+
return axios${!isSyntheticDefaultImportsAllowed ? ".default" : ""}.${verb}(${options});
|
|
158
|
+
}
|
|
159
|
+
`;
|
|
160
|
+
};
|
|
161
|
+
var generateSwrArguments = ({
|
|
162
|
+
operationName,
|
|
163
|
+
mutator,
|
|
164
|
+
isRequestOptions
|
|
165
|
+
}) => {
|
|
166
|
+
const definition = `SWRConfiguration<Awaited<ReturnType<typeof ${operationName}>>, TError> & { swrKey?: Key, enabled?: boolean }`;
|
|
167
|
+
if (!isRequestOptions) {
|
|
168
|
+
return `swrOptions?: ${definition}`;
|
|
169
|
+
}
|
|
170
|
+
return `options?: { swr?:${definition}, ${!mutator ? `axios?: AxiosRequestConfig` : (mutator == null ? void 0 : mutator.hasSecondArg) ? `request?: SecondParameter<typeof ${mutator.name}>` : ""} }
|
|
171
|
+
`;
|
|
172
|
+
};
|
|
173
|
+
var generateSwrImplementation = ({
|
|
174
|
+
operationName,
|
|
175
|
+
swrKeyFnName,
|
|
176
|
+
swrProperties,
|
|
177
|
+
swrKeyProperties,
|
|
178
|
+
params,
|
|
179
|
+
mutator,
|
|
180
|
+
isRequestOptions,
|
|
181
|
+
response,
|
|
182
|
+
swrOptions,
|
|
183
|
+
props,
|
|
184
|
+
doc
|
|
185
|
+
}) => {
|
|
186
|
+
var _a;
|
|
187
|
+
const swrProps = (0, import_core.toObjectString)(props, "implementation");
|
|
188
|
+
const httpFunctionProps = swrProperties;
|
|
189
|
+
const swrKeyImplementation = `const isEnabled = swrOptions?.enabled !== false${params.length ? ` && !!(${params.map(({ name }) => name).join(" && ")})` : ""}
|
|
190
|
+
const swrKey = swrOptions?.swrKey ?? (() => isEnabled ? ${swrKeyFnName}(${swrKeyProperties}) : null);`;
|
|
191
|
+
let errorType = `AxiosError<${response.definition.errors || "unknown"}>`;
|
|
192
|
+
if (mutator) {
|
|
193
|
+
errorType = mutator.hasErrorType ? `ErrorType<${response.definition.errors || "unknown"}>` : response.definition.errors || "unknown";
|
|
194
|
+
}
|
|
195
|
+
return `
|
|
196
|
+
export type ${(0, import_core.pascal)(
|
|
197
|
+
operationName
|
|
198
|
+
)}QueryResult = NonNullable<Awaited<ReturnType<typeof ${operationName}>>>
|
|
199
|
+
export type ${(0, import_core.pascal)(operationName)}QueryError = ${errorType}
|
|
200
|
+
|
|
201
|
+
${doc}export const ${(0, import_core.camel)(
|
|
202
|
+
`use-${operationName}`
|
|
203
|
+
)} = <TError = ${errorType}>(
|
|
204
|
+
${swrProps} ${generateSwrArguments({
|
|
205
|
+
operationName,
|
|
206
|
+
mutator,
|
|
207
|
+
isRequestOptions
|
|
208
|
+
})}
|
|
21
209
|
) => {
|
|
22
210
|
|
|
23
|
-
${
|
|
211
|
+
${isRequestOptions ? `const {swr: swrOptions${!mutator ? `, axios: axiosOptions` : (mutator == null ? void 0 : mutator.hasSecondArg) ? ", request: requestOptions" : ""}} = options ?? {}` : ""}
|
|
24
212
|
|
|
25
|
-
${
|
|
26
|
-
const swrFn = () => ${
|
|
213
|
+
${swrKeyImplementation}
|
|
214
|
+
const swrFn = () => ${operationName}(${httpFunctionProps}${httpFunctionProps ? ", " : ""}${isRequestOptions ? !mutator ? `axiosOptions` : (mutator == null ? void 0 : mutator.hasSecondArg) ? "requestOptions" : "" : ""});
|
|
27
215
|
|
|
28
|
-
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(swrKey, swrFn, ${
|
|
29
|
-
${(
|
|
216
|
+
const query = useSwr<Awaited<ReturnType<typeof swrFn>>, TError>(swrKey, swrFn, ${swrOptions.options ? `{
|
|
217
|
+
${(_a = (0, import_core.stringify)(swrOptions.options)) == null ? void 0 : _a.slice(1, -1)}
|
|
30
218
|
...swrOptions
|
|
31
|
-
}
|
|
219
|
+
}` : "swrOptions"})
|
|
32
220
|
|
|
33
221
|
return {
|
|
34
222
|
swrKey,
|
|
35
223
|
...query
|
|
36
224
|
}
|
|
37
225
|
}
|
|
38
|
-
|
|
226
|
+
`;
|
|
227
|
+
};
|
|
228
|
+
var generateSwrHook = ({
|
|
229
|
+
queryParams,
|
|
230
|
+
operationName,
|
|
231
|
+
body,
|
|
232
|
+
props,
|
|
233
|
+
verb,
|
|
234
|
+
params,
|
|
235
|
+
override,
|
|
236
|
+
mutator,
|
|
237
|
+
response,
|
|
238
|
+
summary,
|
|
239
|
+
deprecated
|
|
240
|
+
}, { route }) => {
|
|
241
|
+
const isRequestOptions = (override == null ? void 0 : override.requestOptions) !== false;
|
|
242
|
+
if (verb !== import_core.Verbs.GET) {
|
|
243
|
+
return "";
|
|
244
|
+
}
|
|
245
|
+
const swrProperties = props.map((param) => {
|
|
246
|
+
if (param.type === import_core.GetterPropType.NAMED_PATH_PARAMS)
|
|
247
|
+
return param.destructured;
|
|
248
|
+
return param.type === import_core.GetterPropType.BODY ? body.implementation : param.name;
|
|
249
|
+
}).join(",");
|
|
250
|
+
const swrKeyProperties = props.filter((prop) => prop.type !== import_core.GetterPropType.HEADER).map((param) => {
|
|
251
|
+
if (param.type === import_core.GetterPropType.NAMED_PATH_PARAMS)
|
|
252
|
+
return param.destructured;
|
|
253
|
+
return param.type === import_core.GetterPropType.BODY ? body.implementation : param.name;
|
|
254
|
+
}).join(",");
|
|
255
|
+
const swrKeyFnName = (0, import_core.camel)(`get-${operationName}-key`);
|
|
256
|
+
const queryKeyProps = (0, import_core.toObjectString)(
|
|
257
|
+
props.filter((prop) => prop.type !== import_core.GetterPropType.HEADER),
|
|
258
|
+
"implementation"
|
|
259
|
+
);
|
|
260
|
+
const doc = (0, import_core.jsDoc)({ summary, deprecated });
|
|
261
|
+
return `export const ${swrKeyFnName} = (${queryKeyProps}) => [\`${route}\`${queryParams ? ", ...(params ? [params]: [])" : ""}${body.implementation ? `, ${body.implementation}` : ""}] as const;
|
|
39
262
|
|
|
40
|
-
${
|
|
41
|
-
|
|
42
|
-
|
|
263
|
+
${generateSwrImplementation({
|
|
264
|
+
operationName,
|
|
265
|
+
swrKeyFnName,
|
|
266
|
+
swrProperties,
|
|
267
|
+
swrKeyProperties,
|
|
268
|
+
params,
|
|
269
|
+
props,
|
|
270
|
+
mutator,
|
|
271
|
+
isRequestOptions,
|
|
272
|
+
response,
|
|
273
|
+
swrOptions: override.swr,
|
|
274
|
+
doc
|
|
275
|
+
})}
|
|
276
|
+
`;
|
|
277
|
+
};
|
|
278
|
+
var generateSwrHeader = ({
|
|
279
|
+
isRequestOptions,
|
|
280
|
+
isMutator,
|
|
281
|
+
hasAwaitedType
|
|
282
|
+
}) => `
|
|
283
|
+
${!hasAwaitedType ? `type AwaitedInput<T> = PromiseLike<T> | T;
|
|
43
284
|
|
|
44
285
|
type Awaited<O> = O extends AwaitedInput<infer T> ? T : never;
|
|
45
286
|
|
|
46
|
-
`}
|
|
47
|
-
${
|
|
287
|
+
` : ""}
|
|
288
|
+
${isRequestOptions && isMutator ? `// eslint-disable-next-line
|
|
48
289
|
type SecondParameter<T extends (...args: any) => any> = T extends (
|
|
49
290
|
config: any,
|
|
50
291
|
args: infer P,
|
|
@@ -52,6 +293,32 @@ ${t}export const ${_core.camel.call(void 0, `use-${e}`)} = <TError = ${l}>(
|
|
|
52
293
|
? P
|
|
53
294
|
: never;
|
|
54
295
|
|
|
55
|
-
|
|
296
|
+
` : ""}`;
|
|
297
|
+
var generateSwr = (verbOptions, options) => {
|
|
298
|
+
const imports = (0, import_core.generateVerbImports)(verbOptions);
|
|
299
|
+
const functionImplementation = generateSwrRequestFunction(
|
|
300
|
+
verbOptions,
|
|
301
|
+
options
|
|
302
|
+
);
|
|
303
|
+
const hookImplementation = generateSwrHook(verbOptions, options);
|
|
304
|
+
return {
|
|
305
|
+
implementation: `${functionImplementation}
|
|
56
306
|
|
|
57
|
-
${
|
|
307
|
+
${hookImplementation}`,
|
|
308
|
+
imports
|
|
309
|
+
};
|
|
310
|
+
};
|
|
311
|
+
var swrClientBuilder = {
|
|
312
|
+
client: generateSwr,
|
|
313
|
+
header: generateSwrHeader,
|
|
314
|
+
dependencies: getSwrDependencies
|
|
315
|
+
};
|
|
316
|
+
var builder = () => () => swrClientBuilder;
|
|
317
|
+
var src_default = builder;
|
|
318
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
319
|
+
0 && (module.exports = {
|
|
320
|
+
builder,
|
|
321
|
+
generateSwr,
|
|
322
|
+
generateSwrHeader,
|
|
323
|
+
getSwrDependencies
|
|
324
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orval/swr",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.21.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
"dist"
|
|
9
9
|
],
|
|
10
10
|
"scripts": {
|
|
11
|
-
"build": "tsup ./src/index.ts --target node12 --
|
|
11
|
+
"build": "tsup ./src/index.ts --target node12 --clean --dts",
|
|
12
12
|
"dev": "tsup ./src/index.ts --target node12 --clean --watch src",
|
|
13
13
|
"lint": "eslint src/**/*.ts"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@orval/core": "6.
|
|
16
|
+
"@orval/core": "6.21.0"
|
|
17
17
|
}
|
|
18
18
|
}
|