@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/CHANGELOG.md +18 -0
- package/dist/cli/main.cjs +90 -30
- package/dist/cli/main.cjs.map +1 -1
- package/dist/cli/main.js +90 -30
- package/dist/cli/main.js.map +1 -1
- package/dist/index.cjs +90 -30
- 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 +90 -30
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -375,16 +375,19 @@ function buildRouterTypeAccess(name) {
|
|
|
375
375
|
__name(buildRouterTypeAccess, "buildRouterTypeAccess");
|
|
376
376
|
function buildApiFile(routes) {
|
|
377
377
|
const contracted = routes.filter((r) => r.contract);
|
|
378
|
+
const hasGetRoutes = contracted.some((r) => r.method === "GET");
|
|
378
379
|
const lines = [
|
|
379
380
|
"// Generated by @dudousxd/nestjs-inertia-codegen. Do not edit.",
|
|
380
|
-
"",
|
|
381
|
-
"import { queryOptions } from '@tanstack/query-core';",
|
|
382
|
-
"import { route } from './routes.js';",
|
|
383
|
-
"import { createFetcher } from '@dudousxd/nestjs-inertia-client';",
|
|
384
|
-
"",
|
|
385
|
-
"export const fetcher = createFetcher();",
|
|
386
381
|
""
|
|
387
382
|
];
|
|
383
|
+
if (hasGetRoutes) {
|
|
384
|
+
lines.push("import { queryOptions } from '@tanstack/query-core';");
|
|
385
|
+
}
|
|
386
|
+
lines.push("import { route } from './routes.js';");
|
|
387
|
+
lines.push("import { createFetcher } from '@dudousxd/nestjs-inertia-client';");
|
|
388
|
+
lines.push("");
|
|
389
|
+
lines.push("export const fetcher = createFetcher();");
|
|
390
|
+
lines.push("");
|
|
388
391
|
if (contracted.length === 0) {
|
|
389
392
|
lines.push("export type ApiRouter = Record<string, never>;");
|
|
390
393
|
lines.push("");
|
|
@@ -899,7 +902,42 @@ function extractParams(path) {
|
|
|
899
902
|
}));
|
|
900
903
|
}
|
|
901
904
|
__name(extractParams, "extractParams");
|
|
902
|
-
function
|
|
905
|
+
function findTypeInFile(name, file) {
|
|
906
|
+
const cls = file.getClass(name);
|
|
907
|
+
if (cls) return {
|
|
908
|
+
kind: "class",
|
|
909
|
+
decl: cls,
|
|
910
|
+
file
|
|
911
|
+
};
|
|
912
|
+
const iface = file.getInterface(name);
|
|
913
|
+
if (iface) return {
|
|
914
|
+
kind: "interface",
|
|
915
|
+
decl: iface,
|
|
916
|
+
file
|
|
917
|
+
};
|
|
918
|
+
const alias = file.getTypeAlias(name);
|
|
919
|
+
if (alias) {
|
|
920
|
+
const typeNode = alias.getTypeNode();
|
|
921
|
+
return {
|
|
922
|
+
kind: "typeAlias",
|
|
923
|
+
text: typeNode ? typeNode.getText() : "unknown"
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
const enumDecl = file.getEnum(name);
|
|
927
|
+
if (enumDecl) {
|
|
928
|
+
const members = enumDecl.getMembers().map((m) => {
|
|
929
|
+
const val = m.getValue();
|
|
930
|
+
return typeof val === "string" ? JSON.stringify(val) : JSON.stringify(m.getName());
|
|
931
|
+
});
|
|
932
|
+
return {
|
|
933
|
+
kind: "enum",
|
|
934
|
+
members
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
return null;
|
|
938
|
+
}
|
|
939
|
+
__name(findTypeInFile, "findTypeInFile");
|
|
940
|
+
function resolveImportedType(name, sourceFile, project) {
|
|
903
941
|
for (const importDecl of sourceFile.getImportDeclarations()) {
|
|
904
942
|
const namedImport = importDecl.getNamedImports().find((n) => n.getName() === name);
|
|
905
943
|
if (!namedImport) continue;
|
|
@@ -919,25 +957,19 @@ function resolveImportedClass(name, sourceFile, project) {
|
|
|
919
957
|
continue;
|
|
920
958
|
}
|
|
921
959
|
}
|
|
922
|
-
const
|
|
923
|
-
if (
|
|
924
|
-
cls,
|
|
925
|
-
file: importedFile
|
|
926
|
-
};
|
|
960
|
+
const result = findTypeInFile(name, importedFile);
|
|
961
|
+
if (result) return result;
|
|
927
962
|
}
|
|
928
963
|
}
|
|
929
964
|
return null;
|
|
930
965
|
}
|
|
931
|
-
__name(
|
|
932
|
-
function
|
|
933
|
-
const local =
|
|
934
|
-
if (local) return
|
|
935
|
-
|
|
936
|
-
file: sourceFile
|
|
937
|
-
};
|
|
938
|
-
return resolveImportedClass(name, sourceFile, project);
|
|
966
|
+
__name(resolveImportedType, "resolveImportedType");
|
|
967
|
+
function findType(name, sourceFile, project) {
|
|
968
|
+
const local = findTypeInFile(name, sourceFile);
|
|
969
|
+
if (local) return local;
|
|
970
|
+
return resolveImportedType(name, sourceFile, project);
|
|
939
971
|
}
|
|
940
|
-
__name(
|
|
972
|
+
__name(findType, "findType");
|
|
941
973
|
function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
|
|
942
974
|
if (depth <= 0) return "unknown";
|
|
943
975
|
if (import_ts_morph.Node.isArrayTypeNode(typeNode)) {
|
|
@@ -966,9 +998,9 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
|
|
|
966
998
|
}
|
|
967
999
|
return "unknown";
|
|
968
1000
|
}
|
|
969
|
-
const resolved =
|
|
1001
|
+
const resolved = findType(name, sourceFile, project);
|
|
970
1002
|
if (resolved) {
|
|
971
|
-
return
|
|
1003
|
+
return expandTypeDecl(resolved, project, depth - 1);
|
|
972
1004
|
}
|
|
973
1005
|
return name;
|
|
974
1006
|
}
|
|
@@ -981,10 +1013,24 @@ function resolveTypeNodeToString(typeNode, sourceFile, project, depth) {
|
|
|
981
1013
|
return typeNode.getText();
|
|
982
1014
|
}
|
|
983
1015
|
__name(resolveTypeNodeToString, "resolveTypeNodeToString");
|
|
984
|
-
function
|
|
1016
|
+
function expandTypeDecl(result, project, depth) {
|
|
1017
|
+
if (depth < 0) return "unknown";
|
|
1018
|
+
switch (result.kind) {
|
|
1019
|
+
case "class":
|
|
1020
|
+
return resolvePropertied(result.decl, result.file, project, depth);
|
|
1021
|
+
case "interface":
|
|
1022
|
+
return resolvePropertied(result.decl, result.file, project, depth);
|
|
1023
|
+
case "typeAlias":
|
|
1024
|
+
return result.text;
|
|
1025
|
+
case "enum":
|
|
1026
|
+
return result.members.join(" | ");
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
__name(expandTypeDecl, "expandTypeDecl");
|
|
1030
|
+
function resolvePropertied(decl, sourceFile, project, depth) {
|
|
985
1031
|
if (depth < 0) return "unknown";
|
|
986
1032
|
const lines = [];
|
|
987
|
-
for (const prop of
|
|
1033
|
+
for (const prop of decl.getProperties()) {
|
|
988
1034
|
const propName = prop.getName();
|
|
989
1035
|
const isOptional = prop.hasQuestionToken();
|
|
990
1036
|
const propTypeNode = prop.getTypeNode();
|
|
@@ -996,7 +1042,7 @@ function resolveClassDeclaration(cls, sourceFile, project, depth) {
|
|
|
996
1042
|
}
|
|
997
1043
|
return `{ ${lines.join("; ")} }`;
|
|
998
1044
|
}
|
|
999
|
-
__name(
|
|
1045
|
+
__name(resolvePropertied, "resolvePropertied");
|
|
1000
1046
|
function extractBodyType(method, sourceFile, project) {
|
|
1001
1047
|
for (const param of method.getParameters()) {
|
|
1002
1048
|
const bodyDecorator = param.getDecorators().find((d) => d.getName() === "Body");
|
|
@@ -1076,9 +1122,9 @@ __name(extractResponseType, "extractResponseType");
|
|
|
1076
1122
|
function resolveIdentifierToClassType(node, sourceFile, project, depth) {
|
|
1077
1123
|
if (!import_ts_morph.Node.isIdentifier(node)) return "unknown";
|
|
1078
1124
|
const name = node.getText();
|
|
1079
|
-
const resolved =
|
|
1125
|
+
const resolved = findType(name, sourceFile, project);
|
|
1080
1126
|
if (resolved) {
|
|
1081
|
-
return
|
|
1127
|
+
return expandTypeDecl(resolved, project, depth - 1);
|
|
1082
1128
|
}
|
|
1083
1129
|
return name;
|
|
1084
1130
|
}
|
|
@@ -1207,7 +1253,21 @@ function extractFromSourceFile(sourceFile, project) {
|
|
|
1207
1253
|
const combined = joinPaths(prefix, handlerPath);
|
|
1208
1254
|
const params = extractParams(combined);
|
|
1209
1255
|
const methodName = method.getName();
|
|
1210
|
-
const
|
|
1256
|
+
const classAsDecorator = cls.getDecorator("As");
|
|
1257
|
+
let classAs;
|
|
1258
|
+
if (classAsDecorator) {
|
|
1259
|
+
const classAsArgs = classAsDecorator.getArguments();
|
|
1260
|
+
const classAsName = decoratorStringArg(classAsArgs[0]);
|
|
1261
|
+
if (classAsName) classAs = classAsName;
|
|
1262
|
+
}
|
|
1263
|
+
const methodAsDecorator = method.getDecorator("As");
|
|
1264
|
+
let methodAs;
|
|
1265
|
+
if (methodAsDecorator) {
|
|
1266
|
+
const methodAsArgs = methodAsDecorator.getArguments();
|
|
1267
|
+
const methodAsName = decoratorStringArg(methodAsArgs[0]);
|
|
1268
|
+
if (methodAsName) methodAs = methodAsName;
|
|
1269
|
+
}
|
|
1270
|
+
const routeName = resolveRouteName(className, methodName, classAs, methodAs);
|
|
1211
1271
|
const dtoContract = extractDtoContract(method, sourceFile, project);
|
|
1212
1272
|
routes.push({
|
|
1213
1273
|
method: httpMethod,
|
|
@@ -1393,7 +1453,7 @@ async function watch(config, onChange) {
|
|
|
1393
1453
|
__name(watch, "watch");
|
|
1394
1454
|
|
|
1395
1455
|
// src/index.ts
|
|
1396
|
-
var VERSION = "1.0.
|
|
1456
|
+
var VERSION = "1.0.6";
|
|
1397
1457
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1398
1458
|
0 && (module.exports = {
|
|
1399
1459
|
CodegenError,
|