@dudousxd/nestjs-codegen 0.6.0 → 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
  });
@@ -4336,6 +4346,7 @@ async function watch(config, onChange) {
4336
4346
  return NO_OP_WATCHER;
4337
4347
  }
4338
4348
  let discovery = null;
4349
+ let lastRoutes = [];
4339
4350
  async function getDiscovery() {
4340
4351
  if (discovery === null) {
4341
4352
  discovery = await PersistentDiscovery.create({
@@ -4349,13 +4360,14 @@ async function watch(config, onChange) {
4349
4360
  }
4350
4361
  try {
4351
4362
  const initialRoutes = (await getDiscovery()).discover();
4363
+ lastRoutes = initialRoutes;
4352
4364
  await generate(config, initialRoutes);
4353
4365
  } catch (err) {
4354
4366
  console.warn(
4355
4367
  `[nestjs-codegen] Initial route discovery failed, falling back to pages-only: ${err instanceof Error ? err.message : String(err)}`
4356
4368
  );
4357
4369
  try {
4358
- await generate(config);
4370
+ await generate(config, lastRoutes);
4359
4371
  } catch {
4360
4372
  }
4361
4373
  }
@@ -4373,7 +4385,7 @@ async function watch(config, onChange) {
4373
4385
  pagesDebounceTimer = setTimeout(async () => {
4374
4386
  pagesDebounceTimer = void 0;
4375
4387
  try {
4376
- await generate(config);
4388
+ await generate(config, lastRoutes);
4377
4389
  } catch (err) {
4378
4390
  console.error(
4379
4391
  "[nestjs-codegen] Pages generation failed:",
@@ -4404,6 +4416,7 @@ async function watch(config, onChange) {
4404
4416
  pendingChangedPaths.clear();
4405
4417
  try {
4406
4418
  const routes = await (await getDiscovery()).rediscover(changed);
4419
+ lastRoutes = routes;
4407
4420
  await generate(config, routes);
4408
4421
  } catch (err) {
4409
4422
  console.error(
@@ -4444,7 +4457,7 @@ async function watch(config, onChange) {
4444
4457
  }
4445
4458
 
4446
4459
  // src/index.ts
4447
- var VERSION = "0.6.0";
4460
+ var VERSION = "0.7.0";
4448
4461
 
4449
4462
  // src/cli/codegen.ts
4450
4463
  async function runCodegen(opts = {}) {