@abyss.ts/express-runner
Advanced tools
Comparing version 0.0.3 to 0.0.4
import { AbyssalApplication, AbyssalContext } from '@abyss.ts/core'; | ||
export { AbyssalContext, Body, Context, Controller, Delete, Get, IAbyssalConfiguration, IAbyssalMiddleware, Inject, Injectable, Param, Patch, Post, Put, Query, Request } from '@abyss.ts/core'; | ||
export { AbyssalContext, AbyssalException, Body, Catch, Context, Controller, Delete, Get, IAbyssalConfiguration, IAbyssalExceptionHandler, IAbyssalMiddleware, Inject, Injectable, Param, Patch, Post, Put, Query, Request, httpStatusCodes } from '@abyss.ts/core'; | ||
import { Request, Response, NextFunction } from 'express'; | ||
@@ -20,5 +20,5 @@ | ||
} | ||
interface IContext extends AbyssalContext<IRequest> { | ||
interface IContext extends AbyssalContext<IRequest, IResponse> { | ||
} | ||
export { ExpressApplication, type IContext, type INext, type IRequest, type IResponse }; |
@@ -5,3 +5,3 @@ var __defProp = Object.defineProperty; | ||
// src/index.ts | ||
import { Get, Put, Body, Post, Patch, Query, Param, Delete, Inject, Request, Context, Controller, Injectable, AbyssalContext as AbyssalContext2 } from "@abyss.ts/core"; | ||
import { Get, Put, Body, Post, Patch, Query, Param, Catch, Delete, Inject, Request, Context, Controller, Injectable, AbyssalContext as AbyssalContext2, httpStatusCodes as httpStatusCodes2, AbyssalException } from "@abyss.ts/core"; | ||
@@ -12,5 +12,5 @@ // src/Application.ts | ||
import detectPort from "detect-port"; | ||
import { createServer as createHttpServer } from "http"; | ||
import express from "express"; | ||
import { createServer as createHttpServer } from "http"; | ||
import { AbyssalContext, AbyssalApplication } from "@abyss.ts/core"; | ||
import { AbyssalContext, AbyssalApplication, getExceptionCatchClassMetadata } from "@abyss.ts/core"; | ||
@@ -82,3 +82,3 @@ // src/asyncStorage.ts | ||
}, httpMethod.toUpperCase(), httpRoute)); | ||
router[httpMethod](httpRoute, async (req, res) => { | ||
router[httpMethod](httpRoute, async (req, res, next) => { | ||
const params = mapParameters({ | ||
@@ -89,4 +89,8 @@ controller, | ||
}); | ||
const result = await exec.bind(controllerInstance)(...params); | ||
res.send(result); | ||
try { | ||
const result = await exec.bind(controllerInstance)(...params); | ||
res.send(result); | ||
} catch (err) { | ||
next(err); | ||
} | ||
}); | ||
@@ -103,2 +107,17 @@ } | ||
// src/BaseExceptionHandler.ts | ||
import { httpStatusCodes } from "@abyss.ts/core"; | ||
var BaseExceptionHandler = class { | ||
static { | ||
__name(this, "BaseExceptionHandler"); | ||
} | ||
async catch(error, ctx, _next) { | ||
const { response } = ctx; | ||
response.status(httpStatusCodes.internalServerError).send({ | ||
status: httpStatusCodes.internalServerError, | ||
message: typeof error === "string" ? error : error.message | ||
}); | ||
} | ||
}; | ||
// src/Application.ts | ||
@@ -110,2 +129,3 @@ var ExpressApplication = class extends AbyssalApplication { | ||
#express; | ||
#baseExceptionHandler; | ||
static #app; | ||
@@ -116,2 +136,3 @@ constructor() { | ||
this._instance = this; | ||
this.#baseExceptionHandler = new BaseExceptionHandler(); | ||
} | ||
@@ -136,2 +157,3 @@ static create() { | ||
const controllers = await this._mapControllers(); | ||
this._mapExceptionHandlers(); | ||
this.#applyMiddlewares(); | ||
@@ -141,2 +163,3 @@ this._mapInjections(); | ||
this.#express.use(router); | ||
this.#applyExceptionHandlers(); | ||
const { port, isStrict } = this._port; | ||
@@ -158,3 +181,3 @@ let runningPort = port; | ||
#createContext() { | ||
this.#express.use((req, _res, next) => { | ||
this.#express.use((req, res, next) => { | ||
Object.assign(req, { | ||
@@ -164,3 +187,4 @@ executionId: nanoid() | ||
const ctx = AbyssalContext.create({ | ||
request: req | ||
request: req, | ||
response: res | ||
}); | ||
@@ -174,5 +198,6 @@ asyncStorage.run(ctx, () => { | ||
for (const middlewareInstace of this._middlewareInstances) { | ||
this.#express.use((req, _res, next) => { | ||
this.#express.use((req, res, next) => { | ||
const ctx = asyncStorage.get() || AbyssalContext.create({ | ||
request: req | ||
request: req, | ||
response: res | ||
}); | ||
@@ -183,6 +208,25 @@ middlewareInstace.use(ctx, next); | ||
} | ||
#applyExceptionHandlers() { | ||
for (const exceptionHandler of this._exceptionHandlers) { | ||
const catchClass = getExceptionCatchClassMetadata(exceptionHandler.constructor); | ||
this.#express.use((err, _req, _res, next) => { | ||
const ctx = asyncStorage.get(); | ||
if (!catchClass || err instanceof catchClass) { | ||
exceptionHandler.catch(err, ctx, next); | ||
} else { | ||
next(err); | ||
} | ||
}); | ||
} | ||
this.#express.use((err, _req, _res, next) => { | ||
const ctx = asyncStorage.get(); | ||
this.#baseExceptionHandler.catch(err, ctx, next); | ||
}); | ||
} | ||
}; | ||
export { | ||
AbyssalContext2 as AbyssalContext, | ||
AbyssalException, | ||
Body, | ||
Catch, | ||
Context, | ||
@@ -200,3 +244,4 @@ Controller, | ||
Query, | ||
Request | ||
Request, | ||
httpStatusCodes2 as httpStatusCodes | ||
}; |
{ | ||
"name": "@abyss.ts/express-runner", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"author": "Alpha", | ||
@@ -27,3 +27,3 @@ "repository": { | ||
"nanoid": "^5.0.7", | ||
"@abyss.ts/core": "^0.0.3" | ||
"@abyss.ts/core": "^0.0.4" | ||
}, | ||
@@ -30,0 +30,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8464
244
+ Added@abyss.ts/core@0.0.4(transitive)
- Removed@abyss.ts/core@0.0.3(transitive)
Updated@abyss.ts/core@^0.0.4