Can send requests to server

This commit is contained in:
2021-05-11 19:03:19 +02:00
parent e9c631e78a
commit b1929fc21f
6 changed files with 116 additions and 35 deletions

36
src/helpers/APIHelper.ts Normal file
View File

@ -0,0 +1,36 @@
/**
* API request helper
*
* @author Pierre Hubert
*/
import { ConfigHelper } from "./ConfigHelper";
/**
* Perform an API request
*
* @param uri Target URI on the server
* @param args Arguments to include in the request
* @returns The result of the request, in case of success,
* @throws An exception in case of failure
*/
export async function serverRequest(uri: string, args?: any): Promise<any> {
const requestArguments = args || {};
const fd = new FormData();
for (let arg in requestArguments) {
if (requestArguments.hasOwnProperty(arg))
fd.append(arg, requestArguments[arg]);
}
// TODO : add access token, once supported
const result = await (
await fetch((await ConfigHelper.apiURL()) + uri, {
method: "POST",
body: fd,
})
).json();
return result;
}

View File

@ -1,16 +1,33 @@
/**
* Account helper
*
*
* @author Pierre Hubert
*/
import { serverRequest } from "./APIHelper";
export interface AuthOptions {
reset_token: string;
}
export class AccountHelper {
/**
* Check whether access token are available
* or not
*/
static get hasAccessToken() : boolean {
// TODO : implement support
return false;
}
}
/**
* Check whether access token are available
* or not
*/
static get hasAccessToken(): boolean {
// TODO : implement support
return false;
}
/**
* Get authentication options for a given email address
*
* @param mail Requested email
*/
static async getAuthOptions(mail: string): Promise<AuthOptions> {
return await serverRequest("accounts/auth_options", {
mail: mail,
});
}
}

View File

@ -0,0 +1,22 @@
/**
* Configuration helper
*
* @author Pierre Hubert
*/
let cache: any;
export class ConfigHelper {
static async load() {
if (!cache)
cache = JSON.parse(await (await fetch("/config.json")).text());
}
/**
* Get server API url
*/
static async apiURL(): Promise<string> {
await this.load();
return cache.api_url;
}
}