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

@katis/weave

v0.5.0

Published

Simple functional dependency injection

Downloads

561

Readme

@katis/weave - Type Safe Dependency Injection for TypeScript

Overview

@katis/weave is a TypeScript library for automatically resolving dependencies between functions.

Features

  • Simple providers: The providers are plain functions making them easy to test.
  • Automatic Dependency Resolution: weave automatically infers and resolves dependencies based on the provided functions.
  • Type Safety: Ensures dependencies are correctly typed, providing compile-time type checking.
  • Runtime Validation: Checks for circular or missing dependencies at runtime, throwing exceptions when such issues are detected.
  • Concise Syntax: Utilizes destructured object syntax for clear and concise dependency declarations.

Installation

Install the package using your preferred package manager:

npm install @katis/weave
pnpm add @katis/weave
yarn add @katis/weave

Usage

Defining Providers

Providers are functions or class constructors that optionally take a single argument defining the dependencies required by the returned value. The provider argument MUST use destructuring syntax, as the argument is used to analyze the dependency graph for circular and missing dependencies at runtime.

Example:

type Deps = {
  dirPath: string;
  readItems: (dirPath: string) => Promise<string[]>;
};
// getItems is a function that first takes it's dependencies as an argument and
// then returns a function that uses those dependencies to read items from a path.
export const getItems =
  ({ dirPath, readItems }: Deps) =>
  () => {
    readItems(dirPath);
  };

Using weave

The weave function takes an object of providers and resolves their dependencies.

Example:

import { weave, Provider } from "@katis/weave";
import fs from "fs/promises";

type FileManagerDeps = {
  dirPath: string;
};

// Classes need to extend Provider() or Provider(BaseClass) to be compatible with weave
class FileManager extends Provider() {
  constructor({ dirPath }: FileManagerDeps) {}

  readFile(fileName: string) {
    return fs.readFile(`${dirPath}/${fileName}`, "utf-8");
  }
}

type ReadConfigDeps = {
  fileManager: FileManager;
};

const readConfig =
  ({ fileManager }: ReadConfigDeps) =>
  () => fileManager.readFile('config.json');

const providers = {
  dirPath: './config'
  fileManager: FileManager,
  readConfig,
};

const dependencyGraph = weave(providers);

dependencyGraph.getItems().then(console.log);

If a dependency is required by a provider but it's not present in the providers object it results in a type error. There are also runtime validations for missing dependencies, but they should not be necessary if strict typing is followed.

Error Handling

  • Type Mismatch: Compile-time errors are generated for mismatched and missing dependencies.
  • Missing Dependencies: weave throws an exception if there are missing dependencies.
  • Circular Dependencies: An exception is also thrown for circular dependencies.

Limitations

Each time a property is read from the dependency graph, the provider function creating it is evaluated again, as are its transitive dependencies providers. This means that deep dependency chains can do a lot of work if the property is read often. Currently there is no support for caching the provider return values beyond returning a constant value from the provider.

There is no support for scoped dependencies, or automatic disposing of dependencies.

Inspiration

License

Copyright 2024 Joni Katajamäki

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.