187 lines
3.2 KiB
TypeScript
187 lines
3.2 KiB
TypeScript
/**
|
|
*/
|
|
namespace _zeitbild.frontend_web.backend
|
|
{
|
|
|
|
/**
|
|
*/
|
|
var _session_key : (null | string) = null;
|
|
|
|
|
|
/**
|
|
*/
|
|
async function call(
|
|
method : lib_plankton.http.enum_method,
|
|
action : string,
|
|
input : (null | any)
|
|
) : Promise<any>
|
|
{
|
|
const with_body : boolean = (
|
|
[
|
|
lib_plankton.http.enum_method.post,
|
|
lib_plankton.http.enum_method.put,
|
|
lib_plankton.http.enum_method.patch,
|
|
].includes(method)
|
|
);
|
|
const http_request : lib_plankton.http.type_request = {
|
|
"version": "HTTP/2",
|
|
"scheme": (
|
|
(_zeitbild.frontend_web.conf.get()["backend"]["scheme"] === "http")
|
|
?
|
|
"http"
|
|
:
|
|
"https"
|
|
),
|
|
"host": lib_plankton.string.coin(
|
|
"{{host}}:{{port}}",
|
|
{
|
|
"host": _zeitbild.frontend_web.conf.get()["backend"]["host"],
|
|
"port": _zeitbild.frontend_web.conf.get()["backend"]["port"].toFixed(0),
|
|
}
|
|
),
|
|
"path": lib_plankton.string.coin(
|
|
"{{base}}{{action}}",
|
|
{
|
|
"base": _zeitbild.frontend_web.conf.get()["backend"]["path"],
|
|
"action": action,
|
|
}
|
|
),
|
|
"method": method,
|
|
"query": (
|
|
(with_body || (input === null))
|
|
?
|
|
null
|
|
:
|
|
("?" + lib_plankton.www_form.encode(input))
|
|
),
|
|
"headers": Object.assign(
|
|
{},
|
|
(
|
|
(! with_body)
|
|
?
|
|
{}
|
|
:
|
|
{"Content-Type": "application/json"}
|
|
),
|
|
(
|
|
(_session_key === null)
|
|
?
|
|
{}
|
|
:
|
|
{"X-Session-Key": _session_key}
|
|
)
|
|
),
|
|
"body": (
|
|
((! with_body) || (input === null))
|
|
?
|
|
null
|
|
:
|
|
/*Buffer.from*/(lib_plankton.json.encode(input))
|
|
),
|
|
};
|
|
const http_response : lib_plankton.http.type_response = await lib_plankton.http.call(http_request);
|
|
const output : any = lib_plankton.json.decode(http_response.body.toString());
|
|
return Promise.resolve<any>(output);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
export async function session_begin(
|
|
name : string,
|
|
password : string
|
|
) : Promise<void>
|
|
{
|
|
_session_key = await call(
|
|
lib_plankton.http.enum_method.post,
|
|
"/session/begin",
|
|
{
|
|
"name": name,
|
|
"password": password,
|
|
}
|
|
);
|
|
return Promise.resolve<void>(undefined);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
export async function session_end(
|
|
) : Promise<void>
|
|
{
|
|
return call(
|
|
lib_plankton.http.enum_method.delete,
|
|
"/session/end",
|
|
null
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
*/
|
|
export async function calendar_list(
|
|
) : Promise<
|
|
Array<
|
|
{
|
|
key : type_calendar_id;
|
|
preview : {
|
|
name : string;
|
|
}
|
|
}
|
|
>
|
|
>
|
|
{
|
|
return call(
|
|
lib_plankton.http.enum_method.get,
|
|
"/calendars",
|
|
null
|
|
);
|
|
}
|
|
|
|
|
|
/**
|
|
* @todo prevent loops
|
|
*/
|
|
export async function events(
|
|
from_pit : _zeitbild.frontend_web.helpers.type_pit,
|
|
to_pit : _zeitbild.frontend_web.helpers.type_pit,
|
|
options : {
|
|
calendar_ids ?: (null | Array<type_calendar_id>);
|
|
} = {}
|
|
) : Promise<
|
|
Array<
|
|
{
|
|
calendar_id : type_calendar_id;
|
|
calendar_name : string;
|
|
event : type_event;
|
|
}
|
|
>
|
|
>
|
|
{
|
|
options = Object.assign(
|
|
{
|
|
"calendar_ids": null,
|
|
},
|
|
options
|
|
);
|
|
|
|
return call(
|
|
lib_plankton.http.enum_method.get,
|
|
"/events",
|
|
Object.assign(
|
|
{
|
|
"from": from_pit,
|
|
"to": to_pit,
|
|
},
|
|
(
|
|
(options.calendar_ids === null)
|
|
?
|
|
{}
|
|
:
|
|
{"calendar_ids": options.calendar_ids.join(",")}
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
}
|