
Security News
Vite Releases Technical Preview of Rolldown-Vite, a Rust-Based Bundler
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
fastify-di-container
Advanced tools
This plugin is the small, convenient dependency container.
You can save the components you want to manage as a global singleton into the container. To get and use those components, you only need to set the parameter name to the name of the saved component.
npm install fastify-di-container --save
with ESM syntax:
import Fastify from 'fastify';
import container from 'fastify-di-container';
// in user.repository.ts
function makeUserRepository() {
return {
findOneFromDB: async (userId) => (
// ... return from db matched with userId
),
}
}
// in user.service.ts
function makeUserService(userRepository/*Parameter name matched with component name*/) {
return {
getUserById: async () => {
const foundUser = await userRepository.findOneFromDB();
return foundUser;
},
}
}
/* Now, UserService component has dependency to userRepository */
async function startServer() {
const app = Fastify();
// Just register each module as component
await app.register(container, {
components: [
// Describe 'Component Summaries' in this.
{
name: 'userService',
constructor: makeUserService,
},
{
name: 'userRepository',
constructor: makeUserRepository,
}
]
});
app.listen(3000);
}
startServer();
with Typescript (It works well on javascript too)
import container from 'fastify-di-container';
import fastify from 'fastify';
const app = fastify();
app.register(container, {
components: [
/*... Component Summaries to register*/
],
containerName: 'customContainer',
onInitialized: <UserService>(componentName, initializedComponent) => {
console.log(componentName);
},
});
const userService = app.customContainer.get<UserService>('userService');
app.listen(3000);
// Only for typescript.
// If you don't use typescript, skip this.
// in type.d.ts
import { IncommingMessage, Server, ServerResponse } from 'http';
import type { Container } from 'fastify-di-container';
declare module 'fastify' {
export interface FastifyInstance<
HttpServer = Server,
HttpRequest = IncommingMessage,
HttpResponse = ServerResponse
> {
customContainer: Container;
}
}
FAQs
This plugin is the small, convenient dependency container.
The npm package fastify-di-container receives a total of 0 weekly downloads. As such, fastify-di-container popularity was classified as not popular.
We found that fastify-di-container demonstrated a not healthy version release cadence and project activity because the last version was released 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
Vite releases Rolldown-Vite, a Rust-based bundler preview offering faster builds and lower memory usage as a drop-in replacement for Vite.
Research
Security News
A malicious npm typosquat uses remote commands to silently delete entire project directories after a single mistyped install.
Research
Security News
Malicious PyPI package semantic-types steals Solana private keys via transitive dependency installs using monkey patching and blockchain exfiltration.