UNPKG

gpudeploy

Version:

Utilities for creating APIs

89 lines (88 loc) 3.95 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const axios_1 = __importDefault(require("axios")); const error_1 = require("./error"); class APIBase { /** * @param param0 * @param param0.baseURL - The base URL for the API * @param param0.reauthenticate - Whether or not to reauthenticate automatically when the access token expires */ constructor({ baseURL = "https://api.gpudeploy.com/v1", reauthenticate = true }) { this.accessToken = null; this.refreshToken = null; this.baseURL = baseURL; this.autoReauth = reauthenticate; } request_(args) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; const { method = "GET", path, unauthenticated = false } = args; const url = `${this.baseURL}/${path.replace(/^\//, "")}`; let headers = { "Content-Type": "application/json" }; if (!unauthenticated && this.accessToken) headers = Object.assign(Object.assign({}, headers), { Authorization: `Bearer ${this.accessToken}` }); const data = method !== "GET" ? args.data : undefined; const params = method === "GET" ? args.data : undefined; let response; try { response = yield axios_1.default.request({ data, headers, method, params, url }); } catch (error) { if (((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.code) === error_1.ERROR_CODES.authenticationInvalid && headers.Authorization && args.reauthenticate && this.autoReauth && this.refreshToken) { yield this.reauthenticate({ refreshToken: this.refreshToken }); return this.request_(Object.assign(Object.assign({}, args), { reauthenticate: false })); } throw error; } return response.data; }); } request(params) { return __awaiter(this, void 0, void 0, function* () { return yield this.request_(params); }); } authenticate(data) { return __awaiter(this, void 0, void 0, function* () { const response = yield this.request({ unauthenticated: true, data, method: "POST", path: "/auth", }); this.accessToken = response.accessToken; this.refreshToken = response.refreshToken; return response; }); } reauthenticate() { return __awaiter(this, arguments, void 0, function* ({ refreshToken } = {}) { const data = { refreshToken: refreshToken || this.refreshToken }; const response = yield this.request({ unauthenticated: true, data, method: "POST", path: "/reauth", }); return response; }); } } exports.default = APIBase;