@dudousxd/nestjs-inertia-codegen 1.0.5 → 1.0.6

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/index.cjs CHANGED
@@ -375,16 +375,19 @@ function buildRouterTypeAccess(name) {
375
375
  __name(buildRouterTypeAccess, "buildRouterTypeAccess");
376
376
  function buildApiFile(routes) {
377
377
  const contracted = routes.filter((r) => r.contract);
378
+ const hasGetRoutes = contracted.some((r) => r.method === "GET");
378
379
  const lines = [
379
380
  "// Generated by @dudousxd/nestjs-inertia-codegen. Do not edit.",
380
- "",
381
- "import { queryOptions } from '@tanstack/query-core';",
382
- "import { route } from './routes.js';",
383
- "import { createFetcher } from '@dudousxd/nestjs-inertia-client';",
384
- "",
385
- "export const fetcher = createFetcher();",
386
381
  ""
387
382
  ];
383
+ if (hasGetRoutes) {
384
+ lines.push("import { queryOptions } from '@tanstack/query-core';");
385
+ }
386
+ lines.push("import { route } from './routes.js';");
387
+ lines.push("import { createFetcher } from '@dudousxd/nestjs-inertia-client';");
388
+ lines.push("");
389
+ lines.push("export const fetcher = createFetcher();");
390
+ lines.push("");
388
391
  if (contracted.length === 0) {
389
392
  lines.push("export type ApiRouter = Record<string, never>;");
390
393
  lines.push("");
@@ -899,7 +902,42 @@ function extractParams(path) {
899
902
  }));
900
903
  }
901
904
  __name(extractParams, "extractParams");
902
- function resolveImportedClass(name, sourceFile, project) {
905
+ function findTypeInFile(name, file) {
906
+ const cls = file.getClass(name);
907
+ if (cls) return {
908
+ kind: "class",
909
+ decl: cls,
910
+ file
911
+ };
912
+ const iface = file.getInterface(name);
913
+ if (iface) return {
914
+ kind: "interface",
915
+ decl: iface,
916
+ file
917
+ };
918
+ const alias = file.getTypeAlias(name);
919
+ if (alias) {
920
+ const typeNode = alias.getTypeNode();
921
+ return {
922
+ kind: "typeAlias",
923
+ text: typeNode ? typeNode.getText() : "unknown"
924
+ };
925
+ }
926
+ const enumDecl = file.getEnum(name);
927
+ if (enumDecl) {
928
+ const members = enumDecl.getMembers().map((m) => {
929
+ const val = m.getValue();
930
+ return typeof val === "string" ? JSON.stringify(val) : JSON.stringify(m.getName());
931
+ });
932
+ return {
933
+ kind: "enum",
934
+ members
935
+ };
936
+ }
937
+ return null;
938
+ }
939
+ __name(findTypeInFile, "findTypeInFile");
940
+ function resolveImportedType(name, sourceFile, project) {
903
941
  for (const importDecl of sourceFile.getImportDeclarations()) {
904
942
  const namedImport = importDecl.getNamedImports().find((n) => n.getName() === name);
905
943
  if (!namedImport) continue;
@@ -919,25 +957,19 @@ function resolveImportedClass(name, sourceFile, project) {
919
957
  continue;
920
958
  }
921
959
  }
922
- const cls = importedFile.getClass(name);
923
- if (cls) return {
924
- cls,
925
- file: importedFile
926
- };
960
+ const result = findTypeInFile(name, importedFile);
961
+ if (result) return result;
927
962
  }
928
963
  }
929
964
  return null;
930
965
  }
931
- __name(resolveImportedClass, "resolveImportedClass");
932
- function findClass(name, sourceFile, project) {
933
- const local = sourceFile.getClass(name);
934
- if (local) return {
935
- cls: local,
936
- file: sourceFile
937
- };
938
- return resolveImportedClass(name, sourceFile, project);
966
+ __name(resolveImportedType, "resolveImportedType");
967
+ function findType(name, sourceFile, project) {
968
+ const local = findTypeInFile(name, sourceFile);
969
+ if (local) return local;
970
+ return resolveImportedType(name, sourceFile, project);
939
971
  }
940
- __name(findClass, "findClass");
972
+ __name(findType, "findType");
941
973
  function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
942
974
  if (depth <= 0) return "unknown";
943
975
  if (import_ts_morph.Node.isArrayTypeNode(typeNode)) {
@@ -966,9 +998,9 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
966
998
  }
967
999
  return "unknown";
968
1000
  }
969
- const resolved = findClass(name, sourceFile, project);
1001
+ const resolved = findType(name, sourceFile, project);
970
1002
  if (resolved) {
971
- return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
1003
+ return expandTypeDecl(resolved, project, depth - 1);
972
1004
  }
973
1005
  return name;
974
1006
  }
@@ -981,10 +1013,24 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
981
1013
  return typeNode.getText();
982
1014
  }
983
1015
  __name(resolveTypeNodeToString, "resolveTypeNodeToString");
984
- function resolveClassDeclaration(cls, sourceFile, project, depth) {
1016
+ function expandTypeDecl(result, project, depth) {
1017
+ if (depth < 0) return "unknown";
1018
+ switch (result.kind) {
1019
+ case "class":
1020
+ return resolvePropertied(result.decl, result.file, project, depth);
1021
+ case "interface":
1022
+ return resolvePropertied(result.decl, result.file, project, depth);
1023
+ case "typeAlias":
1024
+ return result.text;
1025
+ case "enum":
1026
+ return result.members.join(" | ");
1027
+ }
1028
+ }
1029
+ __name(expandTypeDecl, "expandTypeDecl");
1030
+ function resolvePropertied(decl, sourceFile, project, depth) {
985
1031
  if (depth < 0) return "unknown";
986
1032
  const lines = [];
987
- for (const prop of cls.getProperties()) {
1033
+ for (const prop of decl.getProperties()) {
988
1034
  const propName = prop.getName();
989
1035
  const isOptional = prop.hasQuestionToken();
990
1036
  const propTypeNode = prop.getTypeNode();
@@ -996,7 +1042,7 @@ function resolveClassDeclaration(cls, sourceFile, project, depth) {
996
1042
  }
997
1043
  return `{ ${lines.join("; ")} }`;
998
1044
  }
999
- __name(resolveClassDeclaration, "resolveClassDeclaration");
1045
+ __name(resolvePropertied, "resolvePropertied");
1000
1046
  function extractBodyType(method, sourceFile, project) {
1001
1047
  for (const param of method.getParameters()) {
1002
1048
  const bodyDecorator = param.getDecorators().find((d) => d.getName() === "Body");
@@ -1076,9 +1122,9 @@ __name(extractResponseType, "extractResponseType");
1076
1122
  function resolveIdentifierToClassType(node, sourceFile, project, depth) {
1077
1123
  if (!import_ts_morph.Node.isIdentifier(node)) return "unknown";
1078
1124
  const name = node.getText();
1079
- const resolved = findClass(name, sourceFile, project);
1125
+ const resolved = findType(name, sourceFile, project);
1080
1126
  if (resolved) {
1081
- return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
1127
+ return expandTypeDecl(resolved, project, depth - 1);
1082
1128
  }
1083
1129
  return name;
1084
1130
  }
@@ -1407,7 +1453,7 @@ async function watch(config, onChange) {
1407
1453
  __name(watch, "watch");
1408
1454
 
1409
1455
  // src/index.ts
1410
- var VERSION = "1.0.5";
1456
+ var VERSION = "1.0.6";
1411
1457
  // Annotate the CommonJS export names for ESM import in node:
1412
1458
  0 && (module.exports = {
1413
1459
  CodegenError,