mobx-tanstack-query-api 0.36.1 → 0.37.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/cli.cjs +184 -29
- package/cli.cjs.map +1 -1
- package/cli.js +184 -29
- package/cli.js.map +1 -1
- package/package.json +1 -1
package/cli.cjs
CHANGED
|
@@ -56,38 +56,72 @@ ${contract2.content}}`;
|
|
|
56
56
|
const contractType = `${"export "}${templateFn(contract)}`;
|
|
57
57
|
return result + contractType;
|
|
58
58
|
};
|
|
59
|
+
function getResponseContentTypes(raw) {
|
|
60
|
+
const out = [];
|
|
61
|
+
if (Array.isArray(raw.produces)) out.push(...raw.produces);
|
|
62
|
+
const status = raw.responses && Object.keys(raw.responses).find((s) => {
|
|
63
|
+
const code = Number.parseInt(s, 10);
|
|
64
|
+
return code >= 200 && code < 300;
|
|
65
|
+
});
|
|
66
|
+
const content = status && raw.responses[status]?.content;
|
|
67
|
+
if (content && typeof content === "object") out.push(...Object.keys(content));
|
|
68
|
+
return [...new Set(out)];
|
|
69
|
+
}
|
|
70
|
+
function getRequestContentTypes(raw) {
|
|
71
|
+
const out = [];
|
|
72
|
+
if (Array.isArray(raw.consumes)) out.push(...raw.consumes);
|
|
73
|
+
const body = raw.requestBody?.content;
|
|
74
|
+
if (body && typeof body === "object") out.push(...Object.keys(body));
|
|
75
|
+
return [...new Set(out)];
|
|
76
|
+
}
|
|
77
|
+
function getSecuritySchemeNames(security) {
|
|
78
|
+
if (!Array.isArray(security)) return [];
|
|
79
|
+
return security.flatMap(
|
|
80
|
+
(s) => typeof s === "object" && s !== null ? Object.keys(s) : []
|
|
81
|
+
).filter(Boolean);
|
|
82
|
+
}
|
|
83
|
+
function getExternalDocsLine(externalDocs, formatDescription) {
|
|
84
|
+
if (!externalDocs || typeof externalDocs !== "object" || !("url" in externalDocs))
|
|
85
|
+
return null;
|
|
86
|
+
const url = externalDocs.url;
|
|
87
|
+
if (!url) return null;
|
|
88
|
+
const desc = externalDocs.description;
|
|
89
|
+
const extDesc = desc ? ` ${formatDescription(String(desc).slice(0, 60), true)}` : "";
|
|
90
|
+
return { name: "see", content: `${url}${extDesc}`.trim() };
|
|
91
|
+
}
|
|
92
|
+
function getSummaryAndDescriptionLines(rawRoute, formatDescription) {
|
|
93
|
+
const out = [];
|
|
94
|
+
if (rawRoute.summary) {
|
|
95
|
+
const summaryLines = text.splitTextByLines(rawRoute.summary, 60).filter(Boolean).map((line) => ({ content: `**${formatDescription(line, true)}**` }));
|
|
96
|
+
if (summaryLines.length > 0) out.push(...summaryLines, { content: "" });
|
|
97
|
+
}
|
|
98
|
+
if (rawRoute.description) {
|
|
99
|
+
const descriptionLines = text.splitTextByLines(rawRoute.description, 60).filter(Boolean).map((line) => ({ content: formatDescription(line, true) }));
|
|
100
|
+
if (descriptionLines.length > 0)
|
|
101
|
+
out.push(...descriptionLines, { content: "" });
|
|
102
|
+
} else if (!rawRoute.summary) {
|
|
103
|
+
out.push({ content: "No description" });
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
59
107
|
const endpointJSDocTmpl = (params) => {
|
|
60
108
|
const { route, configuration, offset = 0 } = params;
|
|
61
109
|
const { routeName } = route;
|
|
62
110
|
const rawRoute = route.raw;
|
|
63
111
|
const routeRequest = route.request;
|
|
112
|
+
const path2 = routeRequest?.path;
|
|
113
|
+
const method = routeRequest?.method;
|
|
64
114
|
const { utils } = configuration;
|
|
65
115
|
const { _, formatDescription } = utils;
|
|
116
|
+
const schema = configuration.config?.swaggerSchema ?? configuration.swaggerSchema;
|
|
117
|
+
const pathKey = path2?.startsWith("/") ? path2 : `/${path2 || ""}`;
|
|
118
|
+
const methodKey = method?.toLowerCase?.() ?? method;
|
|
119
|
+
const schemaOp = pathKey && methodKey ? schema?.paths?.[pathKey]?.[methodKey] : null;
|
|
120
|
+
const rawWithSpec = schemaOp && typeof schemaOp === "object" ? { ...schemaOp, ...rawRoute } : rawRoute;
|
|
66
121
|
const jsDocLines = [];
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}));
|
|
71
|
-
if (summaryLines.length > 0) {
|
|
72
|
-
jsDocLines.push(...summaryLines, { content: "" });
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
if (rawRoute.description) {
|
|
76
|
-
const descriptionLines = text.splitTextByLines(rawRoute.description, 60).filter(Boolean).map((line) => ({
|
|
77
|
-
content: formatDescription(line, true)
|
|
78
|
-
}));
|
|
79
|
-
if (descriptionLines.length > 0) {
|
|
80
|
-
jsDocLines.push(...descriptionLines, { content: "" });
|
|
81
|
-
} else {
|
|
82
|
-
jsDocLines.push({
|
|
83
|
-
content: "No description"
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
} else {
|
|
87
|
-
jsDocLines.push({
|
|
88
|
-
content: "No description"
|
|
89
|
-
});
|
|
90
|
-
}
|
|
122
|
+
jsDocLines.push(
|
|
123
|
+
...getSummaryAndDescriptionLines(rawRoute, formatDescription)
|
|
124
|
+
);
|
|
91
125
|
if (rawRoute.operationId) {
|
|
92
126
|
jsDocLines.push({
|
|
93
127
|
name: "operationId",
|
|
@@ -104,6 +138,20 @@ const endpointJSDocTmpl = (params) => {
|
|
|
104
138
|
name: "request",
|
|
105
139
|
content: `**${_.upperCase(routeRequest.method)}:${rawRoute.route}**`
|
|
106
140
|
});
|
|
141
|
+
const responseTypes = getResponseContentTypes(rawWithSpec);
|
|
142
|
+
if (responseTypes.length > 0) {
|
|
143
|
+
jsDocLines.push({
|
|
144
|
+
name: "produces",
|
|
145
|
+
content: responseTypes.join(", ")
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
const requestTypes = getRequestContentTypes(rawWithSpec);
|
|
149
|
+
if (requestTypes.length > 0) {
|
|
150
|
+
jsDocLines.push({
|
|
151
|
+
name: "consumes",
|
|
152
|
+
content: requestTypes.join(", ")
|
|
153
|
+
});
|
|
154
|
+
}
|
|
107
155
|
if (rawRoute.deprecated) {
|
|
108
156
|
jsDocLines.push({
|
|
109
157
|
name: "deprecated"
|
|
@@ -121,10 +169,17 @@ const endpointJSDocTmpl = (params) => {
|
|
|
121
169
|
);
|
|
122
170
|
}
|
|
123
171
|
if (routeRequest.security) {
|
|
124
|
-
jsDocLines.push({
|
|
125
|
-
|
|
126
|
-
|
|
172
|
+
jsDocLines.push({ name: "secure" });
|
|
173
|
+
const schemeNames = getSecuritySchemeNames(rawWithSpec.security);
|
|
174
|
+
if (schemeNames.length > 0) {
|
|
175
|
+
jsDocLines.push({ name: "security", content: schemeNames.join(", ") });
|
|
176
|
+
}
|
|
127
177
|
}
|
|
178
|
+
const externalDocsLine = getExternalDocsLine(
|
|
179
|
+
rawWithSpec.externalDocs,
|
|
180
|
+
formatDescription
|
|
181
|
+
);
|
|
182
|
+
if (externalDocsLine) jsDocLines.push(externalDocsLine);
|
|
128
183
|
if (rawRoute.responsesTypes.length > 0) {
|
|
129
184
|
jsDocLines.push({
|
|
130
185
|
name: "responses"
|
|
@@ -219,6 +274,94 @@ const responseContentKind = {
|
|
|
219
274
|
FORM_DATA: '"formData"',
|
|
220
275
|
BYTES: '"bytes"'
|
|
221
276
|
};
|
|
277
|
+
function inferResponseFormatFromRaw(raw) {
|
|
278
|
+
const contentTypes = [];
|
|
279
|
+
if (Array.isArray(raw.produces)) {
|
|
280
|
+
contentTypes.push(...raw.produces);
|
|
281
|
+
}
|
|
282
|
+
const successStatus = raw.responses && Object.keys(raw.responses).find((s) => {
|
|
283
|
+
const code = Number.parseInt(s, 10);
|
|
284
|
+
return code >= 200 && code < 300;
|
|
285
|
+
});
|
|
286
|
+
const content = successStatus && raw.responses[successStatus]?.content;
|
|
287
|
+
if (content && typeof content === "object") {
|
|
288
|
+
contentTypes.push(...Object.keys(content));
|
|
289
|
+
}
|
|
290
|
+
if (contentTypes.length === 0) return null;
|
|
291
|
+
const mimeToFormat = (mime) => {
|
|
292
|
+
if (mime.includes("application/json") || mime.includes("+json"))
|
|
293
|
+
return '"json"';
|
|
294
|
+
if (mime.startsWith("text/")) return '"text"';
|
|
295
|
+
if (mime.includes("form-data") || mime.includes("multipart"))
|
|
296
|
+
return '"formData"';
|
|
297
|
+
if (mime.includes("octet-stream") || mime.includes("spreadsheet") || mime.includes("vnd.") || mime.startsWith("application/") || mime.startsWith("image/") || mime.startsWith("audio/") || mime.startsWith("video/") || mime.startsWith("font/") || mime.startsWith("model/") || mime.startsWith("message/") || mime.startsWith("haptics/"))
|
|
298
|
+
return '"blob"';
|
|
299
|
+
return null;
|
|
300
|
+
};
|
|
301
|
+
const preferredOrder = ['"json"', '"text"', '"formData"', '"blob"'];
|
|
302
|
+
for (const fmt of preferredOrder) {
|
|
303
|
+
const found = contentTypes.map(mimeToFormat).find((f) => f === fmt);
|
|
304
|
+
if (found) return found;
|
|
305
|
+
}
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
function getResponseFormat(responseBodyInfo, raw, configuration, path2, method) {
|
|
309
|
+
const fromContentKind = responseContentKind[responseBodyInfo.success?.schema?.contentKind];
|
|
310
|
+
if (fromContentKind) return fromContentKind;
|
|
311
|
+
const swaggerSchema = configuration.config?.swaggerSchema ?? configuration.swaggerSchema;
|
|
312
|
+
const schemaPaths = swaggerSchema?.paths;
|
|
313
|
+
const pathKey = path2?.startsWith("/") ? path2 : `/${path2 || ""}`;
|
|
314
|
+
const methodKey = method?.toLowerCase?.() ?? method;
|
|
315
|
+
const schemaOperation = pathKey && methodKey ? schemaPaths?.[pathKey]?.[methodKey] : null;
|
|
316
|
+
const rawWithProduces = schemaOperation && typeof schemaOperation === "object" ? { ...schemaOperation, ...raw } : raw;
|
|
317
|
+
return inferResponseFormatFromRaw(rawWithProduces);
|
|
318
|
+
}
|
|
319
|
+
function inferRequestBodyContentTypeFromRaw(raw) {
|
|
320
|
+
const contentTypes = [];
|
|
321
|
+
if (Array.isArray(raw.consumes)) {
|
|
322
|
+
contentTypes.push(...raw.consumes);
|
|
323
|
+
}
|
|
324
|
+
const requestBody = raw.requestBody;
|
|
325
|
+
if (requestBody?.content && typeof requestBody.content === "object") {
|
|
326
|
+
contentTypes.push(...Object.keys(requestBody.content));
|
|
327
|
+
}
|
|
328
|
+
if (contentTypes.length === 0) return null;
|
|
329
|
+
const mimeToContentType = (mime) => {
|
|
330
|
+
if (mime.includes("application/json") || mime.includes("+json"))
|
|
331
|
+
return '"application/json"';
|
|
332
|
+
if (mime.includes("application/x-www-form-urlencoded"))
|
|
333
|
+
return '"application/x-www-form-urlencoded"';
|
|
334
|
+
if (mime.includes("multipart/form-data") || mime.includes("multipart/"))
|
|
335
|
+
return '"multipart/form-data"';
|
|
336
|
+
if (mime.startsWith("text/")) return '"text/plain"';
|
|
337
|
+
if (mime.includes("octet-stream") || mime.startsWith("application/") || mime.startsWith("image/") || mime.startsWith("audio/") || mime.startsWith("video/") || mime.startsWith("font/") || mime.startsWith("model/") || mime.includes("vnd."))
|
|
338
|
+
return '"application/octet-stream"';
|
|
339
|
+
return null;
|
|
340
|
+
};
|
|
341
|
+
const preferredOrder = [
|
|
342
|
+
'"application/json"',
|
|
343
|
+
'"application/x-www-form-urlencoded"',
|
|
344
|
+
'"multipart/form-data"',
|
|
345
|
+
'"text/plain"',
|
|
346
|
+
'"application/octet-stream"'
|
|
347
|
+
];
|
|
348
|
+
for (const ct of preferredOrder) {
|
|
349
|
+
const found = contentTypes.map(mimeToContentType).find((c) => c === ct);
|
|
350
|
+
if (found) return found;
|
|
351
|
+
}
|
|
352
|
+
return null;
|
|
353
|
+
}
|
|
354
|
+
function getRequestBodyContentType(requestBodyInfo, raw, configuration, path2, method) {
|
|
355
|
+
const fromContentKind = requestContentKind[requestBodyInfo?.contentKind];
|
|
356
|
+
if (fromContentKind) return fromContentKind;
|
|
357
|
+
const swaggerSchema = configuration.config?.swaggerSchema ?? configuration.swaggerSchema;
|
|
358
|
+
const schemaPaths = swaggerSchema?.paths;
|
|
359
|
+
const pathKey = path2?.startsWith("/") ? path2 : `/${path2 || ""}`;
|
|
360
|
+
const methodKey = method?.toLowerCase?.() ?? method;
|
|
361
|
+
const schemaOperation = pathKey && methodKey ? schemaPaths?.[pathKey]?.[methodKey] : null;
|
|
362
|
+
const rawWithConsumes = schemaOperation && typeof schemaOperation === "object" ? { ...schemaOperation, ...raw } : raw;
|
|
363
|
+
return inferRequestBodyContentTypeFromRaw(rawWithConsumes);
|
|
364
|
+
}
|
|
222
365
|
const newEndpointTmpl = ({
|
|
223
366
|
route,
|
|
224
367
|
codegenParams,
|
|
@@ -285,8 +428,20 @@ const newEndpointTmpl = ({
|
|
|
285
428
|
const requestInfoMeta = codegenParams.getEndpointMeta?.(route, utils);
|
|
286
429
|
const requestMeta = codegenParams.getRequestMeta?.(route, utils);
|
|
287
430
|
const resultPath = (codegenParams.requestPathPrefix ?? "") + path2 + (codegenParams.requestPathSuffix ?? "");
|
|
288
|
-
const bodyContentType =
|
|
289
|
-
|
|
431
|
+
const bodyContentType = getRequestBodyContentType(
|
|
432
|
+
requestBodyInfo,
|
|
433
|
+
raw,
|
|
434
|
+
configuration,
|
|
435
|
+
path2,
|
|
436
|
+
method
|
|
437
|
+
) || null;
|
|
438
|
+
const responseFormat = getResponseFormat(
|
|
439
|
+
responseBodyInfo,
|
|
440
|
+
raw,
|
|
441
|
+
configuration,
|
|
442
|
+
path2,
|
|
443
|
+
method
|
|
444
|
+
) || null;
|
|
290
445
|
const reservedDataContractNames = _.uniq([
|
|
291
446
|
...requestOutputDataTypes,
|
|
292
447
|
requestOutputErrorType || "any",
|