@dudousxd/nestjs-inertia-codegen 1.0.5 → 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/CHANGELOG.md +9 -0
- package/dist/cli/main.cjs +75 -29
- package/dist/cli/main.cjs.map +1 -1
- package/dist/cli/main.js +75 -29
- package/dist/cli/main.js.map +1 -1
- package/dist/index.cjs +75 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +75 -29
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
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
|
|
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
|
|
870
|
-
if (
|
|
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(
|
|
879
|
-
function
|
|
880
|
-
const local =
|
|
881
|
-
if (local) return
|
|
882
|
-
|
|
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(
|
|
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 =
|
|
948
|
+
const resolved = findType(name, sourceFile, project);
|
|
917
949
|
if (resolved) {
|
|
918
|
-
return
|
|
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
|
|
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
|
|
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(
|
|
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 =
|
|
1072
|
+
const resolved = findType(name, sourceFile, project);
|
|
1027
1073
|
if (resolved) {
|
|
1028
|
-
return
|
|
1074
|
+
return expandTypeDecl(resolved, project, depth - 1);
|
|
1029
1075
|
}
|
|
1030
1076
|
return name;
|
|
1031
1077
|
}
|
|
@@ -1354,7 +1400,7 @@ async function watch(config, onChange) {
|
|
|
1354
1400
|
__name(watch, "watch");
|
|
1355
1401
|
|
|
1356
1402
|
// src/index.ts
|
|
1357
|
-
var VERSION = "1.0.
|
|
1403
|
+
var VERSION = "1.0.6";
|
|
1358
1404
|
|
|
1359
1405
|
// src/cli/codegen.ts
|
|
1360
1406
|
async function runCodegen(opts = {}) {
|