1+ import axios , { AxiosResponse , AxiosError } from 'axios' ;
12import { logError } from '@codesandbox/common/lib/utils/analytics' ;
23import { values } from 'lodash-es' ;
34import { camelizeKeys , decamelizeKeys } from 'humps' ;
4- // import { addNotification } from '../factories';
55
6- /*
7- This effect needs to expose a "configure" method where jwt
8- and error action is passed in
9- */
106const API_ROOT = '/api/v1' ;
117
12- function createHeaders ( { state, jwt } ) {
13- const foundJwt = state . get ( 'jwt' ) || jwt . get ( ) ;
14-
15- return foundJwt
16- ? {
17- Authorization : `Bearer ${ foundJwt } ` ,
18- }
19- : { } ;
20- }
21-
22- const getMessage = ( error : Error & { response ?: any } ) => {
8+ const getMessage = ( error : AxiosError ) => {
239 const response = error . response ;
2410
2511 if ( ! response || response . status >= 500 ) {
2612 logError ( error ) ;
2713 }
2814
29- if ( response && response . result ) {
30- if ( response . result . errors ) {
31- const errors = values ( response . result . errors ) [ 0 ] ;
15+ if ( response && response . data ) {
16+ if ( response . data . errors ) {
17+ const errors = values ( response . data . errors ) [ 0 ] ;
3218 if ( Array . isArray ( errors ) ) {
3319 if ( errors [ 0 ] ) {
3420 error . message = errors [ 0 ] ; // eslint-disable-line no-param-reassign
3521 }
3622 } else {
3723 error . message = errors ; // eslint-disable-line no-param-reassign
3824 }
39- } else if ( response . result . error ) {
40- error . message = response . result . error ; // eslint-disable-line no-param-reassign
25+ } else if ( response . data . error ) {
26+ error . message = response . data . error ; // eslint-disable-line no-param-reassign
4127 } else if ( response . status === 413 ) {
4228 return 'File too large, upload limit is 5MB.' ;
4329 }
@@ -46,34 +32,13 @@ const getMessage = (error: Error & { response?: any }) => {
4632 return error . message ;
4733} ;
4834
49- const showError = ( error , controller ) => {
50- const errorMessage = getMessage ( error ) ;
51-
52- /*
53- TODO: This needs to be handled differently!
54- controller.runSignal(
55- 'showNotification',
56- addNotification(errorMessage, 'error')
57- );
58- */
59-
60- error . apiMessage = errorMessage ; // eslint-disable-line no-param-reassign
61- } ;
62-
63- const handleError = ( error , controller ) => {
64- try {
65- showError ( error , controller ) ;
66- } catch ( e ) {
67- console . error ( e ) ;
68- }
69-
70- throw error ;
71- } ;
72-
73- function handleResponse ( response , { shouldCamelize = true } = { } ) {
35+ function handleResponse (
36+ response : AxiosResponse ,
37+ { shouldCamelize = true } = { }
38+ ) {
7439 const camelizedData = shouldCamelize
75- ? camelizeKeys ( response . result )
76- : response . result ;
40+ ? camelizeKeys ( response . data )
41+ : response . data ;
7742
7843 // Quickfix to prevent underscored dependencies from being camelized.
7944 // Never store data as keys in the future.
@@ -82,63 +47,109 @@ function handleResponse(response, { shouldCamelize = true } = {}) {
8247 camelizedData . data &&
8348 camelizedData . data . npmDependencies
8449 ) {
85- camelizedData . data . npmDependencies = response . result . data . npm_dependencies ;
50+ camelizedData . data . npmDependencies = response . data . data . npm_dependencies ;
8651 }
8752
8853 return camelizedData . data ? camelizedData . data : camelizedData ;
8954}
9055
91- export default {
92- get ( path , query , options ) {
93- return this . context . http
94- . get ( API_ROOT + path , query , {
95- headers : createHeaders ( this . context ) ,
96- } )
97- . then ( response => handleResponse ( response , options ) )
98- . catch ( e => handleError ( e , this . context . controller ) ) ;
99- } ,
100- post ( path , body , options ) {
101- return this . context . http
102- . post ( API_ROOT + path , decamelizeKeys ( body ) , {
103- headers : createHeaders ( this . context ) ,
104- } )
105- . then ( response => handleResponse ( response , options ) )
106- . catch ( e => handleError ( e , this . context . controller ) ) ;
107- } ,
108- patch ( path , body , options ) {
109- return this . context . http
110- . patch ( API_ROOT + path , decamelizeKeys ( body ) , {
111- headers : createHeaders ( this . context ) ,
112- } )
113- . then ( response => handleResponse ( response , options ) )
114- . catch ( e => handleError ( e , this . context . controller ) ) ;
115- } ,
116- put ( path , body , options ) {
117- return this . context . http
118- . put ( API_ROOT + path , decamelizeKeys ( body ) , {
119- headers : createHeaders ( this . context ) ,
120- } )
121- . then ( response => handleResponse ( response , options ) )
122- . catch ( e => handleError ( e , this . context . controller ) ) ;
123- } ,
124- delete ( path , query , options ) {
125- return this . context . http
126- . delete ( API_ROOT + path , query , {
127- headers : createHeaders ( this . context ) ,
128- } )
129- . then ( response => handleResponse ( response , options ) )
130- . catch ( e => handleError ( e , this . context . controller ) ) ;
131- } ,
132- request ( options ) {
133- return this . context . http
134- . request (
135- Object . assign ( options , {
136- url : API_ROOT + options . url ,
137- body : options . body ? camelizeKeys ( options . body ) : null ,
138- headers : createHeaders ( this . context ) ,
139- } )
140- )
141- . then ( response => handleResponse ( response , options ) )
142- . catch ( e => handleError ( e , this . context . controller ) ) ;
143- } ,
56+ type Options = {
57+ provideJwtToken : ( ) => string ;
58+ onError : ( error : string ) => void ;
14459} ;
60+
61+ export default ( ( ) => {
62+ let _options : Options = {
63+ provideJwtToken ( ) {
64+ throw new Error ( 'Missing implementation' ) ;
65+ } ,
66+ onError ( ) {
67+ throw new Error ( 'Missing implementation' ) ;
68+ } ,
69+ } ;
70+
71+ function createHeaders ( jwt : string ) {
72+ return jwt
73+ ? {
74+ Authorization : `Bearer ${ jwt } ` ,
75+ }
76+ : { } ;
77+ }
78+
79+ const showError = error => {
80+ const errorMessage = getMessage ( error ) ;
81+
82+ _options . onError ( errorMessage ) ;
83+ error . apiMessage = errorMessage ; // eslint-disable-line no-param-reassign
84+ } ;
85+
86+ const handleError = error => {
87+ try {
88+ showError ( error ) ;
89+ } catch ( e ) {
90+ console . error ( e ) ;
91+ }
92+
93+ throw error ;
94+ } ;
95+
96+ return {
97+ initialize ( options : Options ) {
98+ _options = options ;
99+ } ,
100+ get ( path : string , params ?: { [ key : string ] : string } , options ?: { } ) {
101+ return axios
102+ . get ( API_ROOT + path , {
103+ params,
104+ headers : createHeaders ( _options . provideJwtToken ( ) ) ,
105+ } )
106+ . then ( response => handleResponse ( response , options ) )
107+ . catch ( e => handleError ( e ) ) ;
108+ } ,
109+ post ( path , body , options ) {
110+ return axios
111+ . post ( API_ROOT + path , decamelizeKeys ( body ) , {
112+ headers : createHeaders ( _options . provideJwtToken ( ) ) ,
113+ } )
114+ . then ( response => handleResponse ( response , options ) )
115+ . catch ( e => handleError ( e ) ) ;
116+ } ,
117+ patch ( path , body , options ) {
118+ return axios
119+ . patch ( API_ROOT + path , decamelizeKeys ( body ) , {
120+ headers : createHeaders ( _options . provideJwtToken ( ) ) ,
121+ } )
122+ . then ( response => handleResponse ( response , options ) )
123+ . catch ( e => handleError ( e ) ) ;
124+ } ,
125+ put ( path , body , options ) {
126+ return axios
127+ . put ( API_ROOT + path , decamelizeKeys ( body ) , {
128+ headers : createHeaders ( _options . provideJwtToken ( ) ) ,
129+ } )
130+ . then ( response => handleResponse ( response , options ) )
131+ . catch ( e => handleError ( e ) ) ;
132+ } ,
133+ delete ( path , params , options ) {
134+ return axios
135+ . delete ( API_ROOT + path , {
136+ params,
137+ headers : createHeaders ( _options . provideJwtToken ( ) ) ,
138+ } )
139+ . then ( response => handleResponse ( response , options ) )
140+ . catch ( e => handleError ( e ) ) ;
141+ } ,
142+ request ( options ) {
143+ return axios
144+ . request (
145+ Object . assign ( options , {
146+ url : API_ROOT + options . url ,
147+ body : options . body ? camelizeKeys ( options . body ) : null ,
148+ headers : createHeaders ( _options . provideJwtToken ( ) ) ,
149+ } )
150+ )
151+ . then ( response => handleResponse ( response , options ) )
152+ . catch ( e => handleError ( e ) ) ;
153+ } ,
154+ } ;
155+ } ) ( ) ;
0 commit comments