Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

provi

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

provi

Minimalistic and cute Service Provider

latest
Source
npmnpm
Version
2.3.3
Version published
Maintainers
1
Created
Source

provi

npm version npm bundle size code coverage typescript supported

Minimalistic and cute Service Provider for server environment (node.js, bun) and for clients (browser, native).

  • You can use it at any place of your application without rewriting your application's architecture or other preparations or initializations.
  • Each dependency can be a class or function.
  • Feel free with your shared logic.

The service provider pattern - also called service locator pattern.

Advantages over Dependency Injection:

  • Zero configuration, easy to use everywhere whole your app.
  • Start-time and run-time dependency resolving, solving the circular dependency problem.
  • Smaller bundle size.

node.js or bun usage

import { provide } from "provi/server"

class Db { /* ... */ }

// Define dependencies using "provide" function
export class App {
  db = provide(Db)
  // ...
  start() {
    this.db.init()
    // ...
  }
}

browser or native usage

import { provide } from "provi/client"

// Define dependencies using "provide" function
class Auth {
  // user = sharedUser()
  // ...
  logout() {
    if (sharedUser().isAnonymous) return
    // ...
  }
}

export const sharedAuth = () => provide(Auth)

in both ways you can use plain javascript functions as dependency constructor

import { provide } from "provi/client"

const User = () => {
  // ...
  return {
    get isAnonymous() {
      return true;
    }
  }
}

export const sharedUser = () => provide(User)

Isolation of async scopes (only in node environment)

Run your app in isolated Service Provider scope. All instances cached for this will be isolated from all cached instances in other scopes. Useful for implementing SSR.

import { isolate } from "provi/ssr"

const html = await isolate(async () => {
  const { run } = provide(Logic); // Isolated instance of app logic
  await run();
  // ...
  return ReactDOMServer.renderToString(<App />);
});

Each isolated instance will be destroyed at the end of the isolated asynchronous function.

Installation

npm install provi

Enjoy your code!

Keywords

service

FAQs

Package last updated on 01 Dec 2023

Did you know?

Socket

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.

Install

Related posts