fix(middleware-io): merge input HttpRequest types with output
MODIFY
packages/middleware-io/src/io.request.middleware.ts
packages/middleware-io/src/io.request.middleware.ts
+import * as t from 'io-ts';
import { HttpError, HttpRequest, HttpStatus } from '@marblejs/core';
import { forkJoin, Observable, of, throwError } from 'rxjs';
import { catchError, map, mergeMap, tap } from 'rxjs/operators';
@@ -7,10 +8,10 @@ import { isTestingMetadataOn } from '@marblejs/core/dist/+internal/testing';
import { ioTypeToJsonSchema, mergeJsonObjects } from './io.json-schema';
interface RequestSchema<TBody extends Schema, TParams extends Schema, TQuery extends Schema> {
- body: TBody;
- params: TParams;
- query: TQuery;
- headers: Schema;
+ body?: TBody;
+ params?: TParams;
+ query?: TQuery;
+ headers?: Schema;
}
enum Context {
@@ -20,8 +21,11 @@ enum Context {
HEADERS = 'headers',
}
-export const requestValidator$ = <TBody extends Schema, TParams extends Schema, TQuery extends Schema>
-(schema: Partial<RequestSchema<TBody, TParams, TQuery>>, options: ValidatorOptions = {}) => {
+export const requestValidator$ = <
+ TBody extends Schema = t.UnknownC,
+ TParams extends Schema = t.UnknownC,
+ TQuery extends Schema = t.UnknownC,
+>(schema: RequestSchema<TBody, TParams, TQuery>, options: ValidatorOptions = {}) => {
const bodyValidator$ = validator$(schema.body, { ...options, context: Context.BODY });
const paramsValidator$ = validator$(schema.params, { ...options, context: Context.PARAMS });
const queryValidator$ = validator$(schema.query, { ...options, context: Context.QUERY });
@@ -37,7 +41,7 @@ export const requestValidator$ = <TBody extends Schema, TParams extends Schema,
};
};
- return (req$: Observable<HttpRequest>) => req$.pipe(
+ return <T extends HttpRequest>(req$: Observable<T>) => req$.pipe(
tap(req => {
if (isTestingMetadataOn()) {
addMetadata(req, 'body');
@@ -57,7 +61,11 @@ export const requestValidator$ = <TBody extends Schema, TParams extends Schema,
req.body = body;
req.params = params;
req.query = query;
- return req as HttpRequest<typeof body, typeof params, typeof query>;
+ return req as HttpRequest<
+ (typeof schema.body extends t.UnknownC | undefined | null ? unknown : typeof body) & T['body'],
+ (typeof schema.params extends t.UnknownC | undefined | null ? unknown : typeof params) & T['params'],
+ (typeof schema.query extends t.UnknownC | undefined | null ? unknown : typeof query) & T['query']
+ >;
}),
catchError((error: IOError) => throwError(
new HttpError(error.message, HttpStatus.BAD_REQUEST, error.data, error.context),

fix(middleware-io): merge input HttpRequest types with output