@tahminator/sapling 1.5.16 → 1.5.18
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/eslint.config.d.ts +2 -0
- package/dist/eslint.config.js +38 -0
- package/dist/exclusions.d.ts +5 -0
- package/dist/exclusions.js +6 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/weakmap.d.ts +15 -0
- package/dist/lib/weakmap.js +77 -0
- package/dist/src/annotation/controller.d.ts +21 -0
- package/dist/src/annotation/controller.js +78 -0
- package/dist/src/annotation/index.d.ts +4 -0
- package/dist/src/annotation/index.js +4 -0
- package/dist/src/annotation/injectable.d.ts +25 -0
- package/dist/src/annotation/injectable.js +69 -0
- package/dist/src/annotation/middleware.d.ts +9 -0
- package/dist/src/annotation/middleware.js +11 -0
- package/dist/src/annotation/route.d.ts +47 -0
- package/dist/src/annotation/route.js +77 -0
- package/dist/src/enum/http.d.ts +68 -0
- package/dist/src/enum/http.js +71 -0
- package/dist/src/enum/index.d.ts +1 -0
- package/dist/src/enum/index.js +1 -0
- package/dist/src/helper/error.d.ts +10 -0
- package/dist/src/helper/error.js +19 -0
- package/dist/src/helper/importer.d.ts +1 -0
- package/dist/src/helper/importer.js +6 -0
- package/dist/src/helper/index.d.ts +4 -0
- package/dist/src/helper/index.js +4 -0
- package/dist/src/helper/redirect.d.ts +14 -0
- package/dist/src/helper/redirect.js +19 -0
- package/dist/src/helper/response.d.ts +68 -0
- package/dist/src/helper/response.js +90 -0
- package/dist/src/helper/sapling.d.ts +101 -0
- package/dist/src/helper/sapling.js +153 -0
- package/dist/src/html/404.d.ts +4 -0
- package/dist/src/html/404.js +15 -0
- package/dist/src/html/index.d.ts +1 -0
- package/dist/src/html/index.js +1 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +5 -0
- package/dist/src/types.d.ts +21 -0
- package/dist/src/types.js +11 -0
- package/dist/vite.config.d.ts +2 -0
- package/dist/vite.config.js +17 -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,19 @@
|
|
|
1
|
+
import { HttpStatus } from "../enum";
|
|
2
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
3
|
+
import { Sapling } from "./sapling";
|
|
4
|
+
/**
|
|
5
|
+
* Ensure that you define a middleware that can handle this error.
|
|
6
|
+
*
|
|
7
|
+
* @see {@link Sapling.loadResponseStatusErrorMiddleware}
|
|
8
|
+
*/
|
|
9
|
+
export class ResponseStatusError extends Error {
|
|
10
|
+
constructor(status, message) {
|
|
11
|
+
super(message !== null && message !== void 0 ? message : "Something went wrong.");
|
|
12
|
+
this.status = status;
|
|
13
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
14
|
+
this.name = `HttpError(${HttpStatus[status]})`;
|
|
15
|
+
if (Error.captureStackTrace) {
|
|
16
|
+
Error.captureStackTrace(this, ResponseStatusError);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getControllers: () => any;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic HTTP redirect wrapped modeled after Spring's `RedirectView`.
|
|
3
|
+
*
|
|
4
|
+
* You can either return `new RedirectView(url)` or `RedirectView.redirect(url)` inside of a controller method.
|
|
5
|
+
*/
|
|
6
|
+
export declare class RedirectView {
|
|
7
|
+
_url: string;
|
|
8
|
+
constructor(url: string);
|
|
9
|
+
getUrl(): string;
|
|
10
|
+
/**
|
|
11
|
+
* Instantiate `RedirectView` with the given `url`.
|
|
12
|
+
*/
|
|
13
|
+
static redirect(url: string): RedirectView;
|
|
14
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic HTTP redirect wrapped modeled after Spring's `RedirectView`.
|
|
3
|
+
*
|
|
4
|
+
* You can either return `new RedirectView(url)` or `RedirectView.redirect(url)` inside of a controller method.
|
|
5
|
+
*/
|
|
6
|
+
export class RedirectView {
|
|
7
|
+
constructor(url) {
|
|
8
|
+
this._url = url;
|
|
9
|
+
}
|
|
10
|
+
getUrl() {
|
|
11
|
+
return this._url;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Instantiate `RedirectView` with the given `url`.
|
|
15
|
+
*/
|
|
16
|
+
static redirect(url) {
|
|
17
|
+
return new RedirectView(url);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { HttpHeaders } from "../types";
|
|
2
|
+
/**
|
|
3
|
+
* Generic HTTP response wrapper modeled after Spring's `ResponseEntity`.
|
|
4
|
+
*
|
|
5
|
+
* Provides status code, headers, and an optional typed body.
|
|
6
|
+
* The body is serialized through `Sapling.serialize`.
|
|
7
|
+
*
|
|
8
|
+
* @typeParam T - the type of the response body
|
|
9
|
+
*/
|
|
10
|
+
export declare class ResponseEntity<T> {
|
|
11
|
+
private readonly _statusCode;
|
|
12
|
+
private readonly _headers;
|
|
13
|
+
private readonly _body;
|
|
14
|
+
constructor(body: T, headers?: HttpHeaders, statusCode?: number);
|
|
15
|
+
/**
|
|
16
|
+
* Create a builder with a 200 status code.
|
|
17
|
+
*
|
|
18
|
+
* @example```ts
|
|
19
|
+
* return ResponseEntity.ok().body({ success: true });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
static ok(): ResponseEntityBuilder;
|
|
23
|
+
/**
|
|
24
|
+
* Create a builder with a custom status code.
|
|
25
|
+
*
|
|
26
|
+
* @example```ts
|
|
27
|
+
* return ResponseEntity.status(HttpStatus.BAD_REQUEST).body({ success: false });
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @see {@link HttpStatus}
|
|
31
|
+
*/
|
|
32
|
+
static status(statusCode: number): ResponseEntityBuilder;
|
|
33
|
+
/**
|
|
34
|
+
* Return status code.
|
|
35
|
+
*/
|
|
36
|
+
getStatusCode(): number;
|
|
37
|
+
/**
|
|
38
|
+
* Return headers.
|
|
39
|
+
*/
|
|
40
|
+
getHeaders(): HttpHeaders;
|
|
41
|
+
/**
|
|
42
|
+
* Return the response body.
|
|
43
|
+
*/
|
|
44
|
+
getBody(): T;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Builder for {@link ResponseEntity}.
|
|
48
|
+
*
|
|
49
|
+
* Forces the status code to be set first, then headers and body,
|
|
50
|
+
* ensuring type safety when constructing the response.
|
|
51
|
+
*/
|
|
52
|
+
export declare class ResponseEntityBuilder {
|
|
53
|
+
private readonly _statusCode;
|
|
54
|
+
private _headers;
|
|
55
|
+
constructor(statusCode: number);
|
|
56
|
+
/**
|
|
57
|
+
* Set all headers as an object with keys and values.
|
|
58
|
+
*/
|
|
59
|
+
headers(headers: HttpHeaders): this;
|
|
60
|
+
/**
|
|
61
|
+
* Add/override a single key and value to the headers.
|
|
62
|
+
*/
|
|
63
|
+
setHeader(key: string, value: string): this;
|
|
64
|
+
/**
|
|
65
|
+
* Set the response body.
|
|
66
|
+
*/
|
|
67
|
+
body<T>(body: T): ResponseEntity<T>;
|
|
68
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
+
import { HttpStatus } from "../enum";
|
|
3
|
+
/**
|
|
4
|
+
* Generic HTTP response wrapper modeled after Spring's `ResponseEntity`.
|
|
5
|
+
*
|
|
6
|
+
* Provides status code, headers, and an optional typed body.
|
|
7
|
+
* The body is serialized through `Sapling.serialize`.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam T - the type of the response body
|
|
10
|
+
*/
|
|
11
|
+
export class ResponseEntity {
|
|
12
|
+
constructor(body, headers = {}, statusCode = 200) {
|
|
13
|
+
this._headers = {};
|
|
14
|
+
this._body = body;
|
|
15
|
+
this._headers = headers;
|
|
16
|
+
this._statusCode = statusCode;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Create a builder with a 200 status code.
|
|
20
|
+
*
|
|
21
|
+
* @example```ts
|
|
22
|
+
* return ResponseEntity.ok().body({ success: true });
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
static ok() {
|
|
26
|
+
return new ResponseEntityBuilder(200);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Create a builder with a custom status code.
|
|
30
|
+
*
|
|
31
|
+
* @example```ts
|
|
32
|
+
* return ResponseEntity.status(HttpStatus.BAD_REQUEST).body({ success: false });
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @see {@link HttpStatus}
|
|
36
|
+
*/
|
|
37
|
+
static status(statusCode) {
|
|
38
|
+
return new ResponseEntityBuilder(statusCode);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Return status code.
|
|
42
|
+
*/
|
|
43
|
+
getStatusCode() {
|
|
44
|
+
return this._statusCode;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Return headers.
|
|
48
|
+
*/
|
|
49
|
+
getHeaders() {
|
|
50
|
+
return this._headers;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Return the response body.
|
|
54
|
+
*/
|
|
55
|
+
getBody() {
|
|
56
|
+
return this._body;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Builder for {@link ResponseEntity}.
|
|
61
|
+
*
|
|
62
|
+
* Forces the status code to be set first, then headers and body,
|
|
63
|
+
* ensuring type safety when constructing the response.
|
|
64
|
+
*/
|
|
65
|
+
export class ResponseEntityBuilder {
|
|
66
|
+
constructor(statusCode) {
|
|
67
|
+
this._headers = {};
|
|
68
|
+
this._statusCode = statusCode;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Set all headers as an object with keys and values.
|
|
72
|
+
*/
|
|
73
|
+
headers(headers) {
|
|
74
|
+
this._headers = headers;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Add/override a single key and value to the headers.
|
|
79
|
+
*/
|
|
80
|
+
setHeader(key, value) {
|
|
81
|
+
this._headers[key] = value;
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Set the response body.
|
|
86
|
+
*/
|
|
87
|
+
body(body) {
|
|
88
|
+
return new ResponseEntity(body, this._headers, this._statusCode);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { Router } from "express";
|
|
2
|
+
import e from "express";
|
|
3
|
+
import type { Class, ExpressMiddlewareFn } from "../types";
|
|
4
|
+
import { ResponseStatusError } from "./error";
|
|
5
|
+
/**
|
|
6
|
+
* Collection of utility functions which are essential for Sapling to function.
|
|
7
|
+
*/
|
|
8
|
+
export declare class Sapling {
|
|
9
|
+
/**
|
|
10
|
+
* If you would prefer to manually resolve your controllers instead, call resolve
|
|
11
|
+
* on the controller class.
|
|
12
|
+
*
|
|
13
|
+
* @example```ts
|
|
14
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
15
|
+
* import TestController from "./path/to/test.controller";
|
|
16
|
+
*
|
|
17
|
+
* const app = express();
|
|
18
|
+
*
|
|
19
|
+
* const router = Sapling.resolve(TestController);
|
|
20
|
+
* app.use(router);
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
static resolve<TClass>(this: void, clazz: Class<TClass>): Router;
|
|
24
|
+
/**
|
|
25
|
+
* Register this function as a middleware in order to utilize Sapling's `deserialize` function.
|
|
26
|
+
*
|
|
27
|
+
* @example```ts
|
|
28
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
29
|
+
* import express from "express";
|
|
30
|
+
*
|
|
31
|
+
* const app = express();
|
|
32
|
+
*
|
|
33
|
+
* app.use(Sapling.json());
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
static json(this: void): ExpressMiddlewareFn;
|
|
37
|
+
/**
|
|
38
|
+
* Register your application with all the necessary middlewares and logics for Sapling to function.
|
|
39
|
+
*
|
|
40
|
+
* @example```ts
|
|
41
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
42
|
+
* import express from "express";
|
|
43
|
+
*
|
|
44
|
+
* const app = express();
|
|
45
|
+
*
|
|
46
|
+
* app.registerApp(app);
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
static registerApp(app: e.Express): void;
|
|
50
|
+
/**
|
|
51
|
+
* Register a middleware that will handle {@link ResponseStatusError}.
|
|
52
|
+
*
|
|
53
|
+
* This middleware will chain to the next middleware if it does not catch {@link ResponseStatusError}.
|
|
54
|
+
* You may still define middleware to handle all other errors in a separate `app.use` call.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* import express from "express";
|
|
59
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
60
|
+
*
|
|
61
|
+
* const app = express();
|
|
62
|
+
*
|
|
63
|
+
* Sapling.loadResponseStatusErrorMiddleware(app, (err, req, res, next) => {
|
|
64
|
+
* // `err` is guaranteed to be of type ResponseStatusError
|
|
65
|
+
* res.status(err.status).json({
|
|
66
|
+
* success: false,
|
|
67
|
+
* message: err.message,
|
|
68
|
+
* });
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
static loadResponseStatusErrorMiddleware(this: void, app: e.Express, fn: (err: ResponseStatusError, request: e.Request, response: e.Response, next: e.NextFunction) => void): void;
|
|
73
|
+
/**
|
|
74
|
+
* Serialize a value into a JSON string.
|
|
75
|
+
*
|
|
76
|
+
* This function is used in {@link ResponseEntity} to serialize the `body`.
|
|
77
|
+
*
|
|
78
|
+
* Use `setSerializeFn` to override underlying implementation.
|
|
79
|
+
*
|
|
80
|
+
* @defaultValue `JSON.stringify`
|
|
81
|
+
*/
|
|
82
|
+
static serialize(this: void, value: any): string;
|
|
83
|
+
/**
|
|
84
|
+
* Replace the function used for `serialize`.
|
|
85
|
+
*/
|
|
86
|
+
static setSerializeFn(this: void, fn: (value: any) => string): void;
|
|
87
|
+
/**
|
|
88
|
+
* De-serialize a JSON string back to a JavaScript object.
|
|
89
|
+
*
|
|
90
|
+
* This function is used to de-serialize a string into a `body`.
|
|
91
|
+
*
|
|
92
|
+
* Use `setDeserializeFn` to override underlying implementation.
|
|
93
|
+
*
|
|
94
|
+
* @defaultValue `JSON.parse`
|
|
95
|
+
*/
|
|
96
|
+
static deserialize<T = any>(this: void, value: string): T;
|
|
97
|
+
/**
|
|
98
|
+
* Replace the function used for `deserialize`
|
|
99
|
+
*/
|
|
100
|
+
static setDeserializeFn(this: void, fn: (value: string) => any): void;
|
|
101
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import e from "express";
|
|
2
|
+
import { _ControllerRegistry } from "../annotation/controller";
|
|
3
|
+
import { ResponseStatusError } from "./error";
|
|
4
|
+
const settings = {
|
|
5
|
+
serialize: JSON.stringify,
|
|
6
|
+
deserialize: JSON.parse,
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Collection of utility functions which are essential for Sapling to function.
|
|
10
|
+
*/
|
|
11
|
+
export class Sapling {
|
|
12
|
+
/**
|
|
13
|
+
* If you would prefer to manually resolve your controllers instead, call resolve
|
|
14
|
+
* on the controller class.
|
|
15
|
+
*
|
|
16
|
+
* @example```ts
|
|
17
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
18
|
+
* import TestController from "./path/to/test.controller";
|
|
19
|
+
*
|
|
20
|
+
* const app = express();
|
|
21
|
+
*
|
|
22
|
+
* const router = Sapling.resolve(TestController);
|
|
23
|
+
* app.use(router);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
static resolve(clazz) {
|
|
27
|
+
const router = _ControllerRegistry.get(clazz);
|
|
28
|
+
if (!router) {
|
|
29
|
+
throw new Error("Controller cannot be found");
|
|
30
|
+
}
|
|
31
|
+
return router;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Register this function as a middleware in order to utilize Sapling's `deserialize` function.
|
|
35
|
+
*
|
|
36
|
+
* @example```ts
|
|
37
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
38
|
+
* import express from "express";
|
|
39
|
+
*
|
|
40
|
+
* const app = express();
|
|
41
|
+
*
|
|
42
|
+
* app.use(Sapling.json());
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
static json() {
|
|
46
|
+
return (request, _response, next) => {
|
|
47
|
+
try {
|
|
48
|
+
if (!request.body) {
|
|
49
|
+
return next();
|
|
50
|
+
}
|
|
51
|
+
if (request.headers["content-type"] !== "application/json") {
|
|
52
|
+
return next();
|
|
53
|
+
}
|
|
54
|
+
if (typeof request.body === "string") {
|
|
55
|
+
request.body = Sapling.deserialize(request.body);
|
|
56
|
+
}
|
|
57
|
+
else if (typeof request.body === "object") {
|
|
58
|
+
// override `express.json()` middleware, if ran, and use Sapling's `deserialize` instead.
|
|
59
|
+
const raw = JSON.stringify(request.body);
|
|
60
|
+
request.body = Sapling.deserialize(raw);
|
|
61
|
+
}
|
|
62
|
+
next();
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
next(err);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Register your application with all the necessary middlewares and logics for Sapling to function.
|
|
71
|
+
*
|
|
72
|
+
* @example```ts
|
|
73
|
+
* import { Sapling } from "@tahminator/sapling";
|
|
74
|
+
* import express from "express";
|
|
75
|
+
*
|
|
76
|
+
* const app = express();
|
|
77
|
+
*
|
|
78
|
+
* app.registerApp(app);
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
static registerApp(app) {
|
|
82
|
+
app.use(e.text({ type: "application/json" }));
|
|
83
|
+
app.use(Sapling.json());
|
|
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
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Serialize a value into a JSON string.
|
|
119
|
+
*
|
|
120
|
+
* This function is used in {@link ResponseEntity} to serialize the `body`.
|
|
121
|
+
*
|
|
122
|
+
* Use `setSerializeFn` to override underlying implementation.
|
|
123
|
+
*
|
|
124
|
+
* @defaultValue `JSON.stringify`
|
|
125
|
+
*/
|
|
126
|
+
static serialize(value) {
|
|
127
|
+
return settings.serialize(value);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Replace the function used for `serialize`.
|
|
131
|
+
*/
|
|
132
|
+
static setSerializeFn(fn) {
|
|
133
|
+
settings.serialize = fn;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* De-serialize a JSON string back to a JavaScript object.
|
|
137
|
+
*
|
|
138
|
+
* This function is used to de-serialize a string into a `body`.
|
|
139
|
+
*
|
|
140
|
+
* Use `setDeserializeFn` to override underlying implementation.
|
|
141
|
+
*
|
|
142
|
+
* @defaultValue `JSON.parse`
|
|
143
|
+
*/
|
|
144
|
+
static deserialize(value) {
|
|
145
|
+
return settings.deserialize(value);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Replace the function used for `deserialize`
|
|
149
|
+
*/
|
|
150
|
+
static setDeserializeFn(fn) {
|
|
151
|
+
settings.deserialize = fn;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default Express.js 404 error page, as a string.
|
|
3
|
+
*/
|
|
4
|
+
export const Html404ErrorPage = (error) => `
|
|
5
|
+
<!DOCTYPE html>
|
|
6
|
+
<html lang="en">
|
|
7
|
+
<head>
|
|
8
|
+
<meta charset="utf-8">
|
|
9
|
+
<title>Error</title>
|
|
10
|
+
</head>
|
|
11
|
+
<body>
|
|
12
|
+
<pre>${error}</pre>
|
|
13
|
+
</body>
|
|
14
|
+
</html>
|
|
15
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./404";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./404";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { NextFunction, Request, Response } from "express";
|
|
2
|
+
export type ExpressRouterMethodKey = "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD" | "USE";
|
|
3
|
+
export type ExpressRouterMethods = Lowercase<ExpressRouterMethodKey>;
|
|
4
|
+
export declare const methodResolve: Record<ExpressRouterMethodKey, ExpressRouterMethods>;
|
|
5
|
+
export type RouteDefinition = {
|
|
6
|
+
/**
|
|
7
|
+
* Express.js HTTP method.
|
|
8
|
+
*/
|
|
9
|
+
method: ExpressRouterMethodKey;
|
|
10
|
+
/**
|
|
11
|
+
* The path to define the route on. Can be a string or RegExp.
|
|
12
|
+
*/
|
|
13
|
+
path: string | RegExp;
|
|
14
|
+
/**
|
|
15
|
+
* The name of the function the `@Route` annotation was applied on.
|
|
16
|
+
*/
|
|
17
|
+
fnName: string;
|
|
18
|
+
};
|
|
19
|
+
export type Class<T> = new (...args: any[]) => T;
|
|
20
|
+
export type HttpHeaders = Record<string, string>;
|
|
21
|
+
export type ExpressMiddlewareFn = ($1: Request, $2: Response, $3: NextFunction) => void;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
|
|
2
|
+
/// <reference types="vitest/config" />
|
|
3
|
+
import tsconfigPaths from "vite-tsconfig-paths";
|
|
4
|
+
import { defineConfig } from "vitest/config";
|
|
5
|
+
// https://vite.dev/config/
|
|
6
|
+
export default defineConfig({
|
|
7
|
+
plugins: [tsconfigPaths()],
|
|
8
|
+
test: {
|
|
9
|
+
globals: true,
|
|
10
|
+
environment: "jsdom",
|
|
11
|
+
coverage: {
|
|
12
|
+
enabled: true,
|
|
13
|
+
provider: "istanbul",
|
|
14
|
+
reporter: ["lcov"],
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tahminator/sapling",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.18",
|
|
4
4
|
"author": "Tahmid Ahmed",
|
|
5
5
|
"description": "A library to help you write cleaner Express.js code",
|
|
6
6
|
"repository": {
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"prettier": "NODE_OPTIONS=\"--experimental-strip-types\" prettier --check .",
|
|
19
19
|
"prettier:fix": "NODE_OPTIONS=\"--experimental-strip-types\" prettier --write .",
|
|
20
20
|
"typecheck": "tsc -b --noEmit",
|
|
21
|
-
"build": "rm -rf dist && tsc"
|
|
21
|
+
"build": "rm -rf dist && tsc --project tsconfig.app.json"
|
|
22
22
|
},
|
|
23
23
|
"main": "./dist/index.js",
|
|
24
24
|
"module": "./dist/index.js",
|