
Security News
OpenGrep Restores Fingerprinting in JSON and SARIF Outputs
OpenGrep has restored fingerprint and metavariable support in JSON and SARIF outputs, making static analysis more effective for CI/CD security automation.
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.
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
OpenGrep has restored fingerprint and metavariable support in JSON and SARIF outputs, making static analysis more effective for CI/CD security automation.
Security News
Security experts warn that recent classification changes obscure the true scope of the NVD backlog as CVE volume hits all-time highs.
Security Fundamentals
Attackers use obfuscation to hide malware in open source packages. Learn how to spot these techniques across npm, PyPI, Maven, and more.