@sdkgen/node-runtime 1.5.4 → 1.6.2

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.
@@ -1,4 +1,5 @@
1
1
  import type { AstJson } from "@sdkgen/parser";
2
+ import type { PartialDeep } from "type-fest";
2
3
  import type { Context } from "./context";
3
4
  import type { SdkgenError, SdkgenErrorWithData } from "./error";
4
5
  import type { DeepReadonly } from "./utils";
@@ -11,6 +12,6 @@ export declare class SdkgenHttpClient {
11
12
  private baseUrl;
12
13
  extra: Map<string, unknown>;
13
14
  constructor(baseUrl: string, astJson: DeepReadonly<AstJson>, errClasses: ErrClasses);
14
- makeRequest(ctx: Context | null, functionName: string, args: unknown): Promise<any>;
15
+ makeRequest(ctx: PartialDeep<Context> | null, functionName: string, args: unknown): Promise<any>;
15
16
  }
16
17
  export {};
@@ -17,16 +17,21 @@ class SdkgenHttpClient {
17
17
  this.baseUrl = new url_1.URL(baseUrl);
18
18
  }
19
19
  async makeRequest(ctx, functionName, args) {
20
+ var _a, _b;
20
21
  const func = this.astJson.functionTable[functionName];
21
22
  if (!func) {
22
23
  throw new Error(`Unknown function ${functionName}`);
23
24
  }
25
+ const extra = {};
26
+ for (const [key, value] of this.extra) {
27
+ extra[key] = value;
28
+ }
24
29
  const requestBody = JSON.stringify({
25
30
  args: encode_decode_1.encode(this.astJson.typeTable, `${functionName}.args`, func.args, args),
26
- deviceInfo: (ctx === null || ctx === void 0 ? void 0 : ctx.request) ? ctx.request.deviceInfo : { id: os_1.hostname(), type: "node" },
27
- extra: Object.assign(Object.assign({}, this.extra), ((ctx === null || ctx === void 0 ? void 0 : ctx.request) ? ctx.request.extra : {})),
31
+ deviceInfo: ((_a = ctx === null || ctx === void 0 ? void 0 : ctx.request) === null || _a === void 0 ? void 0 : _a.deviceInfo) ? ctx.request.deviceInfo : { id: os_1.hostname(), type: "node" },
32
+ extra: Object.assign(Object.assign({}, extra), ((ctx === null || ctx === void 0 ? void 0 : ctx.request) ? ctx.request.extra : {})),
28
33
  name: functionName,
29
- requestId: (ctx === null || ctx === void 0 ? void 0 : ctx.request) ? ctx.request.id + crypto_1.randomBytes(6).toString("hex") : crypto_1.randomBytes(16).toString("hex"),
34
+ requestId: ((_b = ctx === null || ctx === void 0 ? void 0 : ctx.request) === null || _b === void 0 ? void 0 : _b.id) ? ctx.request.id + crypto_1.randomBytes(6).toString("hex") : crypto_1.randomBytes(16).toString("hex"),
30
35
  version: 3,
31
36
  });
32
37
  const options = {
@@ -34,6 +39,9 @@ class SdkgenHttpClient {
34
39
  method: "POST",
35
40
  path: this.baseUrl.pathname,
36
41
  port: this.baseUrl.port,
42
+ headers: {
43
+ "content-type": "application/sdkgen",
44
+ },
37
45
  };
38
46
  const encodedRet = await new Promise((resolve, reject) => {
39
47
  const req = (this.baseUrl.protocol === "http:" ? http_1.request : https_1.request)(options, res => {
@@ -73,7 +73,7 @@ class SdkgenHttpServer {
73
73
  res.write(JSON.stringify(apiConfig.astJson));
74
74
  res.end();
75
75
  });
76
- this.addHttpHandler("GET", /^\/playground/u, (req, res) => {
76
+ this.addHttpHandler("GET", /^\/playground.*/u, (req, res) => {
77
77
  if (!this.introspection) {
78
78
  res.statusCode = 404;
79
79
  res.end();
@@ -214,11 +214,11 @@ class SdkgenHttpServer {
214
214
  }
215
215
  pathRegex += "/?$";
216
216
  for (const header of ann.headers.keys()) {
217
- this.addHeader("Access-Control-Allow-Headers", header);
217
+ this.addHeader("Access-Control-Allow-Headers", header.toLowerCase());
218
218
  }
219
219
  // eslint-disable-next-line @typescript-eslint/no-misused-promises
220
220
  this.addHttpHandler(ann.method, new RegExp(pathRegex, "u"), async (req, res, body) => {
221
- var _a, _b, _c, _d, _e, _f;
221
+ var _a, _b, _c, _d, _e, _f, _g;
222
222
  try {
223
223
  const args = {};
224
224
  const files = [];
@@ -237,16 +237,26 @@ class SdkgenHttpServer {
237
237
  const parsedQuery = query ? querystring_1.parse(query) : {};
238
238
  for (const argName of ann.queryVariables) {
239
239
  const argValue = (_b = parsedQuery[argName]) !== null && _b !== void 0 ? _b : null;
240
+ if (argValue === null) {
241
+ continue;
242
+ }
240
243
  simpleArgs.set(argName, Array.isArray(argValue) ? argValue.join("") : argValue);
241
244
  }
242
245
  for (const [headerName, argName] of ann.headers) {
243
- const argValue = (_c = req.headers[headerName]) !== null && _c !== void 0 ? _c : null;
246
+ const argValue = (_c = req.headers[headerName.toLowerCase()]) !== null && _c !== void 0 ? _c : null;
247
+ if (argValue === null) {
248
+ continue;
249
+ }
244
250
  simpleArgs.set(argName, Array.isArray(argValue) ? argValue.join("") : argValue);
245
251
  }
246
252
  if (!ann.bodyVariable && ((_d = req.headers["content-type"]) === null || _d === void 0 ? void 0 : _d.match(/^application\/x-www-form-urlencoded/iu))) {
247
253
  const parsedBody = querystring_1.parse(body.toString());
254
+ console.log("parsedBody", parsedBody);
248
255
  for (const argName of ann.queryVariables) {
249
256
  const argValue = (_e = parsedBody[argName]) !== null && _e !== void 0 ? _e : null;
257
+ if (argValue === null) {
258
+ continue;
259
+ }
250
260
  simpleArgs.set(argName, Array.isArray(argValue) ? argValue.join("") : argValue);
251
261
  }
252
262
  }
@@ -292,7 +302,7 @@ class SdkgenHttpServer {
292
302
  else if (ann.bodyVariable) {
293
303
  const argName = ann.bodyVariable;
294
304
  const arg = op.args.find(x => x.name === argName);
295
- if (req.headers["content-type"] === "application/json") {
305
+ if (/application\/json/iu.test((_g = req.headers["content-type"]) !== null && _g !== void 0 ? _g : "")) {
296
306
  args[argName] = JSON.parse(body.toString());
297
307
  }
298
308
  else if (arg) {
@@ -529,17 +539,19 @@ class SdkgenHttpServer {
529
539
  console.log(`${new Date().toISOString()} ${message}`);
530
540
  }
531
541
  async handleRequestWithBody(req, res, body, hrStart) {
532
- var _a;
542
+ var _a, _b;
533
543
  const { pathname, query } = url_1.parse((_a = req.url) !== null && _a !== void 0 ? _a : "");
534
544
  let path = pathname !== null && pathname !== void 0 ? pathname : "";
535
545
  if (path.startsWith(this.ignoredUrlPrefix)) {
536
546
  path = path.slice(this.ignoredUrlPrefix.length);
537
547
  }
538
- const externalHandler = this.findBestHandler(path, req);
539
- if (externalHandler) {
540
- this.log(`HTTP ${req.method} ${path}${query ? `?${query}` : ""}`);
541
- externalHandler.handler(req, res, body);
542
- return;
548
+ if (!((_b = req.headers["content-type"]) === null || _b === void 0 ? void 0 : _b.match(/application\/sdkgen/iu))) {
549
+ const externalHandler = this.findBestHandler(path, req);
550
+ if (externalHandler) {
551
+ this.log(`HTTP ${req.method} ${path}${query ? `?${query}` : ""}`);
552
+ externalHandler.handler(req, res, body);
553
+ return;
554
+ }
543
555
  }
544
556
  res.setHeader("Content-Type", "application/json; charset=utf-8");
545
557
  if (req.method === "HEAD") {
@@ -108,6 +108,11 @@ function typeToSchema(definitions, type) {
108
108
  type: "string",
109
109
  };
110
110
  }
111
+ else if (type instanceof parser_1.BigIntPrimitiveType) {
112
+ return {
113
+ type: "string",
114
+ };
115
+ }
111
116
  else if (type instanceof parser_1.OptionalType) {
112
117
  return {
113
118
  oneOf: [typeToSchema(definitions, type.base), { type: "null" }],
@@ -191,7 +196,7 @@ function setupSwagger(server) {
191
196
  `);
192
197
  res.end();
193
198
  });
194
- server.addHttpHandler("GET", /^\/swagger/u, (req, res) => {
199
+ server.addHttpHandler("GET", /^\/swagger.*/u, (req, res) => {
195
200
  if (!server.introspection) {
196
201
  res.statusCode = 404;
197
202
  res.end();