mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 00:23:03 +00:00
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2019 StApps
|
||||
* Copyright (C) 2019-2020 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
@@ -12,12 +12,18 @@
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
import request from 'request';
|
||||
import got, {OptionsOfJSONResponseBody, Response as GotResponse} from 'got';
|
||||
|
||||
/**
|
||||
* Request options that requires a url
|
||||
* Note: adjust request options of got library for backward compatibility
|
||||
*/
|
||||
export interface RequestOptions extends request.CoreOptions {
|
||||
export interface RequestOptions extends Omit<OptionsOfJSONResponseBody, 'json' | 'body'> {
|
||||
/**
|
||||
* Body of the request
|
||||
*/
|
||||
// tslint:disable-next-line:no-any TODO: Use a specific type?
|
||||
body?: any;
|
||||
/**
|
||||
* Target URL of the request
|
||||
*/
|
||||
@@ -27,7 +33,7 @@ export interface RequestOptions extends request.CoreOptions {
|
||||
/**
|
||||
* Response with generic for the type of body that is returned from the request
|
||||
*/
|
||||
export interface Response<TYPE_OF_BODY> extends request.Response {
|
||||
export interface Response<TYPE_OF_BODY> extends GotResponse {
|
||||
/**
|
||||
* Typed body of the response
|
||||
*/
|
||||
@@ -46,33 +52,34 @@ export class HttpClient {
|
||||
async request<TYPE_OF_BODY>(
|
||||
requestConfig: RequestOptions,
|
||||
): Promise<Response<TYPE_OF_BODY>> {
|
||||
const params: request.CoreOptions = {
|
||||
body: {},
|
||||
followAllRedirects: true,
|
||||
json: true,
|
||||
const params: OptionsOfJSONResponseBody = {
|
||||
followRedirect: true,
|
||||
method: 'GET',
|
||||
responseType: 'json',
|
||||
};
|
||||
|
||||
if (typeof requestConfig.body !== 'undefined') {
|
||||
params.body = requestConfig.body;
|
||||
params.json = requestConfig.body;
|
||||
}
|
||||
|
||||
if (typeof requestConfig.headers !== 'undefined') {
|
||||
params.headers = requestConfig.headers;
|
||||
}
|
||||
|
||||
if (requestConfig.method !== 'GET') {
|
||||
if (typeof requestConfig.method !== 'undefined') {
|
||||
params.method = requestConfig.method;
|
||||
}
|
||||
let response: Response<TYPE_OF_BODY>;
|
||||
try {
|
||||
response = await got(requestConfig.url.toString(), params);
|
||||
} catch (err) {
|
||||
if (typeof err.response === 'undefined') {
|
||||
throw err;
|
||||
}
|
||||
// if there is a response (e.g. response with statusCode 404 etc.) provide it
|
||||
response = err.response;
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
request(requestConfig.url.toString(), params, (err, response) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
|
||||
resolve(response);
|
||||
});
|
||||
});
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user