Coder Social home page Coder Social logo

zheksoon / dioma Goto Github PK

View Code? Open in Web Editor NEW
214.0 3.0 3.0 246 KB

Elegant dependency injection container for vanilla JavaScript and TypeScript

License: MIT License

TypeScript 99.29% JavaScript 0.71%
1kb awilix dependency-injection di di-container inversify inversion-of-control ioc ioc-container javascript

dioma's Issues

Types exports are wrong in package.json

According to Are The Types Wrong the types for dioma are not set up correctly. The main issue is that the exports property in package.json defines only one type for both the .cjs as well as the .js file:

"exports": {
    ".": {
      "import": "./dist/dioma.js",
      "require": "./dist/dioma.cjs",
      "types": "./dist/index.d.ts"
    }
  },

This is, however, not possible. It needs to export a separate type file for each:

"exports": {
    ".": {
      "import": {
        "types": "./dist/dioma.d.ts",
        "default": "./dist/dioma.js"
      },
      "require": {
        "types": "./dist/dioma.d.cts",
        "default": "./dist/dioma.cjs"
      }
    },

Note that the types property needs to come first and that the file extension of the types need to match (e.g. .cts and .cjs).

I would also suggest to point the main property export to the ESM export as the package.json defines that this package is an es module.

As an example take a look at openapi-fetch for a library that correctly works in both CommonJS as well as ESM in Node.js: https://github.com/drwpow/openapi-typescript/blob/main/packages/openapi-fetch/package.json.

How to use it with packages?

Example:
package A uses classes from package B.

// package A
import { inject, Scopes } from 'dioma'
import LoggerService from '../../package-b/src/logger'
class SomeClass {
  static scope = Scopes.Singleton()
  constructor(private log = inject(LoggerService)) {
    // ----------------------------^^^^^^^^^^^^^
    // TS2345 Argument of type 'typeof LoggerService' is not assignable to parameter of type 'TokenOrClass'.
  }
}

// package B
import { Scopes } from 'dioma' // this is an other namespace?
export default class LoggerService {
  static scope = Scopes.Singleton()
}

Is it correct to use token-based injections?

Like

// package A - services.js
import { inject, Token, globalContainer } from 'dioma'
import LoggerService from '../../package-b/src/logger'

const services = globalContainer.childContainer('Services')

export const loggerServiceToken = new Token<LoggerService>('LoggerService')
services.register({ token: loggerServiceToken, class: LoggerService })
export const logService = services.inject(loggerServiceToken)

// package A - someclass.js
import { inject, Scopes } from 'dioma'
import LoggerService from './services'
class SomeClass {
  static scope = Scopes.Singleton()
  constructor(private log = inject(loggerServiceToken)) {}
}

Or could it be done easier?

But how could a class of package B inject the Logger of package B?
package B classes did not know the tokens.
Or must I build a wrapper to pass these vars? Sounds really complicated / bloated.

Can `inject` in a class constructor be relative to the container context that initializes it ?

That's maybe a nonsensical question.

I instantiate a container for every web request I take in a controller. The container is created inside the web callback, so I don't have access to it in the global scope where I would need it to inject classes in my constructors.

I guess I need to register every class of my app against this instance of a container.

What would be just cool is a way to injectcall in my classes constructor against the container used by the parent class.

Does that even make sense ? And if so, is that maybe too much magic ?

Promising projact anyway, I have been looking for a no-nonsense DI lib for some time (even built one myself). My goal is to use it in the context of cloudflare workers, where env and services are injected as parameters to the initial fetch call.

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.