@dudousxd/nestjs-inertia-codegen 1.0.3 → 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/cli/main.js CHANGED
@@ -645,7 +645,7 @@ import { join as join9 } from "path";
645
645
  import chokidar from "chokidar";
646
646
 
647
647
  // src/discovery/contracts-fast.ts
648
- import { join as join7, resolve as resolve2 } from "path";
648
+ import { dirname, join as join7, resolve as resolve2 } from "path";
649
649
  import fg2 from "fast-glob";
650
650
  import { Node, Project, SyntaxKind } from "ts-morph";
651
651
  async function discoverContractsFast(opts) {
@@ -681,7 +681,7 @@ async function discoverContractsFast(opts) {
681
681
  }
682
682
  const routes = [];
683
683
  for (const sourceFile of project.getSourceFiles()) {
684
- routes.push(...extractFromSourceFile(sourceFile));
684
+ routes.push(...extractFromSourceFile(sourceFile, project));
685
685
  }
686
686
  return routes;
687
687
  }
@@ -846,11 +846,50 @@ function extractParams(path) {
846
846
  }));
847
847
  }
848
848
  __name(extractParams, "extractParams");
849
- function resolveTypeNodeToString(typeNode, sourceFile, depth) {
849
+ function resolveImportedClass(name, sourceFile, project) {
850
+ for (const importDecl of sourceFile.getImportDeclarations()) {
851
+ const namedImport = importDecl.getNamedImports().find((n) => n.getName() === name);
852
+ if (!namedImport) continue;
853
+ const moduleSpecifier = importDecl.getModuleSpecifierValue();
854
+ if (!moduleSpecifier.startsWith(".")) return null;
855
+ const dir = dirname(sourceFile.getFilePath());
856
+ const candidates = [
857
+ resolve2(dir, `${moduleSpecifier}.ts`),
858
+ resolve2(dir, moduleSpecifier, "index.ts")
859
+ ];
860
+ for (const candidate of candidates) {
861
+ let importedFile = project.getSourceFile(candidate);
862
+ if (!importedFile) {
863
+ try {
864
+ importedFile = project.addSourceFileAtPath(candidate);
865
+ } catch {
866
+ continue;
867
+ }
868
+ }
869
+ const cls = importedFile.getClass(name);
870
+ if (cls) return {
871
+ cls,
872
+ file: importedFile
873
+ };
874
+ }
875
+ }
876
+ return null;
877
+ }
878
+ __name(resolveImportedClass, "resolveImportedClass");
879
+ function findClass(name, sourceFile, project) {
880
+ const local = sourceFile.getClass(name);
881
+ if (local) return {
882
+ cls: local,
883
+ file: sourceFile
884
+ };
885
+ return resolveImportedClass(name, sourceFile, project);
886
+ }
887
+ __name(findClass, "findClass");
888
+ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
850
889
  if (depth <= 0) return "unknown";
851
890
  if (Node.isArrayTypeNode(typeNode)) {
852
891
  const elementType = typeNode.getElementTypeNode();
853
- return `Array<${resolveTypeNodeToString(elementType, sourceFile, depth)}>`;
892
+ return `Array<${resolveTypeNodeToString(elementType, sourceFile, project, depth)}>`;
854
893
  }
855
894
  if (Node.isTypeReference(typeNode)) {
856
895
  const typeName = typeNode.getTypeName();
@@ -862,7 +901,7 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
862
901
  const typeArgs = typeNode.getTypeArguments();
863
902
  const firstTypeArg = typeArgs[0];
864
903
  if (typeArgs.length > 0 && firstTypeArg !== void 0) {
865
- return `Array<${resolveTypeNodeToString(firstTypeArg, sourceFile, depth)}>`;
904
+ return `Array<${resolveTypeNodeToString(firstTypeArg, sourceFile, project, depth)}>`;
866
905
  }
867
906
  return "Array<unknown>";
868
907
  }
@@ -870,13 +909,13 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
870
909
  const typeArgs = typeNode.getTypeArguments();
871
910
  const firstTypeArg = typeArgs[0];
872
911
  if (typeArgs.length > 0 && firstTypeArg !== void 0) {
873
- return resolveTypeNodeToString(firstTypeArg, sourceFile, depth);
912
+ return resolveTypeNodeToString(firstTypeArg, sourceFile, project, depth);
874
913
  }
875
914
  return "unknown";
876
915
  }
877
- const classDec = sourceFile.getClass(name);
878
- if (classDec) {
879
- return resolveClassDeclaration(classDec, sourceFile, depth - 1);
916
+ const resolved = findClass(name, sourceFile, project);
917
+ if (resolved) {
918
+ return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
880
919
  }
881
920
  return name;
882
921
  }
@@ -889,7 +928,7 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
889
928
  return typeNode.getText();
890
929
  }
891
930
  __name(resolveTypeNodeToString, "resolveTypeNodeToString");
892
- function resolveClassDeclaration(cls, sourceFile, depth) {
931
+ function resolveClassDeclaration(cls, sourceFile, project, depth) {
893
932
  if (depth < 0) return "unknown";
894
933
  const lines = [];
895
934
  for (const prop of cls.getProperties()) {
@@ -898,14 +937,14 @@ function resolveClassDeclaration(cls, sourceFile, depth) {
898
937
  const propTypeNode = prop.getTypeNode();
899
938
  let propType = "unknown";
900
939
  if (propTypeNode) {
901
- propType = resolveTypeNodeToString(propTypeNode, sourceFile, depth);
940
+ propType = resolveTypeNodeToString(propTypeNode, sourceFile, project, depth);
902
941
  }
903
942
  lines.push(`${propName}${isOptional ? "?" : ""}: ${propType}`);
904
943
  }
905
944
  return `{ ${lines.join("; ")} }`;
906
945
  }
907
946
  __name(resolveClassDeclaration, "resolveClassDeclaration");
908
- function extractBodyType(method, sourceFile) {
947
+ function extractBodyType(method, sourceFile, project) {
909
948
  for (const param of method.getParameters()) {
910
949
  const bodyDecorator = param.getDecorators().find((d) => d.getName() === "Body");
911
950
  if (!bodyDecorator) continue;
@@ -913,13 +952,13 @@ function extractBodyType(method, sourceFile) {
913
952
  if (bodyArgs.length > 0) continue;
914
953
  const typeNode = param.getTypeNode();
915
954
  if (typeNode) {
916
- return resolveTypeNodeToString(typeNode, sourceFile, 3);
955
+ return resolveTypeNodeToString(typeNode, sourceFile, project, 3);
917
956
  }
918
957
  }
919
958
  return null;
920
959
  }
921
960
  __name(extractBodyType, "extractBodyType");
922
- function extractQueryType(method, sourceFile) {
961
+ function extractQueryType(method, sourceFile, project) {
923
962
  for (const param of method.getParameters()) {
924
963
  const queryDecorator = param.getDecorators().find((d) => d.getName() === "Query");
925
964
  if (!queryDecorator) continue;
@@ -927,13 +966,13 @@ function extractQueryType(method, sourceFile) {
927
966
  if (queryArgs.length > 0) continue;
928
967
  const typeNode = param.getTypeNode();
929
968
  if (typeNode) {
930
- return resolveTypeNodeToString(typeNode, sourceFile, 3);
969
+ return resolveTypeNodeToString(typeNode, sourceFile, project, 3);
931
970
  }
932
971
  }
933
972
  return null;
934
973
  }
935
974
  __name(extractQueryType, "extractQueryType");
936
- function extractParamsType(method, sourceFile) {
975
+ function extractParamsType(method, sourceFile, project) {
937
976
  const entries = [];
938
977
  for (const param of method.getParameters()) {
939
978
  const paramDecorator = param.getDecorators().find((d) => d.getName() === "Param");
@@ -944,13 +983,13 @@ function extractParamsType(method, sourceFile) {
944
983
  if (!Node.isStringLiteral(nameArg)) continue;
945
984
  const paramName = nameArg.getLiteralValue();
946
985
  const typeNode = param.getTypeNode();
947
- const paramType = typeNode ? resolveTypeNodeToString(typeNode, sourceFile, 3) : "string";
986
+ const paramType = typeNode ? resolveTypeNodeToString(typeNode, sourceFile, project, 3) : "string";
948
987
  entries.push(`${paramName}: ${paramType}`);
949
988
  }
950
989
  return entries.length > 0 ? `{ ${entries.join("; ")} }` : null;
951
990
  }
952
991
  __name(extractParamsType, "extractParamsType");
953
- function extractResponseType(method, sourceFile) {
992
+ function extractResponseType(method, sourceFile, project) {
954
993
  const apiResponseDecorator = method.getDecorator("ApiResponse");
955
994
  if (apiResponseDecorator) {
956
995
  const args = apiResponseDecorator.getArguments();
@@ -965,37 +1004,37 @@ function extractResponseType(method, sourceFile) {
965
1004
  const elements = val.getElements();
966
1005
  const firstEl = elements[0];
967
1006
  if (elements.length > 0 && firstEl !== void 0) {
968
- const innerType = resolveIdentifierToClassType(firstEl, sourceFile, 3);
1007
+ const innerType = resolveIdentifierToClassType(firstEl, sourceFile, project, 3);
969
1008
  return `Array<${innerType}>`;
970
1009
  }
971
1010
  return "Array<unknown>";
972
1011
  }
973
- return resolveIdentifierToClassType(val, sourceFile, 3);
1012
+ return resolveIdentifierToClassType(val, sourceFile, project, 3);
974
1013
  }
975
1014
  }
976
1015
  }
977
1016
  const returnTypeNode = method.getReturnTypeNode();
978
1017
  if (returnTypeNode) {
979
- return resolveTypeNodeToString(returnTypeNode, sourceFile, 3);
1018
+ return resolveTypeNodeToString(returnTypeNode, sourceFile, project, 3);
980
1019
  }
981
1020
  return "unknown";
982
1021
  }
983
1022
  __name(extractResponseType, "extractResponseType");
984
- function resolveIdentifierToClassType(node, sourceFile, depth) {
1023
+ function resolveIdentifierToClassType(node, sourceFile, project, depth) {
985
1024
  if (!Node.isIdentifier(node)) return "unknown";
986
1025
  const name = node.getText();
987
- const classDec = sourceFile.getClass(name);
988
- if (classDec) {
989
- return resolveClassDeclaration(classDec, sourceFile, depth - 1);
1026
+ const resolved = findClass(name, sourceFile, project);
1027
+ if (resolved) {
1028
+ return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
990
1029
  }
991
1030
  return name;
992
1031
  }
993
1032
  __name(resolveIdentifierToClassType, "resolveIdentifierToClassType");
994
- function extractDtoContract(method, sourceFile) {
995
- const body = extractBodyType(method, sourceFile);
996
- const query = extractQueryType(method, sourceFile);
997
- const paramsType = extractParamsType(method, sourceFile);
998
- const response = extractResponseType(method, sourceFile);
1033
+ function extractDtoContract(method, sourceFile, project) {
1034
+ const body = extractBodyType(method, sourceFile, project);
1035
+ const query = extractQueryType(method, sourceFile, project);
1036
+ const paramsType = extractParamsType(method, sourceFile, project);
1037
+ const response = extractResponseType(method, sourceFile, project);
999
1038
  if (body === null && query === null && paramsType === null && response === "unknown") {
1000
1039
  return null;
1001
1040
  }
@@ -1017,7 +1056,7 @@ var HTTP_METHOD_DECORATORS = {
1017
1056
  Head: "HEAD",
1018
1057
  All: "ALL"
1019
1058
  };
1020
- function extractFromSourceFile(sourceFile) {
1059
+ function extractFromSourceFile(sourceFile, project) {
1021
1060
  const routes = [];
1022
1061
  const seenNames = /* @__PURE__ */ new Map();
1023
1062
  const classes = sourceFile.getClasses();
@@ -1116,7 +1155,7 @@ function extractFromSourceFile(sourceFile) {
1116
1155
  const params = extractParams(combined);
1117
1156
  const methodName = method.getName();
1118
1157
  const routeName = `${className}.${methodName}`;
1119
- const dtoContract = extractDtoContract(method, sourceFile);
1158
+ const dtoContract = extractDtoContract(method, sourceFile, project);
1120
1159
  routes.push({
1121
1160
  method: httpMethod,
1122
1161
  path: combined,
@@ -1301,7 +1340,7 @@ async function watch(config, onChange) {
1301
1340
  __name(watch, "watch");
1302
1341
 
1303
1342
  // src/index.ts
1304
- var VERSION = "1.0.3";
1343
+ var VERSION = "1.0.4";
1305
1344
 
1306
1345
  // src/cli/codegen.ts
1307
1346
  async function runCodegen(opts = {}) {