@ -7,36 +7,32 @@ import { confirm } from 'bootbox';
const baseUrl = config . relative _path + '/api/v3' ;
const baseUrl = config . relative _path + '/api/v3' ;
function call ( options , callback ) {
async function call ( options , callback ) {
options . url = options . url . startsWith ( '/api' ) ?
options . url = options . url . startsWith ( '/api' ) ?
config . relative _path + options . url :
config . relative _path + options . url :
baseUrl + options . url ;
baseUrl + options . url ;
if ( typeof callback === 'function' ) {
if ( typeof callback === 'function' ) {
xhr ( options , callback) ;
xhr ( options ). then ( result => callback ( null , result) , err => callback( err ) ) ;
return ;
return ;
}
}
return new Promise ( ( resolve , reject ) => {
try {
xhr ( options , function ( err , data ) {
const result = await xhr ( options ) ;
if ( err ) {
return result ;
if ( err . message === 'A valid login session was not found. Please log in and try again.' ) {
} catch ( err ) {
return confirm ( '[[error:api.reauth-required]]' , ( ok ) => {
if ( err . message === 'A valid login session was not found. Please log in and try again.' ) {
if ( ok ) {
return confirm ( '[[error:api.reauth-required]]' , ( ok ) => {
ajaxify . go ( 'login' ) ;
if ( ok ) {
}
ajaxify . go ( 'login' ) ;
} ) ;
}
}
} ) ;
return reject ( err ) ;
}
}
throw err ;
}
resolve ( data ) ;
} ) ;
} ) ;
}
}
async function xhr ( options , cb ) {
async function xhr ( options ) {
// Normalize body based on type
// Normalize body based on type
const { url } = options ;
const { url } = options ;
delete options . url ;
delete options . url ;
@ -79,16 +75,14 @@ async function xhr(options, cb) {
if ( ! res . ok ) {
if ( ! res . ok ) {
if ( response ) {
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 , (
return isJSON && response && response . hasOwnProperty ( 'status' ) && response . hasOwnProperty ( 'response' ) ?
isJSON && response && response . hasOwnProperty ( 'status' ) && response . hasOwnProperty ( 'response' ) ?
response . response :
response . response :
response ;
response
) ) ;
}
}
export function get ( route , data , onSuccess ) {
export function get ( route , data , onSuccess ) {