namespace _espe.backend { /** */ var _session_key_chest : lib_plankton.storage.type_chest; /** * @todo use backend call */ export async function logged_in( ) : Promise { let session_key : (null | string); try { session_key = (await _session_key_chest.read("session_key")); } catch (error) { session_key = null; } return Promise.resolve(session_key !== null); } /** */ export function init( ) : Promise { _session_key_chest = lib_plankton.storage.localstorage.implementation_chest( { "corner": "aum", } ); return Promise.resolve(undefined); } /** */ async function abstract_call( http_method : string, path : string, options : { data ?: type_input; } = {} ) : Promise { options = Object.assign( { "data": null, }, options ); let session_key : (null | string); try { session_key = (await _session_key_chest.read("session_key")); } catch (error) { session_key = null; } const without_content : boolean = ["HEAD","OPTIONS","GET"].includes(http_method); return ( fetch( lib_plankton.string.coin( ("{{scheme}}://{{host}}:{{port}}{{path_base}}{{path_action}}"), { "scheme": _espe.conf.get().backend.scheme, "host": _espe.conf.get().backend.host, "port": _espe.conf.get().backend.port.toFixed(0), "path_base": _espe.conf.get().backend.path_base, "path_action": path, } ), { "method": http_method, "headers": Object.assign( {}, ((! without_content) ? {"Content-Type": "application/json"} : {}), ((session_key !== null) ? {"X-Session-Key": session_key} : {}) ), "body": ( without_content ? null : JSON.stringify(options.data) ), } ) .then( x => { if ((x.status >= 200) && (x.status < 300)) { return x.json(); } else { return Promise.reject(new Error("unexpected response status code")); } } ) ); } /** */ export async function login( name : string, password : string ) : Promise { const session_key : string = await abstract_call<{name : string; password : string;}, string>( "POST", "/session/begin", { "data": { "name": name, "password": password, } } ); await _session_key_chest.write("session_key", session_key); } /** */ export async function logout( ) : Promise { try { await abstract_call( "DELETE", "/session/end" ); } catch (error) { // do nothing } await _session_key_chest.delete("session_key"); } /** */ export async function email( receivers : Array, subject : string, content : string ) : Promise { return ( abstract_call( "POST", "/email", { "data": { "receivers": receivers, "subject": subject, "content": content, }, } ) ); } /** */ export async function verification_get( data : any ) : Promise { return ( abstract_call( "POST", "/verification/get", { "data": { "data": data, }, } ) ); } /** */ export async function verification_check( data : any, verification : string ) : Promise { return ( abstract_call( "POST", "/verification/check", { "data": { "data": data, "verification": verification, } } ) ); } /** */ export async function member_list( search_term : (null | string) ) : Promise< Array< { id : int; preview : { membership_number : string; name_real_value : string; name_real_index : int; }; } > > { return ( abstract_call( "GET", ("/member/list" + ((search_term === null) ? "" : ("?search_term=" + search_term))) ) ); } /** */ export async function member_get( id : int ) : Promise< { membership_number : string; name_real_value : string; name_real_index : int; email_address_private : (null | string); registered : boolean; enabled : boolean; email_use_veiled_address : boolean; email_use_nominal_address : boolean; email_redirect_to_private_address : boolean; email_allow_sending : boolean; password_set : boolean; } > { return abstract_call( "GET", ("/member/read/" + id.toFixed(0)) ); } /** */ export async function member_project( data : { membership_number : string; name_real_value : string; email_address_private : (null | string); } ) : Promise { return abstract_call( "POST", "/member/project", { "data": { "membership_number": data.membership_number, "name_real_value": data.name_real_value, "email_address_private": data.email_address_private, } } ); } /** */ export async function member_get2( id : int, key : string ) : Promise< { name_real_value : string; name_real_index : int; name_login : string; email_address_veiled : string; email_address_nominal : string; } > { return abstract_call( "GET", lib_plankton.string.coin( "/member/info/{{id}}?key={{key}}", { "id": id.toFixed(0), "key": key, } ) ); } /** */ export async function member_register( id : int, verification : string, data : { email_use_veiled_address : boolean; email_use_nominal_address : boolean; email_redirect_to_private_address : boolean; password : (null | string); } ) : Promise { return ( abstract_call( "POST", lib_plankton.string.coin( "/member/register/{{id}}?key={{key}}", { "id": id.toFixed(0), "key": verification, } ), { "data": data, } ) ); } /** */ export async function member_modify( id : int, data : { email_address_private : (null | string); registered : boolean; enabled : boolean; } ) : Promise { return abstract_call( "PATCH", ("/member/modify/" + id.toFixed(0)), { "data": data } ); } /** */ export async function member_summon( id : int, url : string ) : Promise { return ( abstract_call( "POST", ("/member/summon/" + id.toFixed(0)), { "data": { "url_template": url, }, } ) ); } }