-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
126 lines (105 loc) · 3.04 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
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import createNonceMiddleware from './middlewares/nonce';
import createRootURLMiddleware from './middlewares/root-url';
import createPreloadingMiddleware from './middlewares/preloading';
import namespaceEndpointMiddleware from './middlewares/namespace-endpoint';
import httpV1Middleware from './middlewares/http-v1';
const middlewares = [];
function registerMiddleware( middleware ) {
middlewares.push( middleware );
}
function checkCloudflareError( error ) {
if ( typeof error === 'string' && error.indexOf( 'Cloudflare Ray ID' ) >= 0 ) {
throw {
code: 'cloudflare_error',
};
}
}
function apiFetch( options ) {
const raw = ( nextOptions ) => {
const { url, path, body, data, parse = true, ...remainingOptions } = nextOptions;
const headers = remainingOptions.headers || {};
if ( ! headers[ 'Content-Type' ] && data ) {
headers[ 'Content-Type' ] = 'application/json';
}
const responsePromise = window.fetch(
url || path,
{
...remainingOptions,
credentials: 'include',
body: body || JSON.stringify( data ),
headers,
}
);
const checkStatus = ( response ) => {
if ( response.status >= 200 && response.status < 300 ) {
return response;
}
throw response;
};
const parseResponse = ( response ) => {
if ( parse ) {
return response.json ? response.json() : Promise.reject( response );
}
return response;
};
return responsePromise
.then( checkStatus )
.then( parseResponse )
.catch( ( response ) => {
if ( ! parse ) {
throw response;
}
const invalidJsonError = {
code: 'invalid_json',
message: __( 'The response is not a valid JSON response.' ),
};
if ( ! response || ! response.json ) {
throw invalidJsonError;
}
/*
* Response data is a stream, which will be consumed by the .json() call.
* If we need to re-use this data to send to the Cloudflare error handler,
* we need a clone of the original response, so the stream can be consumed
* in the .text() call, instead.
*/
const responseClone = response.clone();
return response.json()
.catch( async () => {
const text = await responseClone.text();
checkCloudflareError( text );
throw invalidJsonError;
} )
.then( ( error ) => {
const unknownError = {
code: 'unknown_error',
message: __( 'An unknown error occurred.' ),
};
checkCloudflareError( error );
throw error || unknownError;
} );
} );
};
const steps = [
raw,
httpV1Middleware,
namespaceEndpointMiddleware,
...middlewares,
];
const next = ( nextOptions ) => {
const nextMiddleware = steps.pop();
return nextMiddleware( nextOptions, next );
};
return next( options );
}
apiFetch.use = registerMiddleware;
apiFetch.createNonceMiddleware = createNonceMiddleware;
apiFetch.createPreloadingMiddleware = createPreloadingMiddleware;
apiFetch.createRootURLMiddleware = createRootURLMiddleware;
export default apiFetch;