@dudousxd/nestjs-codegen 0.6.1 → 0.7.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/cli/main.js CHANGED
@@ -122,6 +122,7 @@ function applyDefaults(userConfig, cwd) {
122
122
  },
123
123
  app,
124
124
  fetcher: userConfig.fetcher ?? null,
125
+ serialization: userConfig.serialization ?? "json",
125
126
  forms: {
126
127
  enabled: userConfig.forms?.enabled ?? true,
127
128
  watch: userConfig.forms?.watch ?? "src/**/*.dto.ts",
@@ -705,7 +706,11 @@ function emitFilterQueryTypeArgs(c) {
705
706
  function emitFilterQueryType(c) {
706
707
  return `import('@dudousxd/nestjs-filter-client').TypedFilterQuery<${emitFilterQueryTypeArgs(c)}>`;
707
708
  }
708
- function buildResponseType(c, outDir) {
709
+ function buildResponseType(c, outDir, serialization) {
710
+ const raw = rawResponseType(c, outDir);
711
+ return serialization === "json" ? `Jsonify<${raw}>` : raw;
712
+ }
713
+ function rawResponseType(c, outDir) {
709
714
  const respRef = c.contractSource.responseRef;
710
715
  if (c.contractSource.stream) {
711
716
  if (respRef) return respRef.isArray ? `Array<${respRef.name}>` : respRef.name;
@@ -728,7 +733,7 @@ function buildErrorType(c) {
728
733
  }
729
734
  return c.contractSource.error ?? "unknown";
730
735
  }
731
- function emitRouterTypeBlock(tree, indent, outDir) {
736
+ function emitRouterTypeBlock(tree, indent, outDir, serialization) {
732
737
  const pad = " ".repeat(indent);
733
738
  const lines = [];
734
739
  for (const [key, node] of tree) {
@@ -741,7 +746,7 @@ function emitRouterTypeBlock(tree, indent, outDir) {
741
746
  const query = queryRef ? queryRef.isArray ? `Array<${queryRef.name}>` : queryRef.name : isFilterQuery ? emitFilterQueryType(c) : c.contractSource.query ?? "never";
742
747
  const bodyRef = c.contractSource.bodyRef;
743
748
  const body = method === "GET" ? "never" : bodyRef ? bodyRef.isArray ? `Array<${bodyRef.name}>` : bodyRef.name : c.contractSource.body ?? "never";
744
- const response = buildResponseType(c, outDir);
749
+ const response = buildResponseType(c, outDir, serialization);
745
750
  const error = buildErrorType(c);
746
751
  const params = buildParamsType(c.params);
747
752
  const safeMethod = JSON.stringify(method);
@@ -753,7 +758,7 @@ function emitRouterTypeBlock(tree, indent, outDir) {
753
758
  );
754
759
  } else {
755
760
  lines.push(`${pad}${objKey}: {`);
756
- lines.push(...emitRouterTypeBlock(node.children, indent + 2, outDir));
761
+ lines.push(...emitRouterTypeBlock(node.children, indent + 2, outDir, serialization));
757
762
  lines.push(`${pad}};`);
758
763
  }
759
764
  }
@@ -958,6 +963,7 @@ var EMPTY_PATH_NAMESPACE = [
958
963
  ];
959
964
  function buildApiFile(routes, outDir, opts = {}) {
960
965
  const fetcherImportPath = opts.fetcherImportPath;
966
+ const serialization = opts.serialization ?? "json";
961
967
  const extensions = opts.extensions ?? [];
962
968
  const { layer } = resolveApiSlots(extensions);
963
969
  const memberExts = extensions.filter((e) => e.apiMembers);
@@ -1014,6 +1020,9 @@ function buildApiFile(routes, outDir, opts = {}) {
1014
1020
  );
1015
1021
  const runtimeImport = fetcherImportPath ?? "@dudousxd/nestjs-client";
1016
1022
  lines.push(`import type { Fetcher } from '${runtimeImport}';`);
1023
+ if (serialization === "json" && contracted.length > 0) {
1024
+ lines.push(`import type { Jsonify } from '${runtimeImport}';`);
1025
+ }
1017
1026
  if (importsByFile.size > 0 && outDir) {
1018
1027
  lines.push("");
1019
1028
  const emittedNames = /* @__PURE__ */ new Set();
@@ -1073,7 +1082,7 @@ function buildApiFile(routes, outDir, opts = {}) {
1073
1082
  insertIntoTree(tree, segments, leaf, name);
1074
1083
  }
1075
1084
  lines.push("export type ApiRouter = {");
1076
- lines.push(...emitRouterTypeBlock(tree, 2, outDir ?? ""));
1085
+ lines.push(...emitRouterTypeBlock(tree, 2, outDir ?? "", serialization));
1077
1086
  lines.push("};");
1078
1087
  lines.push("");
1079
1088
  lines.push(...emitReqHelper());
@@ -2091,6 +2100,7 @@ async function generate(config, inputRoutes = []) {
2091
2100
  if (hasContracts) {
2092
2101
  await emitApi(routes, config.codegen.outDir, {
2093
2102
  ...config.fetcher?.importPath ? { fetcherImportPath: config.fetcher.importPath } : {},
2103
+ serialization: config.serialization,
2094
2104
  extensions,
2095
2105
  ctx
2096
2106
  });
@@ -4447,7 +4457,7 @@ async function watch(config, onChange) {
4447
4457
  }
4448
4458
 
4449
4459
  // src/index.ts
4450
- var VERSION = "0.6.1";
4460
+ var VERSION = "0.7.0";
4451
4461
 
4452
4462
  // src/cli/codegen.ts
4453
4463
  async function runCodegen(opts = {}) {