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

@spea/pea

v0.0.4

Published

A lightweight Dependency Injection (DI) framework for Node.js, based on proxies.

Downloads

281

Readme

Injection

A lightweight Dependency Injection (DI) framework for Node.js, based on proxies.

Overview

Injection is a simple yet powerful DI framework designed to make dependency management in Node.js applications easier and more flexible. It leverages JavaScript proxies to provide a seamless integration experience.

Note: This project is still a work in progress. APIs may change, and additional features are planned.

Features

  • Lightweight and only 3 methods, context.register, context.resolve and pea.
  • Proxy-based lazy loading of dependencies
  • No dependencies
  • Type-safe and fully typed
  • Not based on decorators.
  • Constructor injection
  • Factory injection
  • Primitive injection

Dependencies and Requirements

This has no runtime dependencies. It also works with most modern JS runtimes.

Installation

npm install @spea/pea

or

yarn add @spea/pea

Basic Usage

Here's a simple example of how to use Injection:

import { pea, context } from '@spea/pea';

// Define a service
class DatabaseService {
  connect() {
    console.log('Connected to database');
  }
}

// Register the service
context.register(DatabaseService);

// Use the service
class UserService {
  constructor(private db = pea(DatabaseService)) {}

  getUsers() {
    this.db.connect();
    // ... fetch users
  }
}

const userService = context.resolve(UserService);
userService.getUsers(); // Outputs: Connected to database

Advanced Usage

Custom Symbols

You can use symbols to register and retrieve services:

import { pea, context } from '@spea/pea';

const dbService = Symbol('DatabaseService');

class DatabaseService {
  connect() {
    console.log('Connected to database');
  }
}
//this makes pea work with symbols
declare module "@spea/registry" {
    export interface Registry {
        [dbService]: typeof DatabaseService;
    }
}

context.register(dbService, DatabaseService);

class UserService {
  constructor(private db = pea(dbService)) {}

  getUsers() {
    this.db.connect();
    // ... fetch users
  }
}
...
const userService = context.resolve(UserService);
userService.getUsers(); // Outputs: Connected to database

API Reference

pea(service, type?)

Returns a proxy for the given service. If the service is a symbol, the type can be used to specify the type of the service. Symbols should be registered with the context. For example:

const dbService = Symbol('DatabaseService');

class DatabaseService {
  connect() {
    console.log('Connected to database');
  }
}
declare module "@spea/pea" {
    export interface Registry {
        [dbService]: InstanceOf<typeof DatabaseService>;
    }
}

context.register(dbService, DatabaseService);

class UserService {
  constructor(private db = pea(dbService)) {}

  getUsers() {
    this.db.connect();
    // ... fetch users
  }
}
const userService = context.resolve(UserService);
userService.getUsers(); // Outputs: Connected to database

context.register(service, ...args)

Registers a service with the given arguments.

context.resolve(service, ...args)

Resolves a service with the given arguments.

context.visit(service, fn)

Visits all dependencies of a service. This can be used to destroy all dependencies, or something, else. The return of fn becomes the new value of the dependency. As primitives do not have dependencies, they do not get visited.

Roadmap

  • Improve documentation and add more examples
  • Finish AsyncLocal work for scope.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.