# @appolo/context

> appolo context module

Latest version **8.0.2** (published 2022-05-18) · 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 | 8.0.2 |
| Published | 2022-05-18 |
| First published | 2019-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 25.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | roman svichar |
| Maintainers | shmoop207 |
| Keywords | appolo, context |

## Links

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

## Dependencies (2)

- [@appolo/core](https://npm.io/package/@appolo/core.md) ^8.0.9
- [appolo-context](https://npm.io/package/appolo-context.md) ^6.0.9

## Recent versions

- 8.0.2 (latest) — 2022-05-18
- 8.0.1 — 2022-05-18
- 8.0.0 — 2020-12-21
- 7.0.2 — 2020-03-28
- 7.0.0 — 2020-03-28
- 0.0.5 — 2020-02-13
- 0.0.4 — 2020-02-13
- 0.0.3 — 2019-02-03

## README

Request context module for [`appolo`](http://https://github.com/shmoop207/appolo) built with [appolo-context](https://github.com/shmoop207/appolo-context) and `async_hooks`.

> new context will be create for every request

## Installation

```javascript
npm i @appolo/context
```

## Options
| key | Description | Type | Default
| --- | --- | --- | --- |
| `id` | id of context injector  | `string`|  `context`|

in config/modules/all.ts

```javascript
import {ContextModule} from '@appolo/context';

export = async function (app: App) {
   await app.module(ContextModule);
}
```

## Usage
first define your context using the `@context` decorator
```javascript
import {context} from '@appolo/context';

@context()
export class MyContext {

    @inject() env: any;

    constructor(req, res) {

    }

    private _user: string;

    public set user(value: string) {

        this._user = value
    }

    public get user(): string {
        return this._user
    }
}

```
For the example we will define a middleware to set the context data
```javascript
@define()
@singleton()
export class UserMiddleware extends Middleware {
    @inject() context: MyContext;

    async run(req, res, next) {
        this.context.user = req.query.userName;
        next()
    }
}

```
Now we can access the context from any class using `@inject context`
> note that the context is uniq for every request and can not share data between requests

```javascript
@define()
@singleton()
export class SomeManager {

    @inject() context: MyContext ;

    public async getName():Promise<string>{
        return this.context.user;
    }
}

```

In the controller we will put the middleware and access the manager to get the context name
```javascript
@controller()
export class ContextController extends Controller {

    @inject() someManager: Manager;

    @get("/test/context/")
    @validation("userName", validator.string().required())
    @middleware(UserMiddleware)
    async test(req: IRequest, res: IResponse) {

        let userName = await this.someManager.getName()

        return {userName}
    }
}

```

You can also access the current context from `getContext` function

```javascript
import {getContext} from '@appolo/context';

@define()
@singleton()
export class SomeManager {

    public async getName():Promise<string>{
        return getContext().user;
    }
}

```

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