# @whook/http-transaction

> Isolated HTTP Transactions for the Whook framework

Latest version **18.0.0** (published 2024-12-05) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @whook/http-transaction
pnpm add @whook/http-transaction
yarn add @whook/http-transaction
bun add @whook/http-transaction
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 18.0.0 |
| Published | 2024-12-05 |
| First published | 2019-02-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=20.11.1 |
| Dependencies | 8 |
| Unpacked size | 132.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 31 |
| Author | Nicolas Froidure |
| Maintainers | nfroidure |
| Keywords | whook, knifecycle, REST, HTTP, OpenAPI, web, service, transaction |

## Links

- npm: https://www.npmjs.com/package/@whook/http-transaction
- Repository: https://github.com/nfroidure/whook
- Issues: https://github.com/nfroidure/whook/issues
- npm.io page: https://npm.io/package/@whook/http-transaction

## Dependencies (8)

- [ms](https://npm.io/package/ms.md) ^2.1.3
- [yerror](https://npm.io/package/yerror.md) ^8.0.0
- [statuses](https://npm.io/package/statuses.md) ^2.0.1
- [type-fest](https://npm.io/package/type-fest.md) ^4.30.0
- [knifecycle](https://npm.io/package/knifecycle.md) ^18.0.0
- [yhttperror](https://npm.io/package/yhttperror.md) ^8.0.0
- [openapi-types](https://npm.io/package/openapi-types.md) ^12.1.3
- [common-services](https://npm.io/package/common-services.md) ^17.0.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 18.0.0 (latest) — 2024-12-05
- 17.0.2 — 2024-10-02
- 17.0.0 — 2024-07-16
- 16.1.1 — 2024-05-27
- 16.0.0 — 2024-02-27
- 15.0.0 — 2024-02-20
- 14.0.0 — 2023-11-07
- 13.2.0 — 2023-09-29
- 13.1.0 — 2023-08-20
- 13.0.0 — 2023-08-16
- 12.0.1 — 2023-01-05
- 12.0.0 — 2022-12-30
- 11.0.1 — 2022-11-14
- 11.0.0 — 2022-11-10
- 10.0.6 — 2022-09-30
- … 72 more at https://npm.io/package/@whook/http-transaction/versions

## README

[//]: # ( )
[//]: # (This file is automatically generated by a `metapak`)
[//]: # (module. Do not change it  except between the)
[//]: # (`content:start/end` flags, your changes would)
[//]: # (be overridden.)
[//]: # ( )
# @whook/http-transaction
> Isolated HTTP Transactions for the Whook framework

[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/whook/blob/main/packages/whook-http-transaction/LICENSE)


[//]: # (::contents:start)

[Whook](https://github.com/nfroidure/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](https://github.com/nfroidure/whook/blob/e7470fed860a8e1644b15625c9db4dd8198b70a6/packages/whook-example/src/handlers/putEcho.test.js)
  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).

[//]: # (::contents:end)

# API
## Functions

<dl>
<dt><a href="#initHTTPTransaction">initHTTPTransaction(services)</a> ⇒ <code><a href="#WhookHTTPTransaction">Promise.&lt;WhookHTTPTransaction&gt;</a></code></dt>
<dd><p>Instantiate the httpTransaction service</p>
</dd>
<dt><a href="#pickFirstHeaderValue">pickFirstHeaderValue(name, headers)</a> ⇒ <code>string</code></dt>
<dd><p>Pick the first header value if exists</p>
</dd>
<dt><a href="#pickAllHeaderValues">pickAllHeaderValues(name, headers)</a> ⇒ <code>Array</code></dt>
<dd><p>Pick header values</p>
</dd>
<dt><a href="#initAPM">initAPM(services)</a> ⇒ <code>Promise.&lt;Object&gt;</code></dt>
<dd><p>Application monitoring service that simply log stringified contents.</p>
</dd>
<dt><a href="#initObfuscator">initObfuscator(services)</a> ⇒ <code>Promise.&lt;Object&gt;</code></dt>
<dd><p>Obfuscate sensible informations.</p>
</dd>
</dl>

## Typedefs

<dl>
<dt><a href="#WhookHTTPTransaction">WhookHTTPTransaction</a></dt>
<dd></dd>
</dl>

<a name="initHTTPTransaction"></a>

## initHTTPTransaction(services) ⇒ [<code>Promise.&lt;WhookHTTPTransaction&gt;</code>](#WhookHTTPTransaction)
Instantiate the httpTransaction service

**Kind**: global function  
**Returns**: [<code>Promise.&lt;WhookHTTPTransaction&gt;</code>](#WhookHTTPTransaction) - A promise of the httpTransaction function  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| services | <code>Object</code> |  | The services to inject |
| [services.TIMEOUT] | <code>Number</code> | <code>30000</code> | A number indicating how many ms the transaction  should take to complete before being cancelled. |
| [services.TRANSACTIONS] | <code>Object</code> | <code>{}</code> | A hash of every current transactions |
| services.delay | <code>Object</code> |  | A delaying service |
| services.obfuscator | <code>Object</code> |  | A service to avoid logging sensible informations |
| [services.log] | <code>function</code> |  | A logging function |
| [services.apm] | <code>function</code> |  | An apm function |
| [services.time] | <code>function</code> |  | A timing function |
| [services.uniqueId] | <code>function</code> |  | A function returning unique identifiers |

**Example**  
```js
import initHTTPTransaction from '@whook/http-transaction';
import { log } from 'node:console';

const httpTransaction = await initHTTPTransaction({
  log,
  time: Date.now.bind(Date),
});
```
<a name="initHTTPTransaction..httpTransaction"></a>

### initHTTPTransaction~httpTransaction(req, res) ⇒ <code>Array</code>
Create a new HTTP transaction

**Kind**: inner method of [<code>initHTTPTransaction</code>](#initHTTPTransaction)  
**Returns**: <code>Array</code> - The normalized request and the HTTP
transaction created in an array.  

| Param | Type | Description |
| --- | --- | --- |
| req | <code>HTTPRequest</code> | A raw NodeJS HTTP incoming message |
| res | <code>HTTPResponse</code> | A raw NodeJS HTTP response |

<a name="pickFirstHeaderValue"></a>

## pickFirstHeaderValue(name, headers) ⇒ <code>string</code>
Pick the first header value if exists

**Kind**: global function  
**Returns**: <code>string</code> - The value if defined.  

| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | The header name |
| headers | <code>Object</code> | The headers map |

<a name="pickAllHeaderValues"></a>

## pickAllHeaderValues(name, headers) ⇒ <code>Array</code>
Pick header values

**Kind**: global function  
**Returns**: <code>Array</code> - The values in an array.  

| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | The header name |
| headers | <code>Object</code> | The headers map |

<a name="initAPM"></a>

## initAPM(services) ⇒ <code>Promise.&lt;Object&gt;</code>
Application monitoring service that simply log stringified contents.

**Kind**: global function  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise of the apm service.  

| Param | Type | Description |
| --- | --- | --- |
| services | <code>Object</code> | The services to inject |
| [services.log] | <code>function</code> | A logging function |

<a name="initObfuscator"></a>

## initObfuscator(services) ⇒ <code>Promise.&lt;Object&gt;</code>
Obfuscate sensible informations.

**Kind**: global function  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise of an object containing the gathered constants.  

| Param | Type | Description |
| --- | --- | --- |
| services | <code>Object</code> | The service dependend on |
| [services.SHIELD_CHAR] | <code>Object</code> | The char for replacing sensible informations |
| [services.MAX_CLEAR_CHARS] | <code>Object</code> | The maximum clear chars to display |
| [services.MAX_CLEAR_RATIO] | <code>Object</code> | The maximum clear chars ratio to display |
| [services.SENSIBLE_PROPS] | <code>Object</code> | Sensible properties names |
| [services.SENSIBLE_HEADERS] | <code>Object</code> | Sensible headers names |

**Example**  
```js
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!
```
<a name="WhookHTTPTransaction"></a>

## WhookHTTPTransaction
**Kind**: global typedef  

* [WhookHTTPTransaction](#WhookHTTPTransaction)
    * [.id](#WhookHTTPTransaction.id)
    * [.start](#WhookHTTPTransaction.start) ⇒ <code>Promise.&lt;Object&gt;</code>
    * [.catch](#WhookHTTPTransaction.catch) ⇒ <code>Promise</code>
    * [.end](#WhookHTTPTransaction.end) ⇒ <code>Promise.&lt;Object&gt;</code>

<a name="WhookHTTPTransaction.id"></a>

### WhookHTTPTransaction.id
Id of the transaction

**Kind**: static property of [<code>WhookHTTPTransaction</code>](#WhookHTTPTransaction)  
<a name="WhookHTTPTransaction.start"></a>

### WhookHTTPTransaction.start ⇒ <code>Promise.&lt;Object&gt;</code>
Start the transaction

**Kind**: static property of [<code>WhookHTTPTransaction</code>](#WhookHTTPTransaction)  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise to be resolved with the signed token.  

| Param | Type | Description |
| --- | --- | --- |
| buildResponse | <code>function</code> | A function that builds a response |

<a name="WhookHTTPTransaction.catch"></a>

### WhookHTTPTransaction.catch ⇒ <code>Promise</code>
Catch a transaction error

**Kind**: static property of [<code>WhookHTTPTransaction</code>](#WhookHTTPTransaction)  
**Returns**: <code>Promise</code> - A promise to be resolved with the signed token.  

| Param | Type | Description |
| --- | --- | --- |
| err | <code>Error</code> | A function that builds a response |

<a name="WhookHTTPTransaction.end"></a>

### WhookHTTPTransaction.end ⇒ <code>Promise.&lt;Object&gt;</code>
End the transaction

**Kind**: static property of [<code>WhookHTTPTransaction</code>](#WhookHTTPTransaction)  
**Returns**: <code>Promise.&lt;Object&gt;</code> - A promise to be resolved with the signed token.  

| Param | Type | Description |
| --- | --- | --- |
| response | <code>Object</code> | A response for the transaction |


# Authors
- [Nicolas Froidure](http://insertafter.com/en/index.html)

# License
[MIT](https://github.com/nfroidure/whook/blob/main/packages/whook-http-transaction/LICENSE)

---
_Source: https://npm.io/package/@whook/http-transaction · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
