@sdkgen/node-runtime 0.0.0-dev.20231002144112 → 0.0.0-dev.20231002175619

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.
Files changed (66) hide show
  1. package/dist/api-config.js +15 -0
  2. package/dist/context.js +2 -0
  3. package/dist/encode-decode.js +338 -0
  4. package/dist/error.js +27 -0
  5. package/dist/execute.js +53 -0
  6. package/dist/http-client.js +103 -0
  7. package/dist/http-server.js +930 -0
  8. package/dist/index.js +7 -0
  9. package/dist/swagger.js +448 -0
  10. package/dist/test-wrapper.js +53 -0
  11. package/dist/utils.js +4 -0
  12. package/package.json +18 -11
  13. package/.eslintignore +0 -1
  14. package/.eslintrc.json +0 -3
  15. package/.prettierrc +0 -12
  16. package/.vscode/settings.json +0 -14
  17. package/dist/spec/error.spec.d.ts +0 -1
  18. package/dist/spec/error.spec.js +0 -15
  19. package/dist/spec/rest/rest.spec.d.ts +0 -1
  20. package/dist/spec/rest/rest.spec.js +0 -353
  21. package/dist/spec/runtime/errors.spec.d.ts +0 -1
  22. package/dist/spec/runtime/errors.spec.js +0 -43
  23. package/dist/spec/runtime/middleware.spec.d.ts +0 -1
  24. package/dist/spec/runtime/middleware.spec.js +0 -100
  25. package/dist/spec/simple/legacyNodeClient.d.ts +0 -17
  26. package/dist/spec/simple/legacyNodeClient.js +0 -128
  27. package/dist/spec/simple/simple.spec.d.ts +0 -1
  28. package/dist/spec/simple/simple.spec.js +0 -113
  29. package/dist/spec/types.d.ts +0 -1
  30. package/dist/spec/types.js +0 -60
  31. package/dist/spec/types.spec.d.ts +0 -1
  32. package/dist/spec/types.spec.js +0 -128
  33. package/dist/src/api-config.js +0 -19
  34. package/dist/src/context.js +0 -2
  35. package/dist/src/encode-decode.js +0 -376
  36. package/dist/src/error.js +0 -32
  37. package/dist/src/execute.js +0 -56
  38. package/dist/src/http-client.js +0 -105
  39. package/dist/src/http-server.js +0 -941
  40. package/dist/src/index.js +0 -18
  41. package/dist/src/swagger.js +0 -439
  42. package/dist/src/test-wrapper.js +0 -52
  43. package/dist/src/utils.js +0 -7
  44. package/dist/tsconfig.tsbuildinfo +0 -1
  45. package/src/api-config.ts +0 -26
  46. package/src/context.ts +0 -41
  47. package/src/encode-decode.ts +0 -471
  48. package/src/error.ts +0 -30
  49. package/src/execute.ts +0 -67
  50. package/src/http-client.ts +0 -131
  51. package/src/http-server.ts +0 -1137
  52. package/src/index.ts +0 -7
  53. package/src/swagger.ts +0 -498
  54. package/src/test-wrapper.ts +0 -66
  55. package/src/utils.ts +0 -17
  56. /package/dist/{src/api-config.d.ts → api-config.d.ts} +0 -0
  57. /package/dist/{src/context.d.ts → context.d.ts} +0 -0
  58. /package/dist/{src/encode-decode.d.ts → encode-decode.d.ts} +0 -0
  59. /package/dist/{src/error.d.ts → error.d.ts} +0 -0
  60. /package/dist/{src/execute.d.ts → execute.d.ts} +0 -0
  61. /package/dist/{src/http-client.d.ts → http-client.d.ts} +0 -0
  62. /package/dist/{src/http-server.d.ts → http-server.d.ts} +0 -0
  63. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  64. /package/dist/{src/swagger.d.ts → swagger.d.ts} +0 -0
  65. /package/dist/{src/test-wrapper.d.ts → test-wrapper.d.ts} +0 -0
  66. /package/dist/{src/utils.d.ts → utils.d.ts} +0 -0
@@ -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
- });
@@ -1,17 +0,0 @@
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
- }
@@ -1,128 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.ApiClient = void 0;
27
- /* eslint-disable */
28
- const http = __importStar(require("http"));
29
- const https = __importStar(require("https"));
30
- const url_1 = require("url");
31
- class ApiClient {
32
- constructor(baseUrl = "https://", useStaging = false) {
33
- this.baseUrl = baseUrl;
34
- this.useStaging = useStaging;
35
- this.deviceId = null;
36
- }
37
- async getUser(id) {
38
- const args = {
39
- id: id,
40
- };
41
- const ret = await this.makeRequest({ name: "getUser", args });
42
- return {
43
- name: ret.name,
44
- age: ret.age | 0,
45
- };
46
- }
47
- async ping() {
48
- const ret = await this.makeRequest({ name: "ping", args: {} });
49
- return ret;
50
- }
51
- async setPushToken(token) {
52
- const args = {
53
- token: token,
54
- };
55
- await this.makeRequest({ name: "setPushToken", args });
56
- }
57
- device() {
58
- const device = {
59
- type: "node",
60
- };
61
- if (this.deviceId)
62
- device.id = this.deviceId;
63
- return device;
64
- }
65
- randomBytesHex(len) {
66
- let hex = "";
67
- for (let i = 0; i < 2 * len; ++i)
68
- hex += "0123456789abcdef"[Math.floor(Math.random() * 16)];
69
- return hex;
70
- }
71
- async makeRequest({ name, args }) {
72
- const deviceData = this.device();
73
- const body = {
74
- id: this.randomBytesHex(8),
75
- device: deviceData,
76
- name: name,
77
- args: args,
78
- };
79
- const url = new url_1.URL(this.baseUrl + (this.useStaging ? "-staging" : "") + "/" + name);
80
- const options = {
81
- hostname: url.hostname,
82
- path: url.pathname,
83
- port: url.port,
84
- method: "POST",
85
- };
86
- return new Promise((resolve, reject) => {
87
- const req = (url.protocol === "http:" ? http : https).request(options, resp => {
88
- let data = "";
89
- resp.on("data", chunk => {
90
- data += chunk;
91
- });
92
- resp.on("end", () => {
93
- try {
94
- const response = JSON.parse(data);
95
- try {
96
- this.deviceId = response.deviceId;
97
- if (response.ok) {
98
- resolve(response.result);
99
- }
100
- else {
101
- reject(response.error);
102
- }
103
- }
104
- catch (e) {
105
- console.error(e);
106
- reject({ type: "Fatal", message: `${e}` });
107
- }
108
- }
109
- catch (e) {
110
- console.error(e);
111
- reject({
112
- type: "BadFormattedResponse",
113
- message: `Response couldn't be parsed as JSON (${data}):\n${e}`,
114
- });
115
- }
116
- });
117
- });
118
- req.on("error", e => {
119
- console.error(`problem with request: ${e.message}`);
120
- reject({ type: "Fatal", message: e.toString() });
121
- });
122
- // write data to request body
123
- req.write(JSON.stringify(body));
124
- req.end();
125
- });
126
- }
127
- }
128
- exports.ApiClient = ApiClient;
@@ -1 +0,0 @@
1
- export {};