@@ -57,80 +57,53 @@ class GnEndpoints {
5757 this . body = body ;
5858 this . params = [ params ] ;
5959
60- if ( ! this . accessToken ) {
61- this . getAccessToken ( ) . then ( this . directReq . bind ( this ) ) ;
62- } else {
63- this . withTokenReq . call ( this ) ;
64- }
65-
60+ this . getAccessToken ( ) . then ( this . directReq . bind ( this ) ) ;
6661 return this . defer . promise ;
6762 }
6863
6964 private getAccessToken ( ) : Promise < any > {
65+ const self = this ;
7066 const gnAuth = new GnAuth ( this . options , this . constants ) ;
7167 return gnAuth
7268 . getAccessToken ( )
7369 . then ( ( response ) => {
74- this . accessToken = response . access_token ;
75- return this . accessToken ;
70+ self . accessToken = response . access_token ;
71+ return response . access_token ;
7672 } )
7773 . catch ( ( err ) => {
7874 return err ;
7975 } ) ;
8076 }
8177
82- private getResponse ( response : any , body : any ) {
83- return this . options . rawResponse ? response : body ;
84- }
85-
86- private req ( ) {
78+ private async req ( callback : any ) {
8779 const req : any = this . getParams . call ( this , this . endpoint . route ) ;
8880 req . method = this . endpoint . method ;
8981 axios ( req )
9082 . then ( ( res ) => {
91- console . log ( res . data ) ;
83+ callback ( res ) ;
9284 } )
9385 . catch ( ( error ) => {
94- console . log ( error . response . data ) ;
86+ callback ( error ) ;
9587 } ) ;
9688 }
9789
9890 private directReq ( ) {
99- this . directReqCallback . bind ( this ) ;
100- this . req ( ) ;
91+ this . req ( this . directReqCallback . bind ( this ) ) ;
10192 }
10293
103- private directReqCallback ( err : any , httpResponse : { statusCode : number } , bodyResponse : any ) {
104- const response = this . getResponse ( httpResponse , bodyResponse ) ;
105-
106- if ( err ) {
107- this . defer . reject ( err ) ;
108- } else if ( httpResponse . statusCode !== 200 ) {
109- this . defer . reject ( response ) ;
110- } else {
111- this . defer . resolve ( response ) ;
112- }
113- }
114-
115- private withTokenReq ( ) {
116- this . withTokenReqCallback . bind ( this ) ;
117- }
118-
119- private withTokenReqCallback (
120- err : any ,
121- httpResponse : { statusCode : number } ,
122- httpResponseBody : any
123- ) {
124- const response = this . getResponse ( httpResponse , httpResponseBody ) ;
125-
126- if ( err ) {
127- this . defer . reject ( err ) ;
128- } else if ( httpResponse . statusCode === 401 ) {
129- this . getAccessToken ( ) . then ( this . directReq . bind ( this ) ) ;
130- } else if ( httpResponse . statusCode !== 200 ) {
131- this . defer . reject ( response ) ;
132- } else {
133- this . defer . resolve ( response ) ;
94+ private directReqCallback ( rawResponse : any ) {
95+ if ( rawResponse . data ) {
96+ if ( rawResponse . status < 300 ) {
97+ if ( rawResponse . data . data ) {
98+ this . defer . resolve ( rawResponse . data . data ) ;
99+ } else {
100+ this . defer . resolve ( rawResponse . data ) ;
101+ }
102+ } else {
103+ this . defer . reject ( rawResponse . data ) ;
104+ }
105+ } else if ( rawResponse . response && rawResponse . response . data ) {
106+ this . defer . reject ( rawResponse . response . data ) ;
134107 }
135108 }
136109
@@ -145,6 +118,7 @@ class GnEndpoints {
145118 this . params . forEach ( ( obj : any ) => {
146119 if ( obj ) {
147120 Object . entries ( obj ) . forEach ( ( entrie : any ) => {
121+ // eslint-disable-next-line prefer-destructuring
148122 params [ entrie [ 0 ] ] = entrie [ 1 ] ;
149123 } ) ;
150124 }
0 commit comments