improvement - optional HttpStatus in EffectResponse
MODIFY
example/controllers.ts
example/controllers.ts
import { map } from 'rxjs/operators';
-import { Effect, HttpStatus, matchPath, matchType } from '../src';
+import { Effect, matchPath, matchType } from '../src';
export const root$: Effect = request$ => request$
.pipe(
matchPath('/'),
matchType('GET'),
map(req => ({
- status: HttpStatus.OK,
body: { data: `API root @ ${req.url}` },
}))
);
@@ -16,7 +15,6 @@ export const hello$: Effect = request$ => request$
matchPath('/hello'),
matchType('POST'),
map(req => ({
- status: HttpStatus.OK,
body: { data: `Hello, ${req.body.data}! @ ${req.url}` },
}))
);

MODIFY
src/effects/effects.interface.ts
src/effects/effects.interface.ts
@@ -2,7 +2,7 @@ import { Observable } from 'rxjs';
import { HttpRequest, HttpResponse, HttpStatus } from '../http.interface';
export interface EffectResponse {
- status: HttpStatus;
+ status?: HttpStatus;
body?: object;
headers?: Record<string, string>;
}

MODIFY
src/response/response.handler.ts
src/response/response.handler.ts
-import { HttpResponse } from '../http.interface';
import { EffectResponse } from '../effects/effects.interface';
+import { HttpResponse, HttpStatus } from '../http.interface';
import { headersFactory } from './responseHeaders.factory';
export const handleResponse = (res: HttpResponse) => (effect: EffectResponse) => {
- res.writeHead(effect.status, headersFactory(effect.headers));
+ const status = effect.status || HttpStatus.OK;
+ res.writeHead(status, headersFactory(effect.headers));
res.end(JSON.stringify(effect.body));
};

improvement - optional HttpStatus in EffectResponse