mobx-tanstack-query-api 0.36.1 → 0.37.1

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