@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.
@@ -1,5 +1,5 @@
1
1
  import { DynamicModule, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common';
2
- import { U as UserConfig } from '../index-_qRai4M3.cjs';
2
+ import { U as UserConfig } from '../index-DgIAN5k5.cjs';
3
3
  import 'ts-morph';
4
4
 
5
5
  /**
@@ -1,5 +1,5 @@
1
1
  import { DynamicModule, OnApplicationBootstrap, OnModuleDestroy } from '@nestjs/common';
2
- import { U as UserConfig } from '../index-_qRai4M3.js';
2
+ import { U as UserConfig } from '../index-DgIAN5k5.js';
3
3
  import 'ts-morph';
4
4
 
5
5
  /**
@@ -114,6 +114,7 @@ function applyDefaults(userConfig, cwd) {
114
114
  },
115
115
  app,
116
116
  fetcher: userConfig.fetcher ?? null,
117
+ serialization: userConfig.serialization ?? "json",
117
118
  forms: {
118
119
  enabled: userConfig.forms?.enabled ?? true,
119
120
  watch: userConfig.forms?.watch ?? "src/**/*.dto.ts",
@@ -2793,7 +2794,11 @@ function emitFilterQueryTypeArgs(c) {
2793
2794
  function emitFilterQueryType(c) {
2794
2795
  return `import('@dudousxd/nestjs-filter-client').TypedFilterQuery<${emitFilterQueryTypeArgs(c)}>`;
2795
2796
  }
2796
- function buildResponseType(c, outDir) {
2797
+ function buildResponseType(c, outDir, serialization) {
2798
+ const raw = rawResponseType(c, outDir);
2799
+ return serialization === "json" ? `Jsonify<${raw}>` : raw;
2800
+ }
2801
+ function rawResponseType(c, outDir) {
2797
2802
  const respRef = c.contractSource.responseRef;
2798
2803
  if (c.contractSource.stream) {
2799
2804
  if (respRef) return respRef.isArray ? `Array<${respRef.name}>` : respRef.name;
@@ -2816,7 +2821,7 @@ function buildErrorType(c) {
2816
2821
  }
2817
2822
  return c.contractSource.error ?? "unknown";
2818
2823
  }
2819
- function emitRouterTypeBlock(tree, indent, outDir) {
2824
+ function emitRouterTypeBlock(tree, indent, outDir, serialization) {
2820
2825
  const pad = " ".repeat(indent);
2821
2826
  const lines = [];
2822
2827
  for (const [key, node] of tree) {
@@ -2829,7 +2834,7 @@ function emitRouterTypeBlock(tree, indent, outDir) {
2829
2834
  const query = queryRef ? queryRef.isArray ? `Array<${queryRef.name}>` : queryRef.name : isFilterQuery ? emitFilterQueryType(c) : c.contractSource.query ?? "never";
2830
2835
  const bodyRef = c.contractSource.bodyRef;
2831
2836
  const body = method === "GET" ? "never" : bodyRef ? bodyRef.isArray ? `Array<${bodyRef.name}>` : bodyRef.name : c.contractSource.body ?? "never";
2832
- const response = buildResponseType(c, outDir);
2837
+ const response = buildResponseType(c, outDir, serialization);
2833
2838
  const error = buildErrorType(c);
2834
2839
  const params = buildParamsType(c.params);
2835
2840
  const safeMethod = JSON.stringify(method);
@@ -2841,7 +2846,7 @@ function emitRouterTypeBlock(tree, indent, outDir) {
2841
2846
  );
2842
2847
  } else {
2843
2848
  lines.push(`${pad}${objKey}: {`);
2844
- lines.push(...emitRouterTypeBlock(node.children, indent + 2, outDir));
2849
+ lines.push(...emitRouterTypeBlock(node.children, indent + 2, outDir, serialization));
2845
2850
  lines.push(`${pad}};`);
2846
2851
  }
2847
2852
  }
@@ -3046,6 +3051,7 @@ var EMPTY_PATH_NAMESPACE = [
3046
3051
  ];
3047
3052
  function buildApiFile(routes, outDir, opts = {}) {
3048
3053
  const fetcherImportPath = opts.fetcherImportPath;
3054
+ const serialization = opts.serialization ?? "json";
3049
3055
  const extensions = opts.extensions ?? [];
3050
3056
  const { layer } = resolveApiSlots(extensions);
3051
3057
  const memberExts = extensions.filter((e) => e.apiMembers);
@@ -3102,6 +3108,9 @@ function buildApiFile(routes, outDir, opts = {}) {
3102
3108
  );
3103
3109
  const runtimeImport = fetcherImportPath ?? "@dudousxd/nestjs-client";
3104
3110
  lines.push(`import type { Fetcher } from '${runtimeImport}';`);
3111
+ if (serialization === "json" && contracted.length > 0) {
3112
+ lines.push(`import type { Jsonify } from '${runtimeImport}';`);
3113
+ }
3105
3114
  if (importsByFile.size > 0 && outDir) {
3106
3115
  lines.push("");
3107
3116
  const emittedNames = /* @__PURE__ */ new Set();
@@ -3161,7 +3170,7 @@ function buildApiFile(routes, outDir, opts = {}) {
3161
3170
  insertIntoTree(tree, segments, leaf, name);
3162
3171
  }
3163
3172
  lines.push("export type ApiRouter = {");
3164
- lines.push(...emitRouterTypeBlock(tree, 2, outDir ?? ""));
3173
+ lines.push(...emitRouterTypeBlock(tree, 2, outDir ?? "", serialization));
3165
3174
  lines.push("};");
3166
3175
  lines.push("");
3167
3176
  lines.push(...emitReqHelper());
@@ -4179,6 +4188,7 @@ async function generate(config, inputRoutes = []) {
4179
4188
  if (hasContracts) {
4180
4189
  await emitApi(routes, config.codegen.outDir, {
4181
4190
  ...config.fetcher?.importPath ? { fetcherImportPath: config.fetcher.importPath } : {},
4191
+ serialization: config.serialization,
4182
4192
  extensions,
4183
4193
  ctx
4184
4194
  });