16.0.0 • Published 2 months ago

@whook/http-transaction v16.0.0

Weekly downloads
218
License
MIT
Repository
github
Last release
2 months ago

@whook/http-transaction

Isolated HTTP Transactions for the Whook framework

GitHub license

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

ParamTypeDefaultDescription
servicesObjectThe services to inject
services.TIMEOUTNumber30000A number indicating how many ms the transaction should take to complete before being cancelled.
services.TRANSACTIONSObject{}A hash of every current transactions
services.delayObjectA delaying service
services.obfuscatorObjectA service to avoid logging sensible informations
services.logfunctionA logging function
services.apmfunctionAn apm function
services.timefunctionA timing function
services.uniqueIdfunctionA 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.

ParamTypeDescription
reqHTTPRequestA raw NodeJS HTTP incoming message
resHTTPResponseA raw NodeJS HTTP response

pickFirstHeaderValue(name, headers) ⇒ string

Pick the first header value if exists

Kind: global function
Returns: string - The value if defined.

ParamTypeDescription
namestringThe header name
headersObjectThe headers map

pickAllHeaderValues(name, headers) ⇒ Array

Pick header values

Kind: global function
Returns: Array - The values in an array.

ParamTypeDescription
namestringThe header name
headersObjectThe 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.

ParamTypeDescription
servicesObjectThe services to inject
services.logfunctionA logging function

initObfuscator(services) ⇒ Promise.<Object>

Obfuscate sensible informations.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the gathered constants.

ParamTypeDescription
servicesObjectThe service dependend on
services.SHIELD_CHARObjectThe char for replacing sensible informations
services.MAX_CLEAR_CHARSObjectThe maximum clear chars to display
services.MAX_CLEAR_RATIOObjectThe maximum clear chars ratio to display
services.SENSIBLE_PROPSObjectSensible properties names
services.SENSIBLE_HEADERSObjectSensible 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.

ParamTypeDescription
buildResponsefunctionA 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.

ParamTypeDescription
errErrorA 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.

ParamTypeDescription
responseObjectA response for the transaction

Authors

License

MIT

16.0.0

2 months ago

15.0.0

3 months ago

13.1.0

9 months ago

13.2.0

7 months ago

14.0.0

6 months ago

13.0.0

9 months ago

10.0.6

2 years ago

10.0.0

2 years ago

10.0.1

2 years ago

10.0.2

2 years ago

10.0.3

2 years ago

10.0.4

2 years ago

12.0.0

1 year ago

12.0.1

1 year ago

11.0.0

1 year ago

11.0.1

1 year ago

9.0.1

2 years ago

9.0.0

2 years ago

8.5.0

3 years ago

8.3.0

3 years ago

8.1.0

3 years ago

8.0.4

3 years ago

8.0.0

3 years ago

8.0.3

3 years ago

7.1.3

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.0

3 years ago

6.0.0

3 years ago

5.1.4

4 years ago

5.1.3

4 years ago

5.1.2

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.4

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-alpha.44

4 years ago

4.0.0-alpha.43

4 years ago

4.0.0-alpha.42

4 years ago

4.0.0-alpha.41

4 years ago

4.0.0-alpha.40

4 years ago

4.0.0-alpha.39

4 years ago

4.0.0-alpha.38

4 years ago

4.0.0-alpha.37

4 years ago

4.0.0-alpha.36

4 years ago

4.0.0-alpha.35

4 years ago

4.0.0-alpha.34

4 years ago

4.0.0-alpha.33

4 years ago

4.0.0-alpha.32

4 years ago

4.0.0-alpha.31

4 years ago

4.0.0-alpha.30

4 years ago

4.0.0-alpha.29

4 years ago

4.0.0-alpha.28

4 years ago

4.0.0-alpha.27

4 years ago

4.0.0-alpha.26

4 years ago

4.0.0-alpha.25

4 years ago

4.0.0-alpha.24

4 years ago

4.0.0-alpha.20

4 years ago

4.0.0-alpha.23

4 years ago

4.0.0-alpha.22

4 years ago

4.0.0-alpha.21

4 years ago

4.0.0-alpha.19

4 years ago

4.0.0-alpha.18

4 years ago

4.0.0-alpha.17

4 years ago

4.0.0-alpha.16

4 years ago

4.0.0-alpha.15

4 years ago

4.0.0-alpha.14

4 years ago

4.0.0-alpha.13

4 years ago

4.0.0-alpha.12

4 years ago

4.0.0-alpha.11

5 years ago

4.0.0-alpha.10

5 years ago

4.0.0-alpha.9

5 years ago

4.0.0-alpha.8

5 years ago

4.0.0-alpha.7

5 years ago

4.0.0-alpha.6

5 years ago

4.0.0-alpha.5

5 years ago

4.0.0-alpha.4

5 years ago

4.0.0-alpha.3

5 years ago

4.0.0-alpha.0

5 years ago