@dudousxd/nestjs-inertia-codegen 1.0.4 → 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.js CHANGED
@@ -334,16 +334,19 @@ function buildRouterTypeAccess(name) {
334
334
  __name(buildRouterTypeAccess, "buildRouterTypeAccess");
335
335
  function buildApiFile(routes) {
336
336
  const contracted = routes.filter((r) => r.contract);
337
+ const hasGetRoutes = contracted.some((r) => r.method === "GET");
337
338
  const lines = [
338
339
  "// Generated by @dudousxd/nestjs-inertia-codegen. Do not edit.",
339
- "",
340
- "import { queryOptions } from '@tanstack/query-core';",
341
- "import { route } from './routes.js';",
342
- "import { createFetcher } from '@dudousxd/nestjs-inertia-client';",
343
- "",
344
- "export const fetcher = createFetcher();",
345
340
  ""
346
341
  ];
342
+ if (hasGetRoutes) {
343
+ lines.push("import { queryOptions } from '@tanstack/query-core';");
344
+ }
345
+ lines.push("import { route } from './routes.js';");
346
+ lines.push("import { createFetcher } from '@dudousxd/nestjs-inertia-client';");
347
+ lines.push("");
348
+ lines.push("export const fetcher = createFetcher();");
349
+ lines.push("");
347
350
  if (contracted.length === 0) {
348
351
  lines.push("export type ApiRouter = Record<string, never>;");
349
352
  lines.push("");
@@ -858,7 +861,42 @@ function extractParams(path) {
858
861
  }));
859
862
  }
860
863
  __name(extractParams, "extractParams");
861
- function resolveImportedClass(name, sourceFile, project) {
864
+ function findTypeInFile(name, file) {
865
+ const cls = file.getClass(name);
866
+ if (cls) return {
867
+ kind: "class",
868
+ decl: cls,
869
+ file
870
+ };
871
+ const iface = file.getInterface(name);
872
+ if (iface) return {
873
+ kind: "interface",
874
+ decl: iface,
875
+ file
876
+ };
877
+ const alias = file.getTypeAlias(name);
878
+ if (alias) {
879
+ const typeNode = alias.getTypeNode();
880
+ return {
881
+ kind: "typeAlias",
882
+ text: typeNode ? typeNode.getText() : "unknown"
883
+ };
884
+ }
885
+ const enumDecl = file.getEnum(name);
886
+ if (enumDecl) {
887
+ const members = enumDecl.getMembers().map((m) => {
888
+ const val = m.getValue();
889
+ return typeof val === "string" ? JSON.stringify(val) : JSON.stringify(m.getName());
890
+ });
891
+ return {
892
+ kind: "enum",
893
+ members
894
+ };
895
+ }
896
+ return null;
897
+ }
898
+ __name(findTypeInFile, "findTypeInFile");
899
+ function resolveImportedType(name, sourceFile, project) {
862
900
  for (const importDecl of sourceFile.getImportDeclarations()) {
863
901
  const namedImport = importDecl.getNamedImports().find((n) => n.getName() === name);
864
902
  if (!namedImport) continue;
@@ -878,25 +916,19 @@ function resolveImportedClass(name, sourceFile, project) {
878
916
  continue;
879
917
  }
880
918
  }
881
- const cls = importedFile.getClass(name);
882
- if (cls) return {
883
- cls,
884
- file: importedFile
885
- };
919
+ const result = findTypeInFile(name, importedFile);
920
+ if (result) return result;
886
921
  }
887
922
  }
888
923
  return null;
889
924
  }
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);
925
+ __name(resolveImportedType, "resolveImportedType");
926
+ function findType(name, sourceFile, project) {
927
+ const local = findTypeInFile(name, sourceFile);
928
+ if (local) return local;
929
+ return resolveImportedType(name, sourceFile, project);
898
930
  }
899
- __name(findClass, "findClass");
931
+ __name(findType, "findType");
900
932
  function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
901
933
  if (depth <= 0) return "unknown";
902
934
  if (Node.isArrayTypeNode(typeNode)) {
@@ -925,9 +957,9 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
925
957
  }
926
958
  return "unknown";
927
959
  }
928
- const resolved = findClass(name, sourceFile, project);
960
+ const resolved = findType(name, sourceFile, project);
929
961
  if (resolved) {
930
- return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
962
+ return expandTypeDecl(resolved, project, depth - 1);
931
963
  }
932
964
  return name;
933
965
  }
@@ -940,10 +972,24 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
940
972
  return typeNode.getText();
941
973
  }
942
974
  __name(resolveTypeNodeToString, "resolveTypeNodeToString");
943
- function resolveClassDeclaration(cls, sourceFile, project, depth) {
975
+ function expandTypeDecl(result, project, depth) {
976
+ if (depth < 0) return "unknown";
977
+ switch (result.kind) {
978
+ case "class":
979
+ return resolvePropertied(result.decl, result.file, project, depth);
980
+ case "interface":
981
+ return resolvePropertied(result.decl, result.file, project, depth);
982
+ case "typeAlias":
983
+ return result.text;
984
+ case "enum":
985
+ return result.members.join(" | ");
986
+ }
987
+ }
988
+ __name(expandTypeDecl, "expandTypeDecl");
989
+ function resolvePropertied(decl, sourceFile, project, depth) {
944
990
  if (depth < 0) return "unknown";
945
991
  const lines = [];
946
- for (const prop of cls.getProperties()) {
992
+ for (const prop of decl.getProperties()) {
947
993
  const propName = prop.getName();
948
994
  const isOptional = prop.hasQuestionToken();
949
995
  const propTypeNode = prop.getTypeNode();
@@ -955,7 +1001,7 @@ function resolveClassDeclaration(cls, sourceFile, project, depth) {
955
1001
  }
956
1002
  return `{ ${lines.join("; ")} }`;
957
1003
  }
958
- __name(resolveClassDeclaration, "resolveClassDeclaration");
1004
+ __name(resolvePropertied, "resolvePropertied");
959
1005
  function extractBodyType(method, sourceFile, project) {
960
1006
  for (const param of method.getParameters()) {
961
1007
  const bodyDecorator = param.getDecorators().find((d) => d.getName() === "Body");
@@ -1035,9 +1081,9 @@ __name(extractResponseType, "extractResponseType");
1035
1081
  function resolveIdentifierToClassType(node, sourceFile, project, depth) {
1036
1082
  if (!Node.isIdentifier(node)) return "unknown";
1037
1083
  const name = node.getText();
1038
- const resolved = findClass(name, sourceFile, project);
1084
+ const resolved = findType(name, sourceFile, project);
1039
1085
  if (resolved) {
1040
- return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
1086
+ return expandTypeDecl(resolved, project, depth - 1);
1041
1087
  }
1042
1088
  return name;
1043
1089
  }
@@ -1166,7 +1212,21 @@ function extractFromSourceFile(sourceFile, project) {
1166
1212
  const combined = joinPaths(prefix, handlerPath);
1167
1213
  const params = extractParams(combined);
1168
1214
  const methodName = method.getName();
1169
- const routeName = `${className}.${methodName}`;
1215
+ const classAsDecorator = cls.getDecorator("As");
1216
+ let classAs;
1217
+ if (classAsDecorator) {
1218
+ const classAsArgs = classAsDecorator.getArguments();
1219
+ const classAsName = decoratorStringArg(classAsArgs[0]);
1220
+ if (classAsName) classAs = classAsName;
1221
+ }
1222
+ const methodAsDecorator = method.getDecorator("As");
1223
+ let methodAs;
1224
+ if (methodAsDecorator) {
1225
+ const methodAsArgs = methodAsDecorator.getArguments();
1226
+ const methodAsName = decoratorStringArg(methodAsArgs[0]);
1227
+ if (methodAsName) methodAs = methodAsName;
1228
+ }
1229
+ const routeName = resolveRouteName(className, methodName, classAs, methodAs);
1170
1230
  const dtoContract = extractDtoContract(method, sourceFile, project);
1171
1231
  routes.push({
1172
1232
  method: httpMethod,
@@ -1352,7 +1412,7 @@ async function watch(config, onChange) {
1352
1412
  __name(watch, "watch");
1353
1413
 
1354
1414
  // src/index.ts
1355
- var VERSION = "1.0.4";
1415
+ var VERSION = "1.0.6";
1356
1416
  export {
1357
1417
  CodegenError,
1358
1418
  ConfigError,