# @hero-js/context

> A utility module for managing contextual data within your Node.js applications.

Latest version **1.1.2** (published 2024-01-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hero-js/context
pnpm add @hero-js/context
yarn add @hero-js/context
bun add @hero-js/context
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2024-01-08 |
| First published | 2023-10-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Sana Yasfp |
| Maintainers | sana_yasfp |
| Keywords | context, hero, herojs, typescript, framework, data, weakMap, weak map |

## Links

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

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2024-01-08
- 1.1.1 — 2023-11-10
- 1.1.0 — 2023-11-10
- 1.0.0 — 2023-10-28

## README

# @hero-js/context

A utility module for managing contextual data within your Node.js applications.

## Installation

You can install this module using npm:

```bash
npm install @hero-js/context
```

## Usage

Here's how you can use this module in your Node.js application:

```javascript
// Import the Context class
const Context = require('@hero-js/context');

// Create a new context instance
const myContext = Context.create('MyContext');

// Set a value in the context
myContext.set('userId', 123);

// Retrieve a value from the context
const userId = myContext.get('userId');

console.log(`User ID: ${userId}`);
```

## Features

- Create and manage named contexts.
- Set and retrieve data within a context.
- Handle default values for missing data.
- Optional support for strict mode in boolean retrieval.

## API

### `Context.create(name: string): Context`

Creates a new context instance with the given name.

### `Context.createVolatileContext(): Context`

Creates a volatile context with a randomly generated name.

### `context.set<T>(key: string, value: T): void`

Sets a value for a specific key in the context.

### `context.get<T>(key: string, options: IGetContextData<T>): T | false | undefined`

Retrieves a value associated with a specific key in the context. Supports default values and optional invalid value checks.

### `context.getOrThrow<T>(key: string, options: IInvalid): T | false`

Retrieves a value from the context and throws an error if not found or if the value is invalid.

### `context.getInt(key: string, options: IGetContextData<number>): number | undefined`

Retrieves an integer value from the context, with optional invalid value checks.

### `context.getIntOrThrow(key: string, options: IInvalid): number`

Retrieves an integer value from the context and throws an error if not found or if the value is invalid.

### `context.getBoolean(key: string, options: IGetContextData<boolean>): boolean | undefined`

Retrieves a boolean value from the context, with optional strict mode for boolean checks.

### `context.getBooleanOrThrow(key: string, options: IInvalid): boolean`

Retrieves a boolean value from the context and throws an error if not found or if the value is invalid.

## License

This module is licensed under the MIT License. See the [LICENSE](https://github.com/hero-js/context/blob/main/LICENSE) file for details.

## Contributing

Contributions are welcome! If you'd like to contribute to this project, please follow the guidelines in [CONTRIBUTING](https://github.com/hero-js/hero/blob/main/CONTRIBUTING.md).

## Changelog

For a history of changes to this module, see the [CHANGELOG](https://github.com/hero-js/context/blob/main/CHANGELOG.md) file.

## Support

If you have any questions or encounter issues, please open a GitHub issue.

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