Output
Every Marble.js listener factory allows you to intercept outgoing messages via dedicated output$ handler.

Building your own output effect

Using HttpOutputEffect you can grab an outgoing Effect response and map it into different one, modifying outgoing data on demand.
1
type Out = {
2
request: HttpRequest;
3
headers: HttpHeaders;
4
status: HttpStatus;
5
body: any;
6
};
7
8
HttpOutputEffect :: Observable<Out> -> Observable<Out>
Copied!
HttpOutputEffect allows you to grab the outgoing response together with corresponding initial request. The output effect works similar to the HttpMiddlewareEffect, but in that case for outgoing responses. Let's build a simple response compression middleware.
output.effect.ts
1
import { HttpOutputEffect } from '@marblejs/http';
2
import { map } from 'rxjs/operators';
3
import * as zlib from 'zlib';
4
5
const output$: HttpOutputEffect = res$ =>
6
res$.pipe(
7
map(({ request, headers, bodu, status }) => {
8
switch(request.headers['accept-encoding']) {
9
case 'br':
10
return ({
11
request,
12
status,
13
headers: { ...headers, 'Content-Encoding': 'br' },
14
body: body.pipe(zlib.createBrotliDecompress()),
15
});
16
case 'gzip':
17
return ({
18
request,
19
status,
20
headers: { ...headers, 'Content-Encoding': 'gzip' },
21
body: body.pipe(zlib.createGunzip()),
22
});
23
case 'deflate':
24
return ({
25
request,
26
status,
27
headers: { ...headers, 'Content-Encoding': 'deflate' },
28
body: body.pipe(zlib.createInflate()),
29
});
30
default:
31
return { status, headers, body, request };
32
}
33
}),
34
);
Copied!
To connect the output effect, all you need to do is to attach it to output$ property in httpListener config object.
1
import { httpListener } from '@marblejs/http';
2
import { output$ } from './output.effect';
3
4
export const listener = httpListener({
5
middlewares: [ ... ],
6
effects: [ ... ],
7
output$, // 👈
8
});
Copied!
Last modified 1mo ago
Copy link