New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@katis/weave

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@katis/weave

Simple functional dependency injection

0.5.0
latest
Source
npm
Version published
Weekly downloads
250
20.19%
Maintainers
0
Weekly downloads
 
Created
Source

@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

  • Playwright Test Fixtures

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.

Keywords

dependency

FAQs

Package last updated on 15 Nov 2024

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