-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
162 lines (133 loc) · 4.67 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* eslint-disable no-console */
/* @flow */
import 'isomorphic-fetch';
import oboe from 'oboe';
import { abortableStream } from './abortable';
import detectIE from './is_ie';
type tDefaults = {
credentials: string,
headers?: Object
};
type tGetOptions = {
headers?: Object,
[key: string]: void
};
const mode = process.env.NODE_ENV || 'development';
const emoji = {
up: '\u2B06',
down: '\u2B07',
error: '\u274C'
};
const isIE = !!detectIE();
function _mergeOptions( defaults: tDefaults, options: Object ) {
return {
...defaults,
...options,
headers: { ...defaults.headers, ...( options.headers || {} ) }
};
}
const shouldLog = mode === 'development' && typeof window !== 'undefined';
function _handleResponse( response ): void | Promise<string | Object> {
if ( response.status > 400 ) {
// if we get an error, try to jsonify and return response. if there is
// an error when doing jsonification, just send text.
return response.json()
.then( json => ( { response: json, code: response.status } ) )
.catch( () => ( { response: response.statusText, code: response.status } ) )
.then( results => {
if ( shouldLog ) {
console.error( `${emoji.error} Fetch error: ${response.url}`, results );
}
return results;
})
.then( results => Promise.reject( results ) );
} else {
const contentType = response.headers.get( 'content-type' );
const results = contentType && ~contentType.indexOf( 'application/json' )
? response.json()
: response.text();
return results
.then( res => {
if ( shouldLog ) {
console.log( `${emoji.down} Fetch response: ${response.url}`, res );
}
return res;
})
.catch( err => {
if ( shouldLog ) {
console.error( `${emoji.error} Fetch error: ${response.url}`, err );
}
});
}
}
function _fetch( defaults: tDefaults ): Function {
return ( url, options = {} ): Promise<tResponse> => {
const mergedOptions = _mergeOptions( defaults, options );
if ( isIE ) {
url += ( ~url.indexOf( '?' ) ? '&cache=' : '?' ) + String( Date.now() );
}
if ( shouldLog ) {
console.log( `${emoji.up} Fetch request: ${url}`, mergedOptions );
}
return fetch( url, mergedOptions )
.then( response => _handleResponse( response ) );
};
}
function init( { cookie = null, makeUrl = url => url, ...otherOptions }: { cookie: ?string, makeUrl?: Function } = {} ) {
const defaults = { headers: {}, ...otherOptions };
if ( cookie ) Object.assign( defaults.headers, { Cookie: cookie } );
const jsonHeaders = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
const goFetch = _fetch( defaults );
const Api = {
delete( url: string ): Promise<any> {
return goFetch( makeUrl( url ), { method: 'DELETE' } );
},
download( url: string ): void {
window.location = makeUrl( url );
},
_get( url: string, options: tGetOptions = {} ): Promise<any> {
return goFetch( url, options );
},
get( url: string, options: tGetOptions = {} ): Promise<any> {
return goFetch( makeUrl( url ), options );
},
_post( url: string, params: Object ): Promise<any> {
const body = JSON.stringify( params );
return goFetch( url, { method: 'POST', body, headers: jsonHeaders } );
},
post( url: string, params: Object ): Promise<any> {
return Api._post( makeUrl( url ), params );
},
put( url: string, params: Object ): Promise<any> {
const body = JSON.stringify( params );
return goFetch( makeUrl( url ), { method: 'PUT', body, headers: jsonHeaders } );
},
stream( { url, method = 'GET', body = {}, abortable = false }: { url: string, method: string, body: Object, abortable?: boolean } ) {
const options = { url: makeUrl( url ), body, method, withCredentials: true };
if ( cookie ) options.headers = { Cookie: cookie };
const request = abortable ? abortableStream( options ) : oboe( options );
// for isomorphic app, we need to return a promise and don't care so much
// about streaming, which is for user's benefit anyway
return typeof window !== 'undefined'
? request
: new Promise( ( resolve, reject ) => {
request.on( 'done', resolve ).on( 'fail', reject );
} );
},
upload( url: string, files: Array<any> ): Promise<any> {
const formData = new FormData();
for ( let i = 0, len = files.length; i < len; i++ ) {
formData.append( 'file', files[i] );
}
return goFetch( makeUrl( url ), {
method: 'POST',
body: formData
} );
}
};
return Api;
}
export default init;