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 {
|
2019-12-07 16:58:52 +05:30
|
|
|
url: string;
|
|
|
|
options: Options;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Middleware {
|
|
|
|
before?: (
|
|
|
|
options: MiddlewareRequestOptions,
|
|
|
|
) => Promise<MiddlewareRequestOptions>;
|
|
|
|
then?: (resp: any, options: MiddlewareRequestOptions) => Promise<any>;
|
|
|
|
catch?: (
|
|
|
|
resp: any,
|
|
|
|
options: MiddlewareRequestOptions,
|
|
|
|
restart: () => Promise<any>,
|
|
|
|
) => Promise<any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A class to handle middleware layer
|
|
|
|
*/
|
|
|
|
class PromiseMiddlewareLayer {
|
|
|
|
private middlewares: Middleware[] = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
}
|
|
|
|
|
2020-05-20 22:05:52 +05:30
|
|
|
if (!this.middlewares.some((mdware) => mdware === middleware)) {
|
2019-12-07 16:58:52 +05:30
|
|
|
this.middlewares.push(middleware);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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>;
|
2020-01-18 02:07:52 +05:30
|
|
|
run(action: Action, data: any, ...rest: any) {
|
2019-12-07 16:58:52 +05:30
|
|
|
const promiseMethod = action === 'catch' ? 'catch' : 'then';
|
|
|
|
|
|
|
|
return this.middlewares
|
2020-05-20 22:05:52 +05:30
|
|
|
.filter((middleware) => middleware[action])
|
2019-12-07 16:58:52 +05:30
|
|
|
.reduce(
|
|
|
|
(promise: Promise<any>, middleware) =>
|
|
|
|
invoke(
|
|
|
|
promise,
|
|
|
|
promiseMethod,
|
2020-01-18 02:07:52 +05:30
|
|
|
)((resp: any) => invoke(middleware, action)(resp, ...rest)),
|
2019-12-07 16:58:52 +05:30
|
|
|
invoke(Promise, action === 'catch' ? 'reject' : 'resolve')(data),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
function invoke(instance: Record<string, any>, method: string) {
|
2019-12-07 16:58:52 +05:30
|
|
|
if (typeof instance[method] !== 'function') {
|
|
|
|
throw new Error(`Can not invoke ${method} on ${instance}`);
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:07:52 +05:30
|
|
|
return (...args: any) => instance[method](...args);
|
2019-12-07 16:58:52 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
export default PromiseMiddlewareLayer;
|