npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

zoddi

v1.3.0

Published

DI/IOC container using Zod

Downloads

6

Readme

Introduction

Zoddi is a dependency injection container that uses Zod for typing. It allows you to define interfaces, implement them, and inject them.

Unlike other frameworks like inversify and autofac, Zoddi does not require additional mapping data -- just interfaces and implementations. You don't need decorators or pairs of interfaces and symbols. This is because Zod types, unlike TypeScript interfaces, survive to runtime.

Basic Usage

const IAnimal = z.strictObject({
	legCount: z.number(),
	getNoise: z.function().args().returns(z.string())
});

const PetOwner = z.strictObject({
	pet: IAnimal
});

type IAnimal = z.infer<typeof IAnimal>;
type PetOwner = z.infer<typeof PetOwner>;

class Dog implements IAnimal {
	get legCount() { return 4; }
	getNoise() { return "woof"; }
};

const c = new Container();
c.bind(PetOwner).with(IAnimal).toFactory((a: IAnimal) => ({ pet: a }));
c.bind(IAnimal).toFactory(() => new Dog());

console.log(c.resolve(PetOwner).pet.getNoise()); // prints "woof"

Advanced usage

Zoddi also supports multiple implementations of a single interface. These are differentiated by keys. The default key is null. You can both bind to and depend on specific keys:

const IAnimal = z.object({
	legCount: z.number(),
	getNoise: z.function().args().returns(z.string())
});

const PetOwner = z.strictObject({
	pet: IAnimal
});

type IAnimal = z.infer<typeof IAnimal>;
type PetOwner = z.infer<typeof PetOwner>;

const sneakyCatKey = Symbol('sneaky-cat');
const sneakyCat: IAnimal = {
	legCount: 4,
	getNoise: () => "meow"
};

const c = new Container();
// Bind to IAnimal only for sneakyCatKey
c.bind(IAnimal, sneakyCatKey).toInstance(sneakyCat);
// Depend on IAnimal only with sneakyCatKey
c.bind(PetOwner).with(dep(IAnimal, sneakyCatKey)).toFactory((a: IAnimal) => ({ pet: a }));

By passing a third argument of strict=false to dep, you can allow fallback to the default (null) key.

The Theory Behind Zoddi

DI frameworks in languages like Java and C# often use reflection to create a better Developer Experience. TypeScript frameworks can't do this, because reflection isn't available, because all the types are gone at runtime.

Zod types, however, live on at runtime. They can be reified into TypeScript types via infer. In effect, rather than start with types and use reflection to work with them at runtime, Zoddi starts with runtime data and uses Zod to reify them at development time, producing the same opportunities for smoother DX as in other languages.