@sdkgen/node-runtime 2.3.1 → 2.4.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.
Files changed (54) hide show
  1. package/dist/api-config.js +19 -0
  2. package/dist/{src/context.d.ts → context.d.ts} +1 -0
  3. package/dist/context.js +3 -0
  4. package/dist/encode-decode.js +344 -0
  5. package/dist/error.js +33 -0
  6. package/dist/execute.js +57 -0
  7. package/dist/http-client.js +107 -0
  8. package/dist/{src/http-server.d.ts → http-server.d.ts} +1 -0
  9. package/dist/http-server.js +941 -0
  10. package/dist/{src/index.js → index.js} +1 -0
  11. package/dist/swagger.js +453 -0
  12. package/dist/test-wrapper.js +58 -0
  13. package/dist/utils.js +8 -0
  14. package/package.json +50 -34
  15. package/.eslintignore +0 -1
  16. package/.eslintrc.json +0 -3
  17. package/.prettierrc +0 -12
  18. package/.vscode/settings.json +0 -14
  19. package/dist/spec/error.spec.d.ts +0 -1
  20. package/dist/spec/error.spec.js +0 -15
  21. package/dist/spec/rest/rest.spec.d.ts +0 -1
  22. package/dist/spec/rest/rest.spec.js +0 -353
  23. package/dist/spec/runtime/errors.spec.d.ts +0 -1
  24. package/dist/spec/runtime/errors.spec.js +0 -43
  25. package/dist/spec/runtime/middleware.spec.d.ts +0 -1
  26. package/dist/spec/runtime/middleware.spec.js +0 -100
  27. package/dist/spec/simple/legacyNodeClient.d.ts +0 -17
  28. package/dist/spec/simple/legacyNodeClient.js +0 -128
  29. package/dist/spec/simple/simple.spec.d.ts +0 -1
  30. package/dist/spec/simple/simple.spec.js +0 -113
  31. package/dist/spec/types.d.ts +0 -1
  32. package/dist/spec/types.js +0 -60
  33. package/dist/spec/types.spec.d.ts +0 -1
  34. package/dist/spec/types.spec.js +0 -128
  35. package/dist/src/api-config.js +0 -19
  36. package/dist/src/context.js +0 -2
  37. package/dist/src/encode-decode.js +0 -376
  38. package/dist/src/error.js +0 -32
  39. package/dist/src/execute.js +0 -56
  40. package/dist/src/http-client.js +0 -105
  41. package/dist/src/http-server.js +0 -941
  42. package/dist/src/swagger.js +0 -439
  43. package/dist/src/test-wrapper.js +0 -52
  44. package/dist/src/utils.js +0 -7
  45. package/dist/tsconfig.tsbuildinfo +0 -1
  46. /package/dist/{src/api-config.d.ts → api-config.d.ts} +0 -0
  47. /package/dist/{src/encode-decode.d.ts → encode-decode.d.ts} +0 -0
  48. /package/dist/{src/error.d.ts → error.d.ts} +0 -0
  49. /package/dist/{src/execute.d.ts → execute.d.ts} +0 -0
  50. /package/dist/{src/http-client.d.ts → http-client.d.ts} +0 -0
  51. /package/dist/{src/index.d.ts → index.d.ts} +0 -0
  52. /package/dist/{src/swagger.d.ts → swagger.d.ts} +0 -0
  53. /package/dist/{src/test-wrapper.d.ts → test-wrapper.d.ts} +0 -0
  54. /package/dist/{src/utils.d.ts → utils.d.ts} +0 -0
@@ -1,439 +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
- exports.setupSwagger = void 0;
7
- const parser_1 = require("@sdkgen/parser");
8
- const serve_handler_1 = __importDefault(require("serve-handler"));
9
- const swagger_ui_dist_1 = require("swagger-ui-dist");
10
- const swaggerUiAssetPath = (0, swagger_ui_dist_1.getAbsoluteFSPath)();
11
- function objectFromEntries(entries) {
12
- return Object.assign({}, ...Array.from(entries, ([k, v]) => ({ [k]: v })));
13
- }
14
- function typeToSchema(definitions, type) {
15
- if (type instanceof parser_1.EnumType) {
16
- return {
17
- enum: type.values.map(x => x.value),
18
- type: "string",
19
- };
20
- }
21
- else if (type instanceof parser_1.StructType) {
22
- return {
23
- properties: objectFromEntries(type.fields.map(field => [
24
- field.name,
25
- Object.assign({ description: field.annotations
26
- .filter(x => x instanceof parser_1.DescriptionAnnotation)
27
- .map(x => x.text)
28
- .join(" ") || undefined }, typeToSchema(definitions, field.type)),
29
- ])),
30
- required: type.fields.filter(f => !(f.type instanceof parser_1.OptionalType)).map(f => f.name),
31
- type: "object",
32
- additionalProperties: false,
33
- };
34
- }
35
- else if (type instanceof parser_1.StringPrimitiveType ||
36
- type instanceof parser_1.UuidPrimitiveType ||
37
- type instanceof parser_1.HexPrimitiveType ||
38
- type instanceof parser_1.HtmlPrimitiveType ||
39
- type instanceof parser_1.Base64PrimitiveType) {
40
- return {
41
- type: "string",
42
- };
43
- }
44
- else if (type instanceof parser_1.UrlPrimitiveType) {
45
- return {
46
- format: "uri",
47
- type: "string",
48
- };
49
- }
50
- else if (type instanceof parser_1.DatePrimitiveType) {
51
- return {
52
- format: "date",
53
- type: "string",
54
- };
55
- }
56
- else if (type instanceof parser_1.DateTimePrimitiveType) {
57
- return {
58
- format: "date-time",
59
- type: "string",
60
- };
61
- }
62
- else if (type instanceof parser_1.CpfPrimitiveType) {
63
- return {
64
- type: "string",
65
- };
66
- }
67
- else if (type instanceof parser_1.CnpjPrimitiveType) {
68
- return {
69
- type: "string",
70
- };
71
- }
72
- else if (type instanceof parser_1.BoolPrimitiveType) {
73
- return {
74
- type: "boolean",
75
- };
76
- }
77
- else if (type instanceof parser_1.BytesPrimitiveType) {
78
- return {
79
- format: "byte",
80
- type: "string",
81
- };
82
- }
83
- else if (type instanceof parser_1.IntPrimitiveType) {
84
- return {
85
- format: "int32",
86
- type: "integer",
87
- };
88
- }
89
- else if (type instanceof parser_1.UIntPrimitiveType) {
90
- return {
91
- format: "int32",
92
- minimum: 0,
93
- type: "integer",
94
- };
95
- }
96
- else if (type instanceof parser_1.MoneyPrimitiveType) {
97
- return {
98
- format: "int64",
99
- type: "integer",
100
- };
101
- }
102
- else if (type instanceof parser_1.FloatPrimitiveType) {
103
- return {
104
- type: "number",
105
- };
106
- }
107
- else if (type instanceof parser_1.EmailPrimitiveType) {
108
- return {
109
- type: "string",
110
- };
111
- }
112
- else if (type instanceof parser_1.BigIntPrimitiveType) {
113
- return {
114
- type: "string",
115
- };
116
- }
117
- else if (type instanceof parser_1.DecimalPrimitiveType) {
118
- return {
119
- type: "string",
120
- };
121
- }
122
- else if (type instanceof parser_1.JsonPrimitiveType) {
123
- return {};
124
- }
125
- else if (type instanceof parser_1.OptionalType) {
126
- return {
127
- oneOf: [typeToSchema(definitions, type.base), { type: "null" }],
128
- };
129
- }
130
- else if (type instanceof parser_1.ArrayType) {
131
- return {
132
- items: typeToSchema(definitions, type.base),
133
- type: "array",
134
- };
135
- }
136
- else if (type instanceof parser_1.TypeReference) {
137
- if (!definitions[type.name]) {
138
- definitions[type.name] = typeToSchema(definitions, type.type);
139
- }
140
- return { $ref: `#/components/schemas/${type.name}` };
141
- }
142
- throw new Error(`Unhandled type ${type.constructor.name}`);
143
- }
144
- function getSwaggerJson(apiConfig) {
145
- var _a;
146
- const schemas = {};
147
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
148
- const paths = {};
149
- for (const op of apiConfig.ast.operations) {
150
- const throwAnnotations = op.annotations.filter(ann => ann instanceof parser_1.ThrowsAnnotation);
151
- let possibleErrors = throwAnnotations.map(ann => apiConfig.ast.errors.find(err => err.name === ann.error)).filter(x => x);
152
- if (possibleErrors.length === 0) {
153
- possibleErrors = apiConfig.ast.errors;
154
- }
155
- const errorsByStatus = new Map();
156
- for (const error of possibleErrors) {
157
- const statusAnnotation = error.annotations.find(ann => ann instanceof parser_1.StatusCodeAnnotation);
158
- const statusCode = statusAnnotation ? statusAnnotation.statusCode : error.name === "Fatal" ? 500 : 400;
159
- const errorList = (_a = errorsByStatus.get(statusCode)) !== null && _a !== void 0 ? _a : [];
160
- errorList.push(error);
161
- errorsByStatus.set(statusCode, errorList);
162
- }
163
- const errorResponses = Object.fromEntries([...errorsByStatus.entries()].map(([status, errors]) => [
164
- status,
165
- {
166
- description: errors
167
- .map(error => error.name)
168
- .sort((a, b) => a.localeCompare(b))
169
- .join("<br>"),
170
- content: {
171
- "application/json": {
172
- schema: {
173
- anyOf: errors.map(error => ({
174
- properties: Object.assign({ message: {
175
- type: "string",
176
- }, type: {
177
- enum: [error.name],
178
- type: "string",
179
- } }, (error.dataType instanceof parser_1.VoidPrimitiveType
180
- ? {}
181
- : {
182
- data: typeToSchema(schemas, error.dataType),
183
- })),
184
- required: ["type", "message", ...(error.dataType instanceof parser_1.VoidPrimitiveType ? [] : ["data"])],
185
- type: "object",
186
- additionalProperties: false,
187
- })),
188
- },
189
- },
190
- },
191
- },
192
- ]));
193
- for (const ann of op.annotations) {
194
- if (ann instanceof parser_1.RestAnnotation) {
195
- if (!paths[ann.path]) {
196
- paths[ann.path] = {};
197
- }
198
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
199
- paths[ann.path][ann.method.toLowerCase()] = {
200
- operationId: op.name,
201
- parameters: [
202
- ...ann.pathVariables.map(name => ({
203
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
204
- arg: op.args.find(arg => arg.name === name),
205
- location: "path",
206
- name,
207
- })),
208
- ...ann.queryVariables.map(name => ({
209
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
210
- arg: op.args.find(arg => arg.name === name),
211
- location: "query",
212
- name,
213
- })),
214
- ...[...ann.headers.entries()].map(([header, name]) => ({
215
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
216
- arg: op.args.find(arg => arg.name === name),
217
- location: "header",
218
- name: header,
219
- })),
220
- ].map(({ name, location, arg }) => ({
221
- description: arg.annotations
222
- .filter(x => x instanceof parser_1.DescriptionAnnotation)
223
- .map(x => x.text)
224
- .join(" ") || undefined,
225
- in: location,
226
- name,
227
- required: !(arg.type instanceof parser_1.OptionalType),
228
- schema: typeToSchema(schemas, arg.type),
229
- })),
230
- requestBody: ann.bodyVariable
231
- ? {
232
- content: Object.assign(Object.assign({}, (() => {
233
- var _a;
234
- const bodyType = (_a = op.args.find(arg => arg.name === ann.bodyVariable)) === null || _a === void 0 ? void 0 : _a.type;
235
- return bodyType instanceof parser_1.BoolPrimitiveType ||
236
- bodyType instanceof parser_1.IntPrimitiveType ||
237
- bodyType instanceof parser_1.UIntPrimitiveType ||
238
- bodyType instanceof parser_1.FloatPrimitiveType ||
239
- bodyType instanceof parser_1.StringPrimitiveType ||
240
- bodyType instanceof parser_1.DatePrimitiveType ||
241
- bodyType instanceof parser_1.DateTimePrimitiveType ||
242
- bodyType instanceof parser_1.MoneyPrimitiveType ||
243
- bodyType instanceof parser_1.CpfPrimitiveType ||
244
- bodyType instanceof parser_1.CnpjPrimitiveType ||
245
- bodyType instanceof parser_1.EmailPrimitiveType ||
246
- bodyType instanceof parser_1.HtmlPrimitiveType ||
247
- bodyType instanceof parser_1.UuidPrimitiveType ||
248
- bodyType instanceof parser_1.HexPrimitiveType ||
249
- bodyType instanceof parser_1.BytesPrimitiveType ||
250
- bodyType instanceof parser_1.Base64PrimitiveType
251
- ? {
252
- [bodyType instanceof parser_1.HtmlPrimitiveType ? "text/html" : "text/plain"]: {
253
- schema: typeToSchema(schemas, bodyType),
254
- },
255
- }
256
- : {};
257
- })()), { "application/json": {
258
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
259
- schema: typeToSchema(schemas, op.args.find(arg => arg.name === ann.bodyVariable).type),
260
- } }),
261
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
262
- required: !(op.args.find(arg => arg.name === ann.bodyVariable).type instanceof parser_1.OptionalType),
263
- }
264
- : undefined,
265
- responses: Object.assign(Object.assign(Object.assign({}, (op.returnType instanceof parser_1.OptionalType || op.returnType instanceof parser_1.VoidPrimitiveType
266
- ? { [ann.method === "GET" ? "404" : "204"]: {} }
267
- : {})), (op.returnType instanceof parser_1.VoidPrimitiveType
268
- ? {}
269
- : {
270
- 200: {
271
- description: "",
272
- content: Object.assign(Object.assign({}, (() => {
273
- return op.returnType instanceof parser_1.BoolPrimitiveType ||
274
- op.returnType instanceof parser_1.IntPrimitiveType ||
275
- op.returnType instanceof parser_1.UIntPrimitiveType ||
276
- op.returnType instanceof parser_1.FloatPrimitiveType ||
277
- op.returnType instanceof parser_1.StringPrimitiveType ||
278
- op.returnType instanceof parser_1.DatePrimitiveType ||
279
- op.returnType instanceof parser_1.DateTimePrimitiveType ||
280
- op.returnType instanceof parser_1.MoneyPrimitiveType ||
281
- op.returnType instanceof parser_1.CpfPrimitiveType ||
282
- op.returnType instanceof parser_1.CnpjPrimitiveType ||
283
- op.returnType instanceof parser_1.EmailPrimitiveType ||
284
- op.returnType instanceof parser_1.UuidPrimitiveType ||
285
- op.returnType instanceof parser_1.HexPrimitiveType ||
286
- op.returnType instanceof parser_1.BytesPrimitiveType ||
287
- op.returnType instanceof parser_1.Base64PrimitiveType
288
- ? {
289
- "text/plain": {
290
- schema: typeToSchema(schemas, op.returnType),
291
- },
292
- }
293
- : {};
294
- })()), { "application/json": {
295
- schema: typeToSchema(schemas, op.returnType),
296
- } }),
297
- },
298
- })), errorResponses),
299
- summary: op.annotations
300
- .filter(x => x instanceof parser_1.DescriptionAnnotation)
301
- .map(x => x.text)
302
- .join(" ") || undefined,
303
- tags: [ann.path.split("/")[1]],
304
- };
305
- }
306
- }
307
- }
308
- const securitySchemes = {
309
- bearerAuth: {
310
- type: "http",
311
- scheme: "bearer",
312
- },
313
- };
314
- const security = [
315
- {
316
- bearerAuth: [],
317
- },
318
- ];
319
- return {
320
- openapi: "3.0.0",
321
- info: {
322
- title: "",
323
- version: "",
324
- },
325
- paths,
326
- components: {
327
- schemas,
328
- securitySchemes,
329
- },
330
- security,
331
- };
332
- }
333
- function setupSwagger(server) {
334
- server.addHttpHandler("GET", "/swagger", (req, res) => {
335
- if (!server.introspection) {
336
- res.statusCode = 404;
337
- res.end();
338
- return;
339
- }
340
- res.setHeader("content-type", "text/html");
341
- res.write(`
342
- <!DOCTYPE html>
343
- <html lang="en">
344
- <head>
345
- <meta charset="UTF-8">
346
- <title>Swagger UI</title>
347
- <link rel="stylesheet" type="text/css" href="/swagger/swagger-ui.css" >
348
- <link rel="icon" type="image/png" href="/swagger/favicon-32x32.png" sizes="32x32" />
349
- <link rel="icon" type="image/png" href="/swagger/favicon-16x16.png" sizes="16x16" />
350
- <style>
351
- html {
352
- box-sizing: border-box;
353
- overflow: -moz-scrollbars-vertical;
354
- overflow-y: scroll;
355
- }
356
-
357
- *, *:before, *:after {
358
- box-sizing: inherit;
359
- }
360
-
361
- body {
362
- margin: 0;
363
- background: #fafafa;
364
- }
365
-
366
- .topbar {
367
- display: none !important;
368
- }
369
- </style>
370
- </head>
371
-
372
- <body>
373
- <div id="swagger-ui"></div>
374
- <script src="swagger/swagger-ui-bundle.js"> </script>
375
- <script src="swagger/swagger-ui-standalone-preset.js"> </script>
376
- <script>
377
- window.onload = function() {
378
- window.ui = SwaggerUIBundle({
379
- spec: {
380
- ...${JSON.stringify(getSwaggerJson(server.apiConfig))},
381
- servers: [{ url: location.origin + location.pathname.replace(/\\/swagger$/, "") }]
382
- },
383
- dom_id: '#swagger-ui',
384
- deepLinking: true,
385
- presets: [
386
- SwaggerUIBundle.presets.apis,
387
- SwaggerUIStandalonePreset,
388
- ],
389
- plugins: [
390
- SwaggerUIBundle.plugins.DownloadUrl,
391
- ],
392
- layout: "StandaloneLayout"
393
- });
394
- }
395
- </script>
396
- </body>
397
- </html>
398
- `);
399
- res.end();
400
- });
401
- server.addHttpHandler("GET", /^\/swagger.*/u, (req, res) => {
402
- if (!server.introspection) {
403
- res.statusCode = 404;
404
- res.end();
405
- return;
406
- }
407
- if (req.url) {
408
- req.url = req.url.replace(/\/swagger/u, "");
409
- }
410
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
411
- (0, serve_handler_1.default)(req, res, {
412
- cleanUrls: false,
413
- directoryListing: false,
414
- etag: true,
415
- public: swaggerUiAssetPath,
416
- }).catch(e => {
417
- console.error(e);
418
- res.statusCode = 500;
419
- res.write(`${e}`);
420
- res.end();
421
- });
422
- });
423
- server.addHttpHandler("GET", "/swagger.json", (req, res) => {
424
- if (!server.introspection) {
425
- res.statusCode = 404;
426
- res.end();
427
- return;
428
- }
429
- try {
430
- res.write(JSON.stringify(getSwaggerJson(server.apiConfig)));
431
- }
432
- catch (error) {
433
- console.error(error);
434
- res.statusCode = 500;
435
- }
436
- res.end();
437
- });
438
- }
439
- exports.setupSwagger = setupSwagger;
@@ -1,52 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.apiTestWrapper = void 0;
4
- /* eslint-disable @typescript-eslint/no-explicit-any */
5
- /* eslint-disable @typescript-eslint/no-unused-vars */
6
- /* eslint-disable @typescript-eslint/no-unsafe-assignment */
7
- /* eslint-disable @typescript-eslint/no-unsafe-call */
8
- /* eslint-disable @typescript-eslint/no-unsafe-member-access */
9
- /* eslint-disable @typescript-eslint/no-unsafe-return */
10
- const crypto_1 = require("crypto");
11
- const encode_decode_1 = require("./encode-decode");
12
- const execute_1 = require("./execute");
13
- function apiTestWrapper(api, extraContext = {}) {
14
- const wrappedApi = new api.constructor();
15
- for (const functionName of Object.keys(api.astJson.functionTable)) {
16
- wrappedApi.fn[functionName] = async (partialCtx, args) => {
17
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
18
- const encodedArgs = (0, encode_decode_1.encode)(api.astJson.typeTable, `fn.${functionName}.args`, api.astJson.functionTable[functionName].args, args);
19
- const ctx = Object.assign(Object.assign(Object.assign({}, extraContext), partialCtx), { request: {
20
- args: encodedArgs,
21
- deviceInfo: (_b = (_a = partialCtx.request) === null || _a === void 0 ? void 0 : _a.deviceInfo) !== null && _b !== void 0 ? _b : {
22
- fingerprint: null,
23
- id: (0, crypto_1.randomBytes)(16).toString("hex"),
24
- language: null,
25
- platform: null,
26
- timezone: null,
27
- type: "test",
28
- version: null,
29
- },
30
- extra: (_d = (_c = partialCtx.request) === null || _c === void 0 ? void 0 : _c.extra) !== null && _d !== void 0 ? _d : {},
31
- files: (_f = (_e = partialCtx.request) === null || _e === void 0 ? void 0 : _e.files) !== null && _f !== void 0 ? _f : [],
32
- headers: (_h = (_g = partialCtx.request) === null || _g === void 0 ? void 0 : _g.headers) !== null && _h !== void 0 ? _h : {},
33
- id: (_k = (_j = partialCtx.request) === null || _j === void 0 ? void 0 : _j.id) !== null && _k !== void 0 ? _k : (0, crypto_1.randomBytes)(16).toString("hex"),
34
- ip: (_m = (_l = partialCtx.request) === null || _l === void 0 ? void 0 : _l.ip) !== null && _m !== void 0 ? _m : "0.0.0.0",
35
- name: functionName,
36
- version: 3,
37
- }, response: {
38
- headers: new Map(),
39
- } });
40
- const reply = await (0, execute_1.executeRequest)(ctx, api);
41
- if (reply.error) {
42
- throw reply.error;
43
- }
44
- else {
45
- const decodedRet = (0, encode_decode_1.decode)(api.astJson.typeTable, `fn.${functionName}.ret`, api.astJson.functionTable[functionName].ret, JSON.parse(JSON.stringify(reply.result)));
46
- return decodedRet;
47
- }
48
- };
49
- }
50
- return wrappedApi;
51
- }
52
- exports.apiTestWrapper = apiTestWrapper;
package/dist/src/utils.js DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.has = void 0;
4
- function has(target, property) {
5
- return property in target;
6
- }
7
- exports.has = has;