@bejibun/core 0.1.41 → 0.1.42
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/CHANGELOG.md +19 -0
- package/builders/RouterBuilder.js +1 -1
- package/exceptions/ExceptionHandler.d.ts +7 -0
- package/exceptions/ExceptionHandler.js +33 -0
- package/exceptions/RuntimeException.d.ts +4 -0
- package/exceptions/RuntimeException.js +14 -0
- package/exceptions/index.d.ts +1 -0
- package/exceptions/index.js +1 -0
- package/package.json +2 -2
- package/server.d.ts +1 -0
- package/server.js +46 -0
- package/types/router.d.ts +1 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,25 @@ All notable changes to this project will be documented in this file.
|
|
|
3
3
|
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
+
## [v0.1.42](https://github.com/crenata/bejibun-core/compare/v0.1.41...v0.1.42) - 2025-10-21
|
|
7
|
+
|
|
8
|
+
### 🩹 Fixes
|
|
9
|
+
- Fix controller path on router builder
|
|
10
|
+
|
|
11
|
+
### 📖 Changes
|
|
12
|
+
What's New :
|
|
13
|
+
- Adding base exception handler
|
|
14
|
+
- Adding runtime exception
|
|
15
|
+
- Adding `server.ts` for init serve
|
|
16
|
+
|
|
17
|
+
### ❤️Contributors
|
|
18
|
+
- Havea Crenata ([@crenata](https://github.com/crenata))
|
|
19
|
+
- Ghulje ([@ghulje](https://github.com/ghulje))
|
|
20
|
+
|
|
21
|
+
**Full Changelog**: https://github.com/crenata/bejibun-core/blob/master/CHANGELOG.md
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
6
25
|
## [v0.1.41](https://github.com/crenata/bejibun-core/compare/v0.1.40...v0.1.41) - 2025-10-20
|
|
7
26
|
|
|
8
27
|
### 🩹 Fixes
|
|
@@ -111,7 +111,7 @@ export default class RouterBuilder {
|
|
|
111
111
|
throw new RouterInvalidException(`Invalid router controller definition: ${definition}.`);
|
|
112
112
|
}
|
|
113
113
|
const controllerPath = path.resolve(App.rootPath(), this.baseNamespace);
|
|
114
|
-
const location = Bun.resolveSync(
|
|
114
|
+
const location = Bun.resolveSync(`./${controllerName}.ts`, controllerPath);
|
|
115
115
|
let ControllerClass;
|
|
116
116
|
try {
|
|
117
117
|
ControllerClass = require(location).default;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationError } from "objection";
|
|
2
|
+
import ModelNotFoundException from "../exceptions/ModelNotFoundException";
|
|
3
|
+
import ValidatorException from "../exceptions/ValidatorException";
|
|
4
|
+
export default class ExceptionHandler {
|
|
5
|
+
handle(error: Bun.ErrorLike | ModelNotFoundException | ValidatorException | ValidationError): globalThis.Response;
|
|
6
|
+
route(request: Bun.BunRequest): globalThis.Response;
|
|
7
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import Logger from "@bejibun/logger";
|
|
2
|
+
import { defineValue } from "@bejibun/utils";
|
|
3
|
+
import HttpMethodEnum from "@bejibun/utils/enums/HttpMethodEnum";
|
|
4
|
+
import { ValidationError } from "objection";
|
|
5
|
+
import ModelNotFoundException from "../exceptions/ModelNotFoundException";
|
|
6
|
+
import ValidatorException from "../exceptions/ValidatorException";
|
|
7
|
+
import Response from "../facades/Response";
|
|
8
|
+
export default class ExceptionHandler {
|
|
9
|
+
handle(error) {
|
|
10
|
+
Logger.setContext("APP").error(error.message).trace(error.stack);
|
|
11
|
+
if (error instanceof ModelNotFoundException ||
|
|
12
|
+
error instanceof ValidatorException)
|
|
13
|
+
return Response
|
|
14
|
+
.setMessage(error.message)
|
|
15
|
+
.setStatus(error.code)
|
|
16
|
+
.send();
|
|
17
|
+
if (error instanceof ValidationError)
|
|
18
|
+
return Response
|
|
19
|
+
.setMessage(error.message)
|
|
20
|
+
.setStatus(error.statusCode)
|
|
21
|
+
.send();
|
|
22
|
+
return Response
|
|
23
|
+
.setMessage(defineValue(error.message, "Internal server error."))
|
|
24
|
+
.setStatus(500)
|
|
25
|
+
.send();
|
|
26
|
+
}
|
|
27
|
+
route(request) {
|
|
28
|
+
return Response
|
|
29
|
+
.setMessage("What are you looking for doesn't exists.")
|
|
30
|
+
.setStatus(request.method === HttpMethodEnum.Options ? 204 : 404)
|
|
31
|
+
.send();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Logger from "@bejibun/logger";
|
|
2
|
+
import { defineValue } from "@bejibun/utils";
|
|
3
|
+
export default class RuntimeException extends Error {
|
|
4
|
+
code;
|
|
5
|
+
constructor(message, code) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "RuntimeException";
|
|
8
|
+
this.code = defineValue(code, 500);
|
|
9
|
+
Logger.setContext(this.name).error(this.message).trace(this.stack);
|
|
10
|
+
if (Error.captureStackTrace) {
|
|
11
|
+
Error.captureStackTrace(this, RuntimeException);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
package/exceptions/index.d.ts
CHANGED
package/exceptions/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bejibun/core",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.42",
|
|
4
4
|
"author": "Havea Crenata <havea.crenata@gmail.com>",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"main": "index.js",
|
|
10
10
|
"module": "index.js",
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@bejibun/app": "^0.1.
|
|
12
|
+
"@bejibun/app": "^0.1.13",
|
|
13
13
|
"@bejibun/cors": "^0.1.11",
|
|
14
14
|
"@bejibun/logger": "^0.1.18",
|
|
15
15
|
"@bejibun/utils": "^0.1.14",
|
package/server.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "./bootstrap";
|
package/server.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import App from "@bejibun/app";
|
|
2
|
+
import Logger from "@bejibun/logger";
|
|
3
|
+
import RuntimeException from "./exceptions/RuntimeException";
|
|
4
|
+
import Router from "./facades/Router";
|
|
5
|
+
import "./bootstrap";
|
|
6
|
+
const exceptionHandlerPath = App.appPath("exceptions/handler.ts");
|
|
7
|
+
let ExceptionHandler;
|
|
8
|
+
try {
|
|
9
|
+
ExceptionHandler = require(exceptionHandlerPath).default;
|
|
10
|
+
}
|
|
11
|
+
catch {
|
|
12
|
+
throw new RuntimeException(`Missing exception handler class [${exceptionHandlerPath}].`);
|
|
13
|
+
}
|
|
14
|
+
const apiRoutesPath = App.routesPath("api.ts");
|
|
15
|
+
let ApiRoutes;
|
|
16
|
+
try {
|
|
17
|
+
ApiRoutes = require(apiRoutesPath).default;
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
throw new RuntimeException(`Missing api file on routes directory [${apiRoutesPath}].`);
|
|
21
|
+
}
|
|
22
|
+
const webRoutesPath = App.routesPath("web.ts");
|
|
23
|
+
let WebRoutes;
|
|
24
|
+
try {
|
|
25
|
+
WebRoutes = require(webRoutesPath).default;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
throw new RuntimeException(`Missing web file on routes directory [${webRoutesPath}].`);
|
|
29
|
+
}
|
|
30
|
+
const server = Bun.serve({
|
|
31
|
+
development: Bun.env.NODE_ENV !== "production" && {
|
|
32
|
+
// Enable browser hot reloading in development
|
|
33
|
+
hmr: true,
|
|
34
|
+
// Echo console logs from the browser to the server
|
|
35
|
+
console: true
|
|
36
|
+
},
|
|
37
|
+
error: new ExceptionHandler().handle,
|
|
38
|
+
port: Bun.env.APP_PORT,
|
|
39
|
+
routes: {
|
|
40
|
+
...Router.namespace("app/exceptions").any("/*", "Handler@route"),
|
|
41
|
+
"/": require(App.publicPath("index.html")),
|
|
42
|
+
...ApiRoutes,
|
|
43
|
+
...WebRoutes
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
Logger.setContext("APP").info(`🚀 Server running at ${server.url.origin}`);
|
package/types/router.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export type HandlerType = (request: BunRequest) => Promise<Response>;
|
|
1
|
+
export type HandlerType = (request: Bun.BunRequest) => Promise<Response>;
|
|
4
2
|
export type RouterGroup = Record<string, Record<string, HandlerType>>;
|
|
5
3
|
export type ResourceAction = "index" | "store" | "show" | "update" | "destroy";
|