koblog/bl-kernel/js/api.js

113 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-11-01 11:55:34 +01:00
class API {
2020-11-30 22:00:54 +01:00
constructor(apiURL, apiToken, apiAuth) {
this.apiURL = "http://localhost:9000/api/";
this.body = {
token: '45643a4071fad6a12261bb0763550feb',
authentication: '18a8410f0043d004c2e87f404170e112'
}
}
2020-11-01 11:55:34 +01:00
2020-11-30 22:00:54 +01:00
async createPage(args) {
var url = this.apiURL + "pages";
var body = Object.assign({}, this.body, args);
try {
var response = await fetch(url, {
credentials: "same-origin",
method: "POST",
body: JSON.stringify(body),
headers: new Headers({
"Content-Type": "application/json"
})
});
var json = await response.json();
return json.data.key;
} catch (err) {
console.log(err);
return true;
}
}
2020-11-01 11:55:34 +01:00
2020-11-30 22:00:54 +01:00
/*
Save page fields
2020-11-01 11:55:34 +01:00
2020-11-30 22:00:54 +01:00
$args['pageKey'] string Page key for the page to edit
$args array Arguments can be any of the fields from a page
2020-11-01 11:55:34 +01:00
2020-11-30 22:00:54 +01:00
returns string New page key
*/
async savePage(args) {
var url = this.apiURL + "pages/" + args['pageKey'];
var body = Object.assign({}, this.body, args);
try {
var response = await fetch(url, {
credentials: "same-origin",
method: "PUT",
body: JSON.stringify(body),
headers: new Headers({
"Content-Type": "application/json"
})
});
var json = await response.json();
return json.data.key;
} catch (err) {
console.log(err);
return true;
}
}
2020-11-01 11:55:34 +01:00
2020-11-30 22:00:54 +01:00
/*
Generates unique slug text for the a page
2020-11-01 11:55:34 +01:00
2020-11-30 22:00:54 +01:00
$args['pageKey'] string Page key for the page to generate the slug url
$args['text'] string Text that you want to generate the slug url
$args['parentKey'] string Parent page key if the page has one, if not empty string
2020-11-01 11:55:34 +01:00
2020-11-30 22:00:54 +01:00
returns string Slug text
*/
async friendlyURL(args) {
var url = this.apiURL + "helper/friendly-url/";
var parameters = "?token=" + this.body.token + "&authentication=" + this.body.authentication;
parameters = parameters + "&pageKey=" + args['pageKey'];
parameters = parameters + "&text=" + args['text'];
parameters = parameters + "&parentKey=" + args['parentKey'];
try {
const response = await fetch(url + parameters, {
method: "GET"
});
var json = await response.json();
return json.data;
} catch (err) {
console.log(err);
return true;
}
}
2020-11-01 11:55:34 +01:00
2020-11-30 22:00:54 +01:00
/*
Save settings
$args array Arguments can be any of the fields from settings
returns array
*/
async saveSettings(args) {
var url = this.apiURL + "settings";
var body = Object.assign({}, this.body, args);
try {
var response = await fetch(url, {
credentials: "same-origin",
method: "PUT",
body: JSON.stringify(body),
headers: new Headers({
"Content-Type": "application/json"
})
});
var json = await response.json();
return json.data;
} catch (err) {
console.log(err);
return true;
}
}
}