use()
respond()
check()
wait()
join()
reject()
next()
timeout()
check()
operatorChecks each packet based on given function, if it passes, will pass it down the observable sequence, otherwise will respond with specified status and message:
1import { check } from 'rxxpress';
2
3router.get('/')
4.pipe(
5 check(({req}) => !!req.query.token, {
6 status: 400,
7 message: 'I need a token!'
8 }),
9 respond(({req}) => `Got token:: ${req.query.token}`)
10)
11.subscribe();
If not provided, status 500
and an empty message will be sent:
1router.get('/')
2.pipe(
3 check(({req}) => !!req.query.token),
4 respond(({req}) => `Got token:: ${req.query.token}`)
5)
6.subscribe();
You can use validate()
, authorize()
, allow()
and find()
operators
instead of check()
. They are identical except that they respond with
corresponding http status (400
, 401
, 403
, 404
):
1import { validate, authorize, allow, find } from 'rxxpress';
2
3router.get('/')
4.pipe(
5 validate(({req}) => !!req.query.token),
6 respond(({req}) => `Got token:: ${req.query.token}`)
7)
8.subscribe();
Or with a custom message:
1router.get('/')
2.pipe(
3 validate(({req}) => !!req.query.token, 'I need a token!'),
4 respond(({req}) => `Got token:: ${req.query.token}`)
5)
6.subscribe();
Your checks can also be asyncronous:
1import { verify } from 'jsonwebtoken';
2
3
4router.get('/')
5.pipe(
6 validate(({req}) => !!req.query.token),
7 authorize(({req}) => new Promise(resolve => {
8 verify(req.query.token, SECRET, (err, res) => {
9 if (err) resolve(false);
10 else {
11 req._.payload = res;
12 resolve(true);
13 }
14 }));
15 })),
16 ...
17)
18.subscribe();
Your checks can also return observables:
1router.get('/:user_id')
2.pipe(
3 authenticate(),
4 allow(({req}) => of(req.params.user_id === req._.user.id).pipe(delay(200))),
5 json(({req}) => req._.user)
6)
The first value emitted by return observable will be used.
check()
operator will not conduct checks on incoming requests
if they are already responded to. Also if the request is responded to while
the check is being conducted, it will ignore the result of the check
and ignore the packet as a whole.
If the check fails, the packet won't be passed down the observable sequence. Otherwise, it will be passed down.
Additionally, if a request is already responded to or if it is responded to while the check is being conducted, the packet will not be passed down.