2020-04-05 00:57:34 +09:00
"use strict" ;
var _ _extends = ( this && this . _ _extends ) || ( function ( ) {
var extendStatics = function ( d , b ) {
extendStatics = Object . setPrototypeOf ||
( { _ _proto _ _ : [ ] } instanceof Array && function ( d , b ) { d . _ _proto _ _ = b ; } ) ||
2022-06-13 19:03:12 +09:00
function ( d , b ) { for ( var p in b ) if ( Object . prototype . hasOwnProperty . call ( b , p ) ) d [ p ] = b [ p ] ; } ;
2020-04-05 00:57:34 +09:00
return extendStatics ( d , b ) ;
} ;
return function ( d , b ) {
2022-06-13 19:03:12 +09:00
if ( typeof b !== "function" && b !== null )
throw new TypeError ( "Class extends value " + String ( b ) + " is not a constructor or null" ) ;
2020-04-05 00:57:34 +09:00
extendStatics ( d , b ) ;
function _ _ ( ) { this . constructor = d ; }
d . prototype = b === null ? Object . create ( b ) : ( _ _ . prototype = b . prototype , new _ _ ( ) ) ;
} ;
} ) ( ) ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2022-06-13 19:03:12 +09:00
exports . APIPromise = exports . BadResponseError = void 0 ;
2020-04-15 07:45:51 +09:00
function typeGuard ( checker ) {
return function ( x ) {
return checker ( x ) ;
} ;
}
2020-04-05 00:57:34 +09:00
var BadResponseError = /** @class */ ( function ( _super ) {
_ _extends ( BadResponseError , _super ) ;
2020-04-15 07:45:51 +09:00
function BadResponseError ( res , label ) {
2022-06-13 19:03:12 +09:00
var _this = _super . call ( this , "" . concat ( label , " status code: " ) . concat ( res . status , "\ndata: " ) . concat ( typeof res . data === 'object' ? JSON . stringify ( res . data ) : res . data ) ) || this ;
2020-04-05 00:57:34 +09:00
_this . res = res ;
Object . setPrototypeOf ( _this , BadResponseError . prototype ) ;
return _this ;
}
return BadResponseError ;
} ( Error ) ) ;
2020-04-15 07:45:51 +09:00
exports . BadResponseError = BadResponseError ;
2020-04-05 00:57:34 +09:00
var APIPromise = /** @class */ ( function ( ) {
2020-04-15 07:45:51 +09:00
function APIPromise ( resPromise , stps , handlers ) {
2020-04-05 00:57:34 +09:00
var _this = this ;
2020-04-15 07:45:51 +09:00
this . handlers = handlers ;
this . promise = resPromise . then ( function ( res ) {
var status = res . status , data = res . data ;
if ( ! typeGuard ( function ( x ) { return stps . hasOwnProperty ( x ) ; } ) ( status ) ) {
// unexpected status
throw new BadResponseError ( res , 'Unexpected' ) ;
}
var r = stps [ status ] ( data ) ;
if ( ! typeGuard ( function ( x ) { return _this . handlers . hasOwnProperty ( x ) ; } ) ( status ) ) {
// unhandled status
throw new BadResponseError ( res , 'Unhandled' ) ;
}
var handler = _this . handlers [ status ] ;
return handler ( r ) ;
2020-04-05 00:57:34 +09:00
} ) ;
}
2020-04-15 07:45:51 +09:00
APIPromise . init = function ( res , stps , kRsvs ) {
var handlers = { } ;
for ( var _i = 0 , kRsvs _1 = kRsvs ; _i < kRsvs _1 . length ; _i ++ ) {
var kRsv = kRsvs _1 [ _i ] ;
handlers [ kRsv ] = function ( x ) { return x ; } ;
}
return new APIPromise ( res , stps , handlers ) ;
} ;
APIPromise . prototype . on = function ( status , handler ) {
var self = this ;
self . handlers [ status ] = handler ;
return self ;
} ;
2020-04-05 00:57:34 +09:00
APIPromise . prototype . then = function ( onRsv , onRjt ) {
return this . promise . then ( onRsv , onRjt ) ;
} ;
APIPromise . prototype . catch = function ( onRjt ) {
return this . then ( undefined , onRjt ) ;
} ;
return APIPromise ;
} ( ) ) ;
exports . APIPromise = APIPromise ;