Skip to content

Commit

Permalink
Add ioc module
Browse files Browse the repository at this point in the history
  • Loading branch information
exeto committed Oct 10, 2024
1 parent aaadf2e commit 894bc67
Show file tree
Hide file tree
Showing 17 changed files with 2,847 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/healthy-cats-behave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"polyuse": minor
---

Initial release
42 changes: 42 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: CI

on:
pull_request:
push:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
ci:
name: CI
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9

- name: Install Node.js
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: 20

- name: Install dependencies
run: pnpm install

- name: Build
run: pnpm run build

- name: Lint
run: pnpm run lint

- name: Test
run: pnpm run test
38 changes: 38 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release

on:
push:
branches:
- main

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
release:
name: Release
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9

- name: Install Node.js
uses: actions/setup-node@v4
with:
cache: pnpm
node-version: 20

- name: Install dependencies
run: pnpm install

- name: Create release pull request or publish to npm
uses: changesets/action@v1
with:
publish: pnpm run release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
dist
3 changes: 3 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pnpm run build
pnpm run lint
pnpm run test
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pnpm-lock.yaml
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Timofey Dergachev
Copyright (c) Timofey Dergachev <[email protected]> (https://exeto.dev)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
93 changes: 93 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# polyuse

A modular library that provides building blocks for creating your own framework.

> **Note:** This package is ESM only.
## Installation

```bash
npm install polyuse
```

## Modules

Currently, polyuse includes the following modules:

- `ioc`: Inversion of Control container

More modules will be added in future releases.

## Usage

### IoC Container Example

```typescript
import {
createContainer,
createSingletonFactory,
createTransientFactory,
} from "polyuse/ioc";

class Logger {
log(message: string) {
console.log(`[${new Date().toISOString()}] ${message}`);
}
}

const loggerFactory = createSingletonFactory(() => new Logger());

class Database {
constructor(private readonly logger: Logger) {}

connect() {
this.logger.log("Connected to database");
}

query(sql: string) {
this.logger.log(`Executing query: ${sql}`);
}
}

const databaseFactory = createSingletonFactory(
() => new Database(loggerFactory.create()),
);

class UserService {
constructor(
private readonly logger: Logger,
private readonly database: Database,
) {}

createUser(username: string) {
this.logger.log(`Creating user: ${username}`);
this.database.query(`INSERT INTO users (username) VALUES ('${username}')`);
}
}

const userServiceFactory = createTransientFactory(
() => new UserService(loggerFactory.create(), databaseFactory.create()),
);

const container = createContainer({
database: databaseFactory,
userService: userServiceFactory,
});

// Usage
container.database.connect();

const userService1 = container.resolve("userService");
const userService2 = container.resolve("userService");

userService1.createUser("alice");
userService2.createUser("bob");
```

### Getting the container type

To get the type of the container for type-safe usage:

```typescript
type Container = typeof container;
```
9 changes: 9 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import js from "@eslint/js";
import ts from "typescript-eslint";

export default [
{ files: ["**/*.{js,mjs,cjs,ts}"] },
{ ignores: ["dist"] },
js.configs.recommended,
...ts.configs.recommended,
];
42 changes: 38 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
{
"name": "polyuse",
"version": "0.0.0",
"description": "A modular library that provides building blocks for creating your own framework",
"license": "MIT",
"author": {
"name": "Timofey Dergachev",
"email": "[email protected]",
"url": "https://exeto.dev"
"homepage": "https://polyuse.dev",
"author": "Timofey Dergachev <[email protected]> (https://exeto.dev)",
"repository": "exeto/polyuse",
"type": "module",
"exports": {
".": "./dist/index.js",
"./ioc": "./dist/ioc/index.js"
},
"sideEffects": false,
"keywords": [
"di",
"ioc"
],
"files": [
"dist"
],
"engines": {
"node": ">=18"
},
"scripts": {
"dev": "vitest",
"build": "rm -rf dist && tsc",
"lint": "eslint && prettier --check .",
"lint:fix": "eslint --fix && prettier --write .",
"test": "vitest run",
"release": "pnpm run build && changeset publish",
"prepare": "husky"
},
"devDependencies": {
"@changesets/cli": "^2.27.9",
"@eslint/js": "^9.12.0",
"eslint": "^9.12.0",
"husky": "^9.1.6",
"prettier": "^3.3.3",
"typescript": "^5.6.2",
"typescript-eslint": "^8.8.0",
"vitest": "^2.1.2"
}
}
Loading

0 comments on commit 894bc67

Please sign in to comment.