@cripty2001/utils 0.0.33 → 0.0.35
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/Appserver.d.ts +4 -3
- package/dist/Appserver.js +13 -5
- package/package.json +7 -2
package/dist/Appserver.d.ts
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
import { JSONEncodable } from '.';
|
|
2
|
+
import { Static, TSchema } from '@sinclair/typebox';
|
|
2
3
|
export type AppserverHandler<I extends JSONEncodable, U extends JSONEncodable, O extends JSONEncodable> = (input: I, user: U | null) => Promise<O> | O;
|
|
3
4
|
export type AppserverUsergetter<U extends JSONEncodable> = (token: string) => Promise<U | null>;
|
|
4
5
|
declare class AppserverError extends Error {
|
|
5
6
|
code: string;
|
|
6
7
|
status: number;
|
|
7
|
-
constructor(code: string, message: string, status?: number);
|
|
8
|
+
constructor(code: string, message: string, payload?: JSONEncodable, status?: number);
|
|
8
9
|
}
|
|
9
10
|
export declare class AppserverHandledError extends AppserverError {
|
|
10
|
-
constructor(code: string, message: string);
|
|
11
|
+
constructor(code: string, message: string, payload?: JSONEncodable);
|
|
11
12
|
}
|
|
12
13
|
export declare class Appserver<U extends JSONEncodable> {
|
|
13
14
|
private app;
|
|
14
15
|
private parseUser;
|
|
15
16
|
constructor(port: number, parseUser: AppserverUsergetter<U>);
|
|
16
17
|
private parseInput;
|
|
17
|
-
register<
|
|
18
|
+
register<ISchema extends TSchema, O extends JSONEncodable, I extends Static<ISchema> & JSONEncodable = Static<ISchema> & JSONEncodable>(action: string, inputSchema: ISchema, handler: AppserverHandler<I, U, O>): void;
|
|
18
19
|
}
|
|
19
20
|
export {};
|
package/dist/Appserver.js
CHANGED
|
@@ -5,18 +5,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Appserver = exports.AppserverHandledError = void 0;
|
|
7
7
|
const express_1 = __importDefault(require("express"));
|
|
8
|
+
const value_1 = require("@sinclair/typebox/value");
|
|
8
9
|
class AppserverError extends Error {
|
|
9
10
|
code;
|
|
10
11
|
status;
|
|
11
|
-
constructor(code, message, status = 500) {
|
|
12
|
+
constructor(code, message, payload = {}, status = 500) {
|
|
12
13
|
super(message);
|
|
13
14
|
this.code = code;
|
|
14
15
|
this.status = status;
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
18
|
class AppserverHandledError extends AppserverError {
|
|
18
|
-
constructor(code, message) {
|
|
19
|
-
super(code, message);
|
|
19
|
+
constructor(code, message, payload = {}) {
|
|
20
|
+
super(code, message, payload);
|
|
20
21
|
}
|
|
21
22
|
}
|
|
22
23
|
exports.AppserverHandledError = AppserverHandledError;
|
|
@@ -46,10 +47,17 @@ class Appserver {
|
|
|
46
47
|
user: token ? await this.parseUser(token) : null
|
|
47
48
|
};
|
|
48
49
|
}
|
|
49
|
-
register(action, handler) {
|
|
50
|
+
register(action, inputSchema, handler) {
|
|
50
51
|
this.app.post(`/exec/${action}`, async (req, res) => {
|
|
51
52
|
try {
|
|
52
|
-
const { data, user } = await this.parseInput(req);
|
|
53
|
+
const { data: unsafeData, user } = await this.parseInput(req);
|
|
54
|
+
if (!value_1.Value.Check(inputSchema, unsafeData))
|
|
55
|
+
return res
|
|
56
|
+
.status(422)
|
|
57
|
+
.json({
|
|
58
|
+
errors: [...value_1.Value.Errors(inputSchema, unsafeData)]
|
|
59
|
+
});
|
|
60
|
+
const data = unsafeData;
|
|
53
61
|
const output = await handler(data, user);
|
|
54
62
|
return res
|
|
55
63
|
.json(output);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.35",
|
|
4
4
|
"description": "Internal Set of utils. If you need them use them, otherwise go to the next package ;)",
|
|
5
5
|
"homepage": "https://github.com/cripty2001/utils#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -36,7 +36,8 @@
|
|
|
36
36
|
"peerDependencies": {
|
|
37
37
|
"dotenv": "^17.2.3",
|
|
38
38
|
"express": "^5.1.0",
|
|
39
|
-
"react": "^19"
|
|
39
|
+
"react": "^19",
|
|
40
|
+
"@sinclair/typebox": "^0.34.41"
|
|
40
41
|
},
|
|
41
42
|
"peerDependenciesMeta": {
|
|
42
43
|
"react": {
|
|
@@ -63,6 +64,10 @@
|
|
|
63
64
|
"./appserver": {
|
|
64
65
|
"import": "./dist/Appserver.js",
|
|
65
66
|
"types": "./dist/Appserver.d.ts"
|
|
67
|
+
},
|
|
68
|
+
"./appserver/server": {
|
|
69
|
+
"import": "./dist/Appserver.js",
|
|
70
|
+
"types": "./dist/Appserver.d.ts"
|
|
66
71
|
}
|
|
67
72
|
}
|
|
68
73
|
}
|