@autobe/utils 0.10.0
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 +21 -0
- package/lib/StringUtil.d.ts +4 -0
- package/lib/StringUtil.js +43 -0
- package/lib/StringUtil.js.map +1 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +20 -0
- package/lib/index.js.map +1 -0
- package/lib/interface/AutoBeEndpointComparator.d.ts +5 -0
- package/lib/interface/AutoBeEndpointComparator.js +10 -0
- package/lib/interface/AutoBeEndpointComparator.js.map +1 -0
- package/lib/interface/index.d.ts +3 -0
- package/lib/interface/index.js +20 -0
- package/lib/interface/index.js.map +1 -0
- package/lib/interface/invertOpenApiDocument.d.ts +3 -0
- package/lib/interface/invertOpenApiDocument.js +55 -0
- package/lib/interface/invertOpenApiDocument.js.map +1 -0
- package/lib/interface/transformOpenApiDocument.d.ts +3 -0
- package/lib/interface/transformOpenApiDocument.js +56 -0
- package/lib/interface/transformOpenApiDocument.js.map +1 -0
- package/lib/test/AutoBeTestExpressionValidator.d.ts +39 -0
- package/lib/test/AutoBeTestExpressionValidator.js +129 -0
- package/lib/test/AutoBeTestExpressionValidator.js.map +1 -0
- package/lib/test/AutoBeTestStatementValidator.d.ts +10 -0
- package/lib/test/AutoBeTestStatementValidator.js +41 -0
- package/lib/test/AutoBeTestStatementValidator.js.map +1 -0
- package/lib/test/IAutoBeTextValidateContext.d.ts +9 -0
- package/lib/test/IAutoBeTextValidateContext.js +3 -0
- package/lib/test/IAutoBeTextValidateContext.js.map +1 -0
- package/lib/test/index.d.ts +2 -0
- package/lib/test/index.js +19 -0
- package/lib/test/index.js.map +1 -0
- package/lib/test/validateTestApiOperateStatement.d.ts +3 -0
- package/lib/test/validateTestApiOperateStatement.js +76 -0
- package/lib/test/validateTestApiOperateStatement.js.map +1 -0
- package/lib/test/validateTestExpression.d.ts +3 -0
- package/lib/test/validateTestExpression.js +7 -0
- package/lib/test/validateTestExpression.js.map +1 -0
- package/lib/test/validateTestFunction.d.ts +2 -0
- package/lib/test/validateTestFunction.js +7 -0
- package/lib/test/validateTestFunction.js.map +1 -0
- package/lib/test/validateTestStatement.d.ts +3 -0
- package/lib/test/validateTestStatement.js +7 -0
- package/lib/test/validateTestStatement.js.map +1 -0
- package/package.json +42 -0
- package/src/StringUtil.ts +45 -0
- package/src/index.ts +4 -0
- package/src/interface/AutoBeEndpointComparator.ts +12 -0
- package/src/interface/index.ts +3 -0
- package/src/interface/invertOpenApiDocument.ts +67 -0
- package/src/interface/transformOpenApiDocument.ts +54 -0
- package/src/test/AutoBeTestExpressionValidator.ts +253 -0
- package/src/test/AutoBeTestStatementValidator.ts +72 -0
- package/src/test/IAutoBeTextValidateContext.ts +10 -0
- package/src/test/index.ts +2 -0
- package/src/test/validateTestApiOperateStatement.ts +92 -0
- package/src/test/validateTestExpression.ts +10 -0
- package/src/test/validateTestFunction.ts +7 -0
- package/src/test/validateTestStatement.ts +10 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { AutoBeTest } from "@autobe/interface";
|
|
2
|
+
|
|
3
|
+
import { IAutoBeTextValidateContext } from "./IAutoBeTextValidateContext";
|
|
4
|
+
import { validateTestApiOperateStatement } from "./validateTestApiOperateStatement";
|
|
5
|
+
import { validateTestExpression } from "./validateTestExpression";
|
|
6
|
+
import { validateTestStatement } from "./validateTestStatement";
|
|
7
|
+
|
|
8
|
+
export namespace AutoBeTestStatementValidator {
|
|
9
|
+
export const block = (
|
|
10
|
+
ctx: IAutoBeTextValidateContext,
|
|
11
|
+
item: AutoBeTest.IBlock,
|
|
12
|
+
path: string,
|
|
13
|
+
): void => {
|
|
14
|
+
item.statements.forEach((stmt, i) =>
|
|
15
|
+
validateTestStatement(ctx, stmt, `${path}.statements[${i}]`),
|
|
16
|
+
);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export const apiOperateStatement = (
|
|
20
|
+
ctx: IAutoBeTextValidateContext,
|
|
21
|
+
item: AutoBeTest.IApiOperateStatement,
|
|
22
|
+
path: string,
|
|
23
|
+
): void => {
|
|
24
|
+
validateTestApiOperateStatement(ctx, item, path);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export const expressionStatement = (
|
|
28
|
+
ctx: IAutoBeTextValidateContext,
|
|
29
|
+
item: AutoBeTest.IExpressionStatement,
|
|
30
|
+
path: string,
|
|
31
|
+
): void => {
|
|
32
|
+
validateTestExpression(ctx, item.expression, `${path}.expression`);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const ifStatement = (
|
|
36
|
+
ctx: IAutoBeTextValidateContext,
|
|
37
|
+
item: AutoBeTest.IIfStatement,
|
|
38
|
+
path: string,
|
|
39
|
+
): void => {
|
|
40
|
+
validateTestExpression(ctx, item.condition, `${path}.condition`);
|
|
41
|
+
block(ctx, item.thenStatement, `${path}.thenStatement`);
|
|
42
|
+
if (!!item.elseStatement)
|
|
43
|
+
if (item.elseStatement.type === "block")
|
|
44
|
+
block(ctx, item.elseStatement, `${path}.elseStatement`);
|
|
45
|
+
else
|
|
46
|
+
validateTestStatement(ctx, item.elseStatement, `${path}.elseStatement`);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const returnStatement = (
|
|
50
|
+
ctx: IAutoBeTextValidateContext,
|
|
51
|
+
item: AutoBeTest.IReturnStatement,
|
|
52
|
+
path: string,
|
|
53
|
+
): void => {
|
|
54
|
+
validateTestExpression(ctx, item.expression, `${path}.expression`);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const throwStatement = (
|
|
58
|
+
ctx: IAutoBeTextValidateContext,
|
|
59
|
+
item: AutoBeTest.IThrowStatement,
|
|
60
|
+
path: string,
|
|
61
|
+
): void => {
|
|
62
|
+
validateTestExpression(ctx, item.expression, `${path}.expression`);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// export const variableDeclaration = (
|
|
66
|
+
// ctx: IAutoBeTextValidateContext,
|
|
67
|
+
// item: AutoBeTest.IVariableDeclaration,
|
|
68
|
+
// path: string,
|
|
69
|
+
// ): void => {
|
|
70
|
+
// validateTestExpression(ctx, item.initializer, `${path}.initializer`);
|
|
71
|
+
// };
|
|
72
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AutoBeOpenApi, AutoBeTest } from "@autobe/interface";
|
|
2
|
+
import { HashMap } from "tstl";
|
|
3
|
+
import { IValidation } from "typia";
|
|
4
|
+
|
|
5
|
+
export interface IAutoBeTextValidateContext {
|
|
6
|
+
function: AutoBeTest.IFunction;
|
|
7
|
+
document: AutoBeOpenApi.IDocument;
|
|
8
|
+
endpoints: HashMap<AutoBeOpenApi.IEndpoint, AutoBeOpenApi.IOperation>;
|
|
9
|
+
errors: IValidation.IError[];
|
|
10
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { AutoBeOpenApi, AutoBeTest } from "@autobe/interface";
|
|
2
|
+
import { HashMap } from "tstl";
|
|
3
|
+
import typia from "typia";
|
|
4
|
+
|
|
5
|
+
import { IAutoBeTextValidateContext } from "./IAutoBeTextValidateContext";
|
|
6
|
+
|
|
7
|
+
export const validateTestApiOperateStatement = (
|
|
8
|
+
ctx: IAutoBeTextValidateContext,
|
|
9
|
+
stmt: AutoBeTest.IApiOperateStatement,
|
|
10
|
+
path: string,
|
|
11
|
+
): void => {
|
|
12
|
+
// Check API endpoint
|
|
13
|
+
const it: HashMap.Iterator<
|
|
14
|
+
AutoBeOpenApi.IEndpoint,
|
|
15
|
+
AutoBeOpenApi.IOperation
|
|
16
|
+
> = ctx.endpoints.find(stmt.endpoint);
|
|
17
|
+
if (it.equals(ctx.endpoints.end()) === true) {
|
|
18
|
+
ctx.errors.push({
|
|
19
|
+
path: `${path}.endpoint`,
|
|
20
|
+
value: stmt.endpoint,
|
|
21
|
+
expected: ctx.document.operations
|
|
22
|
+
.map((o) => `AutoBeOpenApi.IEndpoint<"${o.method}", "${o.path}">`)
|
|
23
|
+
.join(" | "),
|
|
24
|
+
description: [
|
|
25
|
+
"You can use only endpoints defined in the OpenAPI document.",
|
|
26
|
+
"",
|
|
27
|
+
ctx.document.operations
|
|
28
|
+
.map((o) => `- { method: "${o.method}", path: "${o.path}" }`)
|
|
29
|
+
.join("\n"),
|
|
30
|
+
].join("\n"),
|
|
31
|
+
});
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const operation: AutoBeOpenApi.IOperation = it.second;
|
|
35
|
+
|
|
36
|
+
// Check function argument
|
|
37
|
+
const needArgument: boolean =
|
|
38
|
+
operation.requestBody !== null || operation.parameters.length !== 0;
|
|
39
|
+
if (!!stmt.argument !== needArgument) {
|
|
40
|
+
// required argument, but is not
|
|
41
|
+
// or not required, but is provided
|
|
42
|
+
ctx.errors.push({
|
|
43
|
+
path: `${path}.argument`,
|
|
44
|
+
value: stmt.argument,
|
|
45
|
+
expected:
|
|
46
|
+
needArgument === true
|
|
47
|
+
? typia.reflect.name<AutoBeTest.IObjectLiteralExpression>()
|
|
48
|
+
: "null",
|
|
49
|
+
});
|
|
50
|
+
} else if (!!stmt.argument) {
|
|
51
|
+
// check properties
|
|
52
|
+
const keys: Set<string> = new Set(
|
|
53
|
+
stmt.argument.properties.map((p) => p.name),
|
|
54
|
+
);
|
|
55
|
+
for (const p of operation.parameters) // path parameters
|
|
56
|
+
if (keys.has(p.name) === false)
|
|
57
|
+
ctx.errors.push({
|
|
58
|
+
path: `${path}.argument.${p.name}`,
|
|
59
|
+
value: undefined,
|
|
60
|
+
expected: JSON.stringify(p.schema),
|
|
61
|
+
description: `Parameter "${p.name}" is required, and its type is ${JSON.stringify(p.schema)}. However, you did not provide it.`,
|
|
62
|
+
});
|
|
63
|
+
if (operation.requestBody !== null && keys.has("body") === false) {
|
|
64
|
+
// request body is not provided
|
|
65
|
+
ctx.errors.push({
|
|
66
|
+
path: `${path}.argument.body`,
|
|
67
|
+
value: undefined,
|
|
68
|
+
expected: operation.requestBody.typeName,
|
|
69
|
+
description: `Request body is required, and its type is "${operation.requestBody.typeName}". However, you did not provide it.`,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Check function return type
|
|
75
|
+
if (!!stmt.variableName !== !!operation.responseBody) {
|
|
76
|
+
ctx.errors.push({
|
|
77
|
+
path: `${path}.variableName`,
|
|
78
|
+
value: stmt.variableName,
|
|
79
|
+
...(stmt.variableName === null || stmt.variableName === undefined
|
|
80
|
+
? {
|
|
81
|
+
expected: "string",
|
|
82
|
+
description:
|
|
83
|
+
"You have to provide variable name. Every API operations that returning value require it, and it is used to store the response body.",
|
|
84
|
+
}
|
|
85
|
+
: {
|
|
86
|
+
expected: typia.reflect.name<AutoBeTest.IIdentifier>(),
|
|
87
|
+
description:
|
|
88
|
+
"You don't have to provide variable name. The API operation does not return any value, so it is not needed.",
|
|
89
|
+
}),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AutoBeTest } from "@autobe/interface";
|
|
2
|
+
|
|
3
|
+
import { AutoBeTestExpressionValidator } from "./AutoBeTestExpressionValidator";
|
|
4
|
+
import { IAutoBeTextValidateContext } from "./IAutoBeTextValidateContext";
|
|
5
|
+
|
|
6
|
+
export const validateTestExpression = (
|
|
7
|
+
ctx: IAutoBeTextValidateContext,
|
|
8
|
+
item: AutoBeTest.IExpression,
|
|
9
|
+
path: string,
|
|
10
|
+
): void => AutoBeTestExpressionValidator[item.type](ctx, item as any, path);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { IAutoBeTextValidateContext } from "./IAutoBeTextValidateContext";
|
|
2
|
+
import { validateTestStatement } from "./validateTestStatement";
|
|
3
|
+
|
|
4
|
+
export const validateTestFunction = (ctx: IAutoBeTextValidateContext): void =>
|
|
5
|
+
ctx.function.statements.forEach((stmt, i) =>
|
|
6
|
+
validateTestStatement(ctx, stmt, `$input.function.statements[${i}]`),
|
|
7
|
+
);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { AutoBeTest } from "@autobe/interface";
|
|
2
|
+
|
|
3
|
+
import { AutoBeTestStatementValidator } from "./AutoBeTestStatementValidator";
|
|
4
|
+
import { IAutoBeTextValidateContext } from "./IAutoBeTextValidateContext";
|
|
5
|
+
|
|
6
|
+
export const validateTestStatement = (
|
|
7
|
+
ctx: IAutoBeTextValidateContext,
|
|
8
|
+
item: AutoBeTest.IStatement,
|
|
9
|
+
path: string,
|
|
10
|
+
): void => AutoBeTestStatementValidator[item.type](ctx, item as any, path);
|