2019-12-07 16:58:52 +05:30
|
|
|
import { Options } from './request';
|
|
|
|
|
|
|
|
type Action = 'catch' | 'then' | 'before';
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
export interface MiddlewareRequestOptions {
|
2020-05-24 04:38:24 +05:30
|
|
|
url: string;
|
|
|
|
options: Options;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export interface Middleware {
|
2020-05-24 04:38:24 +05:30
|
|
|
before?: (options: MiddlewareRequestOptions) => Promise<MiddlewareRequestOptions>;
|
|
|
|
then?: (resp: any, options: MiddlewareRequestOptions) => Promise<any>;
|
|
|
|
catch?: (resp: any, options: MiddlewareRequestOptions, restart: () => Promise<any>) => Promise<any>;
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class to handle middleware layer
|
|
|
|
*/
|
|
|
|
class PromiseMiddlewareLayer {
|
2020-05-24 04:38:24 +05:30
|
|
|
private middlewares: Middleware[] = [];
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
/**
|
|
|
|
* Adds middleware into layer.
|
|
|
|
*
|
|
|
|
* `middleware` is an object, that may have multiple keys of type function.
|
|
|
|
* Each key is a name of an action that may be passed through the layer. Each
|
|
|
|
* function should return a Promise. An action with name catch has a special
|
|
|
|
* meaning: it will be invoked as catch callback on Promise (for middlewares,
|
|
|
|
* that should handle errors)
|
|
|
|
*
|
|
|
|
* @param {object} middleware
|
|
|
|
*/
|
|
|
|
add(middleware: Middleware) {
|
|
|
|
if (typeof middleware !== 'object') {
|
|
|
|
throw new Error('A middleware must be an object');
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
if (!this.middlewares.some((mdware) => mdware === middleware)) {
|
|
|
|
this.middlewares.push(middleware);
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
/**
|
|
|
|
* @param {string} action - the name of middleware's hook
|
|
|
|
* @param {object} data - the initial data to pass through middlewares chain
|
|
|
|
* @param {Function} restart - a function to restart current request (for `catch` hook)
|
|
|
|
*
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
run(
|
|
|
|
action: 'before',
|
|
|
|
options: MiddlewareRequestOptions,
|
|
|
|
arg3?: never,
|
|
|
|
arg4?: never,
|
|
|
|
): Promise<MiddlewareRequestOptions>;
|
|
|
|
run(action: 'then', resp: any, options: MiddlewareRequestOptions, arg4?: never): Promise<any>;
|
|
|
|
run(action: 'catch', resp: any, options: MiddlewareRequestOptions, restart: () => Promise<any>): Promise<any>;
|
|
|
|
run(action: Action, data: any, ...rest: any) {
|
|
|
|
const promiseMethod = action === 'catch' ? 'catch' : 'then';
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return this.middlewares
|
|
|
|
.filter((middleware) => middleware[action])
|
|
|
|
.reduce(
|
|
|
|
(promise: Promise<any>, middleware) =>
|
|
|
|
invoke(promise, promiseMethod)((resp: any) => invoke(middleware, action)(resp, ...rest)),
|
|
|
|
invoke(Promise, action === 'catch' ? 'reject' : 'resolve')(data),
|
|
|
|
);
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
function invoke(instance: Record<string, any>, method: string) {
|
2020-05-24 04:38:24 +05:30
|
|
|
if (typeof instance[method] !== 'function') {
|
|
|
|
throw new Error(`Can not invoke ${method} on ${instance}`);
|
|
|
|
}
|
2019-12-07 16:58:52 +05:30
|
|
|
|
2020-05-24 04:38:24 +05:30
|
|
|
return (...args: any) => instance[method](...args);
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export default PromiseMiddlewareLayer;
|