UNPKG

@freemework/common

Version:

Common library of the Freemework Project.

197 lines 8.15 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.FWebClient = void 0; const index_js_1 = require("../lifecycle/index.js"); const f_http_client_js_1 = require("./f_http_client.js"); const f_limit_js_1 = require("../limit/f_limit.js"); const index_js_2 = require("../cancellation/index.js"); const f_limit_in_memory_js_1 = require("../limit/f_limit_in_memory.js"); const querystring = __importStar(require("querystring")); class FWebClient extends index_js_1.FDisposableBase { _baseUrl; _userAgent; _httpClient; _limitHandle; constructor(url, opts) { super(); this._baseUrl = typeof url === "string" ? new URL(url) : url; if (opts !== undefined) { const { limit, httpClient, userAgent } = opts; if (limit !== undefined) { this._limitHandle = f_limit_js_1.FLimit.isLimitOpts(limit.instance) ? { instance: new f_limit_in_memory_js_1.FLimitInMemory(limit.instance), isOwnInstance: true, timeout: limit.timeout } : { instance: limit.instance, isOwnInstance: false, timeout: limit.timeout }; } if (httpClient !== undefined) { if ("invoke" in httpClient) { this._httpClient = httpClient; } else { this._httpClient = new f_http_client_js_1.FHttpClient({ ...httpClient }); } } else { this._httpClient = new f_http_client_js_1.FHttpClient(); } if (userAgent !== undefined) { this._userAgent = userAgent; } } else { this._httpClient = new f_http_client_js_1.FHttpClient(); } } get(executionContext, urlPath, opts) { super.verifyNotDisposed(); const { queryArgs = undefined, headers = undefined, limitWeight = undefined } = (() => opts || {})(); const path = queryArgs !== undefined ? urlPath + "?" + querystring.stringify(queryArgs) : urlPath; return this.invoke(executionContext, path, "GET", { headers: headers, limitWeight: limitWeight }); } postJson(executionContext, urlPath, opts) { // Serialize JSON if body is object const friendlyBody = Buffer.from(JSON.stringify(opts.postData)); const friendlyHeaders = opts.headers !== undefined ? { ...opts.headers } : {}; if (!("Content-Type" in friendlyHeaders)) { friendlyHeaders["Content-Type"] = "application/json"; } friendlyHeaders["Content-Length"] = friendlyBody.byteLength; return this.invoke(executionContext, urlPath, "POST", { headers: friendlyHeaders, body: friendlyBody }); } postForm(executionContext, urlPath, opts) { super.verifyNotDisposed(); const { postArgs = undefined, headers = undefined, limitWeight = undefined } = (() => opts || {})(); const bodyStr = postArgs && querystring.stringify(postArgs); const { body, bodyLength } = (() => { if (bodyStr !== undefined) { const bodyBuffer = Buffer.from(bodyStr); return { body: bodyBuffer, bodyLength: bodyBuffer.byteLength }; } else { return { body: undefined, bodyLength: 0 }; } })(); const friendlyHeaders = (() => { const baseHeaders = { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": bodyLength }; return headers !== undefined ? { ...baseHeaders, ...headers } : baseHeaders; })(); return this.invoke(executionContext, urlPath, "POST", { body: body, headers: friendlyHeaders, limitWeight: limitWeight }); } async invoke(executionContext, path, method, opts) { super.verifyNotDisposed(); const cancellationToken = index_js_2.FCancellationExecutionContext.of(executionContext).cancellationToken; let friendlyBody = null; let friendlyHeaders = {}; let limitToken = null; let limitWeight = 1; if (opts !== undefined) { const { headers, body } = opts; if (headers !== undefined) { friendlyHeaders = { ...headers }; } if (this._userAgent !== undefined && !("User-Agent" in friendlyHeaders)) { friendlyHeaders["User-Agent"] = this._userAgent; } if (body !== undefined) { friendlyBody = body; } if (opts.limitWeight !== undefined) { limitWeight = opts.limitWeight; } } try { if (this._limitHandle !== undefined) { const a = await this._limitHandle.instance.accrueTokenLazy(limitWeight, this._limitHandle.timeout, cancellationToken); limitToken = a; } else { friendlyHeaders["X-FLimit-Weight"] = limitWeight; } const url = new URL(path, this._baseUrl); const invokeArgs = { url, method, headers: friendlyHeaders }; if (friendlyBody !== null) { invokeArgs.body = friendlyBody; } const invokeResponse = await this._httpClient.invoke(executionContext, invokeArgs); const { statusCode, statusDescription, headers: responseHeaders, body } = invokeResponse; const response = { get statusCode() { return statusCode; }, get statusDescription() { return statusDescription; }, get headers() { return responseHeaders; }, get body() { return body; }, get bodyAsJson() { return JSON.parse(body.toString()); } }; if (limitToken !== null) { limitToken.commit(); } return response; } catch (e) { if (limitToken !== null) { if (e instanceof f_http_client_js_1.FHttpClient.CommunicationError) { // Token was not spent due server side did not do any jobs limitToken.rollback(); } else { limitToken.commit(); } } throw e; } } async onDispose() { if (this._limitHandle !== undefined) { if (this._limitHandle.isOwnInstance) { await this._limitHandle.instance.dispose(); } } } } exports.FWebClient = FWebClient; //# sourceMappingURL=f_web_client.js.map