import { Response, Options } from "got"; const got = require("got"); const pkg = require("../../package.json"); export class AuthRequest { static readonly urlRoot: NonNullable = "https://app.lokalise.com/oauth2/"; static async createPromise( uri: string, method: Options["method"], body: object | object[] | null, host?: string ): Promise { const options: Options = { method: method, prefixUrl: host ?? this.urlRoot, headers: { Accept: "application/json", "User-Agent": `node-lokalise-api/${pkg.version}`, }, agent: false, throwHttpErrors: false, decompress: false, }; options["body"] = JSON.stringify(body); try { const response: Response = await got(uri, options); const responseJSON = JSON.parse(response.body); if (response.statusCode > 399) { return Promise.reject({ ...{ code: response.statusCode }, ...responseJSON, }); } return Promise.resolve({ json: responseJSON, headers: response.headers }); } catch (err) { /* istanbul ignore next */ return Promise.reject(err); } } }