@dudousxd/nestjs-codegen 0.13.0 → 0.13.2

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.
@@ -1467,6 +1467,19 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth, subst = /
1467
1467
  dbg("unresolvable type:", name, "in", sourceFile.getFilePath());
1468
1468
  return "unknown";
1469
1469
  }
1470
+ if (import_ts_morph5.Node.isTypeLiteral(typeNode)) {
1471
+ const members = [];
1472
+ for (const member of typeNode.getMembers()) {
1473
+ if (import_ts_morph5.Node.isPropertySignature(member)) {
1474
+ const memberTypeNode = member.getTypeNode();
1475
+ const memberType = memberTypeNode ? resolveTypeNodeToString(memberTypeNode, sourceFile, project, depth, subst) : "unknown";
1476
+ members.push(`${member.getName()}${member.hasQuestionToken() ? "?" : ""}: ${memberType}`);
1477
+ } else {
1478
+ members.push(member.getText());
1479
+ }
1480
+ }
1481
+ return members.length > 0 ? `{ ${members.join("; ")} }` : "{}";
1482
+ }
1470
1483
  const kind = typeNode.getKind();
1471
1484
  if (kind === import_ts_morph5.SyntaxKind.StringKeyword) return "string";
1472
1485
  if (kind === import_ts_morph5.SyntaxKind.NumberKeyword) return "number";
@@ -1773,10 +1786,7 @@ function extractDtoContract(method, sourceFile, project) {
1773
1786
  const filterInfo = extractApplyFilterInfo(method, sourceFile, project);
1774
1787
  const query = extractQueryType(method, sourceFile, project);
1775
1788
  const uploads = extractUploadedFiles(method);
1776
- if (uploads.fields) {
1777
- const fileObject = `{ ${uploads.fields} }`;
1778
- body = body ? `(${body}) & ${fileObject}` : fileObject;
1779
- }
1789
+ const multipartBody = uploads.fields ? `{ ${uploads.fields} }` : null;
1780
1790
  const streamElement = detectStreamElement(method);
1781
1791
  const isStream = streamElement !== null;
1782
1792
  if (filterInfo && filterInfo.source === "body") {
@@ -1860,7 +1870,8 @@ function extractDtoContract(method, sourceFile, project) {
1860
1870
  bodySchema,
1861
1871
  querySchema,
1862
1872
  stream: isStream,
1863
- multipart: uploads.multipart
1873
+ multipart: uploads.multipart,
1874
+ multipartBody
1864
1875
  };
1865
1876
  }
1866
1877
  function resolveParamClass(method, decoratorName, sourceFile, project) {
@@ -2328,7 +2339,8 @@ function extractDtoRoute(args) {
2328
2339
  bodySchema: dtoContract?.bodySchema ?? null,
2329
2340
  querySchema: dtoContract?.querySchema ?? null,
2330
2341
  stream: dtoContract?.stream ?? false,
2331
- multipart: dtoContract?.multipart ?? false
2342
+ multipart: dtoContract?.multipart ?? false,
2343
+ multipartBody: dtoContract?.multipartBody ?? null
2332
2344
  }
2333
2345
  });
2334
2346
  }
@@ -2939,7 +2951,15 @@ function emitRouterTypeBlock(tree, indent, outDir, serialization) {
2939
2951
  const isFilterQuery = c.contractSource.filterSource === "query" && !!c.contractSource.filterFields?.length;
2940
2952
  const query = queryRef ? queryRef.isArray ? `Array<${queryRef.name}>` : queryRef.name : isFilterQuery ? emitFilterQueryType(c) : c.contractSource.query ?? "never";
2941
2953
  const bodyRef = c.contractSource.bodyRef;
2942
- const body = method === "GET" ? "never" : bodyRef ? bodyRef.isArray ? `Array<${bodyRef.name}>` : bodyRef.name : c.contractSource.body ?? "never";
2954
+ let body = method === "GET" ? "never" : bodyRef ? bodyRef.isArray ? `Array<${bodyRef.name}>` : bodyRef.name : c.contractSource.body ?? "never";
2955
+ const multipartBody = c.contractSource.multipartBody;
2956
+ if (c.contractSource.multipart && multipartBody) {
2957
+ if (body === "never") {
2958
+ body = multipartBody;
2959
+ } else if (!bodyAcceptsAnything(body)) {
2960
+ body = `(${body}) & ${multipartBody}`;
2961
+ }
2962
+ }
2943
2963
  const response = buildResponseType(c, outDir, serialization);
2944
2964
  const error = buildErrorType(c);
2945
2965
  const params = buildParamsType(c.params);
@@ -2958,6 +2978,25 @@ function emitRouterTypeBlock(tree, indent, outDir, serialization) {
2958
2978
  }
2959
2979
  return lines;
2960
2980
  }
2981
+ function topLevelUnionArms(type) {
2982
+ const arms = [];
2983
+ let depth = 0;
2984
+ let start = 0;
2985
+ for (let i = 0; i < type.length; i++) {
2986
+ const ch = type[i];
2987
+ if (ch === "{" || ch === "[" || ch === "<" || ch === "(") depth++;
2988
+ else if (ch === "}" || ch === "]" || ch === ">" || ch === ")") depth--;
2989
+ else if (ch === "|" && depth === 0) {
2990
+ arms.push(type.slice(start, i).trim());
2991
+ start = i + 1;
2992
+ }
2993
+ }
2994
+ arms.push(type.slice(start).trim());
2995
+ return arms;
2996
+ }
2997
+ function bodyAcceptsAnything(body) {
2998
+ return topLevelUnionArms(body).some((arm) => arm === "unknown" || arm === "any");
2999
+ }
2961
3000
  function buildRequestModel(c) {
2962
3001
  const m = c.method.toLowerCase();
2963
3002
  const flat = JSON.stringify(c.name);
@@ -4321,7 +4360,7 @@ async function acquireLock(outDir) {
4321
4360
  }
4322
4361
 
4323
4362
  // src/index.ts
4324
- var VERSION = "0.13.0";
4363
+ var VERSION = "0.13.2";
4325
4364
 
4326
4365
  // src/generate-manifest.ts
4327
4366
  var MANIFEST_FILE = ".codegen-manifest.json";