improvement - imports cleanup + removed unused imports
MODIFY
example/controllers.ts
example/controllers.ts
import { filter, map } from 'rxjs/operators';
-import { Effect, EffectResponse } from '../src';
+import { Effect } from '../src';
import { StatusCode } from '../src/util';
export const root$: Effect = request$ => request$

MODIFY
example/index.ts
example/index.ts
import * as http from 'http';
import { root$, hello$ } from './controllers';
-import { httpListener } from '../src';
+import { httpListener, logger$ } from '../src';
const createServer = () => {
const HOSTNAME = '127.0.0.1';
const PORT = 1337;
- const app = httpListener(
+ const middlewares = [
+ logger$,
+ ];
+
+ const effects = [
root$,
hello$,
- );
+ ];
+
+ const app = httpListener(middlewares, effects);
const httpServer = http
.createServer(app)
.listen(PORT, HOSTNAME, () => {
console.log(`Server running @ http://${HOSTNAME}:${PORT}/`);
});
+
+ httpServer.on('close', () => {
+ console.log(`Connection closed`);
+ });
};
createServer();

MODIFY
src/effects/effects.combiner.ts
src/effects/effects.combiner.ts
-import * as http from 'http';
-import { Observable, merge, forkJoin, of } from 'rxjs';
-import { switchMap, tap } from 'rxjs/operators';
-import { StatusCode } from '../util/status-code.util';
+import { Observable, merge, of } from 'rxjs';
+import { switchMap } from 'rxjs/operators';
import { Effect, EffectResponse } from './effects.interface';
import { HttpRequest, HttpResponse } from '../http.interface';

MODIFY
src/http.listener.ts
src/http.listener.ts
-import * as http from 'http';
import { Subject, of } from 'rxjs';
-import { defaultIfEmpty, map, tap, switchMap } from 'rxjs/operators';
+import { defaultIfEmpty, tap, switchMap } from 'rxjs/operators';
import { Http, HttpRequest, HttpResponse } from './http.interface';
-import { combineEffects, Effect, EffectResponse, combineMiddlewareEffects } from './effects';
-import { loggerMiddleware } from './middlewares';
+import { combineEffects, Effect, combineMiddlewareEffects } from './effects';
import { handleResponse } from './response';
import { StatusCode } from './util';
-const middlewares = [
- loggerMiddleware,
-];
-
-export const httpListener = (...effects: Effect[]) => {
+export const httpListener = (middlewares: Effect<HttpRequest>[], effects: Effect[]) => {
const request$ = new Subject<Http>();
const effect$ = request$
.pipe(
switchMap(({ req, res }) =>
combineMiddlewareEffects(...middlewares)(of(req), res)
.pipe(
- switchMap(req =>
- combineEffects(...effects)(of(req), res)
- .pipe(
+ switchMap(req => combineEffects(...effects)(of(req), res)),
defaultIfEmpty({ status: StatusCode.NOT_FOUND }),
- tap(effect => handleResponse(res)(effect))
- )
- ),
+ tap(handleResponse(res))
)
)
);

MODIFY
src/middlewares/logger.middleware.ts
src/middlewares/logger.middleware.ts
import chalk from 'chalk';
-import { of } from 'rxjs';
-import { map, switchMap, tap } from 'rxjs/operators';
-import { Http, HttpResponse, HttpRequest } from '../http.interface';
+import { tap } from 'rxjs/operators';
+import { HttpRequest } from '../http.interface';
import { Effect } from '../effects/effects.interface';
-import { start } from 'repl';
const getStatusCode = (statusCode: number): string =>
statusCode >= 400
@@ -24,7 +22,7 @@ const printLog = (method: string, url: string, statusCode: number, startTime: [n
);
};
-export const loggerMiddleware: Effect<HttpRequest> = (request$, response) => request$
+export const logger$: Effect<HttpRequest> = (request$, response) => request$
.pipe(
tap(request => {
const startTime = process.hrtime();

MODIFY
src/response/response.handler.ts
src/response/response.handler.ts
-import * as http from 'http';
-import { HttpResponse, HttpHeaders } from '../http.interface';
+import { HttpResponse } from '../http.interface';
import { EffectResponse } from '../effects/effects.interface';
import { headersFactory } from './responseHeaders.factory';

MODIFY
tsconfig.json
tsconfig.json
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": false,
+ "noUnusedLocals": true,
+ "noUnusedParameters": true,
"noEmitOnError": true,
"outDir": "./dist",
"pretty": true,

improvement - imports cleanup + removed unused imports