@tahminator/sapling 1.0.4 → 1.1.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/dist/src/annotation/index.d.ts +0 -1
- package/dist/src/annotation/index.js +0 -1
- package/dist/src/helper/error.d.ts +10 -0
- package/dist/src/helper/error.js +17 -0
- package/dist/src/helper/index.d.ts +2 -0
- package/dist/src/helper/index.js +2 -0
- package/dist/src/helper/sapling.d.ts +24 -0
- package/dist/src/helper/sapling.js +33 -0
- package/package.json +2 -2
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { HttpStatus } from "../enum";
|
|
2
|
+
/**
|
|
3
|
+
* Ensure that you define a middleware that can handle this error.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link Sapling.loadResponseStatusErrorMiddleware}
|
|
6
|
+
*/
|
|
7
|
+
export declare class ResponseStatusError extends Error {
|
|
8
|
+
readonly status: HttpStatus;
|
|
9
|
+
constructor(status: HttpStatus, message?: string);
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { HttpStatus } from "../enum";
|
|
2
|
+
/**
|
|
3
|
+
* Ensure that you define a middleware that can handle this error.
|
|
4
|
+
*
|
|
5
|
+
* @see {@link Sapling.loadResponseStatusErrorMiddleware}
|
|
6
|
+
*/
|
|
7
|
+
export class ResponseStatusError extends Error {
|
|
8
|
+
constructor(status, message) {
|
|
9
|
+
super(message !== null && message !== void 0 ? message : "Something went wrong.");
|
|
10
|
+
this.status = status;
|
|
11
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
12
|
+
this.name = `HttpError(${HttpStatus[status]})`;
|
|
13
|
+
if (Error.captureStackTrace) {
|
|
14
|
+
Error.captureStackTrace(this, ResponseStatusError);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
package/dist/src/helper/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import e, { Router } from "express";
|
|
2
2
|
import { Class, ExpressMiddlewareFn } from "../types";
|
|
3
|
+
import { ResponseStatusError } from "./error";
|
|
3
4
|
/**
|
|
4
5
|
* Collection of utility functions which are essential for Sapling to function.
|
|
5
6
|
*/
|
|
@@ -45,6 +46,29 @@ export declare class Sapling {
|
|
|
45
46
|
* ```
|
|
46
47
|
*/
|
|
47
48
|
static registerApp(app: e.Express): void;
|
|
49
|
+
/**
|
|
50
|
+
* Register a middleware that will handle {@link ResponseStatusError}.
|
|
51
|
+
*
|
|
52
|
+
* This middleware will chain to the next middleware if it does not catch {@link ResponseStatusError}.
|
|
53
|
+
* You may still define middleware to handle all other errors in a separate `app.use` call.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* import express from "express";
|
|
58
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
59
|
+
*
|
|
60
|
+
* const app = express();
|
|
61
|
+
*
|
|
62
|
+
* Sapling.loadResponseStatusErrorMiddleware(app, (err, req, res, next) => {
|
|
63
|
+
* // `err` is guaranteed to be of type ResponseStatusError
|
|
64
|
+
* res.status(err.status).json({
|
|
65
|
+
* success: false,
|
|
66
|
+
* message: err.message,
|
|
67
|
+
* });
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
static loadResponseStatusErrorMiddleware(app: e.Express, fn: (err: ResponseStatusError, request: e.Request, response: e.Response, next: e.NextFunction) => void): void;
|
|
48
72
|
/**
|
|
49
73
|
* Serialize a value into a JSON string.
|
|
50
74
|
*
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import e from "express";
|
|
2
2
|
import { _ControllerRegistry } from "../annotation/controller";
|
|
3
|
+
import { ResponseStatusError } from "./error";
|
|
3
4
|
let settings = {
|
|
4
5
|
serialize: JSON.stringify,
|
|
5
6
|
deserialize: JSON.parse,
|
|
@@ -81,6 +82,38 @@ export class Sapling {
|
|
|
81
82
|
app.use(e.text({ type: "application/json" }));
|
|
82
83
|
app.use(this.json());
|
|
83
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* Register a middleware that will handle {@link ResponseStatusError}.
|
|
87
|
+
*
|
|
88
|
+
* This middleware will chain to the next middleware if it does not catch {@link ResponseStatusError}.
|
|
89
|
+
* You may still define middleware to handle all other errors in a separate `app.use` call.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```ts
|
|
93
|
+
* import express from "express";
|
|
94
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
95
|
+
*
|
|
96
|
+
* const app = express();
|
|
97
|
+
*
|
|
98
|
+
* Sapling.loadResponseStatusErrorMiddleware(app, (err, req, res, next) => {
|
|
99
|
+
* // `err` is guaranteed to be of type ResponseStatusError
|
|
100
|
+
* res.status(err.status).json({
|
|
101
|
+
* success: false,
|
|
102
|
+
* message: err.message,
|
|
103
|
+
* });
|
|
104
|
+
* });
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
static loadResponseStatusErrorMiddleware(app, fn) {
|
|
108
|
+
app.use(((err, req, res, next) => {
|
|
109
|
+
if (err instanceof ResponseStatusError) {
|
|
110
|
+
fn(err, req, res, next);
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
next(err);
|
|
114
|
+
}
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
84
117
|
/**
|
|
85
118
|
* Serialize a value into a JSON string.
|
|
86
119
|
*
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tahminator/sapling",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "A library to help you write cleaner Express.js code",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
7
|
-
"build": "tsc"
|
|
7
|
+
"build": "rm -rf dist && tsc"
|
|
8
8
|
},
|
|
9
9
|
"keywords": [],
|
|
10
10
|
"author": "",
|