@cripty2001/utils 0.0.30 → 0.0.31
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 +18 -0
- package/dist/Appserver.js +69 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/package.json +4 -2
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { JSONEncodable } from '.';
|
|
2
|
+
export type AppserverHandler<I extends JSONEncodable, U extends JSONEncodable, O extends JSONEncodable> = (input: I, user: U | null) => Promise<O> | O;
|
|
3
|
+
declare class AppserverError extends Error {
|
|
4
|
+
code: string;
|
|
5
|
+
status: number;
|
|
6
|
+
constructor(code: string, message: string, status?: number);
|
|
7
|
+
}
|
|
8
|
+
export declare class AppserverHandledError extends AppserverError {
|
|
9
|
+
constructor(code: string, message: string);
|
|
10
|
+
}
|
|
11
|
+
export declare class Appserver<U extends JSONEncodable> {
|
|
12
|
+
private app;
|
|
13
|
+
private parseUser;
|
|
14
|
+
constructor(port: number, parseUser: Appserver<U>['parseUser']);
|
|
15
|
+
private parseInput;
|
|
16
|
+
register<I extends JSONEncodable, O extends JSONEncodable>(action: string, handler: AppserverHandler<I, U, O>): void;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Appserver = exports.AppserverHandledError = void 0;
|
|
7
|
+
const express_1 = __importDefault(require("express"));
|
|
8
|
+
class AppserverError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
status;
|
|
11
|
+
constructor(code, message, status = 500) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.status = status;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
class AppserverHandledError extends AppserverError {
|
|
18
|
+
constructor(code, message) {
|
|
19
|
+
super(code, message);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.AppserverHandledError = AppserverHandledError;
|
|
23
|
+
class Appserver {
|
|
24
|
+
app;
|
|
25
|
+
parseUser;
|
|
26
|
+
constructor(port, parseUser) {
|
|
27
|
+
this.parseUser = parseUser;
|
|
28
|
+
this.app = (0, express_1.default)();
|
|
29
|
+
this.app.listen(port);
|
|
30
|
+
}
|
|
31
|
+
async parseInput(req) {
|
|
32
|
+
if (req.headers['content-type'] !== 'application/json')
|
|
33
|
+
throw new AppserverError('REQUEST_INVALID_TYPE_HEADER', 'Content-Type must be application/json', 400);
|
|
34
|
+
const data = (() => {
|
|
35
|
+
try {
|
|
36
|
+
return JSON.parse(req.body);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
throw new AppserverError('REQUEST_INVALID_BODY', 'Request body is not valid JSON', 400);
|
|
40
|
+
}
|
|
41
|
+
})();
|
|
42
|
+
const authHeader = req.headers['authorization'];
|
|
43
|
+
const token = authHeader?.startsWith('Bearer ') ? authHeader.slice(7) : null;
|
|
44
|
+
return {
|
|
45
|
+
data,
|
|
46
|
+
user: token ? await this.parseUser(token) : null
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
register(action, handler) {
|
|
50
|
+
this.app.post(`/exec/${action}`, async (req, res) => {
|
|
51
|
+
try {
|
|
52
|
+
const { data, user } = await this.parseInput(req);
|
|
53
|
+
const output = await handler(data, user);
|
|
54
|
+
return res
|
|
55
|
+
.json(output);
|
|
56
|
+
}
|
|
57
|
+
catch (e) {
|
|
58
|
+
if (e instanceof AppserverError)
|
|
59
|
+
return res
|
|
60
|
+
.status(e.status)
|
|
61
|
+
.json({ error: e.message, code: e.code });
|
|
62
|
+
return res
|
|
63
|
+
.status(500)
|
|
64
|
+
.json({ error: 'Internal server error', code: 'INTERNAL_SERVERERROR' });
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.Appserver = Appserver;
|
package/dist/index.d.ts
CHANGED
|
@@ -29,5 +29,4 @@ export declare function parseQuery(query: string | Record<string, any> | URLSear
|
|
|
29
29
|
left: URLSearchParams;
|
|
30
30
|
};
|
|
31
31
|
export declare function randBetween(min: number, max: number): number;
|
|
32
|
-
import "dotenv/config";
|
|
33
32
|
export declare function getEnv(key: string, defaultValue?: string): string;
|
package/dist/index.js
CHANGED
|
@@ -135,7 +135,6 @@ function parseQuery(query, fields) {
|
|
|
135
135
|
function randBetween(min, max) {
|
|
136
136
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
137
137
|
}
|
|
138
|
-
require("dotenv/config");
|
|
139
138
|
function getEnv(key, defaultValue) {
|
|
140
139
|
const value = process.env[key] ?? defaultValue;
|
|
141
140
|
if (value === undefined)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.31",
|
|
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": {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"dist"
|
|
23
23
|
],
|
|
24
24
|
"devDependencies": {
|
|
25
|
+
"@types/express": "^5.0.5",
|
|
25
26
|
"@types/node": "^24.9.1",
|
|
26
27
|
"@types/react": "^19",
|
|
27
28
|
"react": "^19",
|
|
@@ -30,10 +31,11 @@
|
|
|
30
31
|
"dependencies": {
|
|
31
32
|
"@cripty2001/whispr": "^0.1.0",
|
|
32
33
|
"@types/lodash": "^4.17.20",
|
|
33
|
-
"dotenv": "^17.2.3",
|
|
34
34
|
"lodash": "^4.17.21"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
+
"dotenv": "^17.2.3",
|
|
38
|
+
"express": "^5.1.0",
|
|
37
39
|
"react": "^19"
|
|
38
40
|
},
|
|
39
41
|
"peerDependenciesMeta": {
|