@migration-planner-ui/ioc 0.0.35 → 0.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +13 -0
- package/README.md +224 -0
- package/package.json +4 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Copyright 2026 Red Hat, Inc.
|
|
2
|
+
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
|
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
|
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
See the License for the specific language governing permissions and
|
|
13
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
# @migration-planner-ui/ioc
|
|
2
|
+
|
|
3
|
+
A lightweight Inversion of Control (IoC) container solution for React applications, inspired by InversifyJS.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @migration-planner-ui/ioc --save
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
or
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
yarn add @migration-planner-ui/ioc
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Peer Dependencies
|
|
18
|
+
|
|
19
|
+
This package requires the following peer dependencies:
|
|
20
|
+
|
|
21
|
+
- `react` ^18.3.1
|
|
22
|
+
- `react-dom` ^18.3.1
|
|
23
|
+
- `react-router-dom` ^6.26.0
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### 1. Define Symbols for Your Dependencies
|
|
28
|
+
|
|
29
|
+
Create a symbols file to define unique identifiers for your dependencies:
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// Symbols.ts
|
|
33
|
+
export const Symbols = Object.freeze({
|
|
34
|
+
MyService: Symbol.for("MyService"),
|
|
35
|
+
ApiClient: Symbol.for("ApiClient"),
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### 2. Create and Configure the Container
|
|
40
|
+
|
|
41
|
+
Create a container instance and register your dependencies:
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
import { Container } from "@migration-planner-ui/ioc";
|
|
45
|
+
import { Symbols } from "./Symbols";
|
|
46
|
+
import { MyService } from "./services/MyService";
|
|
47
|
+
import { ApiClient } from "./clients/ApiClient";
|
|
48
|
+
|
|
49
|
+
function getConfiguredContainer(): Container {
|
|
50
|
+
const container = new Container();
|
|
51
|
+
|
|
52
|
+
// Register dependencies
|
|
53
|
+
container
|
|
54
|
+
.register(Symbols.MyService, new MyService())
|
|
55
|
+
.register(Symbols.ApiClient, new ApiClient());
|
|
56
|
+
|
|
57
|
+
return container;
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### 3. Provide the Container to Your App
|
|
62
|
+
|
|
63
|
+
Wrap your application with the `Provider` component:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
import { Provider } from "@migration-planner-ui/ioc";
|
|
67
|
+
import React from "react";
|
|
68
|
+
import ReactDOM from "react-dom/client";
|
|
69
|
+
|
|
70
|
+
const container = getConfiguredContainer();
|
|
71
|
+
|
|
72
|
+
ReactDOM.createRoot(document.getElementById("root")!).render(
|
|
73
|
+
<React.StrictMode>
|
|
74
|
+
<Provider container={container}>
|
|
75
|
+
<App />
|
|
76
|
+
</Provider>
|
|
77
|
+
</React.StrictMode>
|
|
78
|
+
);
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### 4. Use Dependencies in Components
|
|
82
|
+
|
|
83
|
+
Use the `useInjection` hook to retrieve dependencies in your React components:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
import { useInjection } from "@migration-planner-ui/ioc";
|
|
87
|
+
import { Symbols } from "./Symbols";
|
|
88
|
+
import type { MyServiceInterface } from "./services/MyService";
|
|
89
|
+
|
|
90
|
+
export const MyComponent: React.FC = () => {
|
|
91
|
+
const myService = useInjection<MyServiceInterface>(Symbols.MyService);
|
|
92
|
+
|
|
93
|
+
// Use the injected service
|
|
94
|
+
const handleClick = () => {
|
|
95
|
+
myService.doSomething();
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return <button onClick={handleClick}>Click me</button>;
|
|
99
|
+
};
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## API Reference
|
|
103
|
+
|
|
104
|
+
### `Container`
|
|
105
|
+
|
|
106
|
+
A singleton-scoped dependency injection container.
|
|
107
|
+
|
|
108
|
+
#### Methods
|
|
109
|
+
|
|
110
|
+
##### `register<T>(symbol: symbol, value: T): Container`
|
|
111
|
+
|
|
112
|
+
Registers a dependency with the container.
|
|
113
|
+
|
|
114
|
+
- **Parameters:**
|
|
115
|
+
- `symbol`: A unique symbol identifier for the dependency
|
|
116
|
+
- `value`: The value to register
|
|
117
|
+
- **Returns:** The container instance (for method chaining)
|
|
118
|
+
|
|
119
|
+
##### `get<T>(symbol: symbol): T | undefined`
|
|
120
|
+
|
|
121
|
+
Retrieves a registered dependency from the container.
|
|
122
|
+
|
|
123
|
+
- **Parameters:**
|
|
124
|
+
- `symbol`: The symbol identifier of the dependency to retrieve
|
|
125
|
+
- **Returns:** The registered dependency value, or `undefined` if not registered.
|
|
126
|
+
|
|
127
|
+
### `Provider`
|
|
128
|
+
|
|
129
|
+
A React context provider component that makes the container available to child components.
|
|
130
|
+
|
|
131
|
+
#### Props
|
|
132
|
+
|
|
133
|
+
- `container: Container` - The container instance to provide
|
|
134
|
+
- `children: React.ReactNode` - Child components
|
|
135
|
+
|
|
136
|
+
### `useInjection<T>(symbol: symbol): T`
|
|
137
|
+
|
|
138
|
+
A React hook that retrieves a dependency from the container.
|
|
139
|
+
|
|
140
|
+
- **Parameters:**
|
|
141
|
+
- `symbol`: The symbol identifier of the dependency to retrieve
|
|
142
|
+
- **Returns:** The registered dependency value
|
|
143
|
+
- **Throws:** `ReferenceError` if used outside a `Provider`
|
|
144
|
+
|
|
145
|
+
## Example
|
|
146
|
+
|
|
147
|
+
Here's a complete example demonstrating the full usage:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
// 1. Define symbols
|
|
151
|
+
export const Symbols = Object.freeze({
|
|
152
|
+
UserApi: Symbol.for("UserApi"),
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// 2. Create and configure container
|
|
156
|
+
import { Container, Provider } from "@migration-planner-ui/ioc";
|
|
157
|
+
import { UserApi } from "./api/UserApi";
|
|
158
|
+
|
|
159
|
+
const container = new Container();
|
|
160
|
+
container.register(Symbols.UserApi, new UserApi());
|
|
161
|
+
|
|
162
|
+
// 3. Provide container to app
|
|
163
|
+
function App() {
|
|
164
|
+
return (
|
|
165
|
+
<Provider container={container}>
|
|
166
|
+
<UserProfile />
|
|
167
|
+
</Provider>
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 4. Use dependency in component
|
|
172
|
+
import { useInjection } from "@migration-planner-ui/ioc";
|
|
173
|
+
|
|
174
|
+
function UserProfile() {
|
|
175
|
+
const userApi = useInjection(Symbols.UserApi);
|
|
176
|
+
const [user, setUser] = useState(null);
|
|
177
|
+
|
|
178
|
+
useEffect(() => {
|
|
179
|
+
userApi.getUser().then(setUser);
|
|
180
|
+
}, [userApi]);
|
|
181
|
+
|
|
182
|
+
return <div>{user?.name}</div>;
|
|
183
|
+
}
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Features
|
|
187
|
+
|
|
188
|
+
- **Simple API**: Minimal surface area with just a few core concepts
|
|
189
|
+
- **Type-safe**: Full TypeScript support with generics
|
|
190
|
+
- **React-friendly**: Designed specifically for React applications
|
|
191
|
+
- **Lightweight**: No external dependencies beyond React
|
|
192
|
+
- **Singleton scope**: All dependencies are singleton-scoped
|
|
193
|
+
|
|
194
|
+
## Development
|
|
195
|
+
|
|
196
|
+
### Building
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
yarn build
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Code Quality
|
|
203
|
+
|
|
204
|
+
```bash
|
|
205
|
+
# Check code
|
|
206
|
+
yarn check
|
|
207
|
+
|
|
208
|
+
# Fix code issues
|
|
209
|
+
yarn check:fix
|
|
210
|
+
|
|
211
|
+
# Format code
|
|
212
|
+
yarn format
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
### Cleaning
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
yarn clean
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## License
|
|
222
|
+
|
|
223
|
+
[Apache 2.0](LICENSE)
|
|
224
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@migration-planner-ui/ioc",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.36",
|
|
4
4
|
"description": "A basic IoC solution for React apps (inspired by InversifyJs)",
|
|
5
5
|
"author": "Jonathan Kilzi <jkilzi@redhat.com>",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -47,6 +47,8 @@
|
|
|
47
47
|
"react-router-dom": "^6.26.0"
|
|
48
48
|
},
|
|
49
49
|
"files": [
|
|
50
|
-
"dist"
|
|
50
|
+
"dist",
|
|
51
|
+
"README.md",
|
|
52
|
+
"LICENSE"
|
|
51
53
|
]
|
|
52
54
|
}
|