@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/index.js CHANGED
@@ -657,7 +657,7 @@ import { join as join9 } from "path";
657
657
  import chokidar from "chokidar";
658
658
 
659
659
  // src/discovery/contracts-fast.ts
660
- import { join as join7, resolve as resolve2 } from "path";
660
+ import { dirname, join as join7, resolve as resolve2 } from "path";
661
661
  import fg2 from "fast-glob";
662
662
  import { Node, Project, SyntaxKind } from "ts-morph";
663
663
  async function discoverContractsFast(opts) {
@@ -693,7 +693,7 @@ async function discoverContractsFast(opts) {
693
693
  }
694
694
  const routes = [];
695
695
  for (const sourceFile of project.getSourceFiles()) {
696
- routes.push(...extractFromSourceFile(sourceFile));
696
+ routes.push(...extractFromSourceFile(sourceFile, project));
697
697
  }
698
698
  return routes;
699
699
  }
@@ -858,11 +858,50 @@ function extractParams(path) {
858
858
  }));
859
859
  }
860
860
  __name(extractParams, "extractParams");
861
- function resolveTypeNodeToString(typeNode, sourceFile, depth) {
861
+ function resolveImportedClass(name, sourceFile, project) {
862
+ for (const importDecl of sourceFile.getImportDeclarations()) {
863
+ const namedImport = importDecl.getNamedImports().find((n) => n.getName() === name);
864
+ if (!namedImport) continue;
865
+ const moduleSpecifier = importDecl.getModuleSpecifierValue();
866
+ if (!moduleSpecifier.startsWith(".")) return null;
867
+ const dir = dirname(sourceFile.getFilePath());
868
+ const candidates = [
869
+ resolve2(dir, `${moduleSpecifier}.ts`),
870
+ resolve2(dir, moduleSpecifier, "index.ts")
871
+ ];
872
+ for (const candidate of candidates) {
873
+ let importedFile = project.getSourceFile(candidate);
874
+ if (!importedFile) {
875
+ try {
876
+ importedFile = project.addSourceFileAtPath(candidate);
877
+ } catch {
878
+ continue;
879
+ }
880
+ }
881
+ const cls = importedFile.getClass(name);
882
+ if (cls) return {
883
+ cls,
884
+ file: importedFile
885
+ };
886
+ }
887
+ }
888
+ return null;
889
+ }
890
+ __name(resolveImportedClass, "resolveImportedClass");
891
+ function findClass(name, sourceFile, project) {
892
+ const local = sourceFile.getClass(name);
893
+ if (local) return {
894
+ cls: local,
895
+ file: sourceFile
896
+ };
897
+ return resolveImportedClass(name, sourceFile, project);
898
+ }
899
+ __name(findClass, "findClass");
900
+ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
862
901
  if (depth <= 0) return "unknown";
863
902
  if (Node.isArrayTypeNode(typeNode)) {
864
903
  const elementType = typeNode.getElementTypeNode();
865
- return `Array<${resolveTypeNodeToString(elementType, sourceFile, depth)}>`;
904
+ return `Array<${resolveTypeNodeToString(elementType, sourceFile, project, depth)}>`;
866
905
  }
867
906
  if (Node.isTypeReference(typeNode)) {
868
907
  const typeName = typeNode.getTypeName();
@@ -874,7 +913,7 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
874
913
  const typeArgs = typeNode.getTypeArguments();
875
914
  const firstTypeArg = typeArgs[0];
876
915
  if (typeArgs.length > 0 && firstTypeArg !== void 0) {
877
- return `Array<${resolveTypeNodeToString(firstTypeArg, sourceFile, depth)}>`;
916
+ return `Array<${resolveTypeNodeToString(firstTypeArg, sourceFile, project, depth)}>`;
878
917
  }
879
918
  return "Array<unknown>";
880
919
  }
@@ -882,13 +921,13 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
882
921
  const typeArgs = typeNode.getTypeArguments();
883
922
  const firstTypeArg = typeArgs[0];
884
923
  if (typeArgs.length > 0 && firstTypeArg !== void 0) {
885
- return resolveTypeNodeToString(firstTypeArg, sourceFile, depth);
924
+ return resolveTypeNodeToString(firstTypeArg, sourceFile, project, depth);
886
925
  }
887
926
  return "unknown";
888
927
  }
889
- const classDec = sourceFile.getClass(name);
890
- if (classDec) {
891
- return resolveClassDeclaration(classDec, sourceFile, depth - 1);
928
+ const resolved = findClass(name, sourceFile, project);
929
+ if (resolved) {
930
+ return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
892
931
  }
893
932
  return name;
894
933
  }
@@ -901,7 +940,7 @@ function resolveTypeNodeToString(typeNode, sourceFile, depth) {
901
940
  return typeNode.getText();
902
941
  }
903
942
  __name(resolveTypeNodeToString, "resolveTypeNodeToString");
904
- function resolveClassDeclaration(cls, sourceFile, depth) {
943
+ function resolveClassDeclaration(cls, sourceFile, project, depth) {
905
944
  if (depth < 0) return "unknown";
906
945
  const lines = [];
907
946
  for (const prop of cls.getProperties()) {
@@ -910,14 +949,14 @@ function resolveClassDeclaration(cls, sourceFile, depth) {
910
949
  const propTypeNode = prop.getTypeNode();
911
950
  let propType = "unknown";
912
951
  if (propTypeNode) {
913
- propType = resolveTypeNodeToString(propTypeNode, sourceFile, depth);
952
+ propType = resolveTypeNodeToString(propTypeNode, sourceFile, project, depth);
914
953
  }
915
954
  lines.push(`${propName}${isOptional ? "?" : ""}: ${propType}`);
916
955
  }
917
956
  return `{ ${lines.join("; ")} }`;
918
957
  }
919
958
  __name(resolveClassDeclaration, "resolveClassDeclaration");
920
- function extractBodyType(method, sourceFile) {
959
+ function extractBodyType(method, sourceFile, project) {
921
960
  for (const param of method.getParameters()) {
922
961
  const bodyDecorator = param.getDecorators().find((d) => d.getName() === "Body");
923
962
  if (!bodyDecorator) continue;
@@ -925,13 +964,13 @@ function extractBodyType(method, sourceFile) {
925
964
  if (bodyArgs.length > 0) continue;
926
965
  const typeNode = param.getTypeNode();
927
966
  if (typeNode) {
928
- return resolveTypeNodeToString(typeNode, sourceFile, 3);
967
+ return resolveTypeNodeToString(typeNode, sourceFile, project, 3);
929
968
  }
930
969
  }
931
970
  return null;
932
971
  }
933
972
  __name(extractBodyType, "extractBodyType");
934
- function extractQueryType(method, sourceFile) {
973
+ function extractQueryType(method, sourceFile, project) {
935
974
  for (const param of method.getParameters()) {
936
975
  const queryDecorator = param.getDecorators().find((d) => d.getName() === "Query");
937
976
  if (!queryDecorator) continue;
@@ -939,13 +978,13 @@ function extractQueryType(method, sourceFile) {
939
978
  if (queryArgs.length > 0) continue;
940
979
  const typeNode = param.getTypeNode();
941
980
  if (typeNode) {
942
- return resolveTypeNodeToString(typeNode, sourceFile, 3);
981
+ return resolveTypeNodeToString(typeNode, sourceFile, project, 3);
943
982
  }
944
983
  }
945
984
  return null;
946
985
  }
947
986
  __name(extractQueryType, "extractQueryType");
948
- function extractParamsType(method, sourceFile) {
987
+ function extractParamsType(method, sourceFile, project) {
949
988
  const entries = [];
950
989
  for (const param of method.getParameters()) {
951
990
  const paramDecorator = param.getDecorators().find((d) => d.getName() === "Param");
@@ -956,13 +995,13 @@ function extractParamsType(method, sourceFile) {
956
995
  if (!Node.isStringLiteral(nameArg)) continue;
957
996
  const paramName = nameArg.getLiteralValue();
958
997
  const typeNode = param.getTypeNode();
959
- const paramType = typeNode ? resolveTypeNodeToString(typeNode, sourceFile, 3) : "string";
998
+ const paramType = typeNode ? resolveTypeNodeToString(typeNode, sourceFile, project, 3) : "string";
960
999
  entries.push(`${paramName}: ${paramType}`);
961
1000
  }
962
1001
  return entries.length > 0 ? `{ ${entries.join("; ")} }` : null;
963
1002
  }
964
1003
  __name(extractParamsType, "extractParamsType");
965
- function extractResponseType(method, sourceFile) {
1004
+ function extractResponseType(method, sourceFile, project) {
966
1005
  const apiResponseDecorator = method.getDecorator("ApiResponse");
967
1006
  if (apiResponseDecorator) {
968
1007
  const args = apiResponseDecorator.getArguments();
@@ -977,37 +1016,37 @@ function extractResponseType(method, sourceFile) {
977
1016
  const elements = val.getElements();
978
1017
  const firstEl = elements[0];
979
1018
  if (elements.length > 0 && firstEl !== void 0) {
980
- const innerType = resolveIdentifierToClassType(firstEl, sourceFile, 3);
1019
+ const innerType = resolveIdentifierToClassType(firstEl, sourceFile, project, 3);
981
1020
  return `Array<${innerType}>`;
982
1021
  }
983
1022
  return "Array<unknown>";
984
1023
  }
985
- return resolveIdentifierToClassType(val, sourceFile, 3);
1024
+ return resolveIdentifierToClassType(val, sourceFile, project, 3);
986
1025
  }
987
1026
  }
988
1027
  }
989
1028
  const returnTypeNode = method.getReturnTypeNode();
990
1029
  if (returnTypeNode) {
991
- return resolveTypeNodeToString(returnTypeNode, sourceFile, 3);
1030
+ return resolveTypeNodeToString(returnTypeNode, sourceFile, project, 3);
992
1031
  }
993
1032
  return "unknown";
994
1033
  }
995
1034
  __name(extractResponseType, "extractResponseType");
996
- function resolveIdentifierToClassType(node, sourceFile, depth) {
1035
+ function resolveIdentifierToClassType(node, sourceFile, project, depth) {
997
1036
  if (!Node.isIdentifier(node)) return "unknown";
998
1037
  const name = node.getText();
999
- const classDec = sourceFile.getClass(name);
1000
- if (classDec) {
1001
- return resolveClassDeclaration(classDec, sourceFile, depth - 1);
1038
+ const resolved = findClass(name, sourceFile, project);
1039
+ if (resolved) {
1040
+ return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
1002
1041
  }
1003
1042
  return name;
1004
1043
  }
1005
1044
  __name(resolveIdentifierToClassType, "resolveIdentifierToClassType");
1006
- function extractDtoContract(method, sourceFile) {
1007
- const body = extractBodyType(method, sourceFile);
1008
- const query = extractQueryType(method, sourceFile);
1009
- const paramsType = extractParamsType(method, sourceFile);
1010
- const response = extractResponseType(method, sourceFile);
1045
+ function extractDtoContract(method, sourceFile, project) {
1046
+ const body = extractBodyType(method, sourceFile, project);
1047
+ const query = extractQueryType(method, sourceFile, project);
1048
+ const paramsType = extractParamsType(method, sourceFile, project);
1049
+ const response = extractResponseType(method, sourceFile, project);
1011
1050
  if (body === null && query === null && paramsType === null && response === "unknown") {
1012
1051
  return null;
1013
1052
  }
@@ -1029,7 +1068,7 @@ var HTTP_METHOD_DECORATORS = {
1029
1068
  Head: "HEAD",
1030
1069
  All: "ALL"
1031
1070
  };
1032
- function extractFromSourceFile(sourceFile) {
1071
+ function extractFromSourceFile(sourceFile, project) {
1033
1072
  const routes = [];
1034
1073
  const seenNames = /* @__PURE__ */ new Map();
1035
1074
  const classes = sourceFile.getClasses();
@@ -1128,7 +1167,7 @@ function extractFromSourceFile(sourceFile) {
1128
1167
  const params = extractParams(combined);
1129
1168
  const methodName = method.getName();
1130
1169
  const routeName = `${className}.${methodName}`;
1131
- const dtoContract = extractDtoContract(method, sourceFile);
1170
+ const dtoContract = extractDtoContract(method, sourceFile, project);
1132
1171
  routes.push({
1133
1172
  method: httpMethod,
1134
1173
  path: combined,
@@ -1313,7 +1352,7 @@ async function watch(config, onChange) {
1313
1352
  __name(watch, "watch");
1314
1353
 
1315
1354
  // src/index.ts
1316
- var VERSION = "1.0.3";
1355
+ var VERSION = "1.0.4";
1317
1356
  export {
1318
1357
  CodegenError,
1319
1358
  ConfigError,