41 lines
862 B
TypeScript
41 lines
862 B
TypeScript
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 )
|
|
{
|
|
throw new Error( `POST ${url} failed: ${result.status} ${result.statusText} ${result.text()}` );
|
|
}
|
|
|
|
return result.json() as Promise<O>;
|
|
}
|
|
} |