@sdkgen/node-runtime 0.0.0-dev.20231008144512 → 0.0.0-dev.20231221180829
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/.eslintignore +1 -0
- package/.eslintrc.json +3 -0
- package/.prettierrc +12 -0
- package/.vscode/settings.json +14 -0
- package/dist/spec/error.spec.d.ts +1 -0
- package/dist/spec/error.spec.js +15 -0
- package/dist/spec/rest/rest.spec.d.ts +1 -0
- package/dist/spec/rest/rest.spec.js +353 -0
- package/dist/spec/runtime/errors.spec.d.ts +1 -0
- package/dist/spec/runtime/errors.spec.js +43 -0
- package/dist/spec/runtime/middleware.spec.d.ts +1 -0
- package/dist/spec/runtime/middleware.spec.js +100 -0
- package/dist/spec/simple/legacyNodeClient.d.ts +17 -0
- package/dist/spec/simple/legacyNodeClient.js +128 -0
- package/dist/spec/simple/simple.spec.d.ts +1 -0
- package/dist/spec/simple/simple.spec.js +113 -0
- package/dist/spec/types.d.ts +1 -0
- package/dist/spec/types.js +60 -0
- package/dist/spec/types.spec.d.ts +1 -0
- package/dist/spec/types.spec.js +128 -0
- package/dist/src/api-config.js +19 -0
- package/dist/src/context.js +2 -0
- package/dist/src/encode-decode.js +376 -0
- package/dist/src/error.js +32 -0
- package/dist/src/execute.js +56 -0
- package/dist/src/http-client.js +105 -0
- package/dist/src/http-server.js +941 -0
- package/dist/src/index.js +18 -0
- package/dist/src/swagger.js +439 -0
- package/dist/src/test-wrapper.js +52 -0
- package/dist/src/utils.js +7 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +26 -42
- package/dist/api-config.js +0 -15
- package/dist/context.js +0 -2
- package/dist/encode-decode.js +0 -338
- package/dist/error.js +0 -27
- package/dist/execute.js +0 -53
- package/dist/http-client.js +0 -103
- package/dist/http-server.js +0 -930
- package/dist/index.js +0 -7
- package/dist/swagger.js +0 -448
- package/dist/test-wrapper.js +0 -53
- package/dist/utils.js +0 -4
- /package/dist/{api-config.d.ts → src/api-config.d.ts} +0 -0
- /package/dist/{context.d.ts → src/context.d.ts} +0 -0
- /package/dist/{encode-decode.d.ts → src/encode-decode.d.ts} +0 -0
- /package/dist/{error.d.ts → src/error.d.ts} +0 -0
- /package/dist/{execute.d.ts → src/execute.d.ts} +0 -0
- /package/dist/{http-client.d.ts → src/http-client.d.ts} +0 -0
- /package/dist/{http-server.d.ts → src/http-server.d.ts} +0 -0
- /package/dist/{index.d.ts → src/index.d.ts} +0 -0
- /package/dist/{swagger.d.ts → src/swagger.d.ts} +0 -0
- /package/dist/{test-wrapper.d.ts → src/test-wrapper.d.ts} +0 -0
- /package/dist/{utils.d.ts → src/utils.d.ts} +0 -0
package/.eslintignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
spec/simple/legacyNodeClient.ts
|
package/.eslintrc.json
ADDED
package/.prettierrc
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,353 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface User {
|
|
2
|
+
name: string;
|
|
3
|
+
age: number;
|
|
4
|
+
}
|
|
5
|
+
export type ErrorType = "Fatal" | "Connection";
|
|
6
|
+
export declare class ApiClient {
|
|
7
|
+
private baseUrl;
|
|
8
|
+
private useStaging;
|
|
9
|
+
deviceId: string | null;
|
|
10
|
+
constructor(baseUrl?: string, useStaging?: boolean);
|
|
11
|
+
getUser(id: string): Promise<User>;
|
|
12
|
+
ping(): Promise<string>;
|
|
13
|
+
setPushToken(token: string): Promise<void>;
|
|
14
|
+
private device;
|
|
15
|
+
private randomBytesHex;
|
|
16
|
+
private makeRequest;
|
|
17
|
+
}
|