@dudousxd/nestjs-codegen 0.6.1 → 0.7.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/dist/cli/main.js CHANGED
@@ -44,18 +44,23 @@ async function fileExists(filePath) {
44
44
  }
45
45
  }
46
46
  async function importTs(filePath) {
47
- let tsImport;
47
+ const fileUrl = pathToFileURL(filePath).href;
48
48
  try {
49
- const tsxEsm = await import("tsx/esm/api");
50
- tsImport = tsxEsm.tsImport;
51
- } catch {
52
- throw new ConfigError(
53
- "Failed to load config: `tsx` is required for loading TypeScript config files. Install it as a dev dependency: pnpm add -D tsx"
54
- );
49
+ return await import(fileUrl);
50
+ } catch (nativeError) {
51
+ let tsImport;
52
+ try {
53
+ const tsxEsm = await import("tsx/esm/api");
54
+ tsImport = tsxEsm.tsImport;
55
+ } catch {
56
+ throw new ConfigError(
57
+ "Failed to load config: `tsx` is required for loading TypeScript config files. Install it as a dev dependency: pnpm add -D tsx",
58
+ { cause: nativeError }
59
+ );
60
+ }
61
+ const parentURL = pathToFileURL(`${filePath}__parent__`).href;
62
+ return tsImport(fileUrl, { parentURL });
55
63
  }
56
- const parentURL = pathToFileURL(`${filePath}__parent__`).href;
57
- const fileUrl = pathToFileURL(filePath).href;
58
- return tsImport(fileUrl, { parentURL });
59
64
  }
60
65
  function resolveAbsolute(cwd, p) {
61
66
  if (isAbsolute(p)) return p;
@@ -122,6 +127,7 @@ function applyDefaults(userConfig, cwd) {
122
127
  },
123
128
  app,
124
129
  fetcher: userConfig.fetcher ?? null,
130
+ serialization: userConfig.serialization ?? "json",
125
131
  forms: {
126
132
  enabled: userConfig.forms?.enabled ?? true,
127
133
  watch: userConfig.forms?.watch ?? "src/**/*.dto.ts",
@@ -705,7 +711,11 @@ function emitFilterQueryTypeArgs(c) {
705
711
  function emitFilterQueryType(c) {
706
712
  return `import('@dudousxd/nestjs-filter-client').TypedFilterQuery<${emitFilterQueryTypeArgs(c)}>`;
707
713
  }
708
- function buildResponseType(c, outDir) {
714
+ function buildResponseType(c, outDir, serialization) {
715
+ const raw = rawResponseType(c, outDir);
716
+ return serialization === "json" ? `Jsonify<${raw}>` : raw;
717
+ }
718
+ function rawResponseType(c, outDir) {
709
719
  const respRef = c.contractSource.responseRef;
710
720
  if (c.contractSource.stream) {
711
721
  if (respRef) return respRef.isArray ? `Array<${respRef.name}>` : respRef.name;
@@ -728,7 +738,7 @@ function buildErrorType(c) {
728
738
  }
729
739
  return c.contractSource.error ?? "unknown";
730
740
  }
731
- function emitRouterTypeBlock(tree, indent, outDir) {
741
+ function emitRouterTypeBlock(tree, indent, outDir, serialization) {
732
742
  const pad = " ".repeat(indent);
733
743
  const lines = [];
734
744
  for (const [key, node] of tree) {
@@ -741,7 +751,7 @@ function emitRouterTypeBlock(tree, indent, outDir) {
741
751
  const query = queryRef ? queryRef.isArray ? `Array<${queryRef.name}>` : queryRef.name : isFilterQuery ? emitFilterQueryType(c) : c.contractSource.query ?? "never";
742
752
  const bodyRef = c.contractSource.bodyRef;
743
753
  const body = method === "GET" ? "never" : bodyRef ? bodyRef.isArray ? `Array<${bodyRef.name}>` : bodyRef.name : c.contractSource.body ?? "never";
744
- const response = buildResponseType(c, outDir);
754
+ const response = buildResponseType(c, outDir, serialization);
745
755
  const error = buildErrorType(c);
746
756
  const params = buildParamsType(c.params);
747
757
  const safeMethod = JSON.stringify(method);
@@ -753,7 +763,7 @@ function emitRouterTypeBlock(tree, indent, outDir) {
753
763
  );
754
764
  } else {
755
765
  lines.push(`${pad}${objKey}: {`);
756
- lines.push(...emitRouterTypeBlock(node.children, indent + 2, outDir));
766
+ lines.push(...emitRouterTypeBlock(node.children, indent + 2, outDir, serialization));
757
767
  lines.push(`${pad}};`);
758
768
  }
759
769
  }
@@ -958,6 +968,7 @@ var EMPTY_PATH_NAMESPACE = [
958
968
  ];
959
969
  function buildApiFile(routes, outDir, opts = {}) {
960
970
  const fetcherImportPath = opts.fetcherImportPath;
971
+ const serialization = opts.serialization ?? "json";
961
972
  const extensions = opts.extensions ?? [];
962
973
  const { layer } = resolveApiSlots(extensions);
963
974
  const memberExts = extensions.filter((e) => e.apiMembers);
@@ -1014,6 +1025,9 @@ function buildApiFile(routes, outDir, opts = {}) {
1014
1025
  );
1015
1026
  const runtimeImport = fetcherImportPath ?? "@dudousxd/nestjs-client";
1016
1027
  lines.push(`import type { Fetcher } from '${runtimeImport}';`);
1028
+ if (serialization === "json" && contracted.length > 0) {
1029
+ lines.push(`import type { Jsonify } from '${runtimeImport}';`);
1030
+ }
1017
1031
  if (importsByFile.size > 0 && outDir) {
1018
1032
  lines.push("");
1019
1033
  const emittedNames = /* @__PURE__ */ new Set();
@@ -1073,7 +1087,7 @@ function buildApiFile(routes, outDir, opts = {}) {
1073
1087
  insertIntoTree(tree, segments, leaf, name);
1074
1088
  }
1075
1089
  lines.push("export type ApiRouter = {");
1076
- lines.push(...emitRouterTypeBlock(tree, 2, outDir ?? ""));
1090
+ lines.push(...emitRouterTypeBlock(tree, 2, outDir ?? "", serialization));
1077
1091
  lines.push("};");
1078
1092
  lines.push("");
1079
1093
  lines.push(...emitReqHelper());
@@ -2091,6 +2105,7 @@ async function generate(config, inputRoutes = []) {
2091
2105
  if (hasContracts) {
2092
2106
  await emitApi(routes, config.codegen.outDir, {
2093
2107
  ...config.fetcher?.importPath ? { fetcherImportPath: config.fetcher.importPath } : {},
2108
+ serialization: config.serialization,
2094
2109
  extensions,
2095
2110
  ctx
2096
2111
  });
@@ -4447,7 +4462,7 @@ async function watch(config, onChange) {
4447
4462
  }
4448
4463
 
4449
4464
  // src/index.ts
4450
- var VERSION = "0.6.1";
4465
+ var VERSION = "0.7.1";
4451
4466
 
4452
4467
  // src/cli/codegen.ts
4453
4468
  async function runCodegen(opts = {}) {