ioco
TypeScript icon, indicating that this package has built-in type declarations

0.0.7 • Public • Published

npm version license

Ioco

Ioco

npm i ioco

Ioco is a basic dependency injection library designed specifically for the TypeScript language. The library consists of two parts:

  • A schema generator
  • Dependency resolver

##Usage Examples The resolvers are using the dependency schema. One way is to prepare a custom dependency schema and initializing the TypeScriptContainerBuilder with the schema object in constructor. Another way is to generate a schema using the ioco CLI command with the -c argument and providing the path to a config file.

###Module Registrars

First, you need to provide an entry module with the registrations:

// The location of the file: src/myStartup.ts
import {RegistrationModule, ContainerBuilder} from "ioco";

class ConsoleLogger implements Logger {
    logInfo(message: string): void {
        console.log(`INFO: ${message}`);
    }
}

class SqsNotificationService implements NotificationService {
    private readonly _log: Logger;
    constructor(log: Logger) {
        this._log = log;
    }

    notify(message: any): void {
        this._log.logInfo(JSON.stringify(message));
    }
}

export type Logger = {
    logInfo(message: string): void;
}

export type NotificationService = {
    notify(message: any): void;
}

export class MyStartup implements RegistrationModule {
    register(containerBuilder: ContainerBuilder): void {
        containerBuilder.addService<Logger>(ConsoleLogger, "LOGGER", "Singleton")
        containerBuilder.addService<NotificationService>(SqsNotificationService, "NOTIFICATION_SERVICE", "Transient");
    }
}

Schema Generator

package.json:

{
  ...,
  "scripts": {
    "generate": "ioco -c src/ioco.myStartup.config"
  },
  ...
}

src/ioco.myStartup.config:

{
  "entryModule": {
    "path": "src/myStartup.ts",
    "instance": "MyStartup"
  },
  "outputSchemaPath": "src/dependencies.myStartup.json"
}

To generate the Dependency schema, use the CLI command:

npm run generate

Ioco generator will analyze the AST of the registration module and generate a schema file with all dependency references

Runtime

import {Registrations} from "ioco";
import {MyStartup, NotificationService} from './myStartup';

const schema = require("./dependencies.myStartup.json");
const startup = new MyStartup();
const containerBuilder = new TypeScriptContainerBuilder(<Registrations>schema);
startup.register(containerBuilder);
const serviceProvider = containerBuilder.build();
const notificationService = serviceProvider.resolveOne<NotificationService>("NOTIFICATION_SERVICE");
notificationService.send({
    id: "2022-11-16T07:18:25.024Z",
    message: "Hello"
})

Package Sidebar

Install

npm i ioco

Weekly Downloads

3

Version

0.0.7

License

MIT

Unpacked Size

48.4 kB

Total Files

19

Last publish

Collaborators

  • laparka