# @universal-packages/token-registry

> Simple subject registry by token.

Latest version **1.8.1** (published 2024-11-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @universal-packages/token-registry
pnpm add @universal-packages/token-registry
yarn add @universal-packages/token-registry
bun add @universal-packages/token-registry
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

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

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.8.1 |
| Published | 2024-11-30 |
| First published | 2022-12-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 17.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | David De Anda |
| Maintainers | omarandstuff |

## Links

- npm: https://www.npmjs.com/package/@universal-packages/token-registry
- Repository: https://github.com/universal-packages/universal-token-registry
- Homepage: https://github.com/universal-packages/universal-token-registry#readme
- Issues: https://github.com/universal-packages/universal-token-registry/issues
- npm.io page: https://npm.io/package/@universal-packages/token-registry

## Dependencies (2)

- [@universal-packages/crypto-utils](https://npm.io/package/@universal-packages/crypto-utils.md) ^1.2.0
- [@universal-packages/adapter-resolver](https://npm.io/package/@universal-packages/adapter-resolver.md) ^1.6.1

## Recent versions

- 1.8.1 (latest) — 2024-11-30
- 1.8.0 — 2024-11-30
- 1.7.11 — 2024-08-25
- 1.7.10 — 2024-08-25
- 1.7.9 — 2024-03-23
- 1.7.8 — 2024-03-23
- 1.7.7 — 2024-03-22
- 1.7.6 — 2024-01-01
- 1.7.5 — 2024-01-01
- 1.7.4 — 2023-10-31
- 1.7.3 — 2023-10-30
- 1.7.2 — 2023-10-30
- 1.7.1 — 2023-09-30
- 1.7.0 — 2023-09-27
- 1.6.4 — 2023-09-10
- … 14 more at https://npm.io/package/@universal-packages/token-registry/versions

## README

# Token Registry

[![npm version](https://badge.fury.io/js/@universal-packages%2Ftoken-registry.svg)](https://www.npmjs.com/package/@universal-packages/token-registry)
[![Testing](https://github.com/universal-packages/universal-token-registry/actions/workflows/testing.yml/badge.svg)](https://github.com/universal-packages/universal-token-registry/actions/workflows/testing.yml)
[![codecov](https://codecov.io/gh/universal-packages/universal-token-registry/branch/main/graph/badge.svg?token=CXPJSN8IGL)](https://codecov.io/gh/universal-packages/universal-token-registry)

Simple dictionary registry where keys are autogenerated secure tokens.

## Install

```shell
npm install @universal-packages/token-registry
```

## Registry

`Registry` is the main class interface to start registering data subjects to be retrieved later by their associated token.

```js
import { Registry } from '@universal-packages/token-registry'

const registry = new Registry()

const token = await registry.register({ id: 4 })

const myData = await registry.retrieve(token)

console.log(myData)

// > { id: 4 }
```

> By default a registry uses a memory engine to store data, this may not be suitable for production environments.

### Options

- **`engine`** `Engine` `default: memory`
  Instance of the engine to be used to store the data
- **`engineOptions`** `Object`
  Options to pass to the engine if resolved as adapter.
- **`seed`** `String`
  Helps to add randomness to the token generation between instances

### Instance methods

#### **`register(subject: Object, category?: String)`**

#### **`register(token: string, subject: Object, category?: String)`**

Registers a new data subject under a newly generated token and returns that new token, a category can optionally be passed to group the registered data subjects later. If a token is passed it will be used instead of generating a new one (useful to update a subject).

```js
const token = await registry.register({ id: 4 })
```

#### **`prepare()`** **`async`**

Initialize the internal engine in case it needs preparation.

#### **`release()`** **`async`**

Releases the engine resources in case they need to be disposed before finishing the process.

#### **`retrieve(token: String)`**

Returns the subject register under the provided token.

```js
const subject = await registry.retrieve(token)
```

#### **`retrieveAll(category: String)`**

Returns all the subjects registered under the provided category.

```js
const subjects = await registry.retrieveAll(token)
```

#### **dispose `(token: String)`**

Disposes the data subject registered under the provided token so it's no longer retrievable.

```js
await registry.dispose(token)
```

#### **`clear()`**

Clears all registered data.

```js
await registry.clear()
```

## Engine

To create an engine that suits your requirements you just need to implement a new class as the following:

```js
import MyEngine from './MyEngine'

const registry = new Registry({ engine: new MyEngine() })
```

```js
export default class MyEngine implements EngineInterface {
  constructor(options) {
    // Options passed through the adapters sub system
  }

  prepare() {
    // Initialize any connection using options
  }

  release() {
    // Release any resources or close any connection
  }

  clear() {
    // Clear the engine from all entries
  }

  set(token, subject, category) {
    // Store the subject using the token as key
    // You may need to serialize the subject manually
    // Manage category  for later grouping if present
  }

  get(token) {
    // retrieve the subject from your engine using the token
  }

  getAll(category) {
    // Return an object in the shape of { '${token}': subject }
    // Filter only the tokens that are attached to the category
  }

  delete(token) {
    // delete the entry from your engine using the token
  }
}
```

### Engine Interface

If you are using TypeScript just can implement the `EngineInterface` to ensure the right implementation.

```ts
import { EngineInterface } from '@universal-packages/token-registry'

export default class MyEngine implements EngineInterface {}
```

## Typescript

This library is developed in TypeScript and shipped fully typed.

## Contributing

The development of this library happens in the open on GitHub, and we are grateful to the community for contributing bugfixes and improvements. Read below to learn how you can take part in improving this library.

- [Code of Conduct](./CODE_OF_CONDUCT.md)
- [Contributing Guide](./CONTRIBUTING.md)

### License

[MIT licensed](./LICENSE).

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