@whook/http-transaction
v16.0.0
Published
Isolated HTTP Transactions for the Whook framework
Downloads
687
Maintainers
Readme
@whook/http-transaction
Isolated HTTP Transactions for the Whook framework
Whook takes a very unusual direction when
it comes to dealing with HTTP transactions. It makes requests and responses
serializable (thanks to WhookRequest
and WhookResponse
types) to:
- only work with functions that take request and return responses ( allowing your handlers to be pure functions),
- have easily unit testable handlers thanks to concise snapshots.
This service is intended to build those litteral objects from Node HTTP ones (famously known as req/res) before passing them to the handlers. It also keeps track of running queries and ensure it is well handled by the server before releasing it. If not, the transaction is resolved with an error response (for timeouts or when an error were catched).
API
Functions
Typedefs
initHTTPTransaction(services) ⇒ Promise.<WhookHTTPTransaction>
Instantiate the httpTransaction service
Kind: global function
Returns: Promise.<WhookHTTPTransaction> - A promise of the httpTransaction function
| Param | Type | Default | Description | | --- | --- | --- | --- | | services | Object | | The services to inject | | [services.TIMEOUT] | Number | 30000 | A number indicating how many ms the transaction should take to complete before being cancelled. | | [services.TRANSACTIONS] | Object | {} | A hash of every current transactions | | services.delay | Object | | A delaying service | | services.obfuscator | Object | | A service to avoid logging sensible informations | | [services.log] | function | | A logging function | | [services.apm] | function | | An apm function | | [services.time] | function | | A timing function | | [services.uniqueId] | function | | A function returning unique identifiers |
Example
import initHTTPTransaction from '@whook/http-transaction';
import { log } from 'node:console';
const httpTransaction = await initHTTPTransaction({
log,
time: Date.now.bind(Date),
});
initHTTPTransaction~httpTransaction(req, res) ⇒ Array
Create a new HTTP transaction
Kind: inner method of initHTTPTransaction
Returns: Array - The normalized request and the HTTP
transaction created in an array.
| Param | Type | Description | | --- | --- | --- | | req | HTTPRequest | A raw NodeJS HTTP incoming message | | res | HTTPResponse | A raw NodeJS HTTP response |
pickFirstHeaderValue(name, headers) ⇒ string
Pick the first header value if exists
Kind: global function
Returns: string - The value if defined.
| Param | Type | Description | | --- | --- | --- | | name | string | The header name | | headers | Object | The headers map |
pickAllHeaderValues(name, headers) ⇒ Array
Pick header values
Kind: global function
Returns: Array - The values in an array.
| Param | Type | Description | | --- | --- | --- | | name | string | The header name | | headers | Object | The headers map |
initAPM(services) ⇒ Promise.<Object>
Application monitoring service that simply log stringified contents.
Kind: global function
Returns: Promise.<Object> - A promise of the apm service.
| Param | Type | Description | | --- | --- | --- | | services | Object | The services to inject | | [services.log] | function | A logging function |
initObfuscator(services) ⇒ Promise.<Object>
Obfuscate sensible informations.
Kind: global function
Returns: Promise.<Object> - A promise of an object containing the gathered constants.
| Param | Type | Description | | --- | --- | --- | | services | Object | The service dependend on | | [services.SHIELD_CHAR] | Object | The char for replacing sensible informations | | [services.MAX_CLEAR_CHARS] | Object | The maximum clear chars to display | | [services.MAX_CLEAR_RATIO] | Object | The maximum clear chars ratio to display | | [services.SENSIBLE_PROPS] | Object | Sensible properties names | | [services.SENSIBLE_HEADERS] | Object | Sensible headers names |
Example
import { initObfuscator } from '@whook/http-transaction';
import { alsoInject } from 'knifecycle';
import { log } from 'node:console';
const obfuscator = await initObfuscator();
log(obfuscator('my very secret information!));
// my ...on!
WhookHTTPTransaction
Kind: global typedef
WhookHTTPTransaction.id
Id of the transaction
Kind: static property of WhookHTTPTransaction
WhookHTTPTransaction.start ⇒ Promise.<Object>
Start the transaction
Kind: static property of WhookHTTPTransaction
Returns: Promise.<Object> - A promise to be resolved with the signed token.
| Param | Type | Description | | --- | --- | --- | | buildResponse | function | A function that builds a response |
WhookHTTPTransaction.catch ⇒ Promise
Catch a transaction error
Kind: static property of WhookHTTPTransaction
Returns: Promise - A promise to be resolved with the signed token.
| Param | Type | Description | | --- | --- | --- | | err | Error | A function that builds a response |
WhookHTTPTransaction.end ⇒ Promise.<Object>
End the transaction
Kind: static property of WhookHTTPTransaction
Returns: Promise.<Object> - A promise to be resolved with the signed token.
| Param | Type | Description | | --- | --- | --- | | response | Object | A response for the transaction |