@dudousxd/nestjs-inertia-codegen 1.0.0 → 1.0.4

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
@@ -734,7 +734,7 @@ async function discoverContractsFast(opts) {
734
734
  }
735
735
  const routes = [];
736
736
  for (const sourceFile of project.getSourceFiles()) {
737
- routes.push(...extractFromSourceFile(sourceFile));
737
+ routes.push(...extractFromSourceFile(sourceFile, project));
738
738
  }
739
739
  return routes;
740
740
  }
@@ -899,11 +899,50 @@ function extractParams(path) {
899
899
  }));
900
900
  }
901
901
  __name(extractParams, "extractParams");
902
- function resolveTypeNodeToString(typeNode, sourceFile, depth) {
902
+ function resolveImportedClass(name, sourceFile, project) {
903
+ for (const importDecl of sourceFile.getImportDeclarations()) {
904
+ const namedImport = importDecl.getNamedImports().find((n) => n.getName() === name);
905
+ if (!namedImport) continue;
906
+ const moduleSpecifier = importDecl.getModuleSpecifierValue();
907
+ if (!moduleSpecifier.startsWith(".")) return null;
908
+ const dir = (0, import_node_path8.dirname)(sourceFile.getFilePath());
909
+ const candidates = [
910
+ (0, import_node_path8.resolve)(dir, `${moduleSpecifier}.ts`),
911
+ (0, import_node_path8.resolve)(dir, moduleSpecifier, "index.ts")
912
+ ];
913
+ for (const candidate of candidates) {
914
+ let importedFile = project.getSourceFile(candidate);
915
+ if (!importedFile) {
916
+ try {
917
+ importedFile = project.addSourceFileAtPath(candidate);
918
+ } catch {
919
+ continue;
920
+ }
921
+ }
922
+ const cls = importedFile.getClass(name);
923
+ if (cls) return {
924
+ cls,
925
+ file: importedFile
926
+ };
927
+ }
928
+ }
929
+ return null;
930
+ }
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);
939
+ }
940
+ __name(findClass, "findClass");
941
+ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
903
942
  if (depth <= 0) return "unknown";
904
943
  if (import_ts_morph.Node.isArrayTypeNode(typeNode)) {
905
944
  const elementType = typeNode.getElementTypeNode();
906
- return `Array<${resolveTypeNodeToString(elementType, sourceFile, depth)}>`;
945
+ return `Array<${resolveTypeNodeToString(elementType, sourceFile, project, depth)}>`;
907
946
  }
908
947
  if (import_ts_morph.Node.isTypeReference(typeNode)) {
909
948
  const typeName = typeNode.getTypeName();
@@ -915,7 +954,7 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
915
954
  const typeArgs = typeNode.getTypeArguments();
916
955
  const firstTypeArg = typeArgs[0];
917
956
  if (typeArgs.length > 0 && firstTypeArg !== void 0) {
918
- return `Array<${resolveTypeNodeToString(firstTypeArg, sourceFile, depth)}>`;
957
+ return `Array<${resolveTypeNodeToString(firstTypeArg, sourceFile, project, depth)}>`;
919
958
  }
920
959
  return "Array<unknown>";
921
960
  }
@@ -923,13 +962,13 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
923
962
  const typeArgs = typeNode.getTypeArguments();
924
963
  const firstTypeArg = typeArgs[0];
925
964
  if (typeArgs.length > 0 && firstTypeArg !== void 0) {
926
- return resolveTypeNodeToString(firstTypeArg, sourceFile, depth);
965
+ return resolveTypeNodeToString(firstTypeArg, sourceFile, project, depth);
927
966
  }
928
967
  return "unknown";
929
968
  }
930
- const classDec = sourceFile.getClass(name);
931
- if (classDec) {
932
- return resolveClassDeclaration(classDec, sourceFile, depth - 1);
969
+ const resolved = findClass(name, sourceFile, project);
970
+ if (resolved) {
971
+ return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
933
972
  }
934
973
  return name;
935
974
  }
@@ -942,7 +981,7 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
942
981
  return typeNode.getText();
943
982
  }
944
983
  __name(resolveTypeNodeToString, "resolveTypeNodeToString");
945
- function resolveClassDeclaration(cls, sourceFile, depth) {
984
+ function resolveClassDeclaration(cls, sourceFile, project, depth) {
946
985
  if (depth < 0) return "unknown";
947
986
  const lines = [];
948
987
  for (const prop of cls.getProperties()) {
@@ -951,14 +990,14 @@ function resolveClassDeclaration(cls, sourceFile, depth) {
951
990
  const propTypeNode = prop.getTypeNode();
952
991
  let propType = "unknown";
953
992
  if (propTypeNode) {
954
- propType = resolveTypeNodeToString(propTypeNode, sourceFile, depth);
993
+ propType = resolveTypeNodeToString(propTypeNode, sourceFile, project, depth);
955
994
  }
956
995
  lines.push(`${propName}${isOptional ? "?" : ""}: ${propType}`);
957
996
  }
958
997
  return `{ ${lines.join("; ")} }`;
959
998
  }
960
999
  __name(resolveClassDeclaration, "resolveClassDeclaration");
961
- function extractBodyType(method, sourceFile) {
1000
+ function extractBodyType(method, sourceFile, project) {
962
1001
  for (const param of method.getParameters()) {
963
1002
  const bodyDecorator = param.getDecorators().find((d) => d.getName() === "Body");
964
1003
  if (!bodyDecorator) continue;
@@ -966,13 +1005,13 @@ function extractBodyType(method, sourceFile) {
966
1005
  if (bodyArgs.length > 0) continue;
967
1006
  const typeNode = param.getTypeNode();
968
1007
  if (typeNode) {
969
- return resolveTypeNodeToString(typeNode, sourceFile, 3);
1008
+ return resolveTypeNodeToString(typeNode, sourceFile, project, 3);
970
1009
  }
971
1010
  }
972
1011
  return null;
973
1012
  }
974
1013
  __name(extractBodyType, "extractBodyType");
975
- function extractQueryType(method, sourceFile) {
1014
+ function extractQueryType(method, sourceFile, project) {
976
1015
  for (const param of method.getParameters()) {
977
1016
  const queryDecorator = param.getDecorators().find((d) => d.getName() === "Query");
978
1017
  if (!queryDecorator) continue;
@@ -980,13 +1019,13 @@ function extractQueryType(method, sourceFile) {
980
1019
  if (queryArgs.length > 0) continue;
981
1020
  const typeNode = param.getTypeNode();
982
1021
  if (typeNode) {
983
- return resolveTypeNodeToString(typeNode, sourceFile, 3);
1022
+ return resolveTypeNodeToString(typeNode, sourceFile, project, 3);
984
1023
  }
985
1024
  }
986
1025
  return null;
987
1026
  }
988
1027
  __name(extractQueryType, "extractQueryType");
989
- function extractParamsType(method, sourceFile) {
1028
+ function extractParamsType(method, sourceFile, project) {
990
1029
  const entries = [];
991
1030
  for (const param of method.getParameters()) {
992
1031
  const paramDecorator = param.getDecorators().find((d) => d.getName() === "Param");
@@ -997,13 +1036,13 @@ function extractParamsType(method, sourceFile) {
997
1036
  if (!import_ts_morph.Node.isStringLiteral(nameArg)) continue;
998
1037
  const paramName = nameArg.getLiteralValue();
999
1038
  const typeNode = param.getTypeNode();
1000
- const paramType = typeNode ? resolveTypeNodeToString(typeNode, sourceFile, 3) : "string";
1039
+ const paramType = typeNode ? resolveTypeNodeToString(typeNode, sourceFile, project, 3) : "string";
1001
1040
  entries.push(`${paramName}: ${paramType}`);
1002
1041
  }
1003
1042
  return entries.length > 0 ? `{ ${entries.join("; ")} }` : null;
1004
1043
  }
1005
1044
  __name(extractParamsType, "extractParamsType");
1006
- function extractResponseType(method, sourceFile) {
1045
+ function extractResponseType(method, sourceFile, project) {
1007
1046
  const apiResponseDecorator = method.getDecorator("ApiResponse");
1008
1047
  if (apiResponseDecorator) {
1009
1048
  const args = apiResponseDecorator.getArguments();
@@ -1018,37 +1057,37 @@ function extractResponseType(method, sourceFile) {
1018
1057
  const elements = val.getElements();
1019
1058
  const firstEl = elements[0];
1020
1059
  if (elements.length > 0 && firstEl !== void 0) {
1021
- const innerType = resolveIdentifierToClassType(firstEl, sourceFile, 3);
1060
+ const innerType = resolveIdentifierToClassType(firstEl, sourceFile, project, 3);
1022
1061
  return `Array<${innerType}>`;
1023
1062
  }
1024
1063
  return "Array<unknown>";
1025
1064
  }
1026
- return resolveIdentifierToClassType(val, sourceFile, 3);
1065
+ return resolveIdentifierToClassType(val, sourceFile, project, 3);
1027
1066
  }
1028
1067
  }
1029
1068
  }
1030
1069
  const returnTypeNode = method.getReturnTypeNode();
1031
1070
  if (returnTypeNode) {
1032
- return resolveTypeNodeToString(returnTypeNode, sourceFile, 3);
1071
+ return resolveTypeNodeToString(returnTypeNode, sourceFile, project, 3);
1033
1072
  }
1034
1073
  return "unknown";
1035
1074
  }
1036
1075
  __name(extractResponseType, "extractResponseType");
1037
- function resolveIdentifierToClassType(node, sourceFile, depth) {
1076
+ function resolveIdentifierToClassType(node, sourceFile, project, depth) {
1038
1077
  if (!import_ts_morph.Node.isIdentifier(node)) return "unknown";
1039
1078
  const name = node.getText();
1040
- const classDec = sourceFile.getClass(name);
1041
- if (classDec) {
1042
- return resolveClassDeclaration(classDec, sourceFile, depth - 1);
1079
+ const resolved = findClass(name, sourceFile, project);
1080
+ if (resolved) {
1081
+ return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
1043
1082
  }
1044
1083
  return name;
1045
1084
  }
1046
1085
  __name(resolveIdentifierToClassType, "resolveIdentifierToClassType");
1047
- function extractDtoContract(method, sourceFile) {
1048
- const body = extractBodyType(method, sourceFile);
1049
- const query = extractQueryType(method, sourceFile);
1050
- const paramsType = extractParamsType(method, sourceFile);
1051
- const response = extractResponseType(method, sourceFile);
1086
+ function extractDtoContract(method, sourceFile, project) {
1087
+ const body = extractBodyType(method, sourceFile, project);
1088
+ const query = extractQueryType(method, sourceFile, project);
1089
+ const paramsType = extractParamsType(method, sourceFile, project);
1090
+ const response = extractResponseType(method, sourceFile, project);
1052
1091
  if (body === null && query === null && paramsType === null && response === "unknown") {
1053
1092
  return null;
1054
1093
  }
@@ -1070,7 +1109,7 @@ var HTTP_METHOD_DECORATORS = {
1070
1109
  Head: "HEAD",
1071
1110
  All: "ALL"
1072
1111
  };
1073
- function extractFromSourceFile(sourceFile) {
1112
+ function extractFromSourceFile(sourceFile, project) {
1074
1113
  const routes = [];
1075
1114
  const seenNames = /* @__PURE__ */ new Map();
1076
1115
  const classes = sourceFile.getClasses();
@@ -1169,7 +1208,7 @@ function extractFromSourceFile(sourceFile) {
1169
1208
  const params = extractParams(combined);
1170
1209
  const methodName = method.getName();
1171
1210
  const routeName = `${className}.${methodName}`;
1172
- const dtoContract = extractDtoContract(method, sourceFile);
1211
+ const dtoContract = extractDtoContract(method, sourceFile, project);
1173
1212
  routes.push({
1174
1213
  method: httpMethod,
1175
1214
  path: combined,
@@ -1256,8 +1295,20 @@ async function watch(config, onChange) {
1256
1295
  return NO_OP_WATCHER;
1257
1296
  }
1258
1297
  try {
1259
- await generate(config);
1260
- } catch {
1298
+ const initialRoutes = await discoverContractsFast({
1299
+ cwd: config.codegen.cwd,
1300
+ glob: config.contracts.glob,
1301
+ ...config.app?.tsconfig ? {
1302
+ tsconfig: config.app.tsconfig
1303
+ } : {}
1304
+ });
1305
+ await generate(config, initialRoutes);
1306
+ } catch (err) {
1307
+ console.warn(`[nestjs-inertia-codegen] Initial route discovery failed, falling back to pages-only: ${err instanceof Error ? err.message : String(err)}`);
1308
+ try {
1309
+ await generate(config);
1310
+ } catch {
1311
+ }
1261
1312
  }
1262
1313
  let pagesDebounceTimer;
1263
1314
  const pagesWatcher = import_chokidar.default.watch((0, import_node_path10.join)(config.codegen.cwd, config.pages.glob), {
@@ -1342,7 +1393,7 @@ async function watch(config, onChange) {
1342
1393
  __name(watch, "watch");
1343
1394
 
1344
1395
  // src/index.ts
1345
- var VERSION = "1.0.0";
1396
+ var VERSION = "1.0.4";
1346
1397
  // Annotate the CommonJS export names for ESM import in node:
1347
1398
  0 && (module.exports = {
1348
1399
  CodegenError,