'use strict' ; import { app } from '../../config' ; import { inject } from '../../bases/Utils' ; interface IRestPaths { V2 ? : string ; Type : string ; Id ? : string ; Path1 ? : string ; Path2 ? : string ; Path3 ? : string ; _boundToObjectId ? : string ; fields ? : string ; [ index ] : any ; } interface IRestMethod { get ? : ( ... params : any [ ] ) => any ; query ? : ( ... params : any [ ] ) => any ; update ? : ( ... params : any [ ] ) => any ; delete ? : ( ... params : any [ ] ) => any ; post ? : ( ... params : any [ ] ) => any ; save ? : ( ... params : any [ ] ) => any ; } let fetchTimes = 0 ; let fetchingMap ; fetchingMap = { } ; let RestCallback = function ( path , callback ? ) { return function ( ) { fetchTimes ++ ; fetchingMap [ path ] = null ; if ( typeof ( callback ) === 'function' ) { callback . apply ( null , arguments ) ; } } ; } ; const replaceCallback = ( args , path ) : any [ ] => { const length = args . length ; const _args = [ ] ; let callbacksCount = 0 ; for ( let i = 0 ; i < length ; i ++ ) { let ele = args [ i ] ; if ( typeof ( ele ) === 'function' ) { callbacksCount ++ ; if ( callbacksCount < 2 ) { _args . push ( RestCallback ( path , ele ) ) ; } } else { _args . push ( ele ) ; } } ; switch ( callbacksCount ) { case 0 : const callback = RestCallback ( path ) ; _args . push ( callback , callback ) ; break ; case 1 : _args . push ( RestCallback ( path ) ) ; break ; } return _args ; } ; let request = ( target , key , value ) => { return { value : function ( ... args ) { const arg = args . map ( ( a ) => { if ( typeof ( a ) !== 'function' ) { return JSON . stringify ( a ) ; } } ) . join ( ) ; let fetching = fetchingMap [ arg ] ; if ( fetching ) { return fetching ; } else { let _arguments = replaceCallback ( args , arg ) ; let result = value . value . apply ( this , _arguments ) ; fetchingMap [ arg ] = result ; return result ; } } } ; } ; @ inject ( [ '$resource' ] ) export class RestAPI { private resource : IRestMethod ; private $resource : angular . resource . IResourceService ; constructor ( ) { let path = '/:V2/:Type/:Id/:Path1/:Path2/:Path3' ; this . resource = this . $resource ( app . apiHost + path , null , { query : { method : 'GET' , isArray : true , withCredentials : true } , get : { method : 'GET' , withCredientials : true } , update : { method : 'PUT' , withCredientials : true } , save : { method : 'POST' , withCredientials : true } , post : { method : 'POST' , widthCredientials : true , isArray : true } , 'delete' : { method : 'DELETE' , withCredientials : true } } ) ; } @ request public get ( paths , successCallback ? , errorCallback ? ) { return this . resource . get . apply ( null , arguments ) ; } @ request public query ( paths , successCallback ? , errorCallback ? ) { return this . resource . query . apply ( null , arguments ) ; } @ request public update ( paths , data ? , successCallback ? , errorCallback ? ) { return this . resource . update . apply ( null , arguments ) ; } @ request public save ( paths , data ? , successCallback ? , errorCallback ? ) { return this . resource . save . apply ( null , arguments ) ; } @ request public post ( paths , data ? , successCallback ? , errorCallback ? ) { return this . resource . post . apply ( null , arguments ) ; } @ request public delete ( paths , data ? , successCallback ? , errorCallback ? ) { return this . resource . delete . apply ( null , arguments ) ; } } @ inject ( [ '$resource' ] ) export class DingRestAPI { private resource : IRestMethod ; private $resource : angular . resource . IResourceService ; constructor ( ) { let path = '/:V2/:Type/:Id/:Path1/:Path2/:Path3' ; this . resource = this . $resource ( app . dingApiHost + path , null , { query : { method : 'GET' , isArray : true , withCredentials : true } , get : { method : 'GET' , withCredientials : true } , update : { method : 'PUT' , withCredientials : true } , save : { method : 'POST' , withCredientials : true } , post : { method : 'POST' , widthCredientials : true , isArray : true } , 'delete' : { method : 'DELETE' , withCredientials : true } } ) ; } @ request public get ( paths , successCallback ? , errorCallback ? ) { return this . resource . get . apply ( null , arguments ) ; } @ request public query ( paths , successCallback ? , errorCallback ? ) { return this . resource . query . apply ( null , arguments ) ; } @ request public update ( paths , data ? , successCallback ? , errorCallback ? ) { return this . resource . update . apply ( null , arguments ) ; } @ request public save ( paths , data ? , successCallback ? , errorCallback ? ) { return this . resource . save . apply ( null , arguments ) ; } @ request public post ( paths , data ? , successCallback ? , errorCallback ? ) { return this . resource . post . apply ( null , arguments ) ; } @ request public delete ( paths , successCallback ? , errorCallback ? ) { return this . resource . delete . apply ( null , arguments ) ; } } angular . module ( 'teambition' ) . service ( 'RestAPI' , RestAPI ) . service ( 'DingRestAPI' , DingRestAPI ) ;