# appolo-context

> async hooks context storage

Latest version **6.0.9** (published 2019-01-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install appolo-context
pnpm add appolo-context
yarn add appolo-context
bun add appolo-context
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.0.9 |
| Published | 2019-01-31 |
| First published | 2018-08-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 10.x.x |
| Dependencies | 0 |
| Unpacked size | 14.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Roman Svichar |
| Maintainers | shmoop207 |
| Keywords | context, async, hooks, async_hooks |

## Links

- npm: https://www.npmjs.com/package/appolo-context
- Repository: https://github.com/shmoop207/appolo-context
- Homepage: https://github.com/shmoop207/appolo-context#readme
- Issues: https://github.com/shmoop207/appolo-context/issues
- npm.io page: https://npm.io/package/appolo-context

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 6.0.9 (latest) — 2019-01-31
- 6.0.8 — 2019-01-31
- 6.0.7 — 2018-08-25
- 6.0.6 — 2018-08-24
- 6.0.5 — 2018-08-24
- 6.0.3 — 2018-08-23
- 6.0.2 — 2018-08-23
- 6.0.1 — 2018-08-23
- 6.0.0 — 2018-08-23

## README

# Appolo Context
[![Build Status](https://travis-ci.org/shmoop207/appolo-context.svg?branch=master)](https://travis-ci.org/shmoop207/appolo-context) [![Dependencies status](https://david-dm.org/shmoop207/appolo-context.svg)](https://david-dm.org/shmoop207/appolo-context) [![NPM version](https://badge.fury.io/js/appolo-context.svg)](https://badge.fury.io/js/appolo-context)  [![npm Downloads](https://img.shields.io/npm/dm/appolo-context.svg?style=flat)](https://www.npmjs.com/package/appolo-context)
[![Known Vulnerabilities](https://snyk.io/test/github/shmoop207/appolo-context/badge.svg)](https://snyk.io/test/github/shmoop207/appolo-context)


nodejs async hooks context storage
can be used to create context per request

> Attention! This package is using the new experimental [async hooks](https://nodejs.org/api/async_hooks.html) API.


## Installation:

```javascript
npm install appolo-context --save
```

## Usage
```javascript
import {namespace} from "appolo-context";

let context = namespace.create("someName");
context.initialize();

class A {
 run(){
    let context = namespace.get("someName")

    context.set("id","someId")

    return Promise.resolve();
 }
}

class B {
 run(){
    let context = namespace.get("someName")

    let id = context.get("id");

    return Promise.resolve(id);
 }
}

context.scope(async()=>{
    await new A().run();
    let id  = await new B().run();

    console.log(id) // someId
})


```

## Express Usage
```javascript
import {context} from "appolo-context";
import * as express from "express";

context.initialize();

let app = express();
app.use((req,res,next)=>{
    context.scope(()=>{
        context.set("id",uuid.v1());
        next();
    })
})

app.use(async (req,res,next())=>{
    let reuslt  = await doSomethingAsync();
    res.send(reuslt)
})
```
Now you can retrieve the context in any file or function.
```javascript
import {context} from "appolo-context";

async function doSomethingAsync(){

    return context.get("id");
}

```

### Namespace

#### `create(name:string|Symbol):Context`
Create new context by given name
```javascript
import {namespace} from "appolo-context";

let context = namespace.create("someName");
context.initialize();
```

#### `get(name:string|Symbol):Context`
get context by given name
```javascript
import {namespace} from "appolo-context";

let context = namespace.get("someName");
context.get("someValue")
```

#### `delete(name:string|Symbol)`
delete context by given name and destroy it
```javascript
import {namespace} from "appolo-context";

let context = namespace.delete("someName");
```

#### `get context():Context`
get the default context
```javascript
import {namespace} from "appolo-context";

let context = namespace.context;

context.initialize()
```

### Context
#### `initialize()`
initialize the async hooks must be called before any get or set methods
this will create the async hooks and enable them.

### Context
#### `scope(fn:Function)`
create new context scope and triggers the given function
```javascript
import {context} from "appolo-context";
import * as express from "express";

context.initialize();

let app = express();
app.use((req,res,next)=>{
    context.scope(()=>{
        context.set("id",uuid.v1());
        next();
    })
})
```

#### `get(key:any):any`
get context value by key

#### `set(key:any,value:any)`
set context value by key

#### `enable()`
enable the async hooks

#### `destroy()`
destroy the context and clean all resources.
## License
MIT

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