@sdkgen/node-runtime 2.3.2 → 2.5.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/dist/api-config.js +19 -0
- package/dist/{src/context.d.ts → context.d.ts} +1 -0
- package/dist/context.js +3 -0
- package/dist/encode-decode.js +344 -0
- package/dist/error.js +33 -0
- package/dist/execute.js +57 -0
- package/dist/http-client.js +107 -0
- package/dist/{src/http-server.d.ts → http-server.d.ts} +1 -0
- package/dist/http-server.js +941 -0
- package/dist/{src/index.js → index.js} +1 -0
- package/dist/swagger.js +454 -0
- package/dist/test-wrapper.js +58 -0
- package/dist/utils.js +8 -0
- package/package.json +50 -34
- package/.eslintignore +0 -1
- package/.eslintrc.json +0 -3
- package/.prettierrc +0 -12
- package/.vscode/settings.json +0 -14
- package/dist/spec/error.spec.d.ts +0 -1
- package/dist/spec/error.spec.js +0 -15
- package/dist/spec/rest/rest.spec.d.ts +0 -1
- package/dist/spec/rest/rest.spec.js +0 -353
- package/dist/spec/runtime/errors.spec.d.ts +0 -1
- package/dist/spec/runtime/errors.spec.js +0 -43
- package/dist/spec/runtime/middleware.spec.d.ts +0 -1
- package/dist/spec/runtime/middleware.spec.js +0 -100
- package/dist/spec/simple/legacyNodeClient.d.ts +0 -17
- package/dist/spec/simple/legacyNodeClient.js +0 -128
- package/dist/spec/simple/simple.spec.d.ts +0 -1
- package/dist/spec/simple/simple.spec.js +0 -113
- package/dist/spec/types.d.ts +0 -1
- package/dist/spec/types.js +0 -60
- package/dist/spec/types.spec.d.ts +0 -1
- package/dist/spec/types.spec.js +0 -128
- package/dist/src/api-config.js +0 -19
- package/dist/src/context.js +0 -2
- package/dist/src/encode-decode.js +0 -376
- package/dist/src/error.js +0 -32
- package/dist/src/execute.js +0 -56
- package/dist/src/http-client.js +0 -105
- package/dist/src/http-server.js +0 -941
- package/dist/src/swagger.js +0 -439
- package/dist/src/test-wrapper.js +0 -52
- package/dist/src/utils.js +0 -7
- package/dist/tsconfig.tsbuildinfo +0 -1
- /package/dist/{src/api-config.d.ts → api-config.d.ts} +0 -0
- /package/dist/{src/encode-decode.d.ts → encode-decode.d.ts} +0 -0
- /package/dist/{src/error.d.ts → error.d.ts} +0 -0
- /package/dist/{src/execute.d.ts → execute.d.ts} +0 -0
- /package/dist/{src/http-client.d.ts → http-client.d.ts} +0 -0
- /package/dist/{src/index.d.ts → index.d.ts} +0 -0
- /package/dist/{src/swagger.d.ts → swagger.d.ts} +0 -0
- /package/dist/{src/test-wrapper.d.ts → test-wrapper.d.ts} +0 -0
- /package/dist/{src/utils.d.ts → utils.d.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sdkgen/node-runtime",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.0",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "dist/
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"directories": {
|
|
8
|
+
"lib": "dist"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
6
13
|
"scripts": {
|
|
7
14
|
"test": "TZ=UTC jest --passWithNoTests",
|
|
8
15
|
"eslint:fix": "eslint --fix '{src,spec}/**/*.ts'",
|
|
@@ -19,28 +26,55 @@
|
|
|
19
26
|
"url": "https://github.com/sdkgen/sdkgen/issues"
|
|
20
27
|
},
|
|
21
28
|
"publishConfig": {
|
|
22
|
-
"access": "public"
|
|
29
|
+
"access": "public",
|
|
30
|
+
"provenance": true
|
|
23
31
|
},
|
|
24
32
|
"homepage": "https://github.com/sdkgen/sdkgen#readme",
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@fnando/cnpj": "^1.0.2",
|
|
35
|
+
"@fnando/cpf": "^1.0.2",
|
|
36
|
+
"@sdkgen/csharp-generator": "^2.5.0",
|
|
37
|
+
"@sdkgen/dart-generator": "^2.5.0",
|
|
38
|
+
"@sdkgen/fsharp-generator": "^2.5.0",
|
|
39
|
+
"@sdkgen/kotlin-generator": "^2.5.0",
|
|
40
|
+
"@sdkgen/parser": "^2.5.0",
|
|
41
|
+
"@sdkgen/playground": "^2.5.0",
|
|
42
|
+
"@sdkgen/swift-generator": "^2.5.0",
|
|
43
|
+
"@sdkgen/typescript-generator": "^2.5.0",
|
|
44
|
+
"busboy": "1.5.0",
|
|
45
|
+
"decimal.js": "^10.4.3",
|
|
46
|
+
"file-type": "^16.5.4",
|
|
47
|
+
"request-ip": "^3.3.0",
|
|
48
|
+
"serve-handler": "^6.1.5",
|
|
49
|
+
"swagger-ui-dist": "^5.11.0"
|
|
50
|
+
},
|
|
25
51
|
"devDependencies": {
|
|
26
52
|
"@cubos/eslint-config": "^2.2.0",
|
|
27
|
-
"@types/busboy": "^1.
|
|
28
|
-
"@types/jest": "^29.
|
|
29
|
-
"@types/node": "^
|
|
30
|
-
"@types/request-ip": "0.0.
|
|
31
|
-
"@types/serve-handler": "^6.1.
|
|
32
|
-
"@types/swagger-ui-dist": "^3.30.
|
|
33
|
-
"axios": "^
|
|
53
|
+
"@types/busboy": "^1.5.3",
|
|
54
|
+
"@types/jest": "^29.5.11",
|
|
55
|
+
"@types/node": "^20.10.8",
|
|
56
|
+
"@types/request-ip": "0.0.41",
|
|
57
|
+
"@types/serve-handler": "^6.1.4",
|
|
58
|
+
"@types/swagger-ui-dist": "^3.30.4",
|
|
59
|
+
"axios": "^1.6.5",
|
|
34
60
|
"form-data": "^4.0.0",
|
|
35
|
-
"jest": "^29.
|
|
36
|
-
"json-schema-typed": "^8.0.
|
|
61
|
+
"jest": "^29.7.0",
|
|
62
|
+
"json-schema-typed": "^8.0.1",
|
|
37
63
|
"spec.ts": "^1.1.3",
|
|
38
|
-
"ts-jest": "^29.
|
|
39
|
-
"type-fest": "^
|
|
40
|
-
"typescript": "
|
|
64
|
+
"ts-jest": "^29.1.1",
|
|
65
|
+
"type-fest": "^4.9.0",
|
|
66
|
+
"typescript": "~5.2.2"
|
|
41
67
|
},
|
|
42
68
|
"jest": {
|
|
43
69
|
"transform": {
|
|
70
|
+
"^.+\\.js$": [
|
|
71
|
+
"babel-jest",
|
|
72
|
+
{
|
|
73
|
+
"plugins": [
|
|
74
|
+
"@babel/plugin-transform-modules-commonjs"
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
],
|
|
44
78
|
"^.+\\.tsx?$": "ts-jest"
|
|
45
79
|
},
|
|
46
80
|
"transformIgnorePatterns": [
|
|
@@ -58,23 +92,5 @@
|
|
|
58
92
|
"/spec/"
|
|
59
93
|
]
|
|
60
94
|
},
|
|
61
|
-
"
|
|
62
|
-
"@fnando/cnpj": "^1.0.2",
|
|
63
|
-
"@fnando/cpf": "^1.0.2",
|
|
64
|
-
"@sdkgen/csharp-generator": "2.3.2",
|
|
65
|
-
"@sdkgen/dart-generator": "2.3.2",
|
|
66
|
-
"@sdkgen/fsharp-generator": "2.3.2",
|
|
67
|
-
"@sdkgen/kotlin-generator": "2.3.2",
|
|
68
|
-
"@sdkgen/parser": "2.3.2",
|
|
69
|
-
"@sdkgen/playground": "2.3.2",
|
|
70
|
-
"@sdkgen/swift-generator": "2.3.2",
|
|
71
|
-
"@sdkgen/typescript-generator": "2.3.2",
|
|
72
|
-
"busboy": "1.5.0",
|
|
73
|
-
"decimal.js": "^10.3.1",
|
|
74
|
-
"file-type": "^16.5.3",
|
|
75
|
-
"request-ip": "^3.0.0",
|
|
76
|
-
"serve-handler": "^6.1.3",
|
|
77
|
-
"swagger-ui-dist": "^4.0.0"
|
|
78
|
-
},
|
|
79
|
-
"types": "dist/src/index.d.ts"
|
|
95
|
+
"gitHead": "d95908e3e502b4367f0f9c17b122604c7a6f02e8"
|
|
80
96
|
}
|
package/.eslintignore
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
spec/simple/legacyNodeClient.ts
|
package/.eslintrc.json
DELETED
package/.prettierrc
DELETED
package/.vscode/settings.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"editor.formatOnSave": true,
|
|
3
|
-
"editor.tabSize": 4,
|
|
4
|
-
"files.insertFinalNewline": true,
|
|
5
|
-
"editor.codeActionsOnSave": {
|
|
6
|
-
"source.organizeImports": true
|
|
7
|
-
},
|
|
8
|
-
"eslint.validate": [
|
|
9
|
-
"javascript",
|
|
10
|
-
"javascriptreact",
|
|
11
|
-
"typescript",
|
|
12
|
-
"typescriptreact"
|
|
13
|
-
]
|
|
14
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/spec/error.spec.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
5
|
-
const src_1 = require("../src");
|
|
6
|
-
class TestError extends src_1.SdkgenError {
|
|
7
|
-
}
|
|
8
|
-
describe("Error", () => {
|
|
9
|
-
test("SdkgenError behaves as expected", () => {
|
|
10
|
-
const err1 = new TestError("the error message");
|
|
11
|
-
expect(err1.message).toEqual("the error message");
|
|
12
|
-
expect(err1.type).toEqual("TestError");
|
|
13
|
-
expect(JSON.parse(JSON.stringify(err1))).toEqual({ message: "the error message", type: "TestError" });
|
|
14
|
-
});
|
|
15
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,353 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
/* eslint-disable @typescript-eslint/require-await */
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
8
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
9
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
10
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
11
|
-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
12
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
13
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
14
|
-
const fs_1 = require("fs");
|
|
15
|
-
const parser_1 = require("@sdkgen/parser");
|
|
16
|
-
const typescript_generator_1 = require("@sdkgen/typescript-generator");
|
|
17
|
-
const axios_1 = __importDefault(require("axios"));
|
|
18
|
-
const form_data_1 = __importDefault(require("form-data"));
|
|
19
|
-
const src_1 = require("../../src");
|
|
20
|
-
const ast = new parser_1.Parser(`${__dirname}/api.sdkgen`).parse();
|
|
21
|
-
(0, fs_1.writeFileSync)(`${__dirname}/api.ts`, (0, typescript_generator_1.generateNodeServerSource)(ast).replace(/@sdkgen\/node-runtime/gu, "../../src"));
|
|
22
|
-
const { api, TestError } = require(`${__dirname}/api.ts`);
|
|
23
|
-
(0, fs_1.unlinkSync)(`${__dirname}/api.ts`);
|
|
24
|
-
api.fn.add = async (_ctx, { first, second }) => {
|
|
25
|
-
return `${first}${second}`;
|
|
26
|
-
};
|
|
27
|
-
api.fn.maybe = async (_ctx, { bin }) => {
|
|
28
|
-
return bin === null ? null : Buffer.from(bin, "hex");
|
|
29
|
-
};
|
|
30
|
-
api.fn.hex = async (_ctx, { bin }) => {
|
|
31
|
-
return bin.toString("hex");
|
|
32
|
-
};
|
|
33
|
-
api.fn.obj = async (_ctx, { obj }) => {
|
|
34
|
-
if (obj.val === 0) {
|
|
35
|
-
throw new Error("Value is zero ~ Fatal");
|
|
36
|
-
}
|
|
37
|
-
if (obj.val === -100) {
|
|
38
|
-
throw new TestError("Value is -100 ~ TestError");
|
|
39
|
-
}
|
|
40
|
-
return obj;
|
|
41
|
-
};
|
|
42
|
-
api.fn.returnArg = async (_ctx, { arg }) => {
|
|
43
|
-
return arg;
|
|
44
|
-
};
|
|
45
|
-
api.fn.returnNoArg = async (_ctx) => {
|
|
46
|
-
return "no-arg";
|
|
47
|
-
};
|
|
48
|
-
api.fn.returnArgConcat = async (_ctx, { arg, arg2 }) => {
|
|
49
|
-
return `${arg}${arg2}`;
|
|
50
|
-
};
|
|
51
|
-
async function readAllStream(stream) {
|
|
52
|
-
return new Promise((resolve, reject) => {
|
|
53
|
-
const chunks = [];
|
|
54
|
-
stream.on("error", err => reject(err));
|
|
55
|
-
stream.on("data", data => chunks.push(Buffer.from(data)));
|
|
56
|
-
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
api.fn.uploadFile = async (ctx) => {
|
|
60
|
-
return Promise.all(ctx.request.files.map(async ({ name, contents }) => ({
|
|
61
|
-
data: await readAllStream(contents),
|
|
62
|
-
name,
|
|
63
|
-
})));
|
|
64
|
-
};
|
|
65
|
-
api.fn.getHtml = async () => {
|
|
66
|
-
return "<h1>Hello world!</h1>";
|
|
67
|
-
};
|
|
68
|
-
api.fn.getXml = async () => {
|
|
69
|
-
return "<h1>Hello world!</h1>";
|
|
70
|
-
};
|
|
71
|
-
(0, fs_1.writeFileSync)(`${__dirname}/nodeClient.ts`, (0, typescript_generator_1.generateNodeClientSource)(ast).replace(/@sdkgen\/node-runtime/gu, "../../src"));
|
|
72
|
-
const { ApiClient: NodeApiClient } = require(`${__dirname}/nodeClient.ts`);
|
|
73
|
-
(0, fs_1.unlinkSync)(`${__dirname}/nodeClient.ts`);
|
|
74
|
-
const nodeClient = new NodeApiClient("http://localhost:8001");
|
|
75
|
-
const server = new src_1.SdkgenHttpServer(api, {});
|
|
76
|
-
describe("Rest API", () => {
|
|
77
|
-
beforeAll(async () => {
|
|
78
|
-
await server.listen(8001);
|
|
79
|
-
});
|
|
80
|
-
afterAll(async () => {
|
|
81
|
-
await server.close();
|
|
82
|
-
});
|
|
83
|
-
test("add with sdkgen", async () => {
|
|
84
|
-
expect(await nodeClient.add(null, { first: 1, second: "aa" })).toEqual("1aa");
|
|
85
|
-
});
|
|
86
|
-
const table = [
|
|
87
|
-
{ method: "GET", path: "/add1/1/aa", result: "1aa" },
|
|
88
|
-
{ method: "GET", path: "/add1/1/aa/", result: "1aa" },
|
|
89
|
-
{
|
|
90
|
-
headers: {
|
|
91
|
-
accept: "application/json",
|
|
92
|
-
},
|
|
93
|
-
method: "GET",
|
|
94
|
-
path: "/add1/1/aa",
|
|
95
|
-
result: '"1aa"',
|
|
96
|
-
},
|
|
97
|
-
{ method: "GET", path: "/add2&second=aa&first=1", result: "", statusCode: 404 },
|
|
98
|
-
{ method: "GET", path: "/add2?second=aa&first=1", result: "1aa" },
|
|
99
|
-
{ method: "GET", path: "/add2?first=1&second=aa", result: "1aa" },
|
|
100
|
-
{
|
|
101
|
-
headers: {
|
|
102
|
-
"x-second": "aa",
|
|
103
|
-
},
|
|
104
|
-
method: "GET",
|
|
105
|
-
path: "/add3?first=1",
|
|
106
|
-
result: "1aa",
|
|
107
|
-
},
|
|
108
|
-
{
|
|
109
|
-
headers: {
|
|
110
|
-
accept: "application/json",
|
|
111
|
-
"x-second": "aa",
|
|
112
|
-
},
|
|
113
|
-
method: "GET",
|
|
114
|
-
path: "/add3?first=1",
|
|
115
|
-
result: '"1aa"',
|
|
116
|
-
},
|
|
117
|
-
{
|
|
118
|
-
data: "1",
|
|
119
|
-
headers: {
|
|
120
|
-
"x-second": "aa",
|
|
121
|
-
},
|
|
122
|
-
method: "POST",
|
|
123
|
-
path: "/add4",
|
|
124
|
-
result: "1aa",
|
|
125
|
-
},
|
|
126
|
-
{
|
|
127
|
-
data: "1",
|
|
128
|
-
headers: {
|
|
129
|
-
accept: "application/json",
|
|
130
|
-
"x-second": "aa",
|
|
131
|
-
},
|
|
132
|
-
method: "POST",
|
|
133
|
-
path: "/add4",
|
|
134
|
-
result: '"1aa"',
|
|
135
|
-
},
|
|
136
|
-
{
|
|
137
|
-
data: "aa",
|
|
138
|
-
headers: {
|
|
139
|
-
"x-first": "1",
|
|
140
|
-
},
|
|
141
|
-
method: "POST",
|
|
142
|
-
path: "/add5",
|
|
143
|
-
result: "1aa",
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
data: '"aa"',
|
|
147
|
-
headers: {
|
|
148
|
-
"content-type": "application/json",
|
|
149
|
-
"x-first": "1",
|
|
150
|
-
},
|
|
151
|
-
method: "POST",
|
|
152
|
-
path: "/add5",
|
|
153
|
-
result: "1aa",
|
|
154
|
-
},
|
|
155
|
-
{
|
|
156
|
-
data: '"aa"',
|
|
157
|
-
headers: {
|
|
158
|
-
accept: "application/json",
|
|
159
|
-
"content-type": "application/json",
|
|
160
|
-
"x-first": "1",
|
|
161
|
-
},
|
|
162
|
-
method: "POST",
|
|
163
|
-
path: "/add5",
|
|
164
|
-
result: '"1aa"',
|
|
165
|
-
},
|
|
166
|
-
{ method: "POST", path: "/add6?second=aa&first=1", result: "1aa" },
|
|
167
|
-
{ method: "POST", path: "/add6?first=1&second=aa", result: "1aa" },
|
|
168
|
-
{
|
|
169
|
-
data: "second=aa&first=1",
|
|
170
|
-
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
171
|
-
method: "POST",
|
|
172
|
-
path: "/add6",
|
|
173
|
-
result: "1aa",
|
|
174
|
-
},
|
|
175
|
-
{
|
|
176
|
-
data: "first=1&second=aa",
|
|
177
|
-
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
178
|
-
method: "POST",
|
|
179
|
-
path: "/add6",
|
|
180
|
-
result: "1aa",
|
|
181
|
-
},
|
|
182
|
-
{
|
|
183
|
-
method: "GET",
|
|
184
|
-
path: "/maybe",
|
|
185
|
-
result: "",
|
|
186
|
-
statusCode: 404,
|
|
187
|
-
},
|
|
188
|
-
{
|
|
189
|
-
method: "GET",
|
|
190
|
-
path: "/maybe?bin=4d546864",
|
|
191
|
-
result: "MThd",
|
|
192
|
-
resultHeaders: {
|
|
193
|
-
"content-type": "audio/midi",
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
{
|
|
197
|
-
method: "GET",
|
|
198
|
-
path: "/maybe?bin=61",
|
|
199
|
-
result: "a",
|
|
200
|
-
resultHeaders: {
|
|
201
|
-
"content-type": "application/octet-stream",
|
|
202
|
-
},
|
|
203
|
-
},
|
|
204
|
-
{
|
|
205
|
-
headers: {
|
|
206
|
-
accept: "application/json",
|
|
207
|
-
},
|
|
208
|
-
method: "GET",
|
|
209
|
-
path: "/maybe?bin=61",
|
|
210
|
-
result: '"YQ=="',
|
|
211
|
-
},
|
|
212
|
-
{
|
|
213
|
-
method: "POST",
|
|
214
|
-
path: "/maybe",
|
|
215
|
-
result: "",
|
|
216
|
-
statusCode: 204,
|
|
217
|
-
},
|
|
218
|
-
{
|
|
219
|
-
data: "61",
|
|
220
|
-
method: "POST",
|
|
221
|
-
path: "/maybe",
|
|
222
|
-
result: "a",
|
|
223
|
-
resultHeaders: {
|
|
224
|
-
"content-type": "application/octet-stream",
|
|
225
|
-
},
|
|
226
|
-
},
|
|
227
|
-
{
|
|
228
|
-
data: "a",
|
|
229
|
-
method: "POST",
|
|
230
|
-
path: "/hex",
|
|
231
|
-
result: "61",
|
|
232
|
-
},
|
|
233
|
-
{
|
|
234
|
-
data: `{"val":15}`,
|
|
235
|
-
method: "POST",
|
|
236
|
-
path: "/obj",
|
|
237
|
-
result: `{"val":15}`,
|
|
238
|
-
resultHeaders: {
|
|
239
|
-
"content-type": "application/json",
|
|
240
|
-
},
|
|
241
|
-
},
|
|
242
|
-
{
|
|
243
|
-
data: `{"val":0}`,
|
|
244
|
-
method: "POST",
|
|
245
|
-
path: "/obj",
|
|
246
|
-
result: `{"message":"Value is zero ~ Fatal","type":"Fatal"}`,
|
|
247
|
-
resultHeaders: {
|
|
248
|
-
"content-type": "application/json",
|
|
249
|
-
},
|
|
250
|
-
statusCode: 500,
|
|
251
|
-
},
|
|
252
|
-
{
|
|
253
|
-
data: `{"val":-100}`,
|
|
254
|
-
method: "POST",
|
|
255
|
-
path: "/obj",
|
|
256
|
-
result: `{"message":"Value is -100 ~ TestError","type":"TestError"}`,
|
|
257
|
-
resultHeaders: {
|
|
258
|
-
"content-type": "application/json",
|
|
259
|
-
},
|
|
260
|
-
statusCode: 400,
|
|
261
|
-
},
|
|
262
|
-
{
|
|
263
|
-
method: "POST",
|
|
264
|
-
path: "/upload",
|
|
265
|
-
result: `[]`,
|
|
266
|
-
resultHeaders: {
|
|
267
|
-
"content-type": "application/json",
|
|
268
|
-
},
|
|
269
|
-
statusCode: 200,
|
|
270
|
-
},
|
|
271
|
-
{
|
|
272
|
-
method: "GET",
|
|
273
|
-
path: "/html",
|
|
274
|
-
result: "<h1>Hello world!</h1>",
|
|
275
|
-
resultHeaders: {
|
|
276
|
-
"content-type": "text/html",
|
|
277
|
-
},
|
|
278
|
-
},
|
|
279
|
-
{
|
|
280
|
-
method: "GET",
|
|
281
|
-
path: "/xml",
|
|
282
|
-
result: "<h1>Hello world!</h1>",
|
|
283
|
-
resultHeaders: {
|
|
284
|
-
"content-type": "text/xml",
|
|
285
|
-
},
|
|
286
|
-
},
|
|
287
|
-
{
|
|
288
|
-
method: "GET",
|
|
289
|
-
path: "/foo/haha/hello",
|
|
290
|
-
result: "haha",
|
|
291
|
-
},
|
|
292
|
-
{
|
|
293
|
-
method: "GET",
|
|
294
|
-
path: "/foo/barhaha/hello",
|
|
295
|
-
result: "haha",
|
|
296
|
-
},
|
|
297
|
-
{
|
|
298
|
-
method: "GET",
|
|
299
|
-
path: "/foo/bar/hello",
|
|
300
|
-
result: "bar",
|
|
301
|
-
},
|
|
302
|
-
{
|
|
303
|
-
method: "GET",
|
|
304
|
-
path: "/foo/baz/hello",
|
|
305
|
-
result: "no-arg",
|
|
306
|
-
},
|
|
307
|
-
{
|
|
308
|
-
method: "GET",
|
|
309
|
-
path: "/foo/haha/hello/hehe/world",
|
|
310
|
-
result: "hahahehe",
|
|
311
|
-
},
|
|
312
|
-
{
|
|
313
|
-
method: "GET",
|
|
314
|
-
path: "/foo/barhaha/hello/barhehe/world",
|
|
315
|
-
result: "hahahehe",
|
|
316
|
-
},
|
|
317
|
-
{
|
|
318
|
-
method: "GET",
|
|
319
|
-
path: "/foo/bar/hello/bar/world",
|
|
320
|
-
result: "barbar",
|
|
321
|
-
},
|
|
322
|
-
(() => {
|
|
323
|
-
const form = new form_data_1.default();
|
|
324
|
-
form.append("file", Buffer.from("Hello"), "test.txt");
|
|
325
|
-
return {
|
|
326
|
-
data: form.getBuffer(),
|
|
327
|
-
headers: Object.assign({}, form.getHeaders()),
|
|
328
|
-
method: "POST",
|
|
329
|
-
path: "/upload",
|
|
330
|
-
result: `[{"name":"test.txt","data":"SGVsbG8="}]`,
|
|
331
|
-
resultHeaders: {
|
|
332
|
-
"content-type": "application/json",
|
|
333
|
-
},
|
|
334
|
-
statusCode: 200,
|
|
335
|
-
};
|
|
336
|
-
})(),
|
|
337
|
-
];
|
|
338
|
-
for (const { method, path, result, headers, data, statusCode, resultHeaders } of table) {
|
|
339
|
-
test(`${method} ${path}${headers ? ` with headers ${JSON.stringify(headers)}` : ""}`, async () => {
|
|
340
|
-
const response = await axios_1.default.request({
|
|
341
|
-
data,
|
|
342
|
-
headers,
|
|
343
|
-
method,
|
|
344
|
-
transformResponse: [x => x],
|
|
345
|
-
url: `http://localhost:8001${path}`,
|
|
346
|
-
validateStatus: () => true,
|
|
347
|
-
});
|
|
348
|
-
expect(response.data).toEqual(result);
|
|
349
|
-
expect(response.status).toEqual(statusCode !== null && statusCode !== void 0 ? statusCode : 200);
|
|
350
|
-
expect(response.headers).toMatchObject(resultHeaders !== null && resultHeaders !== void 0 ? resultHeaders : {});
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/* eslint-disable @typescript-eslint/require-await */
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
5
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
6
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
8
|
-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
9
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
10
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
11
|
-
const fs_1 = require("fs");
|
|
12
|
-
const parser_1 = require("@sdkgen/parser");
|
|
13
|
-
const typescript_generator_1 = require("@sdkgen/typescript-generator");
|
|
14
|
-
const src_1 = require("../../src");
|
|
15
|
-
const ast = new parser_1.Parser(`${__dirname}/api.sdkgen`).parse();
|
|
16
|
-
(0, fs_1.writeFileSync)(`${__dirname}/errors-api.ts`, (0, typescript_generator_1.generateNodeServerSource)(ast).replace(/@sdkgen\/node-runtime/gu, "../../src"));
|
|
17
|
-
const { api, CustomError } = require(`${__dirname}/errors-api.ts`);
|
|
18
|
-
(0, fs_1.unlinkSync)(`${__dirname}/errors-api.ts`);
|
|
19
|
-
api.fn.throwCustomError = async (_ctx, args) => {
|
|
20
|
-
throw new CustomError("Some message", args);
|
|
21
|
-
};
|
|
22
|
-
(0, fs_1.writeFileSync)(`${__dirname}/errors-nodeClient.ts`, (0, typescript_generator_1.generateNodeClientSource)(ast).replace(/@sdkgen\/node-runtime/gu, "../../src"));
|
|
23
|
-
const { ApiClient: NodeApiClient } = require(`${__dirname}/errors-nodeClient.ts`);
|
|
24
|
-
(0, fs_1.unlinkSync)(`${__dirname}/errors-nodeClient.ts`);
|
|
25
|
-
const nodeClient = new NodeApiClient("http://localhost:35437");
|
|
26
|
-
const server = new src_1.SdkgenHttpServer(api, {});
|
|
27
|
-
describe("Errors", () => {
|
|
28
|
-
beforeAll(async () => {
|
|
29
|
-
await server.listen(35437);
|
|
30
|
-
});
|
|
31
|
-
afterAll(async () => {
|
|
32
|
-
await server.close();
|
|
33
|
-
});
|
|
34
|
-
test("Errors are passed correctly", async () => {
|
|
35
|
-
await expect(nodeClient.throwCustomError(null, { value: 235 })).rejects.toMatchObject({
|
|
36
|
-
data: {
|
|
37
|
-
value: 235,
|
|
38
|
-
},
|
|
39
|
-
message: "Some message",
|
|
40
|
-
type: "CustomError",
|
|
41
|
-
});
|
|
42
|
-
});
|
|
43
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
/* eslint-disable @typescript-eslint/require-await */
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
5
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
6
|
-
/* eslint-disable @typescript-eslint/no-require-imports */
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
8
|
-
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
9
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
10
|
-
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
11
|
-
const fs_1 = require("fs");
|
|
12
|
-
const parser_1 = require("@sdkgen/parser");
|
|
13
|
-
const typescript_generator_1 = require("@sdkgen/typescript-generator");
|
|
14
|
-
const src_1 = require("../../src");
|
|
15
|
-
const ast = new parser_1.Parser(`${__dirname}/api.sdkgen`).parse();
|
|
16
|
-
(0, fs_1.writeFileSync)(`${__dirname}/middleware-api.ts`, (0, typescript_generator_1.generateNodeServerSource)(ast).replace(/@sdkgen\/node-runtime/gu, "../../src"));
|
|
17
|
-
const { api } = require(`${__dirname}/middleware-api.ts`);
|
|
18
|
-
(0, fs_1.unlinkSync)(`${__dirname}/middleware-api.ts`);
|
|
19
|
-
api.fn.identity = async (_ctx, args) => {
|
|
20
|
-
return args.value;
|
|
21
|
-
};
|
|
22
|
-
api.fn.sum = async (_ctx, args) => {
|
|
23
|
-
return args.a + args.b;
|
|
24
|
-
};
|
|
25
|
-
(0, fs_1.writeFileSync)(`${__dirname}/middleware-nodeClient.ts`, (0, typescript_generator_1.generateNodeClientSource)(ast).replace(/@sdkgen\/node-runtime/gu, "../../src"));
|
|
26
|
-
const { ApiClient: NodeApiClient } = require(`${__dirname}/middleware-nodeClient.ts`);
|
|
27
|
-
(0, fs_1.unlinkSync)(`${__dirname}/middleware-nodeClient.ts`);
|
|
28
|
-
const nodeClient = new NodeApiClient("http://localhost:32542");
|
|
29
|
-
const server = new src_1.SdkgenHttpServer(api, {});
|
|
30
|
-
describe("Middleware", () => {
|
|
31
|
-
beforeAll(async () => {
|
|
32
|
-
await server.listen(32542);
|
|
33
|
-
});
|
|
34
|
-
afterAll(async () => {
|
|
35
|
-
await server.close();
|
|
36
|
-
});
|
|
37
|
-
test("A single middeware can intercept calls", async () => {
|
|
38
|
-
expect(await nodeClient.identity(null, { value: 1 })).toBe(1);
|
|
39
|
-
expect(await nodeClient.identity(null, { value: 2 })).toBe(2);
|
|
40
|
-
expect(await nodeClient.identity(null, { value: 3 })).toBe(3);
|
|
41
|
-
api.use(async (ctx, next) => {
|
|
42
|
-
if (ctx.request.args.value === 2) {
|
|
43
|
-
return {
|
|
44
|
-
result: 17,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
return next();
|
|
48
|
-
});
|
|
49
|
-
expect(await nodeClient.identity(null, { value: 1 })).toBe(1);
|
|
50
|
-
expect(await nodeClient.identity(null, { value: 2 })).toBe(17);
|
|
51
|
-
expect(await nodeClient.identity(null, { value: 3 })).toBe(3);
|
|
52
|
-
api.middlewares.pop();
|
|
53
|
-
});
|
|
54
|
-
test("Multiple middlewares stack", async () => {
|
|
55
|
-
expect(await nodeClient.identity(null, { value: 1 })).toBe(1);
|
|
56
|
-
expect(await nodeClient.identity(null, { value: 2 })).toBe(2);
|
|
57
|
-
expect(await nodeClient.identity(null, { value: 3 })).toBe(3);
|
|
58
|
-
api.use(async (ctx, next) => {
|
|
59
|
-
if (ctx.request.args.value === 2) {
|
|
60
|
-
return {
|
|
61
|
-
result: 17,
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
return next();
|
|
65
|
-
});
|
|
66
|
-
api.use(async (ctx, next) => {
|
|
67
|
-
if (ctx.request.args.value < 3) {
|
|
68
|
-
return {
|
|
69
|
-
result: 10,
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
return next();
|
|
73
|
-
});
|
|
74
|
-
expect(await nodeClient.identity(null, { value: 1 })).toBe(10);
|
|
75
|
-
expect(await nodeClient.identity(null, { value: 2 })).toBe(17);
|
|
76
|
-
expect(await nodeClient.identity(null, { value: 3 })).toBe(3);
|
|
77
|
-
api.middlewares.pop();
|
|
78
|
-
api.middlewares.pop();
|
|
79
|
-
});
|
|
80
|
-
test("A middeware can redirect calls", async () => {
|
|
81
|
-
expect(await nodeClient.identity(null, { value: 1 })).toBe(1);
|
|
82
|
-
expect(await nodeClient.identity(null, { value: 2 })).toBe(2);
|
|
83
|
-
expect(await nodeClient.identity(null, { value: 3 })).toBe(3);
|
|
84
|
-
api.use(async (ctx, next) => {
|
|
85
|
-
if (ctx.request.name === "identity") {
|
|
86
|
-
ctx.request.name = "sum";
|
|
87
|
-
const { value } = ctx.request.args;
|
|
88
|
-
ctx.request.args = {
|
|
89
|
-
a: value,
|
|
90
|
-
b: value,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
return next();
|
|
94
|
-
});
|
|
95
|
-
expect(await nodeClient.identity(null, { value: 1 })).toBe(2);
|
|
96
|
-
expect(await nodeClient.identity(null, { value: 2 })).toBe(4);
|
|
97
|
-
expect(await nodeClient.identity(null, { value: 3 })).toBe(6);
|
|
98
|
-
api.middlewares.pop();
|
|
99
|
-
});
|
|
100
|
-
});
|