# @compassdigital/middleware

> CDL Provider middleware module

Latest version **5.0.3** (published 2022-10-14) · ISC license · 0 weekly downloads

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

## Install

```sh
npm install @compassdigital/middleware
pnpm add @compassdigital/middleware
yarn add @compassdigital/middleware
bun add @compassdigital/middleware
```

## Health

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

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 5.0.3 |
| Published | 2022-10-14 |
| First published | 2019-01-31 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 17.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Robert Pisani |
| Maintainers | hiteshi.patel, gagandeepk, gaurangcompass, sitharam_t, sitharams_t, drashti_patel, mbkhan1995, ds-brian, pallavipulivarthy, rajanikandepucdl, svietz, vivien.igweze, shivani-aggarwal, darren.rahnemoon.cdl, sanjay_tech, brahmrshi_tech, tfox121, rachitaj, matthewclair, qihangsong, collinstonefromcompass, hermsidhu, ananthu.cdl, ospozito, cdl-samir-thaker, jitesh.tfpl, dhruv.seth, ricardodesimas, sbeaury-cdl, zach-cdl, jrodri, svtokarevsv, rcjpisani, danialhasan, lalityabijjam, astrit-cdl, keyur-cdl, roya-cdl, jpdemiranda, toronto.devops, brahmrshiraval, coreycosman, pberoy, andrei-cdl, alberttir, umeshprasad, kieranshb, eirabie, coreycosman.cdl, marlondc, bcardoso, eiston-cdl, icholy, brunnodatum |
| Keywords | cdl |

## Links

- npm: https://www.npmjs.com/package/@compassdigital/middleware
- Repository: https://github.com/compassdigital/compassdigital.middleware
- Homepage: https://github.com/compassdigital/compassdigital.middleware#readme
- Issues: https://github.com/compassdigital/compassdigital.middleware/issues
- npm.io page: https://npm.io/package/@compassdigital/middleware

## Dependencies (11)

- [uuid](https://npm.io/package/uuid.md) ^7.0.2
- [aws-sdk](https://npm.io/package/aws-sdk.md) ^2.581.0
- [dynamoose](https://npm.io/package/dynamoose.md) ^1.6.5
- [redactyl.js](https://npm.io/package/redactyl.js.md) ^1.2.0
- [aws-xray-sdk](https://npm.io/package/aws-xray-sdk.md) ^2.5.0
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.2
- [@compassdigital/id](https://npm.io/package/@compassdigital/id.md) ^3.6.0
- [@compassdigital/auth](https://npm.io/package/@compassdigital/auth.md) ^5.0.1
- [@splitsoftware/splitio](https://npm.io/package/@splitsoftware/splitio.md) ^10.15.5
- [@compassdigital/constant](https://npm.io/package/@compassdigital/constant.md) ^0.3.0
- [@compassdigital/superagent](https://npm.io/package/@compassdigital/superagent.md) ^0.0.8

## Recent versions

- 5.0.3 (latest) — 2022-10-14
- 5.0.2 — 2022-10-14
- 5.0.1 — 2022-10-14
- 5.0.0 — 2022-10-14
- 4.1.0 — 2022-06-08
- 4.0.2 — 2022-04-28
- 4.0.1 — 2022-01-06
- 4.0.0 — 2021-11-03
- 3.3.3 — 2021-08-30
- 3.3.2 — 2021-07-07
- 3.2.2 — 2021-05-07
- 3.2.1 — 2021-05-05
- 3.2.0 — 2021-05-03
- 3.1.0 — 2020-09-23
- 3.0.1 — 2020-04-30
- … 19 more at https://npm.io/package/@compassdigital/middleware/versions

## README

# compassdigital.middleware

## CDL Provider middleware module
- This middleware module extends the CDL core provider with the ability to run functions before requests get handled
- This can be handled at the provider level (for every incoming request) or at the event level (for particular events, ex: "post_order")

### Usage
Registering Provider-level middleware
```js
const Middleware = require("@compassdigital/middleware").handler;
const ActionLogger = require("@compassdigital/middleware").fn.ActionLogger;

/**
 * Inside the Provider constructor.
 * The "bind" call is needed for this particular function but
 * is not needed to make any custom middleware work with this
 * package.
 */
this.middleware = new Middleware(ActionLogger.bind(this));
```

Registering handler-level middleware
```js
const Middleware = require("@compassdigital/middleware").handler;

// Inside the domain-specific Provider
function bodyLogger (req, res, next) {
	/**
	 * req: the request object from the lambda event
	 * res: the response object currently only holds a reference to user-defined local variables specific to the lifecycle of a single request
	 * next: the callback function to call when the middleware is finished work.
	 *	- No parameters passes the middleware off to the next layer in the stack
	 *	- Passing an error will cause the provider's error handler to take over and send the appropiate response
	 *	- Passing true will skip the rest of the middleware for the current request
	 /
	console.log(JSON.stringify(req.body));
	return next();
}

Provider.on("post_test", "*:*:*", [ bodyLogger, async function (req, next) {
	// Business logic here...
}]);
```

#### Feature Flagging
Feature flagging middleware provides an easy way to get state of flags in the req object.

```js
const FeatureFlagging = require('@compassdigital/middleware').fn.FeatureFlagging;
```
You can register the middleware at the provider level with minimal boilerplate code.

```js

UserProvider.use(FeatureFlagging.call(UserProvider));
```

Or at the handler level
```js
Provider.on("post_test", "*:*:*", [FeatureFlagging(), async function (req, next) {
	// Business logic here...
}]);
```

From here on, a `feature_flags` property will be exposed on the `req` object. You can then use the flags as
```js
Provider.on("post_test", "*:*:*", async function (req, next) {
	if (req.feature_flags.my_feature_1) doSomething();
});
```
##### Split API Key

For the middleware to be able to contact Split, an environment variable must be set in the provider's `serverless.yml` file.
```yml
provider:
    environment:
        SPLIT_KEY: ${config:SPLIT_KEY}
```

##### Flag Caching
Before the middleware hits Split to retrieve the flags, it gets a CDL config file that has a cache_time property. This tells the middleware how long it should hold on to the flags before getting them again from Split. This can be changed.
```json
{
    "is_split_enabled": true,
    "cache_time": 10000
}
```

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