Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
@loopback/context
Advanced tools
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.
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.
@inject
and other variants of decorators to express dependencies.$ npm install --save @loopback/context
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'
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: HelloController = await ctx.get('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.
IBM/StrongLoop is an active supporter of open source and welcomes contributions to our projects as well as those of the Node.js community in general. For more information on how to contribute please refer to the Contribution Guide.
Run npm test
from the root folder.
See all contributors.
MIT
FAQs
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
We found that @loopback/context demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.