# @domx/middleware

> Contains low level patterns for middleware and HTMLElement mixin logging

Latest version **1.0.0** (published 2022-10-04) · MIT license · 0 weekly downloads

## Install

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

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-10-04 |
| First published | 2021-08-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 89.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 8 |
| Author | John Horback |
| Maintainers | jhorback |
| Keywords | Custom Elements, Logging, Middleware |

## Links

- npm: https://www.npmjs.com/package/@domx/middleware
- Repository: https://github.com/domxjs/domx
- Homepage: https://github.com/domxjs/domx/blob/master/packages/Middleware/README.md
- Issues: https://github.com/domxjs/domx/issues
- npm.io page: https://npm.io/package/@domx/middleware

## Dependencies (1)

- [@domx/functional](https://npm.io/package/@domx/functional.md) 1.0.0

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.0 (latest) — 2022-10-04
- 0.2.9 — 2021-09-22
- 0.2.8 — 2021-09-10
- 0.2.7 — 2021-08-26

## README

# Middleware &middot; [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://www.mit.edu/~amini/LICENSE.md) [![Build Status](https://travis-ci.com/domxjs/domx.svg?branch=packages/Middleware)](https://travis-ci.com/github/domxjs/domx) [![Lines](https://img.shields.io/badge/Coverage-100%25-brightgreen.svg?style=flat)](https://app.travis-ci.com/github/domxjs/domx/branches) [![npm](https://img.shields.io/npm/v/@domx/middleware)](https://www.npmjs.com/package/@domx/middleware)


Contains low level patterns for middleware and HTMLElement mixin logging.

## Installation
```sh
npm install @domx/middleware
```


## Middleware
A class that can be used to implement a middleware pattern.
Middleware can be used for common concerns such as logging, error handling, reporting, etc.

### Adding middleware
The Middleware class is to be used by modules wanting to expose middleware.
```js
export {useMiddleware};
const mw = new Middleware();
const useMiddleware = (fn) => mw.use(fn);
```

### Executing middleware
There are two function structures that can be used when executing the middleware.
Both take a `next` method and an arguments array to be passed to the `next` function.
Each middleware function is required call the `next` function with
arguments and return the `next` functions return value.

#### Full Example
`TestMiddleware.ts`
```js
import {Middleware} from "@domx/middleware";
export {TestMiddleware, useMiddleware};

interface MwContext {
    test: Array<string>
}

const mw: Middleware = new Middleware();
const useMiddleware: Function = (fn: Function) => mw.use(fn);

class TestMiddleware {
    logTest() {
        const context = {test: ["it"]};
        const returnValue = mw.execute((context: MwContext) => {
            context.test.push("work");
            return "!";
        }, [context]);
        context.test.push(returnValue);
        return context.test.join(" ");
    }
}

```
`addMiddleware.ts`
```js
import {useMiddleware} from "./TestMiddleware";

useMiddleware((next: Function) => (context: MwContext) => {
    context.test.push("did");
    return next(context);
});

```
`runTest.ts`
```js
import {TestMiddleware} from "./TestMiddleware";
import "./addMiddleware.ts";
const testMw = new TestMiddleware();
const returnValue = testMw.logTest();
console.log(returnValue); // logs: "it did work!"
```

#### Using Mapped Arguments
In some cases it may be useful to map an argument to all of the middleware functions before execution.
This would allow for the following middleware function signagure:
```js
const middlewareFunction = mappedArgument => next => passedArgument => {
  // do something with mappedArgument
  return next(passedArgument);
};
```
> Use `mapThenExecute` to run this middleware function signature.
```js
const mw = new Middleware();
mw.use(middlewareFunction);
mw.mapThenExecute((mappedFunction, nextFn, passedArgument));
```


## Logger

A logger to be used by mixins of HTMLElement classes.

It supports logging only certain console method calls and a `logOnly` setting which will filter
out any log messages from this logging implementation that do not have `logOnly` set to true.

### `loggerConfig` decorator
Any classes that include mixins that use the logger can use the `loggerConfig` decorator to filter console log output.
```js
@loggerConfig({
  onlyThis: true,
  level: "debug"
})
class SomeClass extends SomeMixinThatLogs(HTMLElement) {
  someMethod() {
    Logger.log(this, "debug", "This will log");
    Logger.log(this, "log", "This will log but at the set 'debug' level.");
  }
}
customElements.define("some-class", SomeClass);
```


## `Logger.log` method
To be used by the HTMLElement class mixins to allow user control of log output.
```js
Logger.log(this, "console", "Log this", "and this");
```

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