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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Currently Axon is 2X faster than Express. :D please checkout [Axon Benchmarks](.
[Axon telegram channel](https://t.me/axonjs)

Latest change:
- **New**: Improved config system using external config files.
- **Breaking**: Removed `core.loadConfig` – migrate to the new config file setup.
- **New**: Changed router prefix from `core.loadRoute(router, 'prefix')` to `Router('prefix')` or `new AxonRouter('prefix')`

> [!WARNING]
> @mr-mkz/axon deprecated and transferred to @axonlabs/core
Expand Down Expand Up @@ -76,7 +75,6 @@ You can checkout Axon benchmarks document and results from below link.

## Roadmap (still thinking)

- Support controllers better than now.
- Some changes in response structure.
- Response meta generator.
- Auto error detector (maybe)
Expand Down Expand Up @@ -107,7 +105,10 @@ You can access and create routes with just a few steps.
1. creating a variable with a optional name and put `Router()` function in it.
2. define your routes with methods which you want and controller.
- ```js
const router = Router()
// route prefix is optional
const router = Router('prefix') // easier and newer method
// or
// const router = new AxonRouter('prefix');

router.get(path, controller(req, res))
```
Expand Down Expand Up @@ -177,6 +178,8 @@ AxonJs has some types which can help you in developing your applications for aut
- `nextFn`: Type of next function param in middleware.
- `Controller`: Type of controller function.
- `Middleware`: Type of middleware function.
- `HttpMethods`: Type of router http methods.
- `RouterExceptionError`: Type of router exceptions.

### Axon Core config

Expand Down
4 changes: 2 additions & 2 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Axon, Router } from "../src";

const core = Axon();

const router = Router();
const router = Router("/api/v1");

/**
*
Expand Down Expand Up @@ -46,7 +46,7 @@ router.get('/', controller).middleware(async (req, res, next) => {
core.globalMiddleware(testMid);

// second parameter is route prefix which for example change your route from /hello to /api/v1/hello. (Route prefix is optional)
core.loadRoute(router, '/api/v1');
core.loadRoute(router);

// callback function is optional and core has default log message for on start event
// host default is 127.0.0.1 and port default is 8000
Expand Down
2 changes: 1 addition & 1 deletion examples/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const testMid = async (req: Request, res: Response, next: nextFn) => {
core.globalMiddleware(testMid);

core.loadRoute(v1Routes)
core.loadRoute(v2Routes, "/api/v1")
core.loadRoute(v2Routes)

// using plugins for more flexible code and also using ready codes to develop faster than past.
// you can make your own plugins with AxonPlugin interface.
Expand Down
3 changes: 2 additions & 1 deletion examples/routes/v2.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Router } from "../../src";

const router = Router()
// you can set route prefix in Router
const router = Router("/api/v1")

router.get('/hello', async (req, res) => {

Expand Down
35 changes: 33 additions & 2 deletions src/Router/AxonRouter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import RouterException from "./exceptions/RouterException";
import { Controller, HttpMethods, Middleware } from "../types/GlobalTypes";
import addRoutePrefix from "../core/utils/routePrefixHandler";

const duplicateError = (path: string, method: keyof HttpMethods) => {
throw new RouterException({
Expand Down Expand Up @@ -37,7 +38,11 @@ export class AxonRouteHandler {

class AxonRouter {
private routes: HttpMethods;
constructor() {
public prefix: string | undefined;

constructor(prefix?: string) {
this.prefix = prefix;

this.routes = {
GET: {},
POST: {},
Expand All @@ -64,6 +69,8 @@ class AxonRouter {
if (this.routes.GET[path]) {
duplicateError(path, "GET")
}

path = this.handleRoutePrefix(path);

const handler = new AxonRouteHandler(controller);
this.routes.GET[path] = handler
Expand All @@ -88,6 +95,8 @@ class AxonRouter {
duplicateError(path, "POST")
}

path = this.handleRoutePrefix(path);

const handler = new AxonRouteHandler(controller);
this.routes.POST[path] = handler

Expand All @@ -106,6 +115,8 @@ class AxonRouter {
duplicateError(path, "PUT")
}

path = this.handleRoutePrefix(path);

const handler = new AxonRouteHandler(controller);
this.routes.PUT[path] = handler

Expand All @@ -124,6 +135,8 @@ class AxonRouter {
duplicateError(path, "PATCH")
}

path = this.handleRoutePrefix(path);

const handler = new AxonRouteHandler(controller);
this.routes.PATCH[path] = handler

Expand All @@ -142,6 +155,8 @@ class AxonRouter {
duplicateError(path, "DELETE")
}

path = this.handleRoutePrefix(path);

const handler = new AxonRouteHandler(controller);
this.routes.DELETE[path] = handler

Expand All @@ -160,15 +175,31 @@ class AxonRouter {
duplicateError(path, "OPTIONS")
}

path = this.handleRoutePrefix(path);

const handler = new AxonRouteHandler(controller);
this.routes.OPTIONS[path] = handler

return handler;
}

exportRoutes() {
public exportRoutes() {
return this.routes
}

private handleRoutePrefix(path: string) {
if (this.prefix) {
path = addRoutePrefix(path, this.prefix)
}

if (path[0] !== "/")
path = `/${path}`

if (path[path.length - 1] === "/")
path = path.slice(0, -1)

return path;
}
}

export default AxonRouter;
25 changes: 7 additions & 18 deletions src/core/AxonCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ export default class AxonCore {
* @param router instance of Router which routes set with it.
* @param prefix
* @example
* const router = Router()
* const router = Router(); // without prefix
* const router2 = Router("/api/v1"); // with prefix
*
* router.get('/', async (req, res) => {});
*
* core.loadRoute(router) // without prefix
* core.loadRoute(router, '/api/v1') // with prefix
* core.loadRoute(router);
*/
async loadRoute(router: Router, prefix?: string) {
async loadRoute(router: Router) {
this.passRoutes = false;

const routerRoutes: HttpMethods = router.exportRoutes();
Expand All @@ -117,20 +117,8 @@ export default class AxonCore {
if (Object.keys(routerRoutes[method]).length > 0) {
Object.keys(routerRoutes[method]).forEach((route) => {
if (!this.routes[method][route]) {
const originalRoute = route

if (prefix) {
route = addRoutePrefix(route, prefix)
}

if (route[0] !== "/")
route = `/${route}`

if (route[route.length - 1] === "/")
route = route.slice(0, -1)

this.routes[method][route] = routerRoutes[method][originalRoute]
this.routes['OPTIONS'][route] = routerRoutes[method][originalRoute];
this.routes[method][route] = routerRoutes[method][route];
this.routes['OPTIONS'][route] = routerRoutes[method][route];

logger.debug(`loaded route ${method} ${route}`)
} else {
Expand Down Expand Up @@ -489,6 +477,7 @@ export default class AxonCore {
} else if (mode === "http") {
logger.core(colors.whiteBright(`Server started on http://${host}:${portHandler("http")}`));
}
logger.level = "plugin"
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/utils/coreLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const prettyStream = pretty({
coreDebug: 33, // Core debug messages
info: 30,
debug: 20,
trace: 10
plugin: 10
},
customColors: 'fatal:red,error:red,core:magenta,coreDebug:blue,request:cyan,info:green,debug:yellow,trace:white',
customColors: 'fatal:red,error:red,core:magenta,coreDebug:blue,request:cyan,info:green,debug:yellow,plugin:white',
});

const logger = pino(
Expand All @@ -31,7 +31,7 @@ const logger = pino(
coreDebug: 33,
info: 30,
debug: 20,
trace: 10
plugin: 10
},
useOnlyCustomLevels: false
},
Expand Down
12 changes: 9 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@ import AxonRouter from "./Router/AxonRouter";

// Types
import AxonResponse from "./core/response/AxonResponse";
import type { Controller, Middleware , nextFn} from "./types/GlobalTypes";
import type { Controller, Middleware , nextFn, HttpMethods, RouterExceptionError } from "./types/GlobalTypes";
import type { AxonResponseMessage, AxonCorsConfig, AxonHttpsConfig, UnloadRouteParams } from "./types/CoreTypes";
import type { AxonConfig } from "./types/ConfigTypes";
import type { AxonPlugin } from "./types/PluginTypes";

/**
* Instance of AxonRouter for easier usage
* @param prefix prefix for all routes in this router
* @returns {AxonRouter} returns an instance of AxonRouter
* @example
* const router = Router(); // without prefix
* const router2 = Router("/api/v1"); // with prefix
*/
const Router = (): AxonRouter => new AxonRouter();
const Router = (prefix?: string): AxonRouter => new AxonRouter(prefix);

/**
* Instance of AxonCore for easier usage
Expand Down Expand Up @@ -70,5 +74,7 @@ export {
nextFn,
AxonPlugin,
Controller,
Middleware
Middleware,
HttpMethods,
RouterExceptionError
}