mirror of
https://gitlab.com/comunic/comunicconsole
synced 2025-06-20 00:25:19 +00:00
Can send requests to server
This commit is contained in:
36
src/helpers/APIHelper.ts
Normal file
36
src/helpers/APIHelper.ts
Normal 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;
|
||||
}
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
22
src/helpers/ConfigHelper.ts
Normal file
22
src/helpers/ConfigHelper.ts
Normal 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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user