Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .github/wip.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- locations: title
terms:
- DMG
- ⛔
- terms:
- 🚧
- WIP
locations: title
38 changes: 38 additions & 0 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: PR CI

on:
pull_request:
push:
branches:
- main

jobs:
verify:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Set up Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.3.10

- name: Install dependencies
run: bun install --frozen-lockfile

- name: Run lint
run: bun run lint

- name: Check formatting
run: bun run fmt:check

- name: Run typecheck
run: bun run typecheck

- name: Run tests
run: bun run test

- name: Build package
run: bun run build
2 changes: 1 addition & 1 deletion .oxfmt.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"endOfLine": "lf",
"bracketSpacing": true,
"arrowParens": "always",
"ignorePatterns": ["dist/**"]
"ignorePatterns": ["dist/**", "*.json"]
}
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ This repository is focused on a small, explicit DI core for standard ECMAScript
This repository uses Lefthook.

- `pre-commit` runs fast staged-file checks for lint and formatting.
- hooks are contributor-local and are not installed for package consumers.

If hooks stop working locally, reinstall them with `bun run hooks:install`.
Install or reinstall them locally with `bun run hooks:install`.

## Pull request checklist

- PR CI runs lint, format, typecheck, test, and build checks automatically.
- the change is focused and documented;
- scripts in the local checks section pass;
- package exports and Node compatibility were kept intact;
Expand Down
62 changes: 56 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ No environment variables or external services are required for local development

## Public entry points

The package root currently exports:
The package root currently exports runtime values and TypeScript types.

Runtime exports:

- `Container`
- `Service`
- `Inject`
- `Token`

Type-only exports:

- `Constructable` / `AbstractConstructable`
- `ServiceIdentifier`

Expand Down Expand Up @@ -126,10 +132,16 @@ Current behavior:

## Decorators

### `@Service(options?)`
### `@Service(idOrOptions?)`

Registers a class in the default container.

Accepted forms today:

- `@Service()`
- `@Service(id)`
- `@Service({ id, scope })`

Options supported today:

- `id?: ServiceIdentifier`
Expand All @@ -146,7 +158,8 @@ class LoggerService {}
const logger = Container.of().get('logger');
```

Note: a custom string id works for manual resolution through `container.get(...)`, but `@Inject()` currently accepts a constructable class dependency rather than an arbitrary token.
Custom identifiers work for both `container.get(...)` and `@Inject(...)`.
When you use `Token` instances, resolution is based on object identity, so create the token once and reuse the same instance everywhere.

### `@Inject(dependency)`

Expand All @@ -161,6 +174,35 @@ Current characteristics:
- injected fields are defined as writable and configurable own properties on the created instance;
- injected fields are assigned after construction, so they are not available inside constructors or field initializers.

Token example:

```ts
import { Container, Inject, Service, Token } from 'navi-di';

interface Logger {
log(message: string): void;
}

const LOGGER = new Token<Logger>('Logger');

@Service(LOGGER)
class ConsoleLogger implements Logger {
public log(message: string) {
console.log(message);
}
}

@Service()
class HandlerService {
@Inject(LOGGER)
public logger!: Logger;
}

const handler = Container.of().get(HandlerService);

handler.logger.log('hello from token injection');
```

## Container API

### `Container.of(id?)`
Expand Down Expand Up @@ -193,6 +235,13 @@ Supported strategies:

This is especially useful in tests.

### `container.set(metadata)`

Registers or replaces service metadata for a service identifier.

This is a low-level API that powers manual registration scenarios and internal tests.
For application-facing code, prefer `@Service()` unless you specifically need to construct metadata yourself.

## Internal architecture

The implementation is intentionally small and split into a few focused modules:
Expand Down Expand Up @@ -242,9 +291,9 @@ What they do:
- `lint`: run `oxlint` with warnings denied
- `fmt`: format the repository with `oxfmt`
- `fmt:check`: verify formatting without writing changes
- `hooks:*`: install, validate, or run the Lefthook-based Git hooks
- `hooks:*`: install, validate, or run the repo-local Lefthook Git hooks

`bun install` also triggers `postinstall`, which installs the local Git hooks automatically.
Git hooks are optional and repo-local. After cloning, contributors can install them with `bun run hooks:install`.

Current note: `typecheck` uses `tsconfig.json` with `noEmit: true`, while `build` uses `tsconfig.build.json` with `noEmit: false` to emit `dist/` and declaration files.

Expand All @@ -255,7 +304,8 @@ The repository currently enforces:
- strict TypeScript checking;
- `oxlint` for linting;
- `oxfmt` for formatting;
- Lefthook `pre-commit` checks for staged TypeScript, JavaScript, Markdown, YAML, and YML files.
- PR CI checks for lint, format, typecheck, tests, and build;
- optional Lefthook `pre-commit` checks for staged TypeScript, JavaScript, Markdown, YAML, and YML files.

The package `prepack` script runs lint, format check, typecheck, and build before publishing.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "navi-di",
"version": "0.1.0",
"description": "Dependency injection for standard ECMAScript decorators.",
"author": "naviary-sanctuary",
"keywords": [
"bun",
"decorators",
Expand Down Expand Up @@ -51,7 +52,6 @@
"lint": "oxlint . --deny-warnings",
"fmt": "oxfmt --config .oxfmt.json .",
"fmt:check": "oxfmt --check --config .oxfmt.json .",
"postinstall": "lefthook install",
"prepack": "bun run lint && bun run fmt:check && bun run typecheck && bun run build",
"test": "bun test --pass-with-no-tests"
},
Expand Down
Loading