npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@loopback/context

v7.0.1

Published

Facilities to manage artifacts and their dependencies in your Node.js applications. The module exposes TypeScript/JavaScript APIs and decorators to register artifacts, declare dependencies, and resolve artifacts by keys. It also serves as an IoC container

Downloads

279,964

Readme

@loopback/context

This module provides facilities to manage artifacts and their dependencies using Context in your Node.js applications. It can be used independent of the LoopBack framework.

Overview

The @loopback/context package exposes TypeScript/JavaScript APIs and decorators to register artifacts, declare dependencies, and resolve artifacts by keys. The Context also serves as an IoC container to support dependency injection.

Context and Binding are the two core concepts. A context is a registry of bindings and each binding represents a resolvable artifact by the key.

  • Bindings can be fulfilled by a constant, a factory function, a class, or a provider.
  • Bindings can be grouped by tags and searched by tags.
  • Binding scopes can be used to control how a resolved binding value is shared.
  • Bindings can be resolved synchronously or asynchronously.
  • Provide @inject and other variants of decorators to express dependencies.
  • Support Constructor, property, and method injections.
  • Allow contexts to form a hierarchy to share or override bindings.
  • Track bindings and injections during resolution to detect circular dependencies.

Installation

npm install --save @loopback/context

Basic use

Locate an artifact by key

const Context = require('@loopback/context').Context;
const ctx = new Context();
ctx.bind('hello').to('world'); // BindingKey='hello', BindingValue='world'
const helloVal = ctx.getSync('hello');
console.log(helloVal); // => 'world'

The binding can also be located asynchronously:

const helloVal = await ctx.get('hello');
console.log(helloVal); // => 'world'

Dependency injection using context

import {Context, inject} from '@loopback/context';
const ctx = new Context();

// bind 'greeting' to 'Hello' as the constant value
ctx.bind('greeting').to('Hello');

class HelloController {
  constructor(
    // injecting the value bound to `greeting` using context
    @inject('greeting') private greeting: string,
  ) {}

  greet(name) {
    return `${this.greeting}, ${name}`;
  }
}

// Bind 'HelloController' to class HelloController
ctx.bind('HelloController').toClass(HelloController);

async function hello() {
  // Get an instance of HelloController
  const helloController = await ctx.get<HelloController>('HelloController');
  // helloController now has the `greeting` property injected with `Hello`
  console.log(helloController.greet('John')); // => Hello, John
}

hello();

For additional information, please refer to the Context page.

Examples

To learn more about advanced features, check out standalone examples at @loopback/example-context.

Use the following command to download the example project to try out:

lb4 example context

Contributions

Tests

Run npm test from the root folder.

Contributors

See all contributors.

License

MIT