UNPKG

@updatedev/js

Version:

Update JavaScript SDK

128 lines 3.97 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var request_exports = {}; __export(request_exports, { RequestClient: () => RequestClient }); module.exports = __toCommonJS(request_exports); class RequestClient { constructor({ baseUrl, headers, getAuthorizationToken, baseBody }) { this.baseUrl = baseUrl || "https://api.update.dev/v1"; this.headers = this._getDefinedObject(headers); this.baseBody = this._getDefinedObject(baseBody); this.getAuthorizationToken = getAuthorizationToken; this.baseBody = baseBody; } _getDefinedObject(headers) { let definedHeaders = {}; if (headers) { Object.entries(headers).forEach(([key, value]) => { if (value !== void 0) { definedHeaders[key] = value; } }); } return definedHeaders; } _buildQueryString(params) { if (params == null) return ""; const queryString = new URLSearchParams(); Object.keys(params).forEach((key) => { if (params[key] == null) return; queryString.append(key, String(params[key])); }); return queryString.toString(); } async _execute(url, options) { try { const response = await fetch(url, options); return response.json(); } catch (error) { return { data: null, error: { message: "There was an error" }, status: "ERROR" }; } } async _requestCore(options) { const { method, endpoint } = options; let body = options.body; let queryParams = options.queryParams; let requestHeaders = options.headers; let url = `${this.baseUrl}${endpoint}`; if (method === "GET" && this.baseBody) { queryParams = { ...queryParams || {}, ...this.baseBody }; } if (method === "POST" && this.baseBody) { body = { ...body || {}, ...this.baseBody }; } if (method === "GET" && queryParams) { const queryString = this._buildQueryString(queryParams); url = `${url}?${queryString}`; } const headers = new Headers({ ...requestHeaders, ...this.headers || {} }); const requestOptions = { method, headers }; if (method === "POST" && body) { headers.set("Content-Type", "application/json"); requestOptions.body = JSON.stringify(body); } const { data, status, error } = await this._execute(url, requestOptions); if (status === "ERROR") { return { data: null, error, status }; } return { data, error: null, status }; } async request(options) { var _a; let requestOptions = { ...options }; if (((_a = requestOptions.extra) == null ? void 0 : _a.includeUser) != null && requestOptions.extra.includeUser && this.getAuthorizationToken) { const authorizationToken = await this.getAuthorizationToken(); if (authorizationToken) { requestOptions.headers = { ...requestOptions.headers, Authorization: `Bearer ${authorizationToken}` }; } } return this._requestCore(requestOptions); } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RequestClient }); //# sourceMappingURL=request.js.map