@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.
@@ -150,6 +150,7 @@ function applyDefaults(userConfig, cwd) {
150
150
  },
151
151
  app,
152
152
  fetcher: userConfig.fetcher ?? null,
153
+ serialization: userConfig.serialization ?? "json",
153
154
  forms: {
154
155
  enabled: userConfig.forms?.enabled ?? true,
155
156
  watch: userConfig.forms?.watch ?? "src/**/*.dto.ts",
@@ -2814,7 +2815,11 @@ function emitFilterQueryTypeArgs(c) {
2814
2815
  function emitFilterQueryType(c) {
2815
2816
  return `import('@dudousxd/nestjs-filter-client').TypedFilterQuery<${emitFilterQueryTypeArgs(c)}>`;
2816
2817
  }
2817
- function buildResponseType(c, outDir) {
2818
+ function buildResponseType(c, outDir, serialization) {
2819
+ const raw = rawResponseType(c, outDir);
2820
+ return serialization === "json" ? `Jsonify<${raw}>` : raw;
2821
+ }
2822
+ function rawResponseType(c, outDir) {
2818
2823
  const respRef = c.contractSource.responseRef;
2819
2824
  if (c.contractSource.stream) {
2820
2825
  if (respRef) return respRef.isArray ? `Array<${respRef.name}>` : respRef.name;
@@ -2837,7 +2842,7 @@ function buildErrorType(c) {
2837
2842
  }
2838
2843
  return c.contractSource.error ?? "unknown";
2839
2844
  }
2840
- function emitRouterTypeBlock(tree, indent, outDir) {
2845
+ function emitRouterTypeBlock(tree, indent, outDir, serialization) {
2841
2846
  const pad = " ".repeat(indent);
2842
2847
  const lines = [];
2843
2848
  for (const [key, node] of tree) {
@@ -2850,7 +2855,7 @@ function emitRouterTypeBlock(tree, indent, outDir) {
2850
2855
  const query = queryRef ? queryRef.isArray ? `Array<${queryRef.name}>` : queryRef.name : isFilterQuery ? emitFilterQueryType(c) : c.contractSource.query ?? "never";
2851
2856
  const bodyRef = c.contractSource.bodyRef;
2852
2857
  const body = method === "GET" ? "never" : bodyRef ? bodyRef.isArray ? `Array<${bodyRef.name}>` : bodyRef.name : c.contractSource.body ?? "never";
2853
- const response = buildResponseType(c, outDir);
2858
+ const response = buildResponseType(c, outDir, serialization);
2854
2859
  const error = buildErrorType(c);
2855
2860
  const params = buildParamsType(c.params);
2856
2861
  const safeMethod = JSON.stringify(method);
@@ -2862,7 +2867,7 @@ function emitRouterTypeBlock(tree, indent, outDir) {
2862
2867
  );
2863
2868
  } else {
2864
2869
  lines.push(`${pad}${objKey}: {`);
2865
- lines.push(...emitRouterTypeBlock(node.children, indent + 2, outDir));
2870
+ lines.push(...emitRouterTypeBlock(node.children, indent + 2, outDir, serialization));
2866
2871
  lines.push(`${pad}};`);
2867
2872
  }
2868
2873
  }
@@ -3067,6 +3072,7 @@ var EMPTY_PATH_NAMESPACE = [
3067
3072
  ];
3068
3073
  function buildApiFile(routes, outDir, opts = {}) {
3069
3074
  const fetcherImportPath = opts.fetcherImportPath;
3075
+ const serialization = opts.serialization ?? "json";
3070
3076
  const extensions = opts.extensions ?? [];
3071
3077
  const { layer } = resolveApiSlots(extensions);
3072
3078
  const memberExts = extensions.filter((e) => e.apiMembers);
@@ -3123,6 +3129,9 @@ function buildApiFile(routes, outDir, opts = {}) {
3123
3129
  );
3124
3130
  const runtimeImport = fetcherImportPath ?? "@dudousxd/nestjs-client";
3125
3131
  lines.push(`import type { Fetcher } from '${runtimeImport}';`);
3132
+ if (serialization === "json" && contracted.length > 0) {
3133
+ lines.push(`import type { Jsonify } from '${runtimeImport}';`);
3134
+ }
3126
3135
  if (importsByFile.size > 0 && outDir) {
3127
3136
  lines.push("");
3128
3137
  const emittedNames = /* @__PURE__ */ new Set();
@@ -3182,7 +3191,7 @@ function buildApiFile(routes, outDir, opts = {}) {
3182
3191
  insertIntoTree(tree, segments, leaf, name);
3183
3192
  }
3184
3193
  lines.push("export type ApiRouter = {");
3185
- lines.push(...emitRouterTypeBlock(tree, 2, outDir ?? ""));
3194
+ lines.push(...emitRouterTypeBlock(tree, 2, outDir ?? "", serialization));
3186
3195
  lines.push("};");
3187
3196
  lines.push("");
3188
3197
  lines.push(...emitReqHelper());
@@ -4200,6 +4209,7 @@ async function generate(config, inputRoutes = []) {
4200
4209
  if (hasContracts) {
4201
4210
  await emitApi(routes, config.codegen.outDir, {
4202
4211
  ...config.fetcher?.importPath ? { fetcherImportPath: config.fetcher.importPath } : {},
4212
+ serialization: config.serialization,
4203
4213
  extensions,
4204
4214
  ctx
4205
4215
  });