feat: Create an ArrayUnionHandler which flattens the sources results

This commit is contained in:
Joachim Van Herwegen 2022-09-29 16:57:19 +02:00
parent 4223dcf8a4
commit da99ff30f6
5 changed files with 58 additions and 11 deletions

View File

@ -423,6 +423,7 @@ export * from './util/errors/UnauthorizedHttpError';
export * from './util/errors/UnsupportedMediaTypeHttpError';
// Util/Handlers
export * from './util/handlers/ArrayUnionHandler';
export * from './util/handlers/AsyncHandler';
export * from './util/handlers/BooleanHandler';
export * from './util/handlers/ConditionalHandler';

View File

@ -0,0 +1,15 @@
import type { AsyncHandler, AsyncHandlerOutput } from './AsyncHandler';
import { UnionHandler } from './UnionHandler';
/**
* A utility handler that concatenates the results of all its handlers into a single result.
*/
export class ArrayUnionHandler<T extends AsyncHandler<any, any[]>> extends UnionHandler<T> {
public constructor(handlers: T[], requireAll?: boolean, ignoreErrors?: boolean) {
super(handlers, requireAll, ignoreErrors);
}
protected async combine(results: AsyncHandlerOutput<T>[]): Promise<AsyncHandlerOutput<T>> {
return results.flat() as AsyncHandlerOutput<T>;
}
}

View File

@ -1,3 +1,7 @@
type Awaited<T> = T extends PromiseLike<infer U> ? U : T;
export type AsyncHandlerInput<T extends AsyncHandler<any, any>> = Parameters<T['handle']>[0];
export type AsyncHandlerOutput<T extends AsyncHandler<any, any>> = Awaited<ReturnType<T['handle']>>;
/**
* Simple interface for classes that can potentially handle a specific kind of data asynchronously.
*/

View File

@ -1,18 +1,15 @@
import { allFulfilled } from '../PromiseUtil';
import type { AsyncHandlerInput, AsyncHandlerOutput } from './AsyncHandler';
import { AsyncHandler } from './AsyncHandler';
import { filterHandlers, findHandler } from './HandlerUtil';
// Helper types to make sure the UnionHandler has the same in/out types as the AsyncHandler type it wraps
type Awaited<T> = T extends PromiseLike<infer U> ? U : T;
type InType<T extends AsyncHandler<any, any>> = Parameters<T['handle']>[0];
type OutType<T extends AsyncHandler<any, any>> = Awaited<ReturnType<T['handle']>>;
/**
* Utility handler that allows combining the results of multiple handlers into one.
* Will run the handlers and then call the abstract `combine` function with the results,
* which then generates the handler's output.
*/
export abstract class UnionHandler<T extends AsyncHandler<any, any>> extends AsyncHandler<InType<T>, OutType<T>> {
export abstract class UnionHandler<T extends AsyncHandler<any, any>> extends
AsyncHandler<AsyncHandlerInput<T>, AsyncHandlerOutput<T>> {
protected readonly handlers: T[];
private readonly requireAll: boolean;
private readonly ignoreErrors: boolean;
@ -38,7 +35,7 @@ export abstract class UnionHandler<T extends AsyncHandler<any, any>> extends Asy
this.ignoreErrors = ignoreErrors;
}
public async canHandle(input: InType<T>): Promise<void> {
public async canHandle(input: AsyncHandlerInput<T>): Promise<void> {
if (this.requireAll) {
await this.allCanHandle(input);
} else {
@ -47,9 +44,9 @@ export abstract class UnionHandler<T extends AsyncHandler<any, any>> extends Asy
}
}
public async handle(input: InType<T>): Promise<OutType<T>> {
public async handle(input: AsyncHandlerInput<T>): Promise<AsyncHandlerOutput<T>> {
const handlers = this.requireAll ? this.handlers : await filterHandlers(this.handlers, input);
const results = handlers.map((handler): Promise<OutType<T>> => handler.handle(input));
const results = handlers.map((handler): Promise<AsyncHandlerOutput<T>> => handler.handle(input));
return this.combine(await allFulfilled(results, this.ignoreErrors));
}
@ -57,12 +54,12 @@ export abstract class UnionHandler<T extends AsyncHandler<any, any>> extends Asy
* Checks if all handlers can handle the input.
* If not, throw an error based on the errors of the failed handlers.
*/
protected async allCanHandle(input: InType<T>): Promise<void> {
protected async allCanHandle(input: AsyncHandlerInput<T>): Promise<void> {
await allFulfilled(this.handlers.map((handler): Promise<void> => handler.canHandle(input)));
}
/**
* Combines the results of the handlers into a single output.
*/
protected abstract combine(results: OutType<T>[]): Promise<OutType<T>>;
protected abstract combine(results: AsyncHandlerOutput<T>[]): Promise<AsyncHandlerOutput<T>>;
}

View File

@ -0,0 +1,30 @@
import { ArrayUnionHandler } from '../../../../src/util/handlers/ArrayUnionHandler';
import type { AsyncHandler } from '../../../../src/util/handlers/AsyncHandler';
describe('An ArrayUnionHandler', (): void => {
let handlers: jest.Mocked<AsyncHandler<string, number[]>>[];
let handler: ArrayUnionHandler<AsyncHandler<string, number[]>>;
beforeEach(async(): Promise<void> => {
handlers = [
{
canHandle: jest.fn(),
handle: jest.fn().mockResolvedValue([ 1, 2 ]),
} as any,
{
canHandle: jest.fn(),
handle: jest.fn().mockResolvedValue([ 3, 4 ]),
} as any,
];
handler = new ArrayUnionHandler(handlers);
});
it('merges the array results.', async(): Promise<void> => {
await expect(handler.handle('input')).resolves.toEqual([ 1, 2, 3, 4 ]);
expect(handlers[0].handle).toHaveBeenCalledTimes(1);
expect(handlers[0].handle).toHaveBeenLastCalledWith('input');
expect(handlers[1].handle).toHaveBeenCalledTimes(1);
expect(handlers[1].handle).toHaveBeenLastCalledWith('input');
});
});