@autobe/compiler 0.30.0-dev.20260315 → 0.30.1
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/LICENSE +661 -661
- package/lib/database/validateDatabaseApplication.js +318 -318
- package/lib/raw/AutoBeCompilerCommonTemplate.js +5 -5
- package/lib/raw/AutoBeCompilerCommonTemplate.js.map +1 -1
- package/lib/raw/AutoBeCompilerInterfaceTemplate.js +4 -4
- package/lib/raw/AutoBeCompilerInterfaceTemplate.js.map +1 -1
- package/lib/raw/AutoBeCompilerRealizeTemplate.js +27 -27
- package/lib/raw/AutoBeCompilerRealizeTemplate.js.map +1 -1
- package/lib/raw/AutoBeCompilerRealizeTemplateOfPostgres.js +4 -4
- package/lib/raw/AutoBeCompilerRealizeTemplateOfPostgres.js.map +1 -1
- package/lib/raw/AutoBeCompilerRealizeTemplateOfSQLite.js +4 -4
- package/lib/raw/AutoBeCompilerRealizeTemplateOfSQLite.js.map +1 -1
- package/lib/raw/AutoBeCompilerTestTemplate.js +8 -8
- package/lib/raw/AutoBeCompilerTestTemplate.js.map +1 -1
- package/lib/raw/nestjs.json +1640 -1640
- package/lib/raw/test.json +130 -130
- package/package.json +4 -4
- package/src/AutoBeCompiler.ts +93 -93
- package/src/AutoBeTypeScriptCompiler.ts +136 -136
- package/src/database/AutoBeDatabaseCompiler.ts +48 -48
- package/src/database/validateDatabaseApplication.ts +873 -873
- package/src/index.ts +5 -5
- package/src/interface/AutoBeInterfaceCompiler.ts +79 -79
- package/src/raw/AutoBeCompilerCommonTemplate.ts +5 -5
- package/src/raw/AutoBeCompilerInterfaceTemplate.ts +4 -4
- package/src/raw/AutoBeCompilerRealizeTemplate.ts +27 -27
- package/src/raw/AutoBeCompilerRealizeTemplateOfPostgres.ts +4 -4
- package/src/raw/AutoBeCompilerRealizeTemplateOfSQLite.ts +4 -4
- package/src/raw/AutoBeCompilerTestTemplate.ts +8 -8
- package/src/raw/nestjs.json +1640 -1640
- package/src/raw/test.json +130 -130
- package/src/realize/AutoBeRealizeCompiler.ts +42 -42
- package/src/realize/testRealizeProject.ts +78 -78
- package/src/realize/writeRealizeControllers.ts +217 -217
- package/src/test/AutoBeTestCompiler.ts +112 -112
- package/src/test/programmers/AutoBeTestAccessorProgrammer.ts +42 -42
- package/src/test/programmers/AutoBeTestFunctionalProgrammer.ts +87 -87
- package/src/test/programmers/AutoBeTestLiteralProgrammer.ts +65 -65
- package/src/test/programmers/AutoBeTestOperatorProgrammer.ts +84 -84
- package/src/test/programmers/AutoBeTestPredicateProgrammer.ts +131 -131
- package/src/test/programmers/AutoBeTestRandomProgrammer.ts +304 -304
- package/src/test/programmers/AutoBeTestStatementProgrammer.ts +154 -154
- package/src/test/programmers/IAutoBeTestApiFunction.ts +6 -6
- package/src/test/programmers/IAutoBeTestProgrammerContext.ts +11 -11
- package/src/test/programmers/writeTestExpression.ts +29 -29
- package/src/test/programmers/writeTestFunction.ts +103 -103
- package/src/test/programmers/writeTestStatement.ts +19 -19
- package/src/utils/ArrayUtil.ts +21 -21
- package/src/utils/FilePrinter.ts +67 -67
- package/src/utils/ProcessUtil.ts +14 -14
- package/src/utils/shrinkCompileResult.ts +16 -16
- package/README.md +0 -261
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import {
|
|
2
|
-
IAutoBeRealizeCompiler,
|
|
3
|
-
IAutoBeRealizeCompilerListener,
|
|
4
|
-
IAutoBeRealizeControllerProps,
|
|
5
|
-
IAutoBeRealizeTestProps,
|
|
6
|
-
IAutoBeRealizeTestResult,
|
|
7
|
-
} from "@autobe/interface";
|
|
8
|
-
|
|
9
|
-
import { AutoBeCompilerRealizeTemplate } from "../raw/AutoBeCompilerRealizeTemplate";
|
|
10
|
-
import { FilePrinter } from "../utils/FilePrinter";
|
|
11
|
-
import { testRealizeProject } from "./testRealizeProject";
|
|
12
|
-
import { writeRealizeControllers } from "./writeRealizeControllers";
|
|
13
|
-
|
|
14
|
-
export class AutoBeRealizeCompiler implements IAutoBeRealizeCompiler {
|
|
15
|
-
public constructor(
|
|
16
|
-
private readonly listener: IAutoBeRealizeCompilerListener,
|
|
17
|
-
) {}
|
|
18
|
-
|
|
19
|
-
public async controller(
|
|
20
|
-
props: IAutoBeRealizeControllerProps,
|
|
21
|
-
): Promise<Record<string, string>> {
|
|
22
|
-
const result: Record<string, string> = await writeRealizeControllers(props);
|
|
23
|
-
for (const [key, value] of Object.entries(result))
|
|
24
|
-
result[key] = await FilePrinter.beautify(value);
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public test(
|
|
29
|
-
props: IAutoBeRealizeTestProps,
|
|
30
|
-
): Promise<IAutoBeRealizeTestResult> {
|
|
31
|
-
return testRealizeProject(
|
|
32
|
-
{
|
|
33
|
-
...props,
|
|
34
|
-
files: {
|
|
35
|
-
...props.files,
|
|
36
|
-
...AutoBeCompilerRealizeTemplate,
|
|
37
|
-
},
|
|
38
|
-
},
|
|
39
|
-
this.listener.test,
|
|
40
|
-
);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
IAutoBeRealizeCompiler,
|
|
3
|
+
IAutoBeRealizeCompilerListener,
|
|
4
|
+
IAutoBeRealizeControllerProps,
|
|
5
|
+
IAutoBeRealizeTestProps,
|
|
6
|
+
IAutoBeRealizeTestResult,
|
|
7
|
+
} from "@autobe/interface";
|
|
8
|
+
|
|
9
|
+
import { AutoBeCompilerRealizeTemplate } from "../raw/AutoBeCompilerRealizeTemplate";
|
|
10
|
+
import { FilePrinter } from "../utils/FilePrinter";
|
|
11
|
+
import { testRealizeProject } from "./testRealizeProject";
|
|
12
|
+
import { writeRealizeControllers } from "./writeRealizeControllers";
|
|
13
|
+
|
|
14
|
+
export class AutoBeRealizeCompiler implements IAutoBeRealizeCompiler {
|
|
15
|
+
public constructor(
|
|
16
|
+
private readonly listener: IAutoBeRealizeCompilerListener,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
public async controller(
|
|
20
|
+
props: IAutoBeRealizeControllerProps,
|
|
21
|
+
): Promise<Record<string, string>> {
|
|
22
|
+
const result: Record<string, string> = await writeRealizeControllers(props);
|
|
23
|
+
for (const [key, value] of Object.entries(result))
|
|
24
|
+
result[key] = await FilePrinter.beautify(value);
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
public test(
|
|
29
|
+
props: IAutoBeRealizeTestProps,
|
|
30
|
+
): Promise<IAutoBeRealizeTestResult> {
|
|
31
|
+
return testRealizeProject(
|
|
32
|
+
{
|
|
33
|
+
...props,
|
|
34
|
+
files: {
|
|
35
|
+
...props.files,
|
|
36
|
+
...AutoBeCompilerRealizeTemplate,
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
this.listener.test,
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import { FileSystemIterator } from "@autobe/filesystem";
|
|
2
|
-
import {
|
|
3
|
-
IAutoBeRealizeTestListener,
|
|
4
|
-
IAutoBeRealizeTestProps,
|
|
5
|
-
IAutoBeRealizeTestResult,
|
|
6
|
-
IAutoBeRealizeTestService,
|
|
7
|
-
} from "@autobe/interface";
|
|
8
|
-
import crypto from "crypto";
|
|
9
|
-
import fs from "fs";
|
|
10
|
-
import os from "os";
|
|
11
|
-
import { Driver, WorkerConnector } from "tgrid";
|
|
12
|
-
|
|
13
|
-
import { ProcessUtil } from "../utils/ProcessUtil";
|
|
14
|
-
|
|
15
|
-
export async function testRealizeProject(
|
|
16
|
-
props: IAutoBeRealizeTestProps,
|
|
17
|
-
listener: IAutoBeRealizeTestListener,
|
|
18
|
-
): Promise<IAutoBeRealizeTestResult> {
|
|
19
|
-
const cwd: string = `${os.tmpdir()}/autobe-realize-${crypto.randomUUID()}`;
|
|
20
|
-
await fs.promises.mkdtemp(cwd);
|
|
21
|
-
try {
|
|
22
|
-
await setup(props.files, cwd);
|
|
23
|
-
return await test(props, listener, cwd);
|
|
24
|
-
} catch (error) {
|
|
25
|
-
throw error;
|
|
26
|
-
} finally {
|
|
27
|
-
await fs.promises.rm(cwd, {
|
|
28
|
-
recursive: true,
|
|
29
|
-
force: true,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async function setup(
|
|
35
|
-
files: Record<string, string>,
|
|
36
|
-
cwd: string,
|
|
37
|
-
): Promise<void> {
|
|
38
|
-
await FileSystemIterator.save({
|
|
39
|
-
root: cwd,
|
|
40
|
-
files,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
const exec = async (s: string) =>
|
|
44
|
-
ProcessUtil.exec(s, {
|
|
45
|
-
cwd,
|
|
46
|
-
});
|
|
47
|
-
await exec("pnpm install");
|
|
48
|
-
await exec("pnpm run build:prisma");
|
|
49
|
-
await exec("pnpm run build:test");
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async function test(
|
|
53
|
-
props: IAutoBeRealizeTestProps,
|
|
54
|
-
listener: IAutoBeRealizeTestListener,
|
|
55
|
-
cwd: string,
|
|
56
|
-
): Promise<IAutoBeRealizeTestResult> {
|
|
57
|
-
const worker: WorkerConnector<
|
|
58
|
-
null,
|
|
59
|
-
IAutoBeRealizeTestListener,
|
|
60
|
-
IAutoBeRealizeTestService
|
|
61
|
-
> = new WorkerConnector(null, listener, "process");
|
|
62
|
-
await worker.connect(`${cwd}/bin/test/servant.js`, {
|
|
63
|
-
stdio: "ignore",
|
|
64
|
-
cwd,
|
|
65
|
-
});
|
|
66
|
-
const service: Driver<IAutoBeRealizeTestService> = worker.getDriver();
|
|
67
|
-
try {
|
|
68
|
-
const result: IAutoBeRealizeTestResult = await service.execute({
|
|
69
|
-
reset: props.reset,
|
|
70
|
-
simultaneous: props.simultaneous,
|
|
71
|
-
});
|
|
72
|
-
return result;
|
|
73
|
-
} catch (error) {
|
|
74
|
-
throw error;
|
|
75
|
-
} finally {
|
|
76
|
-
await worker.close();
|
|
77
|
-
}
|
|
78
|
-
}
|
|
1
|
+
import { FileSystemIterator } from "@autobe/filesystem";
|
|
2
|
+
import {
|
|
3
|
+
IAutoBeRealizeTestListener,
|
|
4
|
+
IAutoBeRealizeTestProps,
|
|
5
|
+
IAutoBeRealizeTestResult,
|
|
6
|
+
IAutoBeRealizeTestService,
|
|
7
|
+
} from "@autobe/interface";
|
|
8
|
+
import crypto from "crypto";
|
|
9
|
+
import fs from "fs";
|
|
10
|
+
import os from "os";
|
|
11
|
+
import { Driver, WorkerConnector } from "tgrid";
|
|
12
|
+
|
|
13
|
+
import { ProcessUtil } from "../utils/ProcessUtil";
|
|
14
|
+
|
|
15
|
+
export async function testRealizeProject(
|
|
16
|
+
props: IAutoBeRealizeTestProps,
|
|
17
|
+
listener: IAutoBeRealizeTestListener,
|
|
18
|
+
): Promise<IAutoBeRealizeTestResult> {
|
|
19
|
+
const cwd: string = `${os.tmpdir()}/autobe-realize-${crypto.randomUUID()}`;
|
|
20
|
+
await fs.promises.mkdtemp(cwd);
|
|
21
|
+
try {
|
|
22
|
+
await setup(props.files, cwd);
|
|
23
|
+
return await test(props, listener, cwd);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
throw error;
|
|
26
|
+
} finally {
|
|
27
|
+
await fs.promises.rm(cwd, {
|
|
28
|
+
recursive: true,
|
|
29
|
+
force: true,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function setup(
|
|
35
|
+
files: Record<string, string>,
|
|
36
|
+
cwd: string,
|
|
37
|
+
): Promise<void> {
|
|
38
|
+
await FileSystemIterator.save({
|
|
39
|
+
root: cwd,
|
|
40
|
+
files,
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const exec = async (s: string) =>
|
|
44
|
+
ProcessUtil.exec(s, {
|
|
45
|
+
cwd,
|
|
46
|
+
});
|
|
47
|
+
await exec("pnpm install");
|
|
48
|
+
await exec("pnpm run build:prisma");
|
|
49
|
+
await exec("pnpm run build:test");
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function test(
|
|
53
|
+
props: IAutoBeRealizeTestProps,
|
|
54
|
+
listener: IAutoBeRealizeTestListener,
|
|
55
|
+
cwd: string,
|
|
56
|
+
): Promise<IAutoBeRealizeTestResult> {
|
|
57
|
+
const worker: WorkerConnector<
|
|
58
|
+
null,
|
|
59
|
+
IAutoBeRealizeTestListener,
|
|
60
|
+
IAutoBeRealizeTestService
|
|
61
|
+
> = new WorkerConnector(null, listener, "process");
|
|
62
|
+
await worker.connect(`${cwd}/bin/test/servant.js`, {
|
|
63
|
+
stdio: "ignore",
|
|
64
|
+
cwd,
|
|
65
|
+
});
|
|
66
|
+
const service: Driver<IAutoBeRealizeTestService> = worker.getDriver();
|
|
67
|
+
try {
|
|
68
|
+
const result: IAutoBeRealizeTestResult = await service.execute({
|
|
69
|
+
reset: props.reset,
|
|
70
|
+
simultaneous: props.simultaneous,
|
|
71
|
+
});
|
|
72
|
+
return result;
|
|
73
|
+
} catch (error) {
|
|
74
|
+
throw error;
|
|
75
|
+
} finally {
|
|
76
|
+
await worker.close();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -1,217 +1,217 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AutoBeOpenApi,
|
|
3
|
-
AutoBeRealizeAuthorization,
|
|
4
|
-
AutoBeRealizeOperationFunction,
|
|
5
|
-
IAutoBeRealizeControllerProps,
|
|
6
|
-
} from "@autobe/interface";
|
|
7
|
-
import { transformOpenApiDocument } from "@autobe/utils";
|
|
8
|
-
import {
|
|
9
|
-
NestiaMigrateApplication,
|
|
10
|
-
NestiaMigrateNestMethodProgrammer,
|
|
11
|
-
} from "@nestia/migrate";
|
|
12
|
-
import path from "path";
|
|
13
|
-
import ts from "typescript";
|
|
14
|
-
|
|
15
|
-
import { ArrayUtil } from "../utils/ArrayUtil";
|
|
16
|
-
import { FilePrinter } from "../utils/FilePrinter";
|
|
17
|
-
|
|
18
|
-
export const writeRealizeControllers = async (
|
|
19
|
-
props: IAutoBeRealizeControllerProps,
|
|
20
|
-
): Promise<Record<string, string>> => {
|
|
21
|
-
const app: NestiaMigrateApplication = new NestiaMigrateApplication(
|
|
22
|
-
transformOpenApiDocument(props.document),
|
|
23
|
-
);
|
|
24
|
-
const result: Record<string, string> = app.nest({
|
|
25
|
-
simulate: false,
|
|
26
|
-
e2e: false,
|
|
27
|
-
programmer: {
|
|
28
|
-
controllerMethod: (ctx) => {
|
|
29
|
-
const method: ts.MethodDeclaration =
|
|
30
|
-
NestiaMigrateNestMethodProgrammer.write(ctx);
|
|
31
|
-
const operate: AutoBeOpenApi.IOperation | undefined =
|
|
32
|
-
props.document.operations.find(
|
|
33
|
-
(o) => o.method === ctx.route.method && o.path === ctx.route.path,
|
|
34
|
-
);
|
|
35
|
-
const func: AutoBeRealizeOperationFunction | undefined = props.functions
|
|
36
|
-
.filter((f) => f.type === "operation")
|
|
37
|
-
.find(
|
|
38
|
-
(f) =>
|
|
39
|
-
f.endpoint.method === ctx.route.method &&
|
|
40
|
-
f.endpoint.path === ctx.route.path,
|
|
41
|
-
);
|
|
42
|
-
if (func === undefined || operate === undefined) return method; // unreachable
|
|
43
|
-
|
|
44
|
-
const authorization: AutoBeRealizeAuthorization | undefined =
|
|
45
|
-
operate.authorizationActor !== null &&
|
|
46
|
-
operate.authorizationType !== "join" &&
|
|
47
|
-
operate.authorizationType !== "login" &&
|
|
48
|
-
operate.authorizationType !== "refresh"
|
|
49
|
-
? props.authorizations.find(
|
|
50
|
-
(d) => d.actor.name === operate.authorizationActor,
|
|
51
|
-
)
|
|
52
|
-
: undefined;
|
|
53
|
-
|
|
54
|
-
ctx.importer.external({
|
|
55
|
-
type: "instance",
|
|
56
|
-
library: path
|
|
57
|
-
.relative(ctx.controller.location, func.location)
|
|
58
|
-
.replaceAll(path.sep, "/")
|
|
59
|
-
.split(".ts")[0],
|
|
60
|
-
name: func.name,
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const inputArguments: string[] = [
|
|
64
|
-
...(authorization ? [authorization.actor.name] : []),
|
|
65
|
-
...(authorization === undefined &&
|
|
66
|
-
(operate.authorizationType === "login" ||
|
|
67
|
-
operate.authorizationType === "join")
|
|
68
|
-
? ["ip"]
|
|
69
|
-
: []),
|
|
70
|
-
...ctx.route.parameters.map((p) => p.name),
|
|
71
|
-
...(ctx.route.query ? [ctx.route.query.name] : []),
|
|
72
|
-
...(ctx.route.body ? [ctx.route.body.name] : []),
|
|
73
|
-
];
|
|
74
|
-
const call: ts.Expression = ts.factory.createCallExpression(
|
|
75
|
-
ts.factory.createIdentifier(func.name),
|
|
76
|
-
undefined,
|
|
77
|
-
inputArguments.length === 0
|
|
78
|
-
? undefined
|
|
79
|
-
: [
|
|
80
|
-
ts.factory.createObjectLiteralExpression(
|
|
81
|
-
inputArguments.map((name) =>
|
|
82
|
-
ts.factory.createShorthandPropertyAssignment(name),
|
|
83
|
-
),
|
|
84
|
-
true,
|
|
85
|
-
),
|
|
86
|
-
],
|
|
87
|
-
);
|
|
88
|
-
const tryCatch = ts.factory.createTryStatement(
|
|
89
|
-
ts.factory.createBlock(
|
|
90
|
-
[
|
|
91
|
-
ts.factory.createReturnStatement(
|
|
92
|
-
ts.factory.createAwaitExpression(call),
|
|
93
|
-
),
|
|
94
|
-
],
|
|
95
|
-
true,
|
|
96
|
-
),
|
|
97
|
-
ts.factory.createCatchClause(
|
|
98
|
-
ts.factory.createVariableDeclaration(
|
|
99
|
-
ts.factory.createIdentifier("error"),
|
|
100
|
-
undefined,
|
|
101
|
-
undefined,
|
|
102
|
-
undefined,
|
|
103
|
-
),
|
|
104
|
-
ts.factory.createBlock(
|
|
105
|
-
[
|
|
106
|
-
ts.factory.createExpressionStatement(
|
|
107
|
-
ts.factory.createCallExpression(
|
|
108
|
-
ts.factory.createIdentifier("console.log"),
|
|
109
|
-
undefined,
|
|
110
|
-
[ts.factory.createIdentifier("error")],
|
|
111
|
-
),
|
|
112
|
-
),
|
|
113
|
-
ts.factory.createThrowStatement(
|
|
114
|
-
ts.factory.createIdentifier("error"),
|
|
115
|
-
),
|
|
116
|
-
],
|
|
117
|
-
true,
|
|
118
|
-
),
|
|
119
|
-
),
|
|
120
|
-
undefined,
|
|
121
|
-
);
|
|
122
|
-
return ts.factory.updateMethodDeclaration(
|
|
123
|
-
method,
|
|
124
|
-
method.modifiers,
|
|
125
|
-
method.asteriskToken,
|
|
126
|
-
method.name,
|
|
127
|
-
method.questionToken,
|
|
128
|
-
method.typeParameters,
|
|
129
|
-
authorization !== undefined
|
|
130
|
-
? [
|
|
131
|
-
createAuthorizationParameter(ctx, authorization),
|
|
132
|
-
...method.parameters,
|
|
133
|
-
]
|
|
134
|
-
: inputArguments.includes("ip")
|
|
135
|
-
? [createIpParameter(ctx), ...method.parameters]
|
|
136
|
-
: method.parameters,
|
|
137
|
-
method.type,
|
|
138
|
-
ts.factory.createBlock([tryCatch]),
|
|
139
|
-
);
|
|
140
|
-
},
|
|
141
|
-
},
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
const entries: [string, string][] = await ArrayUtil.asyncMap(
|
|
145
|
-
Object.entries(result).filter(([key]) =>
|
|
146
|
-
key.startsWith("src/controllers/"),
|
|
147
|
-
),
|
|
148
|
-
async ([key, value]) => [key, await FilePrinter.beautify(value)],
|
|
149
|
-
);
|
|
150
|
-
return Object.fromEntries(entries);
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const createAuthorizationParameter = (
|
|
154
|
-
ctx: NestiaMigrateNestMethodProgrammer.IContext,
|
|
155
|
-
authorization: AutoBeRealizeAuthorization,
|
|
156
|
-
) =>
|
|
157
|
-
ts.factory.createParameterDeclaration(
|
|
158
|
-
[
|
|
159
|
-
ts.factory.createDecorator(
|
|
160
|
-
ts.factory.createCallExpression(
|
|
161
|
-
ts.factory.createIdentifier(
|
|
162
|
-
ctx.importer.external({
|
|
163
|
-
type: "instance",
|
|
164
|
-
library: path
|
|
165
|
-
.relative(
|
|
166
|
-
ctx.controller.location,
|
|
167
|
-
authorization.decorator.location,
|
|
168
|
-
)
|
|
169
|
-
.replaceAll(path.sep, "/")
|
|
170
|
-
.split(".ts")[0],
|
|
171
|
-
name: authorization.decorator.name,
|
|
172
|
-
}),
|
|
173
|
-
),
|
|
174
|
-
undefined,
|
|
175
|
-
[],
|
|
176
|
-
),
|
|
177
|
-
),
|
|
178
|
-
],
|
|
179
|
-
undefined,
|
|
180
|
-
authorization.actor.name,
|
|
181
|
-
undefined,
|
|
182
|
-
ts.factory.createTypeReferenceNode(
|
|
183
|
-
ctx.importer.external({
|
|
184
|
-
type: "instance",
|
|
185
|
-
library: path
|
|
186
|
-
.relative(ctx.controller.location, authorization.payload.location)
|
|
187
|
-
.replaceAll(path.sep, "/")
|
|
188
|
-
.split(".ts")[0],
|
|
189
|
-
name: authorization.payload.name,
|
|
190
|
-
}),
|
|
191
|
-
),
|
|
192
|
-
undefined,
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
const createIpParameter = (ctx: NestiaMigrateNestMethodProgrammer.IContext) =>
|
|
196
|
-
ts.factory.createParameterDeclaration(
|
|
197
|
-
[
|
|
198
|
-
ts.factory.createDecorator(
|
|
199
|
-
ts.factory.createCallExpression(
|
|
200
|
-
ts.factory.createIdentifier(
|
|
201
|
-
ctx.importer.external({
|
|
202
|
-
type: "instance",
|
|
203
|
-
library: "@nestjs/common",
|
|
204
|
-
name: "Ip",
|
|
205
|
-
}),
|
|
206
|
-
),
|
|
207
|
-
undefined,
|
|
208
|
-
[],
|
|
209
|
-
),
|
|
210
|
-
),
|
|
211
|
-
],
|
|
212
|
-
undefined,
|
|
213
|
-
"ip",
|
|
214
|
-
undefined,
|
|
215
|
-
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
|
|
216
|
-
undefined,
|
|
217
|
-
);
|
|
1
|
+
import {
|
|
2
|
+
AutoBeOpenApi,
|
|
3
|
+
AutoBeRealizeAuthorization,
|
|
4
|
+
AutoBeRealizeOperationFunction,
|
|
5
|
+
IAutoBeRealizeControllerProps,
|
|
6
|
+
} from "@autobe/interface";
|
|
7
|
+
import { transformOpenApiDocument } from "@autobe/utils";
|
|
8
|
+
import {
|
|
9
|
+
NestiaMigrateApplication,
|
|
10
|
+
NestiaMigrateNestMethodProgrammer,
|
|
11
|
+
} from "@nestia/migrate";
|
|
12
|
+
import path from "path";
|
|
13
|
+
import ts from "typescript";
|
|
14
|
+
|
|
15
|
+
import { ArrayUtil } from "../utils/ArrayUtil";
|
|
16
|
+
import { FilePrinter } from "../utils/FilePrinter";
|
|
17
|
+
|
|
18
|
+
export const writeRealizeControllers = async (
|
|
19
|
+
props: IAutoBeRealizeControllerProps,
|
|
20
|
+
): Promise<Record<string, string>> => {
|
|
21
|
+
const app: NestiaMigrateApplication = new NestiaMigrateApplication(
|
|
22
|
+
transformOpenApiDocument(props.document),
|
|
23
|
+
);
|
|
24
|
+
const result: Record<string, string> = app.nest({
|
|
25
|
+
simulate: false,
|
|
26
|
+
e2e: false,
|
|
27
|
+
programmer: {
|
|
28
|
+
controllerMethod: (ctx) => {
|
|
29
|
+
const method: ts.MethodDeclaration =
|
|
30
|
+
NestiaMigrateNestMethodProgrammer.write(ctx);
|
|
31
|
+
const operate: AutoBeOpenApi.IOperation | undefined =
|
|
32
|
+
props.document.operations.find(
|
|
33
|
+
(o) => o.method === ctx.route.method && o.path === ctx.route.path,
|
|
34
|
+
);
|
|
35
|
+
const func: AutoBeRealizeOperationFunction | undefined = props.functions
|
|
36
|
+
.filter((f) => f.type === "operation")
|
|
37
|
+
.find(
|
|
38
|
+
(f) =>
|
|
39
|
+
f.endpoint.method === ctx.route.method &&
|
|
40
|
+
f.endpoint.path === ctx.route.path,
|
|
41
|
+
);
|
|
42
|
+
if (func === undefined || operate === undefined) return method; // unreachable
|
|
43
|
+
|
|
44
|
+
const authorization: AutoBeRealizeAuthorization | undefined =
|
|
45
|
+
operate.authorizationActor !== null &&
|
|
46
|
+
operate.authorizationType !== "join" &&
|
|
47
|
+
operate.authorizationType !== "login" &&
|
|
48
|
+
operate.authorizationType !== "refresh"
|
|
49
|
+
? props.authorizations.find(
|
|
50
|
+
(d) => d.actor.name === operate.authorizationActor,
|
|
51
|
+
)
|
|
52
|
+
: undefined;
|
|
53
|
+
|
|
54
|
+
ctx.importer.external({
|
|
55
|
+
type: "instance",
|
|
56
|
+
library: path
|
|
57
|
+
.relative(ctx.controller.location, func.location)
|
|
58
|
+
.replaceAll(path.sep, "/")
|
|
59
|
+
.split(".ts")[0],
|
|
60
|
+
name: func.name,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const inputArguments: string[] = [
|
|
64
|
+
...(authorization ? [authorization.actor.name] : []),
|
|
65
|
+
...(authorization === undefined &&
|
|
66
|
+
(operate.authorizationType === "login" ||
|
|
67
|
+
operate.authorizationType === "join")
|
|
68
|
+
? ["ip"]
|
|
69
|
+
: []),
|
|
70
|
+
...ctx.route.parameters.map((p) => p.name),
|
|
71
|
+
...(ctx.route.query ? [ctx.route.query.name] : []),
|
|
72
|
+
...(ctx.route.body ? [ctx.route.body.name] : []),
|
|
73
|
+
];
|
|
74
|
+
const call: ts.Expression = ts.factory.createCallExpression(
|
|
75
|
+
ts.factory.createIdentifier(func.name),
|
|
76
|
+
undefined,
|
|
77
|
+
inputArguments.length === 0
|
|
78
|
+
? undefined
|
|
79
|
+
: [
|
|
80
|
+
ts.factory.createObjectLiteralExpression(
|
|
81
|
+
inputArguments.map((name) =>
|
|
82
|
+
ts.factory.createShorthandPropertyAssignment(name),
|
|
83
|
+
),
|
|
84
|
+
true,
|
|
85
|
+
),
|
|
86
|
+
],
|
|
87
|
+
);
|
|
88
|
+
const tryCatch = ts.factory.createTryStatement(
|
|
89
|
+
ts.factory.createBlock(
|
|
90
|
+
[
|
|
91
|
+
ts.factory.createReturnStatement(
|
|
92
|
+
ts.factory.createAwaitExpression(call),
|
|
93
|
+
),
|
|
94
|
+
],
|
|
95
|
+
true,
|
|
96
|
+
),
|
|
97
|
+
ts.factory.createCatchClause(
|
|
98
|
+
ts.factory.createVariableDeclaration(
|
|
99
|
+
ts.factory.createIdentifier("error"),
|
|
100
|
+
undefined,
|
|
101
|
+
undefined,
|
|
102
|
+
undefined,
|
|
103
|
+
),
|
|
104
|
+
ts.factory.createBlock(
|
|
105
|
+
[
|
|
106
|
+
ts.factory.createExpressionStatement(
|
|
107
|
+
ts.factory.createCallExpression(
|
|
108
|
+
ts.factory.createIdentifier("console.log"),
|
|
109
|
+
undefined,
|
|
110
|
+
[ts.factory.createIdentifier("error")],
|
|
111
|
+
),
|
|
112
|
+
),
|
|
113
|
+
ts.factory.createThrowStatement(
|
|
114
|
+
ts.factory.createIdentifier("error"),
|
|
115
|
+
),
|
|
116
|
+
],
|
|
117
|
+
true,
|
|
118
|
+
),
|
|
119
|
+
),
|
|
120
|
+
undefined,
|
|
121
|
+
);
|
|
122
|
+
return ts.factory.updateMethodDeclaration(
|
|
123
|
+
method,
|
|
124
|
+
method.modifiers,
|
|
125
|
+
method.asteriskToken,
|
|
126
|
+
method.name,
|
|
127
|
+
method.questionToken,
|
|
128
|
+
method.typeParameters,
|
|
129
|
+
authorization !== undefined
|
|
130
|
+
? [
|
|
131
|
+
createAuthorizationParameter(ctx, authorization),
|
|
132
|
+
...method.parameters,
|
|
133
|
+
]
|
|
134
|
+
: inputArguments.includes("ip")
|
|
135
|
+
? [createIpParameter(ctx), ...method.parameters]
|
|
136
|
+
: method.parameters,
|
|
137
|
+
method.type,
|
|
138
|
+
ts.factory.createBlock([tryCatch]),
|
|
139
|
+
);
|
|
140
|
+
},
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const entries: [string, string][] = await ArrayUtil.asyncMap(
|
|
145
|
+
Object.entries(result).filter(([key]) =>
|
|
146
|
+
key.startsWith("src/controllers/"),
|
|
147
|
+
),
|
|
148
|
+
async ([key, value]) => [key, await FilePrinter.beautify(value)],
|
|
149
|
+
);
|
|
150
|
+
return Object.fromEntries(entries);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const createAuthorizationParameter = (
|
|
154
|
+
ctx: NestiaMigrateNestMethodProgrammer.IContext,
|
|
155
|
+
authorization: AutoBeRealizeAuthorization,
|
|
156
|
+
) =>
|
|
157
|
+
ts.factory.createParameterDeclaration(
|
|
158
|
+
[
|
|
159
|
+
ts.factory.createDecorator(
|
|
160
|
+
ts.factory.createCallExpression(
|
|
161
|
+
ts.factory.createIdentifier(
|
|
162
|
+
ctx.importer.external({
|
|
163
|
+
type: "instance",
|
|
164
|
+
library: path
|
|
165
|
+
.relative(
|
|
166
|
+
ctx.controller.location,
|
|
167
|
+
authorization.decorator.location,
|
|
168
|
+
)
|
|
169
|
+
.replaceAll(path.sep, "/")
|
|
170
|
+
.split(".ts")[0],
|
|
171
|
+
name: authorization.decorator.name,
|
|
172
|
+
}),
|
|
173
|
+
),
|
|
174
|
+
undefined,
|
|
175
|
+
[],
|
|
176
|
+
),
|
|
177
|
+
),
|
|
178
|
+
],
|
|
179
|
+
undefined,
|
|
180
|
+
authorization.actor.name,
|
|
181
|
+
undefined,
|
|
182
|
+
ts.factory.createTypeReferenceNode(
|
|
183
|
+
ctx.importer.external({
|
|
184
|
+
type: "instance",
|
|
185
|
+
library: path
|
|
186
|
+
.relative(ctx.controller.location, authorization.payload.location)
|
|
187
|
+
.replaceAll(path.sep, "/")
|
|
188
|
+
.split(".ts")[0],
|
|
189
|
+
name: authorization.payload.name,
|
|
190
|
+
}),
|
|
191
|
+
),
|
|
192
|
+
undefined,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const createIpParameter = (ctx: NestiaMigrateNestMethodProgrammer.IContext) =>
|
|
196
|
+
ts.factory.createParameterDeclaration(
|
|
197
|
+
[
|
|
198
|
+
ts.factory.createDecorator(
|
|
199
|
+
ts.factory.createCallExpression(
|
|
200
|
+
ts.factory.createIdentifier(
|
|
201
|
+
ctx.importer.external({
|
|
202
|
+
type: "instance",
|
|
203
|
+
library: "@nestjs/common",
|
|
204
|
+
name: "Ip",
|
|
205
|
+
}),
|
|
206
|
+
),
|
|
207
|
+
undefined,
|
|
208
|
+
[],
|
|
209
|
+
),
|
|
210
|
+
),
|
|
211
|
+
],
|
|
212
|
+
undefined,
|
|
213
|
+
"ip",
|
|
214
|
+
undefined,
|
|
215
|
+
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
|
|
216
|
+
undefined,
|
|
217
|
+
);
|