refactor(core): #HttpRequestBus
MODIFY
packages/core/src/+internal/testing/http.helper.ts
packages/core/src/+internal/testing/http.helper.ts
import * as http from 'http';
import { EventEmitter } from 'events';
-import { Subject } from 'rxjs';
import { delay, tap, mapTo } from 'rxjs/operators';
import {
HttpRequest,
@@ -13,12 +12,13 @@ import {
} from '../../http/http.interface';
import { createContext, lookup, registerAll, bindTo } from '../../context/context';
import { createEffectContext } from '../../effects/effectsContext.factory';
-import { HttpRequestBusToken, HttpServerClientToken } from '../../http/server/http.server.tokens';
import { ServerIO } from '../../listener/listener.interface';
import { LoggerToken, mockLogger } from '../../logger';
import { factorizeRegExpWithParams } from '../../http/router/http.router.params.factory';
import { HttpEffect } from '../../http/effects/http.effects.interface';
import { RoutingItem } from '../../http/router/http.router.interface';
+import { HttpRequestBusToken, HttpRequestBus } from '../../http/server/internal-dependencies/httpRequestBus.reader';
+import { HttpServerClientToken } from '../../http/server/http.server.tokens';
interface HttpRequestMockParams {
url?: string;
@@ -71,7 +71,7 @@ export const createHttpResponse = (data: HttpResponseMockParams = {}) =>
export const createMockEffectContext = () => {
const dependencies = [
bindTo(LoggerToken)(mockLogger),
- bindTo(HttpRequestBusToken)(() => new Subject<HttpRequest>()),
+ bindTo(HttpRequestBusToken)(HttpRequestBus),
bindTo(HttpServerClientToken)(() => http.createServer()),
];
const context = registerAll(dependencies)(createContext());

MODIFY
packages/core/src/http/router/http.router.resolver.v2.ts
packages/core/src/http/router/http.router.resolver.v2.ts
@@ -13,8 +13,8 @@ import {
isHttpRequestError,
} from '../error/http.error.model';
import { useContext } from '../../context/context.hook';
-import { HttpRequestBusToken } from '../server/http.server.tokens';
import { LoggerToken, LoggerTag, LoggerLevel } from '../../logger';
+import { HttpRequestBusToken } from '../server/internal-dependencies/httpRequestBus.reader';
import { Routing, BootstrappedRoutingItem } from './http.router.interface';
import { queryParamsFactory } from './http.router.query.factory';
import { matchRoute } from './http.router.matcher';
@@ -29,7 +29,7 @@ export const resolveRouting = (
output$?: HttpOutputEffect,
error$?: HttpErrorEffect,
) => {
- const requestBusSubject = useContext(HttpRequestBusToken)(ctx.ask);
+ const requestBus = useContext(HttpRequestBusToken)(ctx.ask);
const logger = useContext(LoggerToken)(ctx.ask);
const close$ = fromEvent(ctx.client, 'close').pipe(take(1), share());
@@ -146,7 +146,7 @@ export const resolveRouting = (
req.meta.path = resolvedRoute.path;
resolvedRoute.subject.next(req);
- requestBusSubject.next(req);
+ requestBus.next(req);
};
return {

MODIFY
packages/core/src/http/server/http.server.tokens.ts
packages/core/src/http/server/http.server.tokens.ts
-import { Subject } from 'rxjs';
import { createContextToken } from '../../context/context.token.factory';
-import { HttpServer, HttpRequest } from '../http.interface';
+import { HttpServer } from '../http.interface';
export const HttpServerClientToken = createContextToken<HttpServer>('HttpServerClientToken');
-export const HttpRequestBusToken = createContextToken<Subject<HttpRequest>>('HttpRequestBusToken');

MODIFY
packages/core/src/http/server/http.server.ts
packages/core/src/http/server/http.server.ts
import * as http from 'http';
import * as https from 'https';
-import { Subject, merge, EMPTY } from 'rxjs';
+import { merge, EMPTY } from 'rxjs';
import { takeWhile } from 'rxjs/operators';
import { pipe } from 'fp-ts/lib/pipeable';
import { lookup, bindTo, bindEagerlyTo } from '../../context/context';
import { createEffectContext } from '../../effects/effectsContext.factory';
import { isTestingMetadataOn } from '../../+internal/testing';
import { insertIf } from '../../+internal/utils';
-import { HttpRequest, HttpServer } from '../http.interface';
+import { HttpServer } from '../http.interface';
import { logContext } from '../../context/context.logger';
import { contextFactory } from '../../context/context.helper';
import { ServerIO } from '../../listener/listener.interface';
import { listening$, close$, error$ } from '../effects/http.effects';
import { LoggerTag } from '../../logger';
import { useContext } from '../../context/context.hook';
-import { HttpServerClientToken, HttpRequestBusToken } from './http.server.tokens';
+import { HttpServerClientToken } from './http.server.tokens';
import { CreateServerConfig } from './http.server.interface';
import { isCloseEvent } from './http.server.event';
// internal dependencies
import { HttpRequestMetadataStorage, HttpRequestMetadataStorageToken } from './internal-dependencies/httpRequestMetadataStorage.reader';
import { HttpServerEventStreamToken, HttpServerEventStream } from './internal-dependencies/httpServerEventStream.reader';
+import { HttpRequestBusToken, HttpRequestBus } from './internal-dependencies/httpRequestBus.reader';
export const createServer = async (config: CreateServerConfig) => {
const { listener, event$, port, hostname, dependencies = [], options = {} } = config;
@@ -29,10 +30,10 @@ export const createServer = async (config: CreateServerConfig) => {
? https.createServer(options.httpsOptions)
: http.createServer();
+ const boundHttpServerClient = bindEagerlyTo(HttpServerClientToken)(() => server);
const boundHttpServerEvent = bindEagerlyTo(HttpServerEventStreamToken)(HttpServerEventStream({ server, hostname }));
- const boundHttpServerClient = bindTo(HttpServerClientToken)(() => server);
+ const boundHttpRequestBus = bindEagerlyTo(HttpRequestBusToken)(HttpRequestBus);
const boundHttpRequestMetadataStorage = bindTo(HttpRequestMetadataStorageToken)(HttpRequestMetadataStorage);
- const boundHttpRequestBus = bindTo(HttpRequestBusToken)(() => new Subject<HttpRequest>());
const context = await contextFactory(
boundHttpServerClient,

ADD
packages/core/src/http/server/internal-dependencies/httpRequestBus.reader.ts
+import { Subject } from 'rxjs';
+import { HttpRequest } from '../../http.interface';
+import { createContextToken } from '../../../context/context.token.factory';
+import { createReader } from '../../../context/context.reader.factory';
+
+export type HttpRequestBus = ReturnType<typeof HttpRequestBus>;
+
+export const HttpRequestBusToken = createContextToken<HttpRequestBus>('HttpRequestBus');
+
+export const HttpRequestBus = createReader(_ => new Subject<HttpRequest>());

MODIFY
packages/core/src/index.spec.ts
packages/core/src/index.spec.ts
@@ -32,5 +32,6 @@ describe('@marblejs/core public API', () => {
// internal dependencies
expect(API.HttpServerEventStreamToken).toBeDefined();
expect(API.HttpRequestMetadataStorageToken).toBeDefined();
+ expect(API.HttpRequestBusToken).toBeDefined();
});
});

MODIFY
packages/core/src/index.ts
packages/core/src/index.ts
@@ -16,6 +16,7 @@ export * from './http/http.interface';
// http - server - internal dependencies
export * from './http/server/internal-dependencies/httpRequestMetadataStorage.reader';
export * from './http/server/internal-dependencies/httpServerEventStream.reader';
+export * from './http/server/internal-dependencies/httpRequestBus.reader';
// core - error
export { coreErrorFactory, CoreErrorOptions } from './error/error.factory';

refactor(core): #HttpRequestBus