216 lines
6.7 KiB
JavaScript
216 lines
6.7 KiB
JavaScript
"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 (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.request = void 0;
|
|
/* istanbul ignore file */
|
|
/* tslint:disable */
|
|
/* eslint-disable */
|
|
const form_data_1 = __importDefault(require("form-data"));
|
|
const node_fetch_1 = __importStar(require("node-fetch"));
|
|
const util_1 = require("util");
|
|
const ApiError_1 = require("./ApiError");
|
|
const OpenAPI_1 = require("./OpenAPI");
|
|
function isDefined(value) {
|
|
return value !== undefined && value !== null;
|
|
}
|
|
function isString(value) {
|
|
return typeof value === 'string';
|
|
}
|
|
function isStringWithValue(value) {
|
|
return isString(value) && value !== '';
|
|
}
|
|
function isBinary(value) {
|
|
const isBuffer = Buffer.isBuffer(value);
|
|
const isArrayBuffer = util_1.types.isArrayBuffer(value);
|
|
const isArrayBufferView = util_1.types.isArrayBufferView(value);
|
|
return isBuffer || isArrayBuffer || isArrayBufferView;
|
|
}
|
|
function getQueryString(params) {
|
|
const qs = [];
|
|
Object.keys(params).forEach(key => {
|
|
const value = params[key];
|
|
if (isDefined(value)) {
|
|
if (Array.isArray(value)) {
|
|
value.forEach(value => {
|
|
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
});
|
|
}
|
|
else {
|
|
qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
}
|
|
}
|
|
});
|
|
if (qs.length > 0) {
|
|
return `?${qs.join('&')}`;
|
|
}
|
|
return '';
|
|
}
|
|
function getUrl(options) {
|
|
const path = options.path.replace(/[:]/g, '_');
|
|
const url = `${OpenAPI_1.OpenAPI.BASE}${path}`;
|
|
if (options.query) {
|
|
return `${url}${getQueryString(options.query)}`;
|
|
}
|
|
return url;
|
|
}
|
|
function getFormData(params) {
|
|
const formData = new form_data_1.default();
|
|
Object.keys(params).forEach(key => {
|
|
const value = params[key];
|
|
if (isDefined(value)) {
|
|
formData.append(key, value);
|
|
}
|
|
});
|
|
return formData;
|
|
}
|
|
async function resolve(resolver) {
|
|
if (typeof resolver === 'function') {
|
|
return resolver();
|
|
}
|
|
return resolver;
|
|
}
|
|
async function getHeaders(options) {
|
|
const headers = new node_fetch_1.Headers({
|
|
Accept: 'application/json',
|
|
...OpenAPI_1.OpenAPI.HEADERS,
|
|
...options.headers,
|
|
});
|
|
const token = await resolve(OpenAPI_1.OpenAPI.TOKEN);
|
|
const username = await resolve(OpenAPI_1.OpenAPI.USERNAME);
|
|
const password = await resolve(OpenAPI_1.OpenAPI.PASSWORD);
|
|
if (isStringWithValue(token)) {
|
|
headers.append('Authorization', `Bearer ${token}`);
|
|
}
|
|
if (isStringWithValue(username) && isStringWithValue(password)) {
|
|
const credentials = Buffer.from(`${username}:${password}`).toString('base64');
|
|
headers.append('Authorization', `Basic ${credentials}`);
|
|
}
|
|
if (options.body) {
|
|
if (isBinary(options.body)) {
|
|
headers.append('Content-Type', 'application/octet-stream');
|
|
}
|
|
else if (isString(options.body)) {
|
|
headers.append('Content-Type', 'text/plain');
|
|
}
|
|
else {
|
|
headers.append('Content-Type', 'application/json');
|
|
}
|
|
}
|
|
return headers;
|
|
}
|
|
function getRequestBody(options) {
|
|
if (options.formData) {
|
|
return getFormData(options.formData);
|
|
}
|
|
if (options.body) {
|
|
if (isString(options.body) || isBinary(options.body)) {
|
|
return options.body;
|
|
}
|
|
else {
|
|
return JSON.stringify(options.body);
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
async function sendRequest(options, url) {
|
|
const request = {
|
|
method: options.method,
|
|
headers: await getHeaders(options),
|
|
body: getRequestBody(options),
|
|
};
|
|
return await (0, node_fetch_1.default)(url, request);
|
|
}
|
|
function getResponseHeader(response, responseHeader) {
|
|
if (responseHeader) {
|
|
const content = response.headers.get(responseHeader);
|
|
if (isString(content)) {
|
|
return content;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
async function getResponseBody(response) {
|
|
try {
|
|
const contentType = response.headers.get('Content-Type');
|
|
if (contentType) {
|
|
const isJSON = contentType.toLowerCase().startsWith('application/json');
|
|
if (isJSON) {
|
|
return await response.json();
|
|
}
|
|
else {
|
|
return await response.text();
|
|
}
|
|
}
|
|
}
|
|
catch (error) {
|
|
console.error(error);
|
|
}
|
|
return null;
|
|
}
|
|
function catchErrors(options, result) {
|
|
const errors = {
|
|
400: 'Bad Request',
|
|
401: 'Unauthorized',
|
|
403: 'Forbidden',
|
|
404: 'Not Found',
|
|
500: 'Internal Server Error',
|
|
502: 'Bad Gateway',
|
|
503: 'Service Unavailable',
|
|
...options.errors,
|
|
};
|
|
const error = errors[result.status];
|
|
if (error) {
|
|
throw new ApiError_1.ApiError(result, error);
|
|
}
|
|
if (!result.ok) {
|
|
throw new ApiError_1.ApiError(result, 'Generic Error');
|
|
}
|
|
}
|
|
/**
|
|
* Request using node-fetch client
|
|
* @param options The request options from the the service
|
|
* @returns ApiResult
|
|
* @throws ApiError
|
|
*/
|
|
async function request(options) {
|
|
const url = getUrl(options);
|
|
const response = await sendRequest(options, url);
|
|
const responseBody = await getResponseBody(response);
|
|
const responseHeader = getResponseHeader(response, options.responseHeader);
|
|
const result = {
|
|
url,
|
|
ok: response.ok,
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
body: responseHeader || responseBody,
|
|
};
|
|
catchErrors(options, result);
|
|
return result;
|
|
}
|
|
exports.request = request;
|