library-ts/node/web/Request.ts

41 lines
862 B
TypeScript
Raw Normal View History

2025-11-11 13:13:39 +00:00
export class Request
{
static async getJSON<O>(url: string): Promise<O>
{
let result = await fetch( url,
{
method: 'GET',
headers: { 'Accept': 'application/json' }
}
);
if ( ! result.ok )
{
throw new Error( `GET ${url} failed: ${result.status} ${result.statusText}` );
}
return result.json() as Promise<O>;
}
static async postJSON<I, O>(url: string, input: I): Promise<O>
{
let result = await fetch(
url,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify( input )
});
if ( ! result.ok )
{
2025-11-11 13:24:34 +00:00
throw new Error( `POST ${url} failed: ${result.status} ${result.statusText} ${result.text()}` );
2025-11-11 13:13:39 +00:00
}
return result.json() as Promise<O>;
}
}