@web-pacotes/vault

A small, token based container designed for dependency injection 🫙
592
Install
npm install @web-pacotes/vault
Latest Version:0.0.1
License:none
Last Updated:Jul 16, 2023
Links: GitHub  ·  npm
Maintainer: freitzzz

vault

A small, token based container designed for dependency injection 🫙

npm version npm total downloads bundlephobia bundle size


How to use

Using Vault to store and lookup dependencies is as simple as:

// Create a vault
const vault = new Vault();

// Create some tokens
const token = 'my cool token';

// Store dependencies!
vault.store('my secret message', token);

// Look them up at some point in your program
const dependency = vault.lookup<string>(token);

// It should print "just lookup this dependency: my secret message"
console.info(`just lookup this dependency: ${dependency}`);

Features

  • Store primitive and non-primitive dependencies
  • Prevent store nullable or undefined values
  • Type inferred lookups

Why should I use Vault?

If you're working in a context based environment and you want to retrieve dependencies based on the context state, then Vault was designed for you. Simply register the vault instance to your context and get the dependencies in some point in your app after. Here's an example on how you can use it in SvelteKit:

// src/routes/+page.ts

export const load = (async () => {
    const dependency = new AuthenticationRepositoryImpl(...);
    const vault = new Vault();

    vault.store(dependency, 'AuthenticationRepository');

    setContext('vault', dependency);
});

...

// src/lib/components/AuthenticationStore.ts

const { value } = getContext('vault');
const authenticationRepo = vault.lookup<AuthenticationRepository>('AuthenticationRepository');

const store = writable<AuthenticationState>();

return {
    store.subscribe,
    authenticate: () => authenticationRepo.loginAnonymously(),
};


Bugs and Contributions

Found any bug (including typos) in the package? Do you have any suggestion or feature to include for future releases? Please create an issue via GitHub in order to track each contribution. Also, pull requests are very welcome!

To contribute, start by setting up your local development environment. The setup.md document will onboard you on how to do so!

Related Packages

hcx

HTML Compiler - Routeless, targetless, remote, runnable routes and more ...

468
@chialab/plasma

Generate Custom Elements wrappers for Angular, React, Svelte and Vue.

3,951 2
@aspect-ui-01/toast

Lightweight framework-agnostic toast notifications (~2KB)

3,926
@cfcs/core

Write once, create framework components that supports React, Vue, Svelte, and mo...

21,312,549 119

Version History

Version Released License
0.0.1 none
0.0.0 none