| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- /**
- * Crowi context class for client
- */
- import axios from 'axios'
- export default class Crowi {
- constructor(context, window) {
- this.context = context;
- this.location = window.location || {};
- this.document = window.document || {};
- this.apiGet = this.apiGet.bind(this);
- this.apiPost = this.apiPost.bind(this);
- this.apiRequest = this.apiRequest.bind(this);
- }
- apiGet(path, params) {
- return this.apiRequest('get', path, params);
- }
- apiPost(path, params) {
- return this.apiRequest('post', path, params);
- }
- apiRequest(method, path, params) {
- return new Promise((resolve, reject) => {
- axios[method](`/_api${path}`, {params})
- .then(res => {
- if (res.data.ok) {
- resolve(res.data);
- } else {
- // FIXME?
- throw new Error(res.data);
- }
- }).catch(res => {
- // FIXME?
- throw new Error(res);
- });
- });
- }
- static escape (html, encode) {
- return html
- .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
- .replace(/</g, '<')
- .replace(/>/g, '>')
- .replace(/"/g, '"')
- .replace(/'/g, ''');
- }
- static unescape(html) {
- return html.replace(/&([#\w]+);/g, function(_, n) {
- n = n.toLowerCase();
- if (n === 'colon') return ':';
- if (n.charAt(0) === '#') {
- return n.charAt(1) === 'x'
- ? String.fromCharCode(parseInt(n.substring(2), 16))
- : String.fromCharCode(+n.substring(1));
- }
- return '';
- });
- }
- }
|