mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-22 15:33:13 +01:00
chore: rewrite API module as ESM [breaking]
This commit is contained in:
@@ -1,72 +1,80 @@
|
||||
/* eslint-disable import/no-unresolved */
|
||||
|
||||
'use strict';
|
||||
|
||||
define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
||||
const api = {};
|
||||
const baseUrl = config.relative_path + '/api/v3';
|
||||
import { fire as fireHook } from 'hooks';
|
||||
import { confirm } from 'bootbox';
|
||||
|
||||
function call(options, callback) {
|
||||
options.url = options.url.startsWith('/api') ?
|
||||
config.relative_path + options.url :
|
||||
baseUrl + options.url;
|
||||
const baseUrl = config.relative_path + '/api/v3';
|
||||
|
||||
async function doAjax(cb) {
|
||||
// Allow options to be modified by plugins, etc.
|
||||
({ options } = await hooks.fire('filter:api.options', { options }));
|
||||
function call(options, callback) {
|
||||
options.url = options.url.startsWith('/api') ?
|
||||
config.relative_path + options.url :
|
||||
baseUrl + options.url;
|
||||
|
||||
$.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));
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
if (typeof callback === 'function') {
|
||||
xhr(options, callback);
|
||||
return;
|
||||
}
|
||||
|
||||
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)) : ''),
|
||||
}, onSuccess);
|
||||
}
|
||||
|
||||
api.head = (route, payload, onSuccess) => call({
|
||||
export function head(route, payload, onSuccess) {
|
||||
call({
|
||||
url: route + (payload && Object.keys(payload).length ? ('?' + $.param(payload)) : ''),
|
||||
method: 'head',
|
||||
}, onSuccess);
|
||||
}
|
||||
|
||||
api.post = (route, payload, onSuccess) => call({
|
||||
export function post(route, payload, onSuccess) {
|
||||
call({
|
||||
url: route,
|
||||
method: 'post',
|
||||
data: JSON.stringify(payload || {}),
|
||||
@@ -75,8 +83,10 @@ define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
||||
'x-csrf-token': config.csrf_token,
|
||||
},
|
||||
}, onSuccess);
|
||||
}
|
||||
|
||||
api.patch = (route, payload, onSuccess) => call({
|
||||
export function patch(route, payload, onSuccess) {
|
||||
call({
|
||||
url: route,
|
||||
method: 'patch',
|
||||
data: JSON.stringify(payload || {}),
|
||||
@@ -85,8 +95,10 @@ define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
||||
'x-csrf-token': config.csrf_token,
|
||||
},
|
||||
}, onSuccess);
|
||||
}
|
||||
|
||||
api.put = (route, payload, onSuccess) => call({
|
||||
export function put(route, payload, onSuccess) {
|
||||
call({
|
||||
url: route,
|
||||
method: 'put',
|
||||
data: JSON.stringify(payload || {}),
|
||||
@@ -95,8 +107,10 @@ define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
||||
'x-csrf-token': config.csrf_token,
|
||||
},
|
||||
}, onSuccess);
|
||||
}
|
||||
|
||||
api.del = (route, payload, onSuccess) => call({
|
||||
export function del(route, payload, onSuccess) {
|
||||
call({
|
||||
url: route,
|
||||
method: 'delete',
|
||||
data: JSON.stringify(payload),
|
||||
@@ -105,7 +119,4 @@ define('api', ['hooks', 'bootbox'], (hooks, bootbox) => {
|
||||
'x-csrf-token': config.csrf_token,
|
||||
},
|
||||
}, onSuccess);
|
||||
api.delete = api.del;
|
||||
|
||||
return api;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user