@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/cli/main.js CHANGED
@@ -322,16 +322,19 @@ function buildRouterTypeAccess(name) {
322
322
  __name(buildRouterTypeAccess, "buildRouterTypeAccess");
323
323
  function buildApiFile(routes) {
324
324
  const contracted = routes.filter((r) => r.contract);
325
+ const hasGetRoutes = contracted.some((r) => r.method === "GET");
325
326
  const lines = [
326
327
  "// Generated by @dudousxd/nestjs-inertia-codegen. Do not edit.",
327
- "",
328
- "import { queryOptions } from '@tanstack/query-core';",
329
- "import { route } from './routes.js';",
330
- "import { createFetcher } from '@dudousxd/nestjs-inertia-client';",
331
- "",
332
- "export const fetcher = createFetcher();",
333
328
  ""
334
329
  ];
330
+ if (hasGetRoutes) {
331
+ lines.push("import { queryOptions } from '@tanstack/query-core';");
332
+ }
333
+ lines.push("import { route } from './routes.js';");
334
+ lines.push("import { createFetcher } from '@dudousxd/nestjs-inertia-client';");
335
+ lines.push("");
336
+ lines.push("export const fetcher = createFetcher();");
337
+ lines.push("");
335
338
  if (contracted.length === 0) {
336
339
  lines.push("export type ApiRouter = Record<string, never>;");
337
340
  lines.push("");
@@ -846,7 +849,42 @@ function extractParams(path) {
846
849
  }));
847
850
  }
848
851
  __name(extractParams, "extractParams");
849
- function resolveImportedClass(name, sourceFile, project) {
852
+ function findTypeInFile(name, file) {
853
+ const cls = file.getClass(name);
854
+ if (cls) return {
855
+ kind: "class",
856
+ decl: cls,
857
+ file
858
+ };
859
+ const iface = file.getInterface(name);
860
+ if (iface) return {
861
+ kind: "interface",
862
+ decl: iface,
863
+ file
864
+ };
865
+ const alias = file.getTypeAlias(name);
866
+ if (alias) {
867
+ const typeNode = alias.getTypeNode();
868
+ return {
869
+ kind: "typeAlias",
870
+ text: typeNode ? typeNode.getText() : "unknown"
871
+ };
872
+ }
873
+ const enumDecl = file.getEnum(name);
874
+ if (enumDecl) {
875
+ const members = enumDecl.getMembers().map((m) => {
876
+ const val = m.getValue();
877
+ return typeof val === "string" ? JSON.stringify(val) : JSON.stringify(m.getName());
878
+ });
879
+ return {
880
+ kind: "enum",
881
+ members
882
+ };
883
+ }
884
+ return null;
885
+ }
886
+ __name(findTypeInFile, "findTypeInFile");
887
+ function resolveImportedType(name, sourceFile, project) {
850
888
  for (const importDecl of sourceFile.getImportDeclarations()) {
851
889
  const namedImport = importDecl.getNamedImports().find((n) => n.getName() === name);
852
890
  if (!namedImport) continue;
@@ -866,25 +904,19 @@ function resolveImportedClass(name, sourceFile, project) {
866
904
  continue;
867
905
  }
868
906
  }
869
- const cls = importedFile.getClass(name);
870
- if (cls) return {
871
- cls,
872
- file: importedFile
873
- };
907
+ const result = findTypeInFile(name, importedFile);
908
+ if (result) return result;
874
909
  }
875
910
  }
876
911
  return null;
877
912
  }
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);
913
+ __name(resolveImportedType, "resolveImportedType");
914
+ function findType(name, sourceFile, project) {
915
+ const local = findTypeInFile(name, sourceFile);
916
+ if (local) return local;
917
+ return resolveImportedType(name, sourceFile, project);
886
918
  }
887
- __name(findClass, "findClass");
919
+ __name(findType, "findType");
888
920
  function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
889
921
  if (depth <= 0) return "unknown";
890
922
  if (Node.isArrayTypeNode(typeNode)) {
@@ -913,9 +945,9 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
913
945
  }
914
946
  return "unknown";
915
947
  }
916
- const resolved = findClass(name, sourceFile, project);
948
+ const resolved = findType(name, sourceFile, project);
917
949
  if (resolved) {
918
- return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
950
+ return expandTypeDecl(resolved, project, depth - 1);
919
951
  }
920
952
  return name;
921
953
  }
@@ -928,10 +960,24 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
928
960
  return typeNode.getText();
929
961
  }
930
962
  __name(resolveTypeNodeToString, "resolveTypeNodeToString");
931
- function resolveClassDeclaration(cls, sourceFile, project, depth) {
963
+ function expandTypeDecl(result, project, depth) {
964
+ if (depth < 0) return "unknown";
965
+ switch (result.kind) {
966
+ case "class":
967
+ return resolvePropertied(result.decl, result.file, project, depth);
968
+ case "interface":
969
+ return resolvePropertied(result.decl, result.file, project, depth);
970
+ case "typeAlias":
971
+ return result.text;
972
+ case "enum":
973
+ return result.members.join(" | ");
974
+ }
975
+ }
976
+ __name(expandTypeDecl, "expandTypeDecl");
977
+ function resolvePropertied(decl, sourceFile, project, depth) {
932
978
  if (depth < 0) return "unknown";
933
979
  const lines = [];
934
- for (const prop of cls.getProperties()) {
980
+ for (const prop of decl.getProperties()) {
935
981
  const propName = prop.getName();
936
982
  const isOptional = prop.hasQuestionToken();
937
983
  const propTypeNode = prop.getTypeNode();
@@ -943,7 +989,7 @@ function resolveClassDeclaration(cls, sourceFile, project, depth) {
943
989
  }
944
990
  return `{ ${lines.join("; ")} }`;
945
991
  }
946
- __name(resolveClassDeclaration, "resolveClassDeclaration");
992
+ __name(resolvePropertied, "resolvePropertied");
947
993
  function extractBodyType(method, sourceFile, project) {
948
994
  for (const param of method.getParameters()) {
949
995
  const bodyDecorator = param.getDecorators().find((d) => d.getName() === "Body");
@@ -1023,9 +1069,9 @@ __name(extractResponseType, "extractResponseType");
1023
1069
  function resolveIdentifierToClassType(node, sourceFile, project, depth) {
1024
1070
  if (!Node.isIdentifier(node)) return "unknown";
1025
1071
  const name = node.getText();
1026
- const resolved = findClass(name, sourceFile, project);
1072
+ const resolved = findType(name, sourceFile, project);
1027
1073
  if (resolved) {
1028
- return resolveClassDeclaration(resolved.cls, resolved.file, project, depth - 1);
1074
+ return expandTypeDecl(resolved, project, depth - 1);
1029
1075
  }
1030
1076
  return name;
1031
1077
  }
@@ -1154,7 +1200,21 @@ function extractFromSourceFile(sourceFile, project) {
1154
1200
  const combined = joinPaths(prefix, handlerPath);
1155
1201
  const params = extractParams(combined);
1156
1202
  const methodName = method.getName();
1157
- const routeName = `${className}.${methodName}`;
1203
+ const classAsDecorator = cls.getDecorator("As");
1204
+ let classAs;
1205
+ if (classAsDecorator) {
1206
+ const classAsArgs = classAsDecorator.getArguments();
1207
+ const classAsName = decoratorStringArg(classAsArgs[0]);
1208
+ if (classAsName) classAs = classAsName;
1209
+ }
1210
+ const methodAsDecorator = method.getDecorator("As");
1211
+ let methodAs;
1212
+ if (methodAsDecorator) {
1213
+ const methodAsArgs = methodAsDecorator.getArguments();
1214
+ const methodAsName = decoratorStringArg(methodAsArgs[0]);
1215
+ if (methodAsName) methodAs = methodAsName;
1216
+ }
1217
+ const routeName = resolveRouteName(className, methodName, classAs, methodAs);
1158
1218
  const dtoContract = extractDtoContract(method, sourceFile, project);
1159
1219
  routes.push({
1160
1220
  method: httpMethod,
@@ -1340,7 +1400,7 @@ async function watch(config, onChange) {
1340
1400
  __name(watch, "watch");
1341
1401
 
1342
1402
  // src/index.ts
1343
- var VERSION = "1.0.4";
1403
+ var VERSION = "1.0.6";
1344
1404
 
1345
1405
  // src/cli/codegen.ts
1346
1406
  async function runCodegen(opts = {}) {