@sdkgen/node-runtime 2.3.2 → 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.
- 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 +453 -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
|
@@ -1,376 +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
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
26
|
-
var t = {};
|
|
27
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
28
|
-
t[p] = s[p];
|
|
29
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
30
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
31
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
32
|
-
t[p[i]] = s[p[i]];
|
|
33
|
-
}
|
|
34
|
-
return t;
|
|
35
|
-
};
|
|
36
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
-
exports.decode = exports.encode = void 0;
|
|
38
|
-
const CNPJ = __importStar(require("@fnando/cnpj"));
|
|
39
|
-
const CPF = __importStar(require("@fnando/cpf"));
|
|
40
|
-
const decimal_js_1 = require("decimal.js");
|
|
41
|
-
const simpleStringTypes = ["string", "email", "html", "xml"];
|
|
42
|
-
const simpleTypes = ["json", "bool", "url", "int", "uint", "float", "money", "hex", "uuid", "base64", "void", ...simpleStringTypes];
|
|
43
|
-
class ParseError extends Error {
|
|
44
|
-
constructor(path, type, value) {
|
|
45
|
-
let str;
|
|
46
|
-
try {
|
|
47
|
-
str = JSON.stringify(value);
|
|
48
|
-
}
|
|
49
|
-
catch (err) {
|
|
50
|
-
str = String(value);
|
|
51
|
-
}
|
|
52
|
-
super(`Invalid type at '${path}', expected ${String(type)}, got ${str}`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
function simpleEncodeDecode(path, type, value) {
|
|
56
|
-
if (type === "json") {
|
|
57
|
-
if (value === null || value === undefined) {
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
return JSON.parse(JSON.stringify(value));
|
|
61
|
-
}
|
|
62
|
-
else if (type === "bool") {
|
|
63
|
-
if (typeof value !== "boolean") {
|
|
64
|
-
throw new ParseError(path, type, value);
|
|
65
|
-
}
|
|
66
|
-
return value;
|
|
67
|
-
}
|
|
68
|
-
else if (simpleStringTypes.includes(type)) {
|
|
69
|
-
if (typeof value !== "string") {
|
|
70
|
-
throw new ParseError(path, type, value);
|
|
71
|
-
}
|
|
72
|
-
return value;
|
|
73
|
-
}
|
|
74
|
-
else if (type === "hex") {
|
|
75
|
-
if (typeof value !== "string" || !/^(?:[A-Fa-f0-9]{2})*$/u.test(value)) {
|
|
76
|
-
throw new ParseError(path, type, value);
|
|
77
|
-
}
|
|
78
|
-
return value.toLowerCase();
|
|
79
|
-
}
|
|
80
|
-
else if (type === "uuid") {
|
|
81
|
-
if (typeof value !== "string" || !/^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$/u.test(value)) {
|
|
82
|
-
throw new ParseError(path, type, value);
|
|
83
|
-
}
|
|
84
|
-
return value.toLowerCase();
|
|
85
|
-
}
|
|
86
|
-
else if (type === "base64") {
|
|
87
|
-
if (typeof value !== "string" || Buffer.from(value, "base64").toString("base64") !== value) {
|
|
88
|
-
throw new ParseError(path, type, value);
|
|
89
|
-
}
|
|
90
|
-
return value;
|
|
91
|
-
}
|
|
92
|
-
else if (type === "int") {
|
|
93
|
-
if (typeof value !== "number" || (value | 0) !== value) {
|
|
94
|
-
throw new ParseError(path, type, value);
|
|
95
|
-
}
|
|
96
|
-
return value;
|
|
97
|
-
}
|
|
98
|
-
else if (type === "uint") {
|
|
99
|
-
if (typeof value !== "number" || (value | 0) !== value || value < 0) {
|
|
100
|
-
throw new ParseError(path, type, value);
|
|
101
|
-
}
|
|
102
|
-
return value;
|
|
103
|
-
}
|
|
104
|
-
else if (type === "float") {
|
|
105
|
-
if (typeof value !== "number") {
|
|
106
|
-
throw new ParseError(path, type, value);
|
|
107
|
-
}
|
|
108
|
-
return value;
|
|
109
|
-
}
|
|
110
|
-
else if (type === "money") {
|
|
111
|
-
if (typeof value !== "number" || !Number.isInteger(value)) {
|
|
112
|
-
throw new ParseError(path, type, value);
|
|
113
|
-
}
|
|
114
|
-
return value;
|
|
115
|
-
}
|
|
116
|
-
else if (type === "url") {
|
|
117
|
-
let url;
|
|
118
|
-
if (typeof value === "string") {
|
|
119
|
-
try {
|
|
120
|
-
url = new URL(value);
|
|
121
|
-
}
|
|
122
|
-
catch (e) {
|
|
123
|
-
// ignore
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
if (!url) {
|
|
127
|
-
throw new ParseError(path, type, value);
|
|
128
|
-
}
|
|
129
|
-
return url.toString();
|
|
130
|
-
}
|
|
131
|
-
else if (type === "void") {
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
throw new Error(`Unknown type '${type}' at '${path}'`);
|
|
135
|
-
}
|
|
136
|
-
function encode(typeTable, path, type, value) {
|
|
137
|
-
if (typeof type === "string" && !type.endsWith("?") && type !== "void" && (value === null || value === undefined)) {
|
|
138
|
-
throw new Error(`Invalid type at '${path}', cannot be null`);
|
|
139
|
-
}
|
|
140
|
-
else if (Array.isArray(type)) {
|
|
141
|
-
if (type.every(tag => typeof tag === "string")) {
|
|
142
|
-
for (const tag of type) {
|
|
143
|
-
if (tag === value) {
|
|
144
|
-
return tag;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
else if (typeof value === "object" && value && "tag" in value) {
|
|
149
|
-
const _a = value, { tag: tagValue } = _a, restValue = __rest(_a, ["tag"]);
|
|
150
|
-
for (const entry of type) {
|
|
151
|
-
if (typeof entry === "string") {
|
|
152
|
-
if (entry === tagValue) {
|
|
153
|
-
return entry;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
else {
|
|
157
|
-
const [tag, valueType] = entry;
|
|
158
|
-
if (tag === tagValue) {
|
|
159
|
-
const encodedValues = encode(typeTable, `${path}.${tag}`, valueType, restValue);
|
|
160
|
-
// eslint-disable-next-line max-depth
|
|
161
|
-
if (Object.values(encodedValues).every(v => v === null)) {
|
|
162
|
-
return tag;
|
|
163
|
-
}
|
|
164
|
-
return [tag, encodedValues];
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
throw new ParseError(path, type, value);
|
|
170
|
-
}
|
|
171
|
-
else if (typeof type === "object") {
|
|
172
|
-
if (typeof value !== "object") {
|
|
173
|
-
throw new ParseError(path, type, value);
|
|
174
|
-
}
|
|
175
|
-
const obj = {};
|
|
176
|
-
for (const key of Object.keys(type)) {
|
|
177
|
-
obj[key] = encode(typeTable, `${path}.${key}`, type[key], value[key]);
|
|
178
|
-
}
|
|
179
|
-
return obj;
|
|
180
|
-
}
|
|
181
|
-
else if (typeof type === "string" && type.endsWith("?")) {
|
|
182
|
-
if (value === null || value === undefined) {
|
|
183
|
-
return null;
|
|
184
|
-
}
|
|
185
|
-
return encode(typeTable, path, type.slice(0, type.length - 1), value);
|
|
186
|
-
}
|
|
187
|
-
else if (typeof type === "string" && type.endsWith("[]")) {
|
|
188
|
-
if (!Array.isArray(value)) {
|
|
189
|
-
throw new ParseError(path, type, value);
|
|
190
|
-
}
|
|
191
|
-
return value.map((entry, index) => encode(typeTable, `${path}[${index}]`, type.slice(0, type.length - 2), entry));
|
|
192
|
-
}
|
|
193
|
-
else if (typeof type === "string" && simpleTypes.includes(type)) {
|
|
194
|
-
return simpleEncodeDecode(path, type, value);
|
|
195
|
-
}
|
|
196
|
-
else if (type === "bytes") {
|
|
197
|
-
if (!(value instanceof Buffer)) {
|
|
198
|
-
throw new ParseError(path, type, value);
|
|
199
|
-
}
|
|
200
|
-
return value.toString("base64");
|
|
201
|
-
}
|
|
202
|
-
else if (type === "bigint") {
|
|
203
|
-
if (!(typeof value === "bigint")) {
|
|
204
|
-
throw new ParseError(path, type, value);
|
|
205
|
-
}
|
|
206
|
-
return value.toString();
|
|
207
|
-
}
|
|
208
|
-
else if (type === "cpf") {
|
|
209
|
-
if (typeof value !== "string" || !CPF.isValid(value)) {
|
|
210
|
-
throw new ParseError(path, type, value);
|
|
211
|
-
}
|
|
212
|
-
return CPF.strip(value);
|
|
213
|
-
}
|
|
214
|
-
else if (type === "cnpj") {
|
|
215
|
-
if (typeof value !== "string" || !CNPJ.isValid(value)) {
|
|
216
|
-
throw new ParseError(path, type, value);
|
|
217
|
-
}
|
|
218
|
-
return CNPJ.strip(value);
|
|
219
|
-
}
|
|
220
|
-
else if (type === "date") {
|
|
221
|
-
if (!(value instanceof Date && !isNaN(value.getTime())) && !(typeof value === "string" && /^[0-9]{4}-[01][0-9]-[0123][0-9]$/u.test(value))) {
|
|
222
|
-
throw new ParseError(path, type, value);
|
|
223
|
-
}
|
|
224
|
-
const dateValue = value instanceof Date ? value : new Date(value);
|
|
225
|
-
return `${dateValue.getFullYear().toString().padStart(4, "0")}-${(dateValue.getMonth() + 1).toString().padStart(2, "0")}-${dateValue
|
|
226
|
-
.getDate()
|
|
227
|
-
.toString()
|
|
228
|
-
.padStart(2, "0")}`;
|
|
229
|
-
}
|
|
230
|
-
else if (type === "datetime") {
|
|
231
|
-
if (!(value instanceof Date && !isNaN(value.getTime())) &&
|
|
232
|
-
!(typeof value === "string" &&
|
|
233
|
-
/^[0-9]{4}-[01][0-9]-[0123][0-9]T[012][0-9]:[0123456][0-9]:[0123456][0-9](?:\.[0-9]{1,6})?(?:Z|[+-][012][0-9]:[0123456][0-9])?$/u.test(value))) {
|
|
234
|
-
throw new ParseError(path, type, value);
|
|
235
|
-
}
|
|
236
|
-
return (typeof value === "string" ? new Date(value) : value).toISOString().replace("Z", "");
|
|
237
|
-
}
|
|
238
|
-
else if (type === "decimal") {
|
|
239
|
-
if (typeof value !== "number" && (typeof value !== "string" || !/^-?[0-9]+(?:\.[0-9]+)?$/u.test(value)) && !decimal_js_1.Decimal.isDecimal(value)) {
|
|
240
|
-
throw new ParseError(path, type, value);
|
|
241
|
-
}
|
|
242
|
-
return new decimal_js_1.Decimal(value).toString();
|
|
243
|
-
}
|
|
244
|
-
else {
|
|
245
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
246
|
-
const resolved = typeTable[type];
|
|
247
|
-
if (resolved) {
|
|
248
|
-
return encode(typeTable, path, resolved, value);
|
|
249
|
-
}
|
|
250
|
-
throw new Error(`Unknown type '${String(type)}' at '${path}'`);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
exports.encode = encode;
|
|
254
|
-
function decode(typeTable, path, type, value) {
|
|
255
|
-
if (typeof type === "string" && !type.endsWith("?") && type !== "void" && (value === null || value === undefined)) {
|
|
256
|
-
throw new Error(`Invalid type at '${path}', cannot be null`);
|
|
257
|
-
}
|
|
258
|
-
else if (Array.isArray(type)) {
|
|
259
|
-
if (type.every(tag => typeof tag === "string")) {
|
|
260
|
-
for (const tag of type) {
|
|
261
|
-
if (tag === value) {
|
|
262
|
-
return tag;
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
else {
|
|
267
|
-
for (const entry of type) {
|
|
268
|
-
if (typeof entry === "string") {
|
|
269
|
-
if (entry === value) {
|
|
270
|
-
return { tag: entry };
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
else {
|
|
274
|
-
const [tag, valueType] = entry;
|
|
275
|
-
if (tag === value) {
|
|
276
|
-
const decodedValues = decode(typeTable, `${path}.${tag}`, valueType, {});
|
|
277
|
-
return Object.assign(Object.assign({}, decodedValues), { tag });
|
|
278
|
-
}
|
|
279
|
-
else if (Array.isArray(value) && value.length === 2 && tag === value[0]) {
|
|
280
|
-
const decodedValues = decode(typeTable, `${path}.${tag}`, valueType, value[1]);
|
|
281
|
-
return Object.assign(Object.assign({}, decodedValues), { tag });
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
throw new ParseError(path, type, value);
|
|
287
|
-
}
|
|
288
|
-
else if (typeof type === "object") {
|
|
289
|
-
if (typeof value !== "object") {
|
|
290
|
-
throw new ParseError(path, type, value);
|
|
291
|
-
}
|
|
292
|
-
const obj = {};
|
|
293
|
-
for (const key of Object.keys(type)) {
|
|
294
|
-
obj[key] = decode(typeTable, `${path}.${key}`, type[key], value[key]);
|
|
295
|
-
}
|
|
296
|
-
return obj;
|
|
297
|
-
}
|
|
298
|
-
else if (typeof type === "string" && type.endsWith("?")) {
|
|
299
|
-
if (value === null || value === undefined) {
|
|
300
|
-
return null;
|
|
301
|
-
}
|
|
302
|
-
return decode(typeTable, path, type.slice(0, type.length - 1), value);
|
|
303
|
-
}
|
|
304
|
-
else if (typeof type === "string" && type.endsWith("[]")) {
|
|
305
|
-
if (!Array.isArray(value)) {
|
|
306
|
-
throw new ParseError(path, type, value);
|
|
307
|
-
}
|
|
308
|
-
return value.map((entry, index) => decode(typeTable, `${path}[${index}]`, type.slice(0, type.length - 2), entry));
|
|
309
|
-
}
|
|
310
|
-
else if (typeof type === "string" && simpleTypes.includes(type)) {
|
|
311
|
-
return simpleEncodeDecode(path, type, value);
|
|
312
|
-
}
|
|
313
|
-
else if (type === "bytes") {
|
|
314
|
-
if (typeof value !== "string") {
|
|
315
|
-
throw new ParseError(path, `${String(type)} (base 64)`, value);
|
|
316
|
-
}
|
|
317
|
-
const buffer = Buffer.from(value, "base64");
|
|
318
|
-
if (buffer.toString("base64") !== value) {
|
|
319
|
-
throw new ParseError(path, `${String(type)} (base 64)`, value);
|
|
320
|
-
}
|
|
321
|
-
return buffer;
|
|
322
|
-
}
|
|
323
|
-
else if (type === "bigint") {
|
|
324
|
-
if (typeof value !== "number" && (typeof value !== "string" || !/^-?[0-9]+$/u.test(value))) {
|
|
325
|
-
throw new ParseError(path, type, value);
|
|
326
|
-
}
|
|
327
|
-
return BigInt(value);
|
|
328
|
-
}
|
|
329
|
-
else if (type === "cpf") {
|
|
330
|
-
if (typeof value !== "string" || !CPF.isValid(value)) {
|
|
331
|
-
throw new ParseError(path, type, value);
|
|
332
|
-
}
|
|
333
|
-
return CPF.format(value);
|
|
334
|
-
}
|
|
335
|
-
else if (type === "cnpj") {
|
|
336
|
-
if (typeof value !== "string" || !CNPJ.isValid(value)) {
|
|
337
|
-
throw new ParseError(path, type, value);
|
|
338
|
-
}
|
|
339
|
-
return CNPJ.format(value);
|
|
340
|
-
}
|
|
341
|
-
else if (type === "date") {
|
|
342
|
-
if (typeof value !== "string" || !/^[0-9]{4}-[01][0-9]-[0123][0-9]$/u.test(value)) {
|
|
343
|
-
throw new ParseError(path, type, value);
|
|
344
|
-
}
|
|
345
|
-
const day = parseInt(value.split("-")[2], 10);
|
|
346
|
-
const month = parseInt(value.split("-")[1], 10) - 1;
|
|
347
|
-
const year = parseInt(value.split("-")[0], 10);
|
|
348
|
-
const date = new Date(year, month, day);
|
|
349
|
-
if (date.getFullYear() !== year || date.getMonth() !== month || date.getDate() !== day) {
|
|
350
|
-
throw new ParseError(path, type, value);
|
|
351
|
-
}
|
|
352
|
-
return date;
|
|
353
|
-
}
|
|
354
|
-
else if (type === "datetime") {
|
|
355
|
-
if (typeof value !== "string" || !/^[0-9]{4}-[01][0-9]-[0123][0-9]T[012][0-9]:[0123456][0-9]:[0123456][0-9](?:\.[0-9]{1,6})?Z?$/u.test(value)) {
|
|
356
|
-
throw new ParseError(path, type, value);
|
|
357
|
-
}
|
|
358
|
-
return new Date(`${value.endsWith("Z") ? value : value.concat("Z")}`);
|
|
359
|
-
}
|
|
360
|
-
else if (type === "decimal") {
|
|
361
|
-
if (typeof value !== "number" && (typeof value !== "string" || !/^-?[0-9]+(?:\.[0-9]+)?$/u.test(value))) {
|
|
362
|
-
throw new ParseError(path, type, value);
|
|
363
|
-
}
|
|
364
|
-
return new decimal_js_1.Decimal(value);
|
|
365
|
-
}
|
|
366
|
-
else {
|
|
367
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
|
|
368
|
-
const resolved = typeTable[type];
|
|
369
|
-
if (resolved) {
|
|
370
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
371
|
-
return decode(typeTable, path, resolved, value);
|
|
372
|
-
}
|
|
373
|
-
throw new Error(`Unknown type '${String(type)}' at '${path}'`);
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
exports.decode = decode;
|
package/dist/src/error.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Fatal = exports.SdkgenErrorWithData = exports.SdkgenError = void 0;
|
|
4
|
-
class SdkgenError extends Error {
|
|
5
|
-
get type() {
|
|
6
|
-
return this.constructor.name;
|
|
7
|
-
}
|
|
8
|
-
toJSON() {
|
|
9
|
-
return {
|
|
10
|
-
message: this.message,
|
|
11
|
-
type: this.type,
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
exports.SdkgenError = SdkgenError;
|
|
16
|
-
class SdkgenErrorWithData extends SdkgenError {
|
|
17
|
-
constructor(message, data) {
|
|
18
|
-
super(message);
|
|
19
|
-
this.data = data;
|
|
20
|
-
}
|
|
21
|
-
toJSON() {
|
|
22
|
-
return {
|
|
23
|
-
data: this.data,
|
|
24
|
-
message: this.message,
|
|
25
|
-
type: this.type,
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.SdkgenErrorWithData = SdkgenErrorWithData;
|
|
30
|
-
class Fatal extends SdkgenError {
|
|
31
|
-
}
|
|
32
|
-
exports.Fatal = Fatal;
|
package/dist/src/execute.js
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.executeRequest = void 0;
|
|
4
|
-
const parser_1 = require("@sdkgen/parser");
|
|
5
|
-
const encode_decode_1 = require("./encode-decode");
|
|
6
|
-
const error_1 = require("./error");
|
|
7
|
-
const utils_1 = require("./utils");
|
|
8
|
-
async function executeRequest(ctx, apiConfig) {
|
|
9
|
-
// eslint-disable-next-line func-style
|
|
10
|
-
let next = async () => {
|
|
11
|
-
try {
|
|
12
|
-
const functionDescription = apiConfig.astJson.functionTable[ctx.request.name];
|
|
13
|
-
const functionImplementation = apiConfig.fn[ctx.request.name];
|
|
14
|
-
if (!functionDescription || !functionImplementation) {
|
|
15
|
-
throw new error_1.Fatal(`Function does not exist: ${ctx.request.name}`);
|
|
16
|
-
}
|
|
17
|
-
const args = (0, encode_decode_1.decode)(apiConfig.astJson.typeTable, `${ctx.request.name}.args`, functionDescription.args, ctx.request.args);
|
|
18
|
-
const ret = (await functionImplementation(ctx, args));
|
|
19
|
-
const encodedRet = (0, encode_decode_1.encode)(apiConfig.astJson.typeTable, `${ctx.request.name}.ret`, functionDescription.ret, ret);
|
|
20
|
-
return { result: encodedRet };
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
return { error };
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
for (let i = apiConfig.middlewares.length - 1; i >= 0; --i) {
|
|
27
|
-
const middleware = apiConfig.middlewares[i];
|
|
28
|
-
const previousNext = next;
|
|
29
|
-
next = async () => {
|
|
30
|
-
try {
|
|
31
|
-
return await middleware(ctx, previousNext);
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
return { error };
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
const reply = await next();
|
|
39
|
-
// If errors, check if the error type is one of the @throws annotation. If it isn't, change to Fatal
|
|
40
|
-
if (reply.error) {
|
|
41
|
-
const functionAst = apiConfig.ast.operations.find(op => op.name === ctx.request.name);
|
|
42
|
-
if (functionAst) {
|
|
43
|
-
const allowedErrors = functionAst.annotations.filter(ann => ann instanceof parser_1.ThrowsAnnotation).map(ann => ann.error);
|
|
44
|
-
if (typeof reply.error !== "object" ||
|
|
45
|
-
reply.error === null ||
|
|
46
|
-
!(0, utils_1.has)(reply.error, "type") ||
|
|
47
|
-
typeof reply.error.type !== "string" ||
|
|
48
|
-
(allowedErrors.length > 0 && !allowedErrors.includes(reply.error.type)) ||
|
|
49
|
-
!apiConfig.astJson.errors.map(error => (typeof error === "string" ? error : error[0])).includes(reply.error.type)) {
|
|
50
|
-
Object.defineProperty(reply.error, "type", { value: "Fatal" });
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
return reply;
|
|
55
|
-
}
|
|
56
|
-
exports.executeRequest = executeRequest;
|
package/dist/src/http-client.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.SdkgenHttpClient = void 0;
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
-
/* eslint-disable prefer-promise-reject-errors */
|
|
6
|
-
const crypto_1 = require("crypto");
|
|
7
|
-
const http_1 = require("http");
|
|
8
|
-
const https_1 = require("https");
|
|
9
|
-
const os_1 = require("os");
|
|
10
|
-
const url_1 = require("url");
|
|
11
|
-
const encode_decode_1 = require("./encode-decode");
|
|
12
|
-
const utils_1 = require("./utils");
|
|
13
|
-
class SdkgenHttpClient {
|
|
14
|
-
constructor(baseUrl, astJson, errClasses) {
|
|
15
|
-
this.astJson = astJson;
|
|
16
|
-
this.errClasses = errClasses;
|
|
17
|
-
this.extra = new Map();
|
|
18
|
-
this.baseUrl = new url_1.URL(baseUrl);
|
|
19
|
-
}
|
|
20
|
-
async makeRequest(ctx, functionName, args) {
|
|
21
|
-
var _a, _b, _c, _d;
|
|
22
|
-
const func = this.astJson.functionTable[functionName];
|
|
23
|
-
if (!func) {
|
|
24
|
-
throw new Error(`Unknown function ${functionName}`);
|
|
25
|
-
}
|
|
26
|
-
const extra = {};
|
|
27
|
-
for (const [key, value] of this.extra) {
|
|
28
|
-
extra[key] = value;
|
|
29
|
-
}
|
|
30
|
-
const requestBody = JSON.stringify({
|
|
31
|
-
args: (0, encode_decode_1.encode)(this.astJson.typeTable, `${functionName}.args`, func.args, args),
|
|
32
|
-
deviceInfo: (_b = (_a = ctx === null || ctx === void 0 ? void 0 : ctx.request) === null || _a === void 0 ? void 0 : _a.deviceInfo) !== null && _b !== void 0 ? _b : { id: (0, os_1.hostname)(), type: "node" },
|
|
33
|
-
extra: Object.assign(Object.assign({}, extra), (_c = ctx === null || ctx === void 0 ? void 0 : ctx.request) === null || _c === void 0 ? void 0 : _c.extra),
|
|
34
|
-
name: functionName,
|
|
35
|
-
requestId: ((_d = ctx === null || ctx === void 0 ? void 0 : ctx.request) === null || _d === void 0 ? void 0 : _d.id) ? ctx.request.id + (0, crypto_1.randomBytes)(6).toString("hex") : (0, crypto_1.randomBytes)(16).toString("hex"),
|
|
36
|
-
version: 3,
|
|
37
|
-
});
|
|
38
|
-
const options = {
|
|
39
|
-
hostname: this.baseUrl.hostname,
|
|
40
|
-
method: "POST",
|
|
41
|
-
path: this.baseUrl.pathname,
|
|
42
|
-
port: this.baseUrl.port,
|
|
43
|
-
headers: {
|
|
44
|
-
"content-type": "application/sdkgen",
|
|
45
|
-
},
|
|
46
|
-
};
|
|
47
|
-
const encodedRet = await new Promise((resolve, reject) => {
|
|
48
|
-
const req = (this.baseUrl.protocol === "http:" ? http_1.request : https_1.request)(options, res => {
|
|
49
|
-
let data = "";
|
|
50
|
-
res.on("data", chunk => {
|
|
51
|
-
data += chunk;
|
|
52
|
-
});
|
|
53
|
-
res.on("end", () => {
|
|
54
|
-
try {
|
|
55
|
-
const response = JSON.parse(data);
|
|
56
|
-
if ((0, utils_1.has)(response, "error") && response.error) {
|
|
57
|
-
reject(response.error);
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
resolve((0, utils_1.has)(response, "result") ? response.result : null);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
catch (error) {
|
|
64
|
-
reject({ message: `${error}`, type: "Fatal" });
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
res.on("error", error => {
|
|
68
|
-
reject({ message: `${error}`, type: "Fatal" });
|
|
69
|
-
});
|
|
70
|
-
res.on("aborted", () => {
|
|
71
|
-
reject({ message: "Request aborted", type: "Fatal" });
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
req.on("error", error => {
|
|
75
|
-
reject({ message: `${error}`, type: "Fatal" });
|
|
76
|
-
});
|
|
77
|
-
req.write(requestBody);
|
|
78
|
-
req.end();
|
|
79
|
-
}).catch((error) => {
|
|
80
|
-
var _a;
|
|
81
|
-
if ((0, utils_1.has)(error, "type") && (0, utils_1.has)(error, "message") && typeof error.type === "string" && typeof error.message === "string") {
|
|
82
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
83
|
-
const errClass = (_a = this.errClasses[error.type]) !== null && _a !== void 0 ? _a : this.errClasses.Fatal;
|
|
84
|
-
const errType = errClass.name;
|
|
85
|
-
const errorJson = this.astJson.errors.find(err => (Array.isArray(err) ? err[0] === errType : err === errType));
|
|
86
|
-
let newError;
|
|
87
|
-
if (errorJson && Array.isArray(errorJson) && (0, utils_1.has)(error, "data")) {
|
|
88
|
-
newError = new errClass(error.message, (0, encode_decode_1.decode)(this.astJson.typeTable, `${errClass.name}.data`, errorJson[1], error.data));
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
newError = new errClass(error.message, undefined);
|
|
92
|
-
}
|
|
93
|
-
if (!newError.type) {
|
|
94
|
-
newError.type = errType;
|
|
95
|
-
}
|
|
96
|
-
throw newError;
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
throw error;
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
return (0, encode_decode_1.decode)(this.astJson.typeTable, `${functionName}.ret`, func.ret, encodedRet);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
exports.SdkgenHttpClient = SdkgenHttpClient;
|