style: middlewares code style improvements
MODIFY
packages/middleware-body/src/index.ts
packages/middleware-body/src/index.ts
-import {
- ContentType,
- Effect,
- HttpError,
- HttpRequest,
- HttpStatus
-} from '@marblejs/core';
+import { ContentType, HttpError, HttpRequest, HttpStatus, Middleware } from '@marblejs/core';
import { Observable, of, throwError } from 'rxjs';
-import { catchError, map, switchMap, tap, toArray } from 'rxjs/operators';
+import { catchError, map, switchMap, tap, toArray, mapTo } from 'rxjs/operators';
import { serializeUrlEncoded } from './urlEncoded.serializer';
+const PARSEABLE_METHODS = ['POST', 'PUT', 'PATCH'];
+
const fromReadableStream = (stream: HttpRequest): Observable<any> => {
stream.pause();
return new Observable(observer => {
@@ -47,23 +43,15 @@ const getBody = (req: HttpRequest) =>
})
);
-export const bodyParser$: Effect<HttpRequest> = (request$, response) =>
- request$.pipe(
- switchMap(
- req =>
- ['POST', 'PUT', 'PATCH'].includes(req.method!)
+export const bodyParser$: Middleware = req$ =>
+ req$.pipe(
+ switchMap(req =>
+ PARSEABLE_METHODS.includes(req.method)
? of(req).pipe(
switchMap(getBody),
tap(body => (req.body = body)),
- map(() => req),
- catchError(() =>
- throwError(
- new HttpError(
- 'Request body parse error',
- HttpStatus.BAD_REQUEST
- )
- )
- )
+ mapTo(req),
+ catchError(() => throwError(new HttpError('Request body parse error', HttpStatus.BAD_REQUEST)))
)
: of(req)
)

MODIFY
packages/middleware-joi/src/index.ts
packages/middleware-joi/src/index.ts
import * as Joi from 'joi';
import { Schema, SchemaValidator } from './schema';
-import { Effect, HttpRequest, HttpError, HttpStatus } from '@marblejs/core';
+import { HttpRequest, HttpError, HttpStatus, Middleware } from '@marblejs/core';
import { from, of, throwError } from 'rxjs';
-import {
- mergeMap,
- flatMap,
- catchError,
- mapTo,
- switchMap,
- toArray,
- map
-} from 'rxjs/operators';
+import { mergeMap, flatMap, catchError, mapTo, switchMap, toArray, map } from 'rxjs/operators';
-const validateSource = (req: HttpRequest, rules: Map<string, any>, options) =>
+const validateSource = (rules: Map<string, any>, options: Joi.ValidationOptions) => (req: HttpRequest) =>
from(rules.keys()).pipe(
mergeMap(rule =>
of(req[rule]).pipe(
@@ -31,10 +23,7 @@ const validateSource = (req: HttpRequest, rules: Map<string, any>, options) =>
mapTo(req)
);
-const validator$ = (
- schema: Schema,
- options: Joi.ValidationOptions = {}
-): Effect<HttpRequest> => request$ => {
+const validator$ = (schema: Schema, options: Joi.ValidationOptions = {}): Middleware => req$ => {
const result = Joi.validate(schema, SchemaValidator);
const rules = Object.keys(schema).reduce(
(acc, value) => acc.set(value, Joi.compile(schema[value])),
@@ -45,7 +34,9 @@ const validator$ = (
return throwError(result.error);
}
- return request$.pipe(switchMap(req => validateSource(req, rules, options)));
+ return req$.pipe(
+ switchMap(validateSource(rules, options))
+ );
};
export { Joi, validator$ };

MODIFY
packages/middleware-joi/test/helpers/api.spec-util.ts
packages/middleware-joi/test/helpers/api.spec-util.ts
@@ -8,15 +8,9 @@ const getPost$ = EffectFactory
.matchType('GET')
.use(req$ => req$
.pipe(
- use(
- validator$({
- query: {
- page: Joi.number()
- .integer()
- .greater(1)
- }
- })
- ),
+ use(validator$({
+ query: { page: Joi.number().integer().greater(1) }
+ })),
map(req => req.query),
map(query => ({ status: 200, body: { ...query } }))
));
@@ -26,17 +20,9 @@ const getUser$ = EffectFactory
.matchType('GET')
.use(req$ => req$
.pipe(
- use(
- validator$({
- params: {
- id: Joi.number()
- .integer()
- .min(1)
- .max(10)
- .required()
- }
- })
- ),
+ use(validator$({
+ params: { id: Joi.number().integer().min(1).max(10).required() }
+ })),
map(req => req.params),
map(params => ({ status: 200, body: { id: params!.id } }))
));
@@ -46,16 +32,10 @@ const storePost$ = EffectFactory
.matchType('POST')
.use(req$ => req$
.pipe(
- use(
- validator$({
- query: {
- timestamp: Joi.date().required()
- },
- body: {
- title: Joi.string().required()
- }
- })
- ),
+ use(validator$({
+ query: { timestamp: Joi.date().required() },
+ body: { title: Joi.string().required() },
+ })),
map(resp => ({ body: { ...resp.body, ...resp.query } }))
));
@@ -64,14 +44,12 @@ const postUser$ = EffectFactory
.matchType('POST')
.use(req$ => req$
.pipe(
- use(
- validator$({
+ use(validator$({
body: {
name: Joi.string().required(),
passport: Joi.string().default('marble.js')
}
- })
- ),
+ })),
map(req => req.body),
map(response => ({ body: response }))
));
@@ -81,13 +59,7 @@ const api$ = combineRoutes('/api', [getPost$, getUser$, postUser$, storePost$]);
const middlewares = [
bodyParser$,
validator$(
- {
- headers: {
- token: Joi.string()
- .token()
- .required()
- }
- },
+ { headers: { token: Joi.string().token().required() } },
{ stripUnknown: true }
)
];

MODIFY
packages/middleware-logger/src/index.ts
packages/middleware-logger/src/index.ts
-import { compose, Effect, HttpRequest } from '@marblejs/core';
+import { compose, Middleware } from '@marblejs/core';
import chalk from 'chalk';
import { tap } from 'rxjs/operators';
import { formatTime, getTimeDifferenceMs } from './time.factory';
@@ -17,16 +17,16 @@ const printLog = (
console.info(`%s %s %s %s`, method, url, statusCode, formattedTime);
};
-export const logger$: Effect<HttpRequest> = (request$, response) =>
- request$.pipe(
- tap(request => {
+export const logger$: Middleware = (req$, res) =>
+ req$.pipe(
+ tap(req => {
const startTime = new Date();
- response.on('finish', () =>
+ res.on('finish', () =>
printLog(
- request.method!,
- request.url!,
- getStatusCode(response.statusCode),
+ req.method,
+ req.url,
+ getStatusCode(res.statusCode),
compose(formatTime, getTimeDifferenceMs)(startTime)
)
);

style: middlewares code style improvements