mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-05-07 22:06:07 +02:00
chore: rewrite API module as ESM [breaking]
This commit is contained in:
@@ -1,72 +1,80 @@
|
|||||||
|
/* eslint-disable import/no-unresolved */
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
import { fire as fireHook } from 'hooks';
|
||||||
const api = {};
|
import { confirm } from 'bootbox';
|
||||||
const baseUrl = config.relative_path + '/api/v3';
|
|
||||||
|
|
||||||
function call(options, callback) {
|
const baseUrl = config.relative_path + '/api/v3';
|
||||||
options.url = options.url.startsWith('/api') ?
|
|
||||||
config.relative_path + options.url :
|
|
||||||
baseUrl + options.url;
|
|
||||||
|
|
||||||
async function doAjax(cb) {
|
function call(options, callback) {
|
||||||
// Allow options to be modified by plugins, etc.
|
options.url = options.url.startsWith('/api') ?
|
||||||
({ options } = await hooks.fire('filter:api.options', { options }));
|
config.relative_path + options.url :
|
||||||
|
baseUrl + options.url;
|
||||||
|
|
||||||
$.ajax(options)
|
if (typeof callback === 'function') {
|
||||||
.done((res) => {
|
xhr(options, callback);
|
||||||
cb(null, (
|
return;
|
||||||
res &&
|
|
||||||
res.hasOwnProperty('status') &&
|
|
||||||
res.hasOwnProperty('response') ? res.response : (res || {})
|
|
||||||
));
|
|
||||||
})
|
|
||||||
.fail((ev) => {
|
|
||||||
let errMessage;
|
|
||||||
if (ev.responseJSON) {
|
|
||||||
errMessage = ev.responseJSON.status && ev.responseJSON.status.message ?
|
|
||||||
ev.responseJSON.status.message :
|
|
||||||
ev.responseJSON.error;
|
|
||||||
}
|
|
||||||
|
|
||||||
cb(new Error(errMessage || ev.statusText));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (typeof callback === 'function') {
|
|
||||||
doAjax(callback);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
doAjax(function (err, data) {
|
|
||||||
if (err) {
|
|
||||||
if (err.message === 'A valid login session was not found. Please log in and try again.') {
|
|
||||||
return bootbox.confirm('[[error:api.reauth-required]]', (ok) => {
|
|
||||||
if (ok) {
|
|
||||||
ajaxify.go('login');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return reject(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
resolve(data);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
api.get = (route, payload, onSuccess) => call({
|
return new Promise((resolve, reject) => {
|
||||||
|
xhr(options, function (err, data) {
|
||||||
|
if (err) {
|
||||||
|
if (err.message === 'A valid login session was not found. Please log in and try again.') {
|
||||||
|
return confirm('[[error:api.reauth-required]]', (ok) => {
|
||||||
|
if (ok) {
|
||||||
|
ajaxify.go('login');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return reject(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
resolve(data);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function xhr(options, cb) {
|
||||||
|
// Allow options to be modified by plugins, etc.
|
||||||
|
({ options } = await fireHook('filter:api.options', { options }));
|
||||||
|
|
||||||
|
$.ajax(options)
|
||||||
|
.done((res) => {
|
||||||
|
cb(null, (
|
||||||
|
res &&
|
||||||
|
res.hasOwnProperty('status') &&
|
||||||
|
res.hasOwnProperty('response') ? res.response : (res || {})
|
||||||
|
));
|
||||||
|
})
|
||||||
|
.fail((ev) => {
|
||||||
|
let errMessage;
|
||||||
|
if (ev.responseJSON) {
|
||||||
|
errMessage = ev.responseJSON.status && ev.responseJSON.status.message ?
|
||||||
|
ev.responseJSON.status.message :
|
||||||
|
ev.responseJSON.error;
|
||||||
|
}
|
||||||
|
|
||||||
|
cb(new Error(errMessage || ev.statusText));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function get(route, payload, onSuccess) {
|
||||||
|
call({
|
||||||
url: route + (payload && Object.keys(payload).length ? ('?' + $.param(payload)) : ''),
|
url: route + (payload && Object.keys(payload).length ? ('?' + $.param(payload)) : ''),
|
||||||
}, onSuccess);
|
}, onSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
api.head = (route, payload, onSuccess) => call({
|
export function head(route, payload, onSuccess) {
|
||||||
|
call({
|
||||||
url: route + (payload && Object.keys(payload).length ? ('?' + $.param(payload)) : ''),
|
url: route + (payload && Object.keys(payload).length ? ('?' + $.param(payload)) : ''),
|
||||||
method: 'head',
|
method: 'head',
|
||||||
}, onSuccess);
|
}, onSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
api.post = (route, payload, onSuccess) => call({
|
export function post(route, payload, onSuccess) {
|
||||||
|
call({
|
||||||
url: route,
|
url: route,
|
||||||
method: 'post',
|
method: 'post',
|
||||||
data: JSON.stringify(payload || {}),
|
data: JSON.stringify(payload || {}),
|
||||||
@@ -75,8 +83,10 @@ define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
|||||||
'x-csrf-token': config.csrf_token,
|
'x-csrf-token': config.csrf_token,
|
||||||
},
|
},
|
||||||
}, onSuccess);
|
}, onSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
api.patch = (route, payload, onSuccess) => call({
|
export function patch(route, payload, onSuccess) {
|
||||||
|
call({
|
||||||
url: route,
|
url: route,
|
||||||
method: 'patch',
|
method: 'patch',
|
||||||
data: JSON.stringify(payload || {}),
|
data: JSON.stringify(payload || {}),
|
||||||
@@ -85,8 +95,10 @@ define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
|||||||
'x-csrf-token': config.csrf_token,
|
'x-csrf-token': config.csrf_token,
|
||||||
},
|
},
|
||||||
}, onSuccess);
|
}, onSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
api.put = (route, payload, onSuccess) => call({
|
export function put(route, payload, onSuccess) {
|
||||||
|
call({
|
||||||
url: route,
|
url: route,
|
||||||
method: 'put',
|
method: 'put',
|
||||||
data: JSON.stringify(payload || {}),
|
data: JSON.stringify(payload || {}),
|
||||||
@@ -95,8 +107,10 @@ define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
|||||||
'x-csrf-token': config.csrf_token,
|
'x-csrf-token': config.csrf_token,
|
||||||
},
|
},
|
||||||
}, onSuccess);
|
}, onSuccess);
|
||||||
|
}
|
||||||
|
|
||||||
api.del = (route, payload, onSuccess) => call({
|
export function del(route, payload, onSuccess) {
|
||||||
|
call({
|
||||||
url: route,
|
url: route,
|
||||||
method: 'delete',
|
method: 'delete',
|
||||||
data: JSON.stringify(payload),
|
data: JSON.stringify(payload),
|
||||||
@@ -105,7 +119,4 @@ define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
|||||||
'x-csrf-token': config.csrf_token,
|
'x-csrf-token': config.csrf_token,
|
||||||
},
|
},
|
||||||
}, onSuccess);
|
}, onSuccess);
|
||||||
api.delete = api.del;
|
}
|
||||||
|
|
||||||
return api;
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user