
Security News
Feross on TBPN: How North Korea Hijacked Axios
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.
A lightweight and type-safe dependency injection library for JS/TS, designed to simplify dependency management
FIOC (Functional Inversion Of Control) is a lightweight dependency injection library for JS/TS applications. It simplifies the management of dependencies by providing a flexible and type-safe way to define, register, and resolve dependencies, without the need of reflection or decorators.
Install the library using npm, pnpm or yarn:
npm install fioc
pnpm install fioc
yarn add fioc
First, create tokens for your dependencies using the createDIToken function. The token is a type-safe identifier for your dependency:
import { createDIToken } from "fioc";
interface ApiService {
getData: () => string;
}
const ApiServiceToken = createDIToken<ApiService>().as("ApiService");
Register your implementations using the container builder and resolve them when needed:
import { buildDIContainer } from "fioc";
import { ApiService, ApiServiceToken } from "./interfaces/ApiService";
const HttpApiService: ApiService = {
getData: () => "Hello, World!",
};
// Register dependencies
const container = buildDIContainer()
.register(ApiServiceToken, HttpApiService)
.getResult();
// Resolve dependencies
const apiService = container.resolve(ApiServiceToken);
apiService.getData(); // "Hello, World!"
Factories are functions that construct values based on other dependencies. Common use cases include creating use cases that depend on services or repositories:
import { ApiServiceToken } from "./interfaces/ApiService";
// Define a factory and its token
export const getDataUseCaseFactory =
(apiService: ApiService) => (ids: string[]) =>
apiService.getData(ids);
export const getDataUseCaseToken =
createDIToken<ReturnType<typeof getDataUseCaseFactory>>().as(
"getDataUseCase"
);
// Register the factory with its dependencies
container.registerFactory({
dependencies: [ApiServiceToken],
token: getDataUseCaseToken,
factory: getDataUseCaseFactory,
});
// Resolve and use the factory
const getDataUseCase = container.resolve(getDataUseCaseToken); // Type infers as (ids: string[]) => Promise<string>
getDataUseCase(); // Calls apiService.getData()
You can also use classes with FIOC. The constructorToFactory helper converts class constructors to factory functions:
import { constructorToFactory } from "fioc";
export class GetDataUseCase {
constructor(private apiService: ApiService) {}
execute = () => this.apiService.getData();
}
export const getDataUseCaseToken =
createDIToken<GetDataUseCase>().as("getDataUseCase");
container.registerFactory({
dependencies: [ApiServiceToken],
token: getDataUseCaseToken,
factory: constructorToFactory(GetDataUseCase),
});
For enhanced type safety, use buildStrictDIContainer. It provides compile-time validation of your dependency tree:
import { buildStrictDIContainer } from "fioc";
const container = buildStrictDIContainer()
// Error if token already registered
.register(ApiServiceToken, HttpApiService)
// Error if dependencies not registered
.registerFactory({
dependencies: [ApiServiceToken],
token: useCaseToken,
factory: myFactory,
})
// Safe replacement of existing registrations
.replace(ApiServiceToken, newImplementation)
.replaceFactory({
dependencies: [NewApiServiceToken],
token: useCaseToken,
factory: newFactory,
})
.getResult();
The Container Manager allows you to manage multiple containers - useful for different environments or testing:
import { buildDIManager } from "fioc";
const manager = buildDIManager()
.registerContainer(productionContainer, "prod")
.registerContainer(testContainer, "test")
.getResult()
.setDefaultContainer("prod");
// Get the active container
const container = manager.getContainer();
// Switch containers
manager.setActiveContainer("test");
Use cases for Container Manager:
Contributions are welcome! Feel free to open issues or submit pull requests on GitHub.
This library is licensed under the MIT License. See the LICENSE file for details.
FAQs
A lightweight and type-safe dependency injection library for JS/TS, designed to simplify dependency management
We found that fioc demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.

Security News
OpenSSF has issued a high-severity advisory warning open source developers of an active Slack-based campaign using impersonation to deliver malware.

Research
/Security News
Malicious packages published to npm, PyPI, Go Modules, crates.io, and Packagist impersonate developer tooling to fetch staged malware, steal credentials and wallets, and enable remote access.