2019-12-07 16:58:52 +05:30
|
|
|
import PromiseMiddlewareLayer, { Middleware } from './PromiseMiddlewareLayer';
|
2017-02-26 17:10:07 +05:30
|
|
|
import InternalServerError from './InternalServerError';
|
2018-11-04 13:39:42 +05:30
|
|
|
import RequestAbortedError from './RequestAbortedError';
|
2016-07-30 23:52:33 +05:30
|
|
|
|
|
|
|
const middlewareLayer = new PromiseMiddlewareLayer();
|
2016-02-27 16:28:29 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export type Resp<T> = T & {
|
|
|
|
originalResponse: Response;
|
2019-06-30 19:02:50 +05:30
|
|
|
};
|
2017-08-02 01:30:02 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
export interface Options extends RequestInit {
|
|
|
|
token?: string | null;
|
|
|
|
headers: { [key: string]: any };
|
|
|
|
}
|
2017-08-02 01:30:02 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
const buildOptions = (
|
|
|
|
method: string,
|
|
|
|
data: { [key: string]: any } | undefined,
|
|
|
|
options: Partial<Options> = {},
|
|
|
|
): Options => ({
|
2019-11-27 14:33:32 +05:30
|
|
|
method,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
2019-12-07 16:58:52 +05:30
|
|
|
...options.headers,
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
body: buildQuery(data),
|
|
|
|
...options,
|
2018-03-26 00:46:45 +05:30
|
|
|
});
|
|
|
|
|
2016-06-04 22:41:42 +05:30
|
|
|
export default {
|
2019-11-27 14:33:32 +05:30
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} [data] - request data
|
|
|
|
* @param {object} [options] - additional options for fetch or middlewares
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
post<T>(
|
|
|
|
url: string,
|
|
|
|
data?: { [key: string]: any },
|
|
|
|
options?: Partial<Options>,
|
|
|
|
): Promise<Resp<T>> {
|
2019-11-27 14:33:32 +05:30
|
|
|
return doFetch(url, buildOptions('POST', data, options));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} [data] - request data
|
|
|
|
* @param {object} [options] - additional options for fetch or middlewares
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
get<T>(
|
|
|
|
url: string,
|
|
|
|
data?: { [key: string]: any },
|
|
|
|
options?: Partial<Options>,
|
|
|
|
): Promise<Resp<T>> {
|
2019-11-27 14:33:32 +05:30
|
|
|
url = buildUrl(url, data);
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
return doFetch(url, { ...options, method: 'GET', headers: {} });
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} [data] - request data
|
|
|
|
* @param {object} [options] - additional options for fetch or middlewares
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
delete<T>(
|
|
|
|
url: string,
|
2019-12-07 16:58:52 +05:30
|
|
|
data?: { [key: string]: any },
|
|
|
|
options?: Partial<Options>,
|
2019-11-27 14:33:32 +05:30
|
|
|
): Promise<Resp<T>> {
|
|
|
|
return doFetch(url, buildOptions('DELETE', data, options));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} [data] - request data
|
|
|
|
* @param {object} [options] - additional options for fetch or middlewares
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
put<T>(
|
|
|
|
url: string,
|
|
|
|
data?: { [key: string]: any },
|
|
|
|
options?: Partial<Options>,
|
|
|
|
): Promise<Resp<T>> {
|
2019-11-27 14:33:32 +05:30
|
|
|
return doFetch(url, buildOptions('PUT', data, options));
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes object into encoded key=value presentation
|
|
|
|
*
|
|
|
|
* @param {object} data
|
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
buildQuery,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {object} middleware
|
|
|
|
* @param {Function} [middleware.before] - a function({url, options}), that will be called before executing request.
|
|
|
|
* It will get data object {url, options} as an argument and should return
|
|
|
|
* Promise, that will resolve into new data object
|
|
|
|
* @param {Function} [middleware.then] - a function(resp), that will be called on successful request result. It will
|
|
|
|
* get response as an argument and should return a Promise that resolves to
|
|
|
|
* the new response
|
|
|
|
* @param {Function} [middleware.catch] - a function(resp, restart), that will be called on request fail. It will
|
|
|
|
* get response and callback to restart request as an arguments and should
|
|
|
|
* return a Promise that resolves to the new response.
|
|
|
|
*/
|
|
|
|
addMiddleware(middleware: Middleware) {
|
|
|
|
middlewareLayer.add(middleware);
|
|
|
|
},
|
2016-06-04 22:41:42 +05:30
|
|
|
};
|
2016-02-13 20:58:47 +05:30
|
|
|
|
2018-11-04 13:39:42 +05:30
|
|
|
const checkStatus = (resp: Response) =>
|
2019-11-27 14:33:32 +05:30
|
|
|
resp.status >= 200 && resp.status < 300
|
|
|
|
? Promise.resolve(resp)
|
|
|
|
: Promise.reject(resp);
|
2018-11-04 13:39:42 +05:30
|
|
|
const toJSON = (resp: Response) => {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (!resp.json || resp.status === 0) {
|
|
|
|
// e.g. 'TypeError: Failed to fetch' due to CORS or request was aborted
|
|
|
|
throw new RequestAbortedError(resp);
|
|
|
|
}
|
2017-02-26 17:10:07 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return resp.json().then(
|
|
|
|
json => {
|
|
|
|
json.originalResponse = resp;
|
2017-02-26 17:10:07 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return json;
|
|
|
|
},
|
|
|
|
error => Promise.reject(new InternalServerError(error, resp)),
|
|
|
|
);
|
2017-02-26 17:10:07 +05:30
|
|
|
};
|
2018-11-04 13:39:42 +05:30
|
|
|
const rejectWithJSON = (resp: Response) =>
|
2019-11-27 14:33:32 +05:30
|
|
|
toJSON(resp).then(resp => {
|
|
|
|
if (resp.originalResponse.status >= 500) {
|
|
|
|
throw new InternalServerError(resp, resp.originalResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw resp;
|
|
|
|
});
|
|
|
|
const handleResponseSuccess = resp =>
|
|
|
|
resp.success || typeof resp.success === 'undefined'
|
|
|
|
? Promise.resolve(resp)
|
|
|
|
: Promise.reject(resp);
|
2016-03-02 02:06:14 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
async function doFetch(url: string, options: Options) {
|
2019-11-27 14:33:32 +05:30
|
|
|
// NOTE: we are wrapping fetch, because it is returning
|
2019-12-07 16:58:52 +05:30
|
|
|
// Promise instance that can not be polyfilled with Promise.prototype.finally
|
|
|
|
|
|
|
|
const headers: { [key: string]: string } = { ...options.headers } as any;
|
|
|
|
headers.Accept = 'application/json';
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
options.headers = headers;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
return middlewareLayer
|
|
|
|
.run('before', { url, options })
|
2019-12-07 16:58:52 +05:30
|
|
|
.then(({ url: nextUrl, options: nextOptions }) =>
|
|
|
|
fetch(nextUrl, nextOptions)
|
2019-11-27 14:33:32 +05:30
|
|
|
.then(checkStatus)
|
|
|
|
.then(toJSON, rejectWithJSON)
|
|
|
|
.then(handleResponseSuccess)
|
2019-12-07 16:58:52 +05:30
|
|
|
.then(resp =>
|
|
|
|
middlewareLayer.run('then', resp, {
|
|
|
|
url: nextUrl,
|
|
|
|
options: nextOptions,
|
|
|
|
}),
|
|
|
|
)
|
2019-11-27 14:33:32 +05:30
|
|
|
.catch(resp =>
|
2019-12-07 16:58:52 +05:30
|
|
|
middlewareLayer.run(
|
|
|
|
'catch',
|
|
|
|
resp,
|
|
|
|
{ url: nextUrl, options: nextOptions },
|
|
|
|
() => doFetch(nextUrl, nextOptions),
|
2019-11-27 14:33:32 +05:30
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
2016-06-04 22:24:42 +05:30
|
|
|
}
|
|
|
|
|
2016-07-29 23:45:16 +05:30
|
|
|
/**
|
|
|
|
* Converts specific js values to query friendly values
|
|
|
|
*
|
|
|
|
* @param {any} value
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {string}
|
2016-07-29 23:45:16 +05:30
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
function convertQueryValue(value: any): string {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (typeof value === 'undefined' || value === null) {
|
|
|
|
return '';
|
|
|
|
}
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (value === true) {
|
|
|
|
return '1';
|
|
|
|
}
|
2016-02-23 11:27:16 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
if (value === false) {
|
|
|
|
return '0';
|
|
|
|
}
|
2016-02-26 11:55:47 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
return String(value);
|
2016-06-04 22:41:42 +05:30
|
|
|
}
|
2016-02-27 16:23:58 +05:30
|
|
|
|
2016-07-29 23:45:16 +05:30
|
|
|
/**
|
|
|
|
* Serializes object into encoded key=value presentation
|
|
|
|
*
|
|
|
|
* @param {object} data
|
|
|
|
*
|
2019-11-27 14:33:32 +05:30
|
|
|
* @returns {string}
|
2016-07-29 23:45:16 +05:30
|
|
|
*/
|
2019-12-07 16:58:52 +05:30
|
|
|
function buildQuery(data: { [key: string]: any } = {}): string {
|
2019-11-27 14:33:32 +05:30
|
|
|
return Object.keys(data)
|
|
|
|
.map(keyName =>
|
|
|
|
[keyName, convertQueryValue(data[keyName])]
|
|
|
|
.map(encodeURIComponent)
|
|
|
|
.join('='),
|
|
|
|
)
|
|
|
|
.join('&');
|
2016-06-04 22:41:42 +05:30
|
|
|
}
|
2017-09-09 19:52:19 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
function buildUrl(url: string, data?: { [key: string]: any }): string {
|
2019-11-27 14:33:32 +05:30
|
|
|
if (typeof data === 'object' && Object.keys(data).length) {
|
|
|
|
const separator = url.indexOf('?') === -1 ? '?' : '&';
|
|
|
|
url += separator + buildQuery(data);
|
|
|
|
}
|
2017-09-09 19:52:19 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
return url;
|
2017-09-09 19:52:19 +05:30
|
|
|
}
|