392 lines
9.1 KiB
TypeScript
392 lines
9.1 KiB
TypeScript
![]() |
namespace _wiki_js_cli.api
|
||
|
{
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
var _login_token : (null | string) = null;
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
var _url_base : (null | string) = null;
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export function init(
|
||
|
url_base : string
|
||
|
) : Promise<void>
|
||
|
{
|
||
|
_url_base = url_base;
|
||
|
return Promise.resolve<void>(undefined);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
async function call_generic_graphql(
|
||
|
graphql_query,
|
||
|
options
|
||
|
)
|
||
|
{
|
||
|
options = Object.assign(
|
||
|
{
|
||
|
"variables": {},
|
||
|
},
|
||
|
options
|
||
|
);
|
||
|
const http_request = {
|
||
|
"target": (_url_base + "/graphql"),
|
||
|
"method": "POST",
|
||
|
"headers": Object.assign(
|
||
|
{
|
||
|
"Content-Type": "application/json",
|
||
|
},
|
||
|
(
|
||
|
(_login_token === null)
|
||
|
?
|
||
|
{}
|
||
|
:
|
||
|
{"Cookie": ("jwt=" + _login_token)}
|
||
|
)
|
||
|
),
|
||
|
"body": JSON.stringify(
|
||
|
[
|
||
|
{
|
||
|
"operationName": null,
|
||
|
"variables": options.variables,
|
||
|
"extensions": {},
|
||
|
"query": graphql_query,
|
||
|
}
|
||
|
]
|
||
|
),
|
||
|
};
|
||
|
const http_response = await _wiki_js_cli.helpers.http.call(http_request);
|
||
|
const data = JSON.parse(http_response.body);
|
||
|
return Promise.resolve(data[0]["data"]);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export async function call_finalize(
|
||
|
admin_email : string,
|
||
|
admin_password : string,
|
||
|
site_url : string,
|
||
|
telemetry : boolean
|
||
|
) : Promise<void>
|
||
|
{
|
||
|
const http_request = {
|
||
|
"target": (_wiki_js_cli.conf.get().api.url_base + "/finalize"),
|
||
|
"method": "POST",
|
||
|
"headers": {
|
||
|
"Content-Type": "application/json",
|
||
|
},
|
||
|
"body": JSON.stringify(
|
||
|
{
|
||
|
"adminEmail": admin_email,
|
||
|
"adminPassword": admin_password,
|
||
|
"adminPasswordConfirm": admin_password,
|
||
|
"siteUrl": site_url,
|
||
|
"telemetry": telemetry,
|
||
|
}
|
||
|
),
|
||
|
};
|
||
|
const http_response = await _wiki_js_cli.helpers.http.call(http_request);
|
||
|
const data = JSON.parse(http_response.body);
|
||
|
return Promise.resolve(undefined);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* executes a local login
|
||
|
*/
|
||
|
export async function call_login_local(
|
||
|
username : string,
|
||
|
password : string
|
||
|
) : Promise<void>
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_login_local",
|
||
|
{
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"mutation ($username: String!, $password: String!, $strategy: String!) {authentication {login(username: $username, password: $password, strategy: $strategy) {responseResult {succeeded errorCode slug message __typename} jwt mustChangePwd mustProvideTFA mustSetupTFA continuationToken redirect tfaQRImage __typename} __typename}}",
|
||
|
{
|
||
|
"variables": {
|
||
|
"strategy": "local",
|
||
|
"username": username,
|
||
|
"password": password,
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
.then(
|
||
|
(data) => {
|
||
|
if (data["authentication"]["login"]["responseResult"]["succeeded"]) {
|
||
|
_login_token = data["authentication"]["login"]["jwt"];
|
||
|
return Promise.resolve<void>(undefined);
|
||
|
}
|
||
|
else {
|
||
|
return Promise.reject(new Error("login failed"));
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export function call_email_settings_set(
|
||
|
settings
|
||
|
)
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_email_settings_set",
|
||
|
{
|
||
|
"settings": settings,
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"mutation ($senderName: String!, $senderEmail: String!, $host: String!, $port: Int!, $name: String!, $secure: Boolean!, $verifySSL: Boolean!, $user: String!, $pass: String!, $useDKIM: Boolean!, $dkimDomainName: String!, $dkimKeySelector: String!, $dkimPrivateKey: String!) {mail {updateConfig(senderName: $senderName, senderEmail: $senderEmail, host: $host, port: $port, name: $name, secure: $secure, verifySSL: $verifySSL, user: $user, pass: $pass, useDKIM: $useDKIM, dkimDomainName: $dkimDomainName, dkimKeySelector: $dkimKeySelector, dkimPrivateKey: $dkimPrivateKey) {responseResult {succeeded errorCode slug message __typename} __typename} __typename}}",
|
||
|
{
|
||
|
"variables": {
|
||
|
"senderName": settings.sender_name,
|
||
|
"senderEmail": settings.sender_email_address,
|
||
|
"host": settings.smtp_host,
|
||
|
"port": settings.smtp_port,
|
||
|
"name": settings.name,
|
||
|
"secure": settings.secure,
|
||
|
"verifySSL": settings.verify_ssl,
|
||
|
"user": settings.smtp_username,
|
||
|
"pass": settings.smtp_password,
|
||
|
"useDKIM": settings.use_dkim,
|
||
|
"dkimDomainName": settings.dkim_domain_name,
|
||
|
"dkimKeySelector": settings.dkim_key_selector,
|
||
|
"dkimPrivateKey": settings.dkim_private_key,
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export function call_locale_download(
|
||
|
locale
|
||
|
)
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_locale_download",
|
||
|
{
|
||
|
"locale": locale,
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"mutation ($locale: String!) {localization {downloadLocale(locale: $locale) {responseResult {succeeded errorCode slug message __typename} __typename}__typename}}",
|
||
|
{
|
||
|
"variables": {
|
||
|
"locale": locale,
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export function call_group_list(
|
||
|
name : string
|
||
|
) : Promise<any>
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_group_list",
|
||
|
{
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"{groups {list {id name isSystem userCount createdAt updatedAt __typename} __typename}}",
|
||
|
{
|
||
|
"variables": {
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export function call_group_create(
|
||
|
name : string
|
||
|
) : Promise<int>
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_group_create",
|
||
|
{
|
||
|
"name": name,
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"mutation ($name: String!) {groups {create(name: $name) {responseResult {succeeded errorCode slug message __typename} group {id name createdAt updatedAt __typename} __typename} __typename}}",
|
||
|
{
|
||
|
"variables": {
|
||
|
"name": name,
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @param permissions_general {
|
||
|
* Array<
|
||
|
* string
|
||
|
* >
|
||
|
* }
|
||
|
* @param permissions_page_specific {
|
||
|
* Array<
|
||
|
* {
|
||
|
* id:string;
|
||
|
* path:string;
|
||
|
* roles:Array<
|
||
|
* string
|
||
|
* >;
|
||
|
* match:string;
|
||
|
* deny:boolean;
|
||
|
* locales:Array<
|
||
|
* string
|
||
|
* >;
|
||
|
* }
|
||
|
* >
|
||
|
* }
|
||
|
*/
|
||
|
export function call_group_update(
|
||
|
group_id,
|
||
|
name,
|
||
|
permissions_general,
|
||
|
permissions_page_specific
|
||
|
)
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_group_update",
|
||
|
{
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"mutation ($id: Int!, $name: String!, $redirectOnLogin: String!, $permissions: [String]!, $pageRules: [PageRuleInput]!) {groups {update(id: $id, name: $name, redirectOnLogin: $redirectOnLogin, permissions: $permissions, pageRules: $pageRules) {responseResult {succeeded errorCode slug message __typename} __typename} __typename}}",
|
||
|
{
|
||
|
"variables": {
|
||
|
"id": group_id,
|
||
|
"name": name,
|
||
|
"redirectOnLogin": "/",
|
||
|
"permissions": permissions_general,
|
||
|
"pageRules": permissions_page_specific,
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export function call_authentication_strategy_list(
|
||
|
)
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_authentication_strategy_list",
|
||
|
{
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"{authentication {activeStrategies {key strategy {key title description useForm logo website __typename} config {key value __typename} order isEnabled displayName selfRegistration domainWhitelist autoEnrollGroups __typename} __typename}}",
|
||
|
{
|
||
|
}
|
||
|
)
|
||
|
.then(
|
||
|
(data) => Promise.resolve(data["authentication"]["activeStrategies"])
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export function call_authentication_strategy_set(
|
||
|
strategies
|
||
|
)
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_authentication_strategy_set",
|
||
|
{
|
||
|
"strategies": strategies,
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"mutation ($strategies: [AuthenticationStrategyInput]!) {authentication {updateStrategies(strategies: $strategies) {responseResult {succeeded errorCode slug message __typename} __typename} __typename}}",
|
||
|
{
|
||
|
"variables": {
|
||
|
"strategies": strategies,
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
*/
|
||
|
export function call_theming_set(
|
||
|
dark_mode : boolean,
|
||
|
toc_position : ("left" | "right" | "off")
|
||
|
)
|
||
|
{
|
||
|
_wiki_js_cli.helpers.log.write(
|
||
|
_wiki_js_cli.helpers.log.enum_level.info,
|
||
|
"api_call_theming_set",
|
||
|
{
|
||
|
"dark_mode": dark_mode,
|
||
|
"toc_position": toc_position,
|
||
|
}
|
||
|
);
|
||
|
return (
|
||
|
call_generic_graphql(
|
||
|
"mutation ($theme: String!, $iconset: String!, $darkMode: Boolean!, $tocPosition: String, $injectCSS: String, $injectHead: String, $injectBody: String) {theming {setConfig(theme: $theme, iconset: $iconset, darkMode: $darkMode, tocPosition: $tocPosition, injectCSS: $injectCSS, injectHead: $injectHead, injectBody: $injectBody) {responseResult {succeeded errorCode slug message __typename} __typename} __typename}}",
|
||
|
{
|
||
|
"variables": {
|
||
|
"theme": "default",
|
||
|
"iconset": "mdi",
|
||
|
"darkMode": dark_mode,
|
||
|
"tocPosition": toc_position,
|
||
|
"injectCSS": "",
|
||
|
"injectHead": "",
|
||
|
"injectBody": ""
|
||
|
}
|
||
|
}
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
}
|