mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-19 08:02:55 +00:00
refactor: split api into api, api-cli & api-plugin
This commit is contained in:
@@ -12,75 +12,34 @@
|
||||
* 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 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 Omit<OptionsOfJSONResponseBody, 'json' | 'body'> {
|
||||
/**
|
||||
* Body of the request
|
||||
*/
|
||||
// TODO: Use a specific type?
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
body?: any;
|
||||
/**
|
||||
* Target URL of the request
|
||||
*/
|
||||
url: URL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Response with generic for the type of body that is returned from the request
|
||||
*/
|
||||
export interface Response<TYPE_OF_BODY> extends GotResponse {
|
||||
/**
|
||||
* Typed body of the response
|
||||
*/
|
||||
body: TYPE_OF_BODY;
|
||||
}
|
||||
import type {HttpClientInterface, HttpClientRequest, HttpClientResponse} from './http-client-interface.js';
|
||||
import type {SCResponses} from '@openstapps/core';
|
||||
|
||||
/**
|
||||
* HTTP client that is based on request
|
||||
*/
|
||||
export class HttpClient {
|
||||
export class HttpClient implements HttpClientInterface {
|
||||
/**
|
||||
* Make a request
|
||||
*
|
||||
* @param requestConfig Configuration of the request
|
||||
*/
|
||||
async request<TYPE_OF_BODY>(requestConfig: RequestOptions): Promise<Response<TYPE_OF_BODY>> {
|
||||
const parameters: OptionsOfJSONResponseBody = {
|
||||
followRedirect: true,
|
||||
method: 'GET',
|
||||
responseType: 'json',
|
||||
async request<T extends SCResponses>(requestConfig: HttpClientRequest): Promise<HttpClientResponse<T>> {
|
||||
const parameters: RequestInit = {
|
||||
method: requestConfig.method ?? 'GET',
|
||||
headers: requestConfig.headers ?? {},
|
||||
};
|
||||
|
||||
if (typeof requestConfig.body !== 'undefined') {
|
||||
parameters.json = requestConfig.body;
|
||||
if (requestConfig.body !== undefined) {
|
||||
(parameters.headers as Record<string, string>)['Content-Type'] = 'application/json';
|
||||
parameters.body = JSON.stringify(requestConfig.body);
|
||||
}
|
||||
|
||||
if (typeof requestConfig.headers !== 'undefined') {
|
||||
parameters.headers = requestConfig.headers;
|
||||
}
|
||||
const response = await fetch(requestConfig.url, parameters);
|
||||
|
||||
if (typeof requestConfig.method !== 'undefined') {
|
||||
parameters.method = requestConfig.method;
|
||||
}
|
||||
let response: Response<TYPE_OF_BODY>;
|
||||
try {
|
||||
response = await got(requestConfig.url.toString(), parameters);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
if (typeof (error as any).response === 'undefined') {
|
||||
throw error;
|
||||
}
|
||||
// if there is a response (e.g. response with statusCode 404 etc.) provide it
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
response = (error as any).response as Response<TYPE_OF_BODY>;
|
||||
}
|
||||
|
||||
return response;
|
||||
return {
|
||||
body: await response.json().catch(() => response.text().catch(() => undefined)),
|
||||
headers: response.headers,
|
||||
statusCode: response.status,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user