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
59 changes: 14 additions & 45 deletions src/Router/AxonRouter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import RouterException from "./exceptions/RouterException";
import addRoutePrefix from "../core/utils/routePrefixHandler";
import { FuncController, Middleware, RouteParams, HttpMethods, MiddlewareStorage } from "../types/RouterTypes";
import { logger } from "../core/utils/coreLogger";
import { resolveConfig } from "../core/config/AxonConfig";
import { AxonValidator } from "../core/validation/AxonValidator";
import { ValidationConfig, ValidationSchema, ValidationTargets } from "../types/ValidatorTypes";
import RouterException from "@/Router/exceptions/RouterException";
import addRoutePrefix from "@/core/utils/routePrefixHandler";
import { FuncController, Middleware, RouteParams, HttpMethods, MiddlewareStorage } from "@/types/RouterTypes";
import { logger } from "@/core/utils/coreLogger";
import { resolveConfig } from "@/core/config/AxonConfig";
import { AxonValidator } from "@/core/validation/AxonValidator";
import type { ValidationObj } from "@/types/RouterTypes";

const duplicateError = (path: string, method: keyof HttpMethods) => {
throw new RouterException({
Expand All @@ -21,37 +21,6 @@ let MIDDLEWARE_TIMEOUT: number;

resolveConfig(false).then(config => MIDDLEWARE_TIMEOUT = config.MIDDLEWARE_TIMEOUT || 10000);

interface ValidateObj {
/**
* Validation schema created with Joi, Zod or Yup.
*
* NOTE: Be carefull, query, params and body for validation is always type of object.
*/
schema: ValidationSchema;

/**
* Options of your validation part for this schema.
*
* To prevent errors while adding options, use 'as <type>' to set type of object.
*
* @example
*
* options: {} as Joi.ValidationOptions
* options: {} as Yup.ValidateOptions
* options: {} as z.ParseParams
*/
options?: ValidationConfig;

/**
* Target of your validator middleware.
*
* - body
* - params (path parameters - /user/12)
* - query (query parameters - ?limit=12)
*/
target?: ValidationTargets | "body";
}

export class AxonRouteHandler<P = {}> {
public _controller: FuncController<P>;
public _middlewares: MiddlewareStorage[];
Expand Down Expand Up @@ -117,7 +86,7 @@ class AxonRouter {
* }
* ])
*/
public get<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidateObj[]) {
public get<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidationObj[]) {
if (this.routes.GET[path]) {
duplicateError(path, "GET")
}
Expand Down Expand Up @@ -168,7 +137,7 @@ class AxonRouter {
* }
* ])
*/
public post<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidateObj[]) {
public post<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidationObj[]) {
if (this.routes.POST[path]) {
duplicateError(path, "POST")
}
Expand Down Expand Up @@ -214,7 +183,7 @@ class AxonRouter {
* }
* ])
*/
public put<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidateObj[]) {
public put<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidationObj[]) {
if (this.routes.PUT[path]) {
duplicateError(path, "PUT")
}
Expand Down Expand Up @@ -264,7 +233,7 @@ class AxonRouter {
* }
* ])
*/
public patch<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidateObj[]) {
public patch<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidationObj[]) {
if (this.routes.PATCH[path]) {
duplicateError(path, "PATCH")
}
Expand Down Expand Up @@ -311,7 +280,7 @@ class AxonRouter {
* }
* ])
*/
public delete<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidateObj[]) {
public delete<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidationObj[]) {
if (this.routes.DELETE[path]) {
duplicateError(path, "DELETE")
}
Expand Down Expand Up @@ -347,7 +316,7 @@ class AxonRouter {
* res.send("Hello World");
* });
*/
public options<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidateObj[]) {
public options<Path extends string>(path: Path, controller: FuncController<RouteParams<Path>>, validation?: ValidationObj[]) {
if (this.routes.OPTIONS[path]) {
duplicateError(path, "OPTIONS")
}
Expand Down Expand Up @@ -451,7 +420,7 @@ class AxonRouter {
return path;
}

private registerValidationMiddlewares(validations: ValidateObj[], handler: AxonRouteHandler<any>) {
private registerValidationMiddlewares(validations: ValidationObj[], handler: AxonRouteHandler<any>) {
for (const validator of validations) {
handler.middleware(AxonValidator.validate(validator.schema, validator.options, validator.target), undefined, true);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Router/exceptions/RouterException.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExceptionMeta, RouterExceptionError } from "../../types/GlobalTypes";
import { ExceptionMeta, RouterExceptionError } from "@/types/GlobalTypes";

class RouterException extends Error {
public name: string;
Expand Down
30 changes: 15 additions & 15 deletions src/core/AxonCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@ import { colors } from "@spacingbat3/kolor"
import { performance } from "perf_hooks";

// Utils
import { logger } from "./utils/coreLogger";
import getRequestBody from "./utils/getRequestBody";
import { logger } from "@/core/utils/coreLogger";
import getRequestBody from "@/core/utils/getRequestBody";

// Types
import type { FuncController, Request, Response, Middleware, HttpMethods } from "..";
import type { AxonPlugin } from "../types/PluginTypes";
import type { JsonResponse } from "../types/GlobalTypes";
import type { AxonConfig } from "../types/ConfigTypes";
import type { UnloadRouteParams } from "../types/CoreTypes";
import type { FuncController, Request, Response, Middleware, HttpMethods } from "@/.";
import type { AxonPlugin } from "@/types/PluginTypes";
import type { JsonResponse } from "@/types/GlobalTypes";
import type { AxonConfig } from "@/types/ConfigTypes";
import type { UnloadRouteParams } from "@/types/CoreTypes";

// Exceptions
import { routeDuplicateException } from "./exceptions/CoreExceptions";
import { routeDuplicateException } from "@/core/exceptions/CoreExceptions";

// Instances
import Router from "../Router/AxonRouter";
import Router from "@/Router/AxonRouter";

// Features
import AxonResponse from "./response/AxonResponse";
import AxonCors from "./cors/AxonCors";
import { PluginLoader } from "./plugin/PluginLoader";
import { resolveConfig } from "./config/AxonConfig";
import { unloadRouteService, unloadRoutesService } from "./services/unloadRoutesService";
import { MiddlewareStorage } from "../types/RouterTypes";
import AxonResponse from "@/core/response/AxonResponse";
import AxonCors from "@/core/cors/AxonCors";
import { PluginLoader } from "@/core/plugin/PluginLoader";
import { resolveConfig } from "@/core/config/AxonConfig";
import { unloadRouteService, unloadRoutesService } from "@/core/services/unloadRoutesService";
import { MiddlewareStorage } from "@/types/RouterTypes";

// Default values
const defaultResponses = {
Expand Down
6 changes: 3 additions & 3 deletions src/core/config/AxonConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import esbuild from 'esbuild';
import { pathToFileURL } from 'url';

// utils
import { logger } from '../utils/coreLogger';
import { logger } from '@/core/utils/coreLogger';

// types
import type { AxonConfig } from "../../types/ConfigTypes";
import type { AxonConfig } from "@/types/ConfigTypes";

// default items
import defaultConfig from './defaultConfig';
import defaultConfig from '@/core/config/defaultConfig';

const dynamicImport = new Function('file', 'return import(file)');

Expand Down
2 changes: 1 addition & 1 deletion src/core/config/defaultConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// types
import type { AxonConfig } from "../../types/ConfigTypes";
import type { AxonConfig } from "@/types/ConfigTypes";

export default {
PROJECT_ENV: "development",
Expand Down
2 changes: 1 addition & 1 deletion src/core/cors/AxonCors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assign from "object-assign";
// import { nextFn, Request, Response } from "../..";
import { NextFunc, Request, Response } from "../../types/RouterTypes";
import { NextFunc, Request, Response } from "@/types/RouterTypes";
import vary from "vary";

const defaults = {
Expand Down
4 changes: 2 additions & 2 deletions src/core/exceptions/CoreExceptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import RouterException from "../../Router/exceptions/RouterException"
import { HttpMethods } from "../../types/RouterTypes"
import RouterException from "@/Router/exceptions/RouterException"
import { HttpMethods } from "@/types/RouterTypes"

/**
* throw new route duplicate error from core to client
Expand Down
6 changes: 3 additions & 3 deletions src/core/plugin/PluginLoader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxonPlugin } from "../../types/PluginTypes";
import AxonCore from "../AxonCore";
import { logger } from "../utils/coreLogger";
import { AxonPlugin } from "@/types/PluginTypes";
import AxonCore from "@/core/AxonCore";
import { logger } from "@/core/utils/coreLogger";

export class PluginLoader {
private plugins: AxonPlugin[] = [];
Expand Down
2 changes: 1 addition & 1 deletion src/core/response/AxonResponse.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Response } from "../..";
import { Response } from "@/.";

class AxonResponse {
private res: Response;
Expand Down
6 changes: 3 additions & 3 deletions src/core/services/unloadRoutesService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AxonRouter from "../../Router/AxonRouter";
import { HttpMethods } from "../../types/RouterTypes";
import { logger } from "../utils/coreLogger";
import AxonRouter from "@/Router/AxonRouter";
import { HttpMethods } from "@/types/RouterTypes";
import { logger } from "@/core/utils/coreLogger";

interface UnloadRouteParams {
router?: AxonRouter;
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/getRequestBody.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from "../../types/RouterTypes";
import { Request } from "@/types/RouterTypes";

const getRequestBody = async (req: Request<any>): Promise<string | Record<string, string | undefined> | undefined> => {
return new Promise((resolve, reject) => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/validation/AxonValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as yup from "yup";
import joi from "joi";
import { ZodSchema, z } from "zod";

import { Middleware } from "../../types/RouterTypes";
import { Middleware } from "@/types/RouterTypes";

import type {
ValidationConfig,
Expand Down
70 changes: 58 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@
import * as http from "http";

// Instances
import AxonCore from "./core/AxonCore";
import AxonRouter from "./Router/AxonRouter";
import { logger } from "./core/utils/coreLogger";
import AxonCore from "@/core/AxonCore";
import AxonRouter from "@/Router/AxonRouter";
import { logger } from "@/core/utils/coreLogger";

// Types
import type { RouterExceptionError } from "./types/GlobalTypes";
import type { AxonResponseMessage, AxonCorsConfig, AxonHttpsConfig, UnloadRouteParams } from "./types/CoreTypes";
import type { Request, Response, Middleware, NextFunc, FuncController, HttpMethods } from "./types/RouterTypes";
import type { AxonConfig } from "./types/ConfigTypes";
import type { AxonPlugin, PluginMode } from "./types/PluginTypes";
import type { RouterExceptionError } from "@/types/GlobalTypes";
import type {
AxonResponseMessage,
AxonCorsConfig,
AxonHttpsConfig,
UnloadRouteParams
} from "@/types/CoreTypes";
import type {
Request,
Response,
Middleware,
NextFunc,
FuncController,
HttpMethods,
ValidationObj
} from "@/types/RouterTypes";
import type { AxonConfig } from "@/types/ConfigTypes";
import type { AxonPlugin, PluginMode } from "@/types/PluginTypes";
import type {
ValidationConfig,
ValidationSchema,
ValidationTargets
} from "@/types/ValidatorTypes";

/**
* Instance of AxonRouter for easier usage
Expand All @@ -30,31 +48,59 @@ const Router = (prefix?: string): AxonRouter => new AxonRouter(prefix);
const Axon = (): AxonCore => new AxonCore();

/**
* Instance of logger which used in core to use it in your code.
* Pino logger instance used in Axon core and exposed for external use.
*/
const axonLogger = logger;

interface Headers extends http.OutgoingHttpHeaders { }
// interface Headers extends http.OutgoingHttpHeaders { }
type Headers = http.OutgoingHttpHeaders;

export {
// Cores - Router and Main core
AxonCore,
AxonRouter,

// Core instance generators
Axon,
Router,

// Core config
AxonConfig,

// Configs - configuration feature
AxonResponseMessage,
AxonCorsConfig,
AxonHttpsConfig,

// Main core
UnloadRouteParams,

// Router
HttpMethods,
ValidationObj,

// Request life cycle
Request,
Response,
Headers,
NextFunc,

// Plugin
AxonPlugin,
PluginMode,

// Handlers
FuncController,
Middleware,
HttpMethods,

// Errors
RouterExceptionError,
axonLogger

// Pino logger - Logger feature
axonLogger,

// Axon Validator - Validation feature
ValidationConfig,
ValidationSchema,
ValidationTargets
}
2 changes: 1 addition & 1 deletion src/types/ConfigTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
AxonResponseMessage,
AxonCorsConfig,
AxonHttpsConfig
} from "./CoreTypes";
} from "@/types/CoreTypes";

interface AxonConfig {
/**
Expand Down
7 changes: 5 additions & 2 deletions src/types/CoreTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ServerOptions } from "https";
import AxonRouter from "../Router/AxonRouter";
import { HttpMethods } from "./RouterTypes";
import AxonRouter from "@/Router/AxonRouter";
import { HttpMethods } from "@/types/RouterTypes";

type AxonHttpsConfig = ServerOptions

Expand Down Expand Up @@ -107,6 +107,9 @@ interface AxonResponseMessage {
[key: string]: string | undefined;
}

/**
* Type of input object of unloadRoute method.
*/
interface UnloadRouteParams {
/**
* [Optional]
Expand Down
2 changes: 1 addition & 1 deletion src/types/GlobalTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Headers } from "..";
import { Headers } from "@/.";

export interface JsonResponse {
body: object;
Expand Down
2 changes: 1 addition & 1 deletion src/types/PluginTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import AxonCore from "../core/AxonCore";
import AxonCore from "@/core/AxonCore";

/**
* This type is about the mode of project environment that your plugin built for it.
Expand Down
Loading