Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imp(core, websockets): Effect interface naming #97

Merged
merged 5 commits into from Jan 30, 2019
Prev

imp(core, websockets): HttpMiddleware -> HttpMiddlewareEffect + WsMid…

…dleware -> WsMiddlewareEffect
  • Loading branch information...
JozefFlakus committed Jan 30, 2019
commit d30683699e5c5fcf3f16fdaee5c52900345a9439
@@ -1,9 +1,9 @@
import { HttpError, HttpStatus, HttpMiddleware } from '@marblejs/core';
import { HttpError, HttpStatus, HttpMiddlewareEffect } from '@marblejs/core';
import { iif, of, throwError } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { isAuthorized } from '../fakes/auth.fake';

export const authorize$: HttpMiddleware = req$ =>
export const authorize$: HttpMiddlewareEffect = req$ =>
req$.pipe(
switchMap(req => iif(
() => !isAuthorized(req),
@@ -10,7 +10,7 @@ export interface HttpEffectResponse<T = any> {
body?: T;
}

export interface HttpMiddleware<
export interface HttpMiddlewareEffect<
I extends HttpRequest = HttpRequest,
O extends HttpRequest = HttpRequest,
> extends HttpEffect<I, O> {}
@@ -1,14 +1,14 @@
import { tap, mapTo, filter } from 'rxjs/operators';
import { HttpMiddleware, HttpEffect } from '../http-effects.interface';
import { HttpMiddlewareEffect, HttpEffect } from '../http-effects.interface';
import { combineMiddlewares, combineEffects } from '../effects.combiner';
import { Marbles, createHttpRequest } from '../../+internal';

describe('#combineMiddlewares', () => {
test('combines middlewares into one stream', () => {
// given
const a$: HttpMiddleware = req$ => req$.pipe(tap(req => { req.test = 1; }));
const b$: HttpMiddleware = req$ => req$.pipe(tap(req => { req.test = req.test + 1; }));
const c$: HttpMiddleware = req$ => req$.pipe(tap(req => { req.test = req.test + 1; }));
const a$: HttpMiddlewareEffect = req$ => req$.pipe(tap(req => { req.test = 1; }));
const b$: HttpMiddlewareEffect = req$ => req$.pipe(tap(req => { req.test = req.test + 1; }));
const c$: HttpMiddlewareEffect = req$ => req$.pipe(tap(req => { req.test = req.test + 1; }));
const incomingRequest = createHttpRequest({ url: '/' });
const outgoingRequest = createHttpRequest({ url: '/', test: 3 });

@@ -3,7 +3,7 @@ import { of, throwError } from 'rxjs';
import { mapTo, switchMap } from 'rxjs/operators';
import { httpListener } from './http.listener';
import { EffectFactory } from '../effects/effects.factory';
import { HttpMiddleware } from '../effects/http-effects.interface';
import { HttpMiddlewareEffect } from '../effects/http-effects.interface';

describe('Http listener', () => {
let effectsCombiner;
@@ -16,7 +16,7 @@ describe('Http listener', () => {
.matchType('GET')
.use(req$ => req$.pipe(mapTo( {} )));

const middleware$: HttpMiddleware = req$ => req$;
const middleware$: HttpMiddlewareEffect = req$ => req$;

beforeEach(() => {
jest.unmock('../error/error.effect.ts');
@@ -4,7 +4,7 @@ import { catchError, defaultIfEmpty, mergeMap, tap, takeWhile } from 'rxjs/opera
import { combineMiddlewares } from '../effects/effects.combiner';
import {
HttpEffectResponse,
HttpMiddleware,
HttpMiddlewareEffect,
HttpErrorEffect,
HttpOutputEffect,
} from '../effects/http-effects.interface';
@@ -18,7 +18,7 @@ import { createStaticInjectionContainer } from '../server/server.injector';
import { createEffectMetadata } from '../effects/effectsMetadata.factory';

export interface HttpListenerConfig {
middlewares?: HttpMiddleware[];
middlewares?: HttpMiddlewareEffect[];
effects: (RouteEffect | RouteEffectGroup)[];
error$?: HttpErrorEffect;
output$?: HttpOutputEffect;
@@ -1,4 +1,4 @@
import { HttpMiddleware } from '../effects/http-effects.interface';
import { HttpMiddlewareEffect } from '../effects/http-effects.interface';
import { combineMiddlewares } from '../effects/effects.combiner';
import { isRouteEffectGroup } from './router.helpers';
import {
@@ -12,7 +12,7 @@ import { factorizeRegExpWithParams } from './router.params.factory';

export const factorizeRouting = (
routes: (RouteEffect | RouteEffectGroup)[],
middlewares: HttpMiddleware[] = [],
middlewares: HttpMiddlewareEffect[] = [],
parentPath = '',
): Routing => {
const routing: Routing = [];
@@ -1,23 +1,23 @@
import { HttpMethod, HttpRequest } from '../http.interface';
import { HttpEffect, HttpMiddleware, HttpEffectResponse } from '../effects/http-effects.interface';
import { HttpEffect, HttpMiddlewareEffect, HttpEffectResponse } from '../effects/http-effects.interface';

// Route
export interface RouteEffect<T extends HttpRequest = HttpRequest> {
path: string;
method: HttpMethod;
effect: HttpEffect<T, HttpEffectResponse>;
middleware?: HttpMiddleware;
middleware?: HttpMiddlewareEffect;
}

export interface RouteEffectGroup {
path: string;
middlewares: HttpMiddleware[];
middlewares: HttpMiddlewareEffect[];
effects: (RouteEffect | RouteEffectGroup)[];
}

// Combiner
export interface RouteCombinerConfig {
middlewares?: HttpMiddleware[];
middlewares?: HttpMiddlewareEffect[];
effects: (RouteEffect | RouteEffectGroup)[];
}

@@ -30,7 +30,7 @@ export interface ParametricRegExp {

export interface RoutingMethod {
parameters?: string[] | undefined;
middleware?: HttpMiddleware | undefined;
middleware?: HttpMiddlewareEffect | undefined;
effect: HttpEffect;
}

@@ -41,7 +41,7 @@ export interface RoutingItem {
}

export interface RouteMatched {
middleware?: HttpMiddleware | undefined;
middleware?: HttpMiddlewareEffect | undefined;
effect: HttpEffect;
params: Record<string, string>;
}
@@ -1,7 +1,7 @@
import { mapTo } from 'rxjs/operators';
import { combineRoutes } from '../router.combiner';
import { EffectFactory } from '../../effects/effects.factory';
import { HttpMiddleware, HttpEffect } from '../../effects/http-effects.interface';
import { HttpMiddlewareEffect, HttpEffect } from '../../effects/http-effects.interface';

describe('#combineRoutes', () => {
test('factorizes combined routes for effects only', () => {
@@ -27,8 +27,8 @@ describe('#combineRoutes', () => {
const a$ = EffectFactory.matchPath('/a').matchType('GET').use(effect$);
const b$ = EffectFactory.matchPath('/b').matchType('GET').use(effect$);

const m1$: HttpMiddleware = req$ => req$;
const m2$: HttpMiddleware = req$ => req$;
const m1$: HttpMiddlewareEffect = req$ => req$;
const m2$: HttpMiddlewareEffect = req$ => req$;

// when
const combiner = combineRoutes('/test', {
@@ -1,5 +1,5 @@
import { mapTo } from 'rxjs/operators';
import { HttpEffect, HttpMiddleware } from '../../effects/http-effects.interface';
import { HttpEffect, HttpMiddlewareEffect } from '../../effects/http-effects.interface';
import { RouteEffect, RouteEffectGroup, Routing } from '../router.interface';
import { factorizeRouting } from '../router.factory';

@@ -13,7 +13,7 @@ describe('#factorizeRouting', () => {

test('factorizes routing with nested groups', () => {
// given
const m$: HttpMiddleware = req$ => req$;
const m$: HttpMiddlewareEffect = req$ => req$;
const e1$: HttpEffect = req$ => req$.pipe(mapTo({ body: 'test1' }));
const e2$: HttpEffect = req$ => req$.pipe(mapTo({ body: 'test2' }));
const e3$: HttpEffect = req$ => req$.pipe(mapTo({ body: 'test3' }));
@@ -1,5 +1,5 @@
import { mapTo, tap, map } from 'rxjs/operators';
import { HttpEffect, HttpMiddleware } from '../../effects/http-effects.interface';
import { HttpEffect, HttpMiddlewareEffect } from '../../effects/http-effects.interface';
import { EffectMetadata } from '../../effects/effects.interface';
import { findRoute, resolveRouting } from '../router.resolver';
import { HttpRequest, HttpResponse, HttpMethod } from '../../http.interface';
@@ -185,7 +185,7 @@ describe('#resolveRouting', () => {

test('applies middlewares to found effect', done => {
// given
const middleware$: HttpMiddleware = req$ => req$.pipe(tap(req => req.test = 'test' ));
const middleware$: HttpMiddlewareEffect = req$ => req$.pipe(tap(req => req.test = 'test' ));
const effect$: HttpEffect = req$ => req$.pipe(map(req => ({ body: req.test }) ));

const expectedMachingResult: RouteMatched = { middleware: middleware$, effect: effect$, params: {} };
@@ -1,4 +1,4 @@
import { HttpError, HttpRequest, HttpStatus, HttpMiddleware } from '@marblejs/core';
import { HttpError, HttpRequest, HttpStatus, HttpMiddlewareEffect } from '@marblejs/core';
import { ContentType } from '@marblejs/core/dist/+internal';
import { Observable, of, throwError } from 'rxjs';
import { catchError, map, switchMap, tap, toArray, mapTo } from 'rxjs/operators';
@@ -44,7 +44,7 @@ const getBody = (req: HttpRequest) =>
})
);

export const bodyParser$: HttpMiddleware = req$ =>
export const bodyParser$: HttpMiddlewareEffect = req$ =>
req$.pipe(
switchMap(req =>
PARSEABLE_METHODS.includes(req.method)
@@ -1,6 +1,6 @@
import { throwError, of, Observable } from 'rxjs';
import { map, flatMap, catchError, tap } from 'rxjs/operators';
import { HttpError, HttpStatus, HttpMiddleware, HttpRequest } from '@marblejs/core';
import { HttpError, HttpStatus, HttpMiddlewareEffect, HttpRequest } from '@marblejs/core';
import { parseAuthorizationHeader } from './jwt.util';
import { verifyToken$, VerifyOptions } from './jwt.factory';

@@ -11,7 +11,7 @@ const assignPayloadToRequest = (req: HttpRequest) => (payload: object) => req.us
export const authorize$ = (
config: AuthorizeMiddlewareConfig,
verifyPayload$: (payload: any) => Observable<object>,
): HttpMiddleware => req$ =>
): HttpMiddlewareEffect => req$ =>
req$.pipe(
flatMap(req => of(req).pipe(
map(parseAuthorizationHeader),
@@ -1,4 +1,4 @@
import { HttpMiddleware } from '@marblejs/core';
import { HttpMiddlewareEffect } from '@marblejs/core';
import { timestamp, tap, map } from 'rxjs/operators';
import { LoggerOptions } from './logger.model';
import { loggerHandler } from './logger.handler';
@@ -8,13 +8,13 @@ import { loggerHandler } from './logger.handler';
* [#2] will be deleted in version 2.0,
* [#3] use loggerWithOpts$ instead,
*/
export const logger$: HttpMiddleware = (req$, res, injector) => {
export const logger$: HttpMiddlewareEffect = (req$, res, injector) => {
// tslint:disable-next-line:max-line-length
console.warn('Deprecation warning: logger$ is deprecated since v1.2 and will be removed in v2.0. Use loggerWithOpts$ instead.');
return loggerWithOpts$()(req$, res, injector);
};

export const loggerWithOpts$ = (opts: LoggerOptions = {}): HttpMiddleware => (req$, res) =>
export const loggerWithOpts$ = (opts: LoggerOptions = {}): HttpMiddlewareEffect => (req$, res) =>
req$.pipe(
timestamp(),
tap(loggerHandler(res, opts)),
@@ -2,7 +2,7 @@ import * as http from 'http';
import { Event, Effect } from '@marblejs/core';
import { MarbleWebSocketClient } from '../websocket.interface';

export interface WsMiddleware<
export interface WsMiddlewareEffect<
I = Event,
O = Event,
> extends WsEffect<I, O> {}
@@ -2,7 +2,7 @@ import { Event, EventError, createStaticInjectionContainer, httpServerToken } fr
import { throwError, fromEvent, forkJoin } from 'rxjs';
import { tap, map, mergeMap, first, toArray, take } from 'rxjs/operators';
import { webSocketListener } from '../websocket.listener';
import { WsEffect, WsMiddleware, WsConnectionEffect } from '../../effects/ws-effects.interface';
import { WsEffect, WsMiddlewareEffect, WsConnectionEffect } from '../../effects/ws-effects.interface';
import { WebSocketConnectionError } from '../../error/ws-error.model';
import { EventTransformer } from '../../transformer/transformer.inteface';
import { createWebSocketsTestBed } from '../../+internal';
@@ -90,7 +90,7 @@ describe('WebSocket listener', () => {
const incomingEvent = JSON.stringify({ type: 'EVENT', payload: 0 });
const outgoingEvent = JSON.stringify({ type: 'EVENT', payload: 3 });
const e$: WsEffect = event$ => event$;
const m$: WsMiddleware = event$ => event$.pipe(
const m$: WsMiddlewareEffect = event$ => event$.pipe(
map(event => event as Event<number>),
tap(event => event.payload !== undefined && event.payload++)
);
@@ -30,7 +30,7 @@ type HandleIncomingConnection =

export interface WebSocketListenerConfig {
effects?: WSEffect.WsEffect<any, any>[];
middlewares?: WSEffect.WsMiddleware<any, any>[];
middlewares?: WSEffect.WsMiddlewareEffect<any, any>[];
error$?: WSEffect.WsErrorEffect<Error, any, any>;
eventTransformer?: EventTransformer<Event, any>;
connection$?: WSEffect.WsConnectionEffect;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.