Star

Created With

linkRequest Context



⚠️⚠️ WARNING ⚠️⚠️

This feature MUST be used with cautious. Accessing request context is intended ONLY for webhooks. Functions accessing request context CAN NOT BE USED by TyFON clients.


Sometimes it might be useful to access request context, i.e. the received network request object and a response object for manually responding. This is specifically true when a function is invoked by some client other than a TyFON client, where the request is perhaps not populated according to TyFON conventions.

In order to access request context, first install tyfon-server:

1link$npm i tyfon-server


Now access the context like this:

1linkimport { RequestContextAware } from 'tyfon-server/context';

2linkimport { accessRequestContext } from 'tyfon-server';

3link

4link

5linkexport function myWebHook(this: RequestContextAware) {

6link const context = accessRequestContext(this);

7link

8link context.request.body; // --> this is the request body

9link

10link // ...

11link

12link context.response.status(418).send();

13link}

☝️ You can also return a value (or a promise of a value), which would be serialized to JSON and sent with status code 200, like any other TyFON function. If you respond manually, any returned value will be ignored.


Note that myWebHook() will now be unusable in TyFON client code:

1linkimport { myWebHook } from '@api/my-tyfon-server';

2link

3linkmyWebHook~~~~~~~~~(~)~; // --> `myWebHook()` expects a `this` argument now.



Request Context

Home Conventions Error Handling

CLI Referencechevron_right
Advanced Usagechevron_right