https://github.com/iamsebastiandev/kit-di
A lightweight, dependency free, open source, dependency injection container for SvelteKit
https://github.com/iamsebastiandev/kit-di
depencyinjection library sveltekit
Last synced: 9 months ago
JSON representation
A lightweight, dependency free, open source, dependency injection container for SvelteKit
- Host: GitHub
- URL: https://github.com/iamsebastiandev/kit-di
- Owner: IamSebastianDev
- License: mit
- Created: 2025-08-30T13:01:48.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-09-23T19:50:21.000Z (9 months ago)
- Last Synced: 2025-09-23T21:27:34.609Z (9 months ago)
- Topics: depencyinjection, library, sveltekit
- Language: TypeScript
- Homepage:
- Size: 58.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: contributing.md
- License: license.md
Awesome Lists containing this project
README
# KitDi
A lightweight, dependency free, open source, dependency injection container for **SvelteKit** with support for **global** and **request-scoped** providers.
- ⚡ Global & Request scopes
- 🧩 Discoverable `Module` files
- 🔄 Class, value, factory & async factory providers
- ✅ Synchronous `inject()` with TypeScript support
## Installation
```bash
# Install via npm, bun, ...etc
bun add @iasd/kit-di
```
## Quick Start
### 1) Define a module
```ts
// src/feature/feature.module.ts
import { Module, provide, InjectionToken } from "kit-di";
export const Env = new InjectionToken<{ nodeEnv: string }>("Env");
export class FeatureService {}
export default new Module({
providers: [
// Global value
provide({ provide: Env, useValue: { nodeEnv: process.env.NODE_ENV ?? "development" } }),
// Global class
provide({ provide: FeatureService, useClass: FeatureService }),
// Request-scoped factory (new per request)
provide({
scope: "REQUEST",
provide: new InjectionToken("RequestStart"),
useFactory: () => new Date(),
}),
],
});
```
### 2) Wire it into SvelteKit
```ts
// src/hooks.server.ts
import type { Handle } from "@sveltejs/kit";
import { KitDi } from "kit-di";
const modules = await KitDi.discover();
await modules.register(); // resolve GLOBAL providers once at startup
export const handle: Handle = modules.handle(); // per-request DI context
```
> Using more handlers? Wrap with `sequence(modules.handle(), yourOtherHandle)`.
### 3) Inject dependencies where you need them
```ts
// src/routes/+page.server.ts
import { inject } from "kit-di";
import { FeatureService, Env } from "../feature/feature.module";
export const load = async () => {
const svc = inject(FeatureService); // class instance
const env = inject(Env); // token-bound value
return { env, id: svc.id };
};
```
## Defining Providers
Providers can be created with `provide(...)`:
- **Value**
Bind a concrete value to an `InjectionToken`.
- **Class**
The injector constructs the class (lazy by default, or eager with `eager: true`).
- **Factory (sync)**
A function returning a value (lazy by default).
- **Factory (async)**
A function returning a Promise — **must use** `eager: true` so it resolves during startup/request.
This keeps `inject()` strictly synchronous.
Scope options:
- `"GLOBAL"` (default): singleton for the whole app
- `"REQUEST"`: new instance per request (only available during the request lifecycle)
## Contributing
If you would like to contribute, take a look at the [contribution guide](./contributing.md).
## License
KitDi is licensed under the [MIT License](https://opensource.org/licenses/MIT).