GithubHelp home page GithubHelp logo

softnetics / what-the-dep Goto Github PK

View Code? Open in Web Editor NEW
0.0 0.0 0.0 180 KB

Async compile time dependency injection for typescript

License: MIT License

TypeScript 100.00%
bun compile-time dependency-injection typescript-transformer

what-the-dep's Introduction

what-the-dep

compile time async dependency injection plugin

Installation

bun install @softnetics/what-the-dep

Then create a preload file in your project

// preload.ts
import { whatTheDep } from "@softnetics/what-the-dep";
import { plugin } from "bun";

plugin(whatTheDep());

and add a preload into your bunfig.toml

preload = ["./preload.ts"]

Features

  • Compile time dependency injection
  • Async dependency initialization
  • Circular dependency detection
  • Circular dependency resolution
  • Import dependency from other modules

Usage

import { Container } from "@softnetics/what-the-dep";

const container = new Container();

class A {
  constructor(public b: B) {}
}

class B {
  constructor() {}
}

container.register<A>().register<B>();

const a = await container.get<A>();

Initialize class with a factory

import { Container } from "@softnetics/what-the-dep";

const container = new Container();

class A {
  constructor(public b: B) {}
}

class B {
  constructor() {}
}

container.register<B>().register<A>(async (get) => {
  const b = await get<B>();
  return new A(b);
});

const a = await container.get<A>();

Reference to class using an interface

import { Container } from "@softnetics/what-the-dep";

const container = new Container();

interface IA {
  b: B;
}

class A implements IA {
  constructor(public b: B) {}
}

class B {
  constructor() {}
}

container.register<B>().register<IA, A>(async (get) => {
  const b = await get<B>();
  return new A(b);
});

const a = await container.get<IA>();

what-the-dep's People

Contributors

github-actions[bot] avatar msp5382 avatar peam1146 avatar saenyakorn avatar suphon-t avatar

what-the-dep's Issues

Failed to compile when using get method which not from container

Description

This problem happens when we use variable which contains get method

Minimal reproducible example

https://github.com/miello/poc-issue-what-the-dep/tree/main

Logs

26652 |   }
26653 |   function isDebuggerStatement(node) {
26654 |     return node.kind === 259 /* DebuggerStatement */;
26655 |   }
26656 |   function isVariableDeclaration(node) {
26657 |     return node.kind === 260 /* VariableDeclaration */;
                   ^
TypeError: undefined is not an object (evaluating 'node.kind')
      at isVariableDeclaration (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:26657:12)
      at m (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/@softnetics/what-the-dep/dist/chunk-5EBQ5Y5L.cjs:1:8425)
      at visitNode (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87169:21)
      at visitEachChildOfExpressionStatement (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:88018:32)
      at visitEachChild (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87419:35)
      at m (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/@softnetics/what-the-dep/dist/chunk-5EBQ5Y5L.cjs:1:9698)
      at visitArrayWorker (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87231:51)
      at visitNodes2 (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87202:21)
      at visitLexicalEnvironment (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87258:18)
      at visitEachChildOfSourceFile (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:88447:13)
      at visitEachChild (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87419:35)
      at m (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/@softnetics/what-the-dep/dist/chunk-5EBQ5Y5L.cjs:1:9698)
      at visitNode (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87169:21)
      at N (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/@softnetics/what-the-dep/dist/chunk-5EBQ5Y5L.cjs:1:9734)
      at l (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/@softnetics/what-the-dep/dist/chunk-5EBQ5Y5L.cjs:1:11445)
      at visitNode (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87169:21)
      at visitEachChildOfVariableDeclaration (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:88128:13)
      at visitEachChild (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87419:35)
      at l (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/@softnetics/what-the-dep/dist/chunk-5EBQ5Y5L.cjs:1:11917)
      at visitArrayWorker (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87231:51)
      at visitNodes2 (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87202:21)
      at visitEachChildOfVariableDeclarationList (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:88134:13)
      at visitEachChild (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87419:35)
      at l (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/@softnetics/what-the-dep/dist/chunk-5EBQ5Y5L.cjs:1:11917)
      at visitNode (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87169:21)
      at visitEachChildOfVariableStatement (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:88012:32)
      at visitEachChild (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87419:35)
      at l (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/@softnetics/what-the-dep/dist/chunk-5EBQ5Y5L.cjs:1:11917)
      at visitArrayWorker (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87231:51)
      at visitNodes2 (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87202:21)
      at visitLexicalEnvironment (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:87258:18)
      at visitEachChildOfSourceFile (/home/miello/project/softnetic/poc-issue-what-the-dep/node_modules/typescript/lib/typescript.js:88447:13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.