58 lines
1.1 KiB
TypeScript
58 lines
1.1 KiB
TypeScript
![]() |
namespace _wiki_js_cli.helpers.http
|
||
|
{
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export type type_http_request = {
|
||
|
target : string;
|
||
|
method : string;
|
||
|
headers : Record<string, string>;
|
||
|
body : string;
|
||
|
};
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export type type_http_response = {
|
||
|
status_code : int;
|
||
|
headers : Record<string, string>;
|
||
|
body : string;
|
||
|
};
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export async function call(
|
||
|
http_request : type_http_request
|
||
|
) : Promise<type_http_response>
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.debug,
|
||
|
"http_call_request",
|
||
|
http_request
|
||
|
);
|
||
|
const fetch_request = new Request(
|
||
|
http_request.target,
|
||
|
{
|
||
|
"method": http_request.method,
|
||
|
"headers": http_request.headers,
|
||
|
"body": http_request.body,
|
||
|
}
|
||
|
);
|
||
|
const fetch_response = await fetch(fetch_request);
|
||
|
const http_response = {
|
||
|
"status_code": fetch_response.status,
|
||
|
"headers": Object.fromEntries(fetch_response.headers.entries()),
|
||
|
"body": await fetch_response.text(),
|
||
|
};
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.debug,
|
||
|
"http_call_response",
|
||
|
http_response
|
||
|
);
|
||
|
return http_response;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|