@kanjijs/core 0.2.0-beta.1
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/README.md +55 -0
- package/dist/index.js +1370 -0
- package/package.json +24 -0
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @kanjijs/core
|
|
2
|
+
|
|
3
|
+
The "Brain" of the framework. This package handles the **Declaration Phase**.
|
|
4
|
+
It provides the decorators used to define APIs and the internal storage mechanism to save that metadata for later use by the Adapter.
|
|
5
|
+
|
|
6
|
+
## 📦 Features
|
|
7
|
+
|
|
8
|
+
### Decorators
|
|
9
|
+
|
|
10
|
+
- **`@Controller(prefix: string)`**: Marks a class as a controller and sets the root path.
|
|
11
|
+
- **`@Get(path: string)`**: Registers a GET route.
|
|
12
|
+
- **`@Post(path: string)`**: Registers a POST route.
|
|
13
|
+
- **`@Put`, `@Delete`, `@Patch`**: Support for standard HTTP verbs.
|
|
14
|
+
- **`@Use(...middlewares)`**: Registers Hono-compatible middleware for a route or controller.
|
|
15
|
+
- **`@Module({ ... })`**: Defines a module with `imports`, `controllers`, and `providers`.
|
|
16
|
+
- **`@Inject(token)`**: Token-based dependency injection.
|
|
17
|
+
|
|
18
|
+
### Request Context (AsyncLocalStorage)
|
|
19
|
+
Kanjijs Core includes a built-in `kanjijsContext` (based on Node.js AsyncLocalStorage) to track request state globally without prop drilling. It enables access to `requestId` anywhere in the stack.
|
|
20
|
+
|
|
21
|
+
```typescript
|
|
22
|
+
import { getRequestId } from "@kanjijs/core";
|
|
23
|
+
const reqId = getRequestId();
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Dependency Injection
|
|
27
|
+
**KanjijsIoC**: A lightweight, platform-agnostic IoC container embedded in Core. It handles singleton resolution and dependency injection for your modules and controllers.
|
|
28
|
+
|
|
29
|
+
### Metadata Storage
|
|
30
|
+
|
|
31
|
+
We use a `WeakMap`-based `MetadataStorage` singleton to keep the memory footprint minimal and ensure metadata is garbage collected if the controller class is no longer referenced.
|
|
32
|
+
|
|
33
|
+
```typescript
|
|
34
|
+
// Internal usage
|
|
35
|
+
const routes = MetadataStorage.getRoutes(ControllerClass.prototype);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 🛡️ Exception Handling
|
|
39
|
+
|
|
40
|
+
Kanjijs Core defines the standard exceptions used across the framework.
|
|
41
|
+
|
|
42
|
+
- **`HttpException`**: Base class for HTTP errors.
|
|
43
|
+
- **`ExceptionFilter`**: Interface for implementing custom exception filters.
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { HttpException } from "@kanjijs/core";
|
|
47
|
+
|
|
48
|
+
throw new HttpException("Custom Error", 400);
|
|
49
|
+
// OR
|
|
50
|
+
throw new HttpException({ message: "Validation Failed", errors: [] }, 422);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## 🚫 No Runtime Cost
|
|
54
|
+
|
|
55
|
+
This package does **not** depend on Hono, Bun, or Express. It only uses `reflect-metadata` to store information. The actual execution logic is delegated to the Platform adapters.
|