mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-01 13:20:41 +01:00
Compare commits
2 Commits
renovate/n
...
pkuanvil-u
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66354902ec | ||
|
|
3b9be63dc0 |
@@ -7,36 +7,32 @@ import { confirm } from 'bootbox';
|
||||
|
||||
const baseUrl = config.relative_path + '/api/v3';
|
||||
|
||||
function call(options, callback) {
|
||||
async function call(options, callback) {
|
||||
options.url = options.url.startsWith('/api') ?
|
||||
config.relative_path + options.url :
|
||||
baseUrl + options.url;
|
||||
|
||||
if (typeof callback === 'function') {
|
||||
xhr(options, callback);
|
||||
xhr(options).then(result => callback(null, result), err => callback(err));
|
||||
return;
|
||||
}
|
||||
|
||||
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');
|
||||
}
|
||||
});
|
||||
try {
|
||||
const result = await xhr(options);
|
||||
return result;
|
||||
} catch (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);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
async function xhr(options, cb) {
|
||||
async function xhr(options) {
|
||||
// Normalize body based on type
|
||||
const { url } = options;
|
||||
delete options.url;
|
||||
@@ -79,16 +75,14 @@ async function xhr(options, cb) {
|
||||
|
||||
if (!res.ok) {
|
||||
if (response) {
|
||||
return cb(new Error(isJSON ? response.status.message : response));
|
||||
throw new Error(isJSON ? response.status.message : response);
|
||||
}
|
||||
return cb(new Error(res.statusText));
|
||||
throw new Error(res.statusText);
|
||||
}
|
||||
|
||||
cb(null, (
|
||||
isJSON && response && response.hasOwnProperty('status') && response.hasOwnProperty('response') ?
|
||||
response.response :
|
||||
response
|
||||
));
|
||||
return isJSON && response && response.hasOwnProperty('status') && response.hasOwnProperty('response') ?
|
||||
response.response :
|
||||
response;
|
||||
}
|
||||
|
||||
export function get(route, data, onSuccess) {
|
||||
|
||||
Reference in New Issue
Block a user