@cripty2001/utils 0.0.41 → 0.0.42
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/package.json +7 -3
- package/dist/Appserver.d.ts +0 -20
- package/dist/Appserver.js +0 -77
- package/dist/Logger.d.ts +0 -22
- package/dist/Logger.js +0 -53
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cripty2001/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.42",
|
|
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": {
|
|
@@ -60,8 +60,12 @@
|
|
|
60
60
|
"types": "./dist/react-whispr.d.ts"
|
|
61
61
|
},
|
|
62
62
|
"./logger": {
|
|
63
|
-
"import": "./dist/Logger.js",
|
|
64
|
-
"types": "./dist/Logger.d.ts"
|
|
63
|
+
"import": "./dist/Logger/Logger.js",
|
|
64
|
+
"types": "./dist/Logger/Logger.d.ts"
|
|
65
|
+
},
|
|
66
|
+
"./logger/react": {
|
|
67
|
+
"import": "./dist/Logger/react-display.js",
|
|
68
|
+
"types": "./dist/Logger/react-display.d.ts"
|
|
65
69
|
},
|
|
66
70
|
"./appserver/server": {
|
|
67
71
|
"import": "./dist/Appserver/server.js",
|
package/dist/Appserver.d.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { JSONEncodable } from '.';
|
|
2
|
-
import { Static, TSchema } from '@sinclair/typebox';
|
|
3
|
-
export type AppserverHandler<I extends JSONEncodable, U extends JSONEncodable, O extends JSONEncodable> = (input: I, user: U | null) => Promise<O> | O;
|
|
4
|
-
export type AppserverUsergetter<U extends JSONEncodable> = (token: string) => Promise<U | null>;
|
|
5
|
-
declare class AppserverError extends Error {
|
|
6
|
-
code: string;
|
|
7
|
-
status: number;
|
|
8
|
-
constructor(code: string, message: string, payload?: JSONEncodable, status?: number);
|
|
9
|
-
}
|
|
10
|
-
export declare class AppserverHandledError extends AppserverError {
|
|
11
|
-
constructor(code: string, message: string, payload?: JSONEncodable);
|
|
12
|
-
}
|
|
13
|
-
export declare class Appserver<U extends JSONEncodable> {
|
|
14
|
-
private app;
|
|
15
|
-
private parseUser;
|
|
16
|
-
constructor(port: number, parseUser: AppserverUsergetter<U>);
|
|
17
|
-
private parseInput;
|
|
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;
|
|
19
|
-
}
|
|
20
|
-
export {};
|
package/dist/Appserver.js
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
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
|
-
const value_1 = require("@sinclair/typebox/value");
|
|
9
|
-
class AppserverError extends Error {
|
|
10
|
-
code;
|
|
11
|
-
status;
|
|
12
|
-
constructor(code, message, payload = {}, status = 500) {
|
|
13
|
-
super(message);
|
|
14
|
-
this.code = code;
|
|
15
|
-
this.status = status;
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
class AppserverHandledError extends AppserverError {
|
|
19
|
-
constructor(code, message, payload = {}) {
|
|
20
|
-
super(code, message, payload);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.AppserverHandledError = AppserverHandledError;
|
|
24
|
-
class Appserver {
|
|
25
|
-
app;
|
|
26
|
-
parseUser;
|
|
27
|
-
constructor(port, parseUser) {
|
|
28
|
-
this.parseUser = parseUser;
|
|
29
|
-
this.app = (0, express_1.default)();
|
|
30
|
-
this.app.listen(port);
|
|
31
|
-
}
|
|
32
|
-
async parseInput(req) {
|
|
33
|
-
if (req.headers['content-type'] !== 'application/json')
|
|
34
|
-
throw new AppserverError('REQUEST_INVALID_TYPE_HEADER', 'Content-Type must be application/json', 400);
|
|
35
|
-
const data = (() => {
|
|
36
|
-
try {
|
|
37
|
-
return JSON.parse(req.body);
|
|
38
|
-
}
|
|
39
|
-
catch {
|
|
40
|
-
throw new AppserverError('REQUEST_INVALID_BODY', 'Request body is not valid JSON', 400);
|
|
41
|
-
}
|
|
42
|
-
})();
|
|
43
|
-
const authHeader = req.headers['authorization'];
|
|
44
|
-
const token = authHeader?.startsWith('Bearer ') ? authHeader.slice(7) : null;
|
|
45
|
-
return {
|
|
46
|
-
data,
|
|
47
|
-
user: token ? await this.parseUser(token) : null
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
register(action, inputSchema, handler) {
|
|
51
|
-
this.app.post(`/exec/${action}`, async (req, res) => {
|
|
52
|
-
try {
|
|
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;
|
|
61
|
-
const output = await handler(data, user);
|
|
62
|
-
return res
|
|
63
|
-
.json(output);
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
if (e instanceof AppserverError)
|
|
67
|
-
return res
|
|
68
|
-
.status(e.status)
|
|
69
|
-
.json({ error: e.message, code: e.code });
|
|
70
|
-
return res
|
|
71
|
-
.status(500)
|
|
72
|
-
.json({ error: 'Internal server error', code: 'INTERNAL_SERVERERROR' });
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
exports.Appserver = Appserver;
|
package/dist/Logger.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { Whispr } from "@cripty2001/whispr";
|
|
2
|
-
type LoggerItem = {
|
|
3
|
-
id: string;
|
|
4
|
-
date: Date;
|
|
5
|
-
message: string;
|
|
6
|
-
severity: "success" | "info" | "warning" | "error";
|
|
7
|
-
context: string | undefined;
|
|
8
|
-
dismiss: () => void;
|
|
9
|
-
trace: string | undefined;
|
|
10
|
-
};
|
|
11
|
-
declare class Logger {
|
|
12
|
-
private static instance;
|
|
13
|
-
lines: Whispr<LoggerItem[]>;
|
|
14
|
-
private setLines;
|
|
15
|
-
private constructor();
|
|
16
|
-
private reconstructError;
|
|
17
|
-
static getInstance(): Logger;
|
|
18
|
-
log(message: string, severity?: LoggerItem['severity'], context?: string): void;
|
|
19
|
-
logError(error: Error | DOMException | any): void;
|
|
20
|
-
}
|
|
21
|
-
declare const _default: Logger;
|
|
22
|
-
export default _default;
|
package/dist/Logger.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const whispr_1 = require("@cripty2001/whispr");
|
|
4
|
-
const _1 = require(".");
|
|
5
|
-
class Logger {
|
|
6
|
-
static instance;
|
|
7
|
-
lines;
|
|
8
|
-
setLines;
|
|
9
|
-
constructor() {
|
|
10
|
-
[this.lines, this.setLines] = whispr_1.Whispr.create([]);
|
|
11
|
-
window.addEventListener("unhandledrejection", (e) => this.log(e.reason, "error"));
|
|
12
|
-
window.addEventListener("error", (e) => this.logError(this.reconstructError(e)));
|
|
13
|
-
}
|
|
14
|
-
reconstructError(e) {
|
|
15
|
-
if (e.error && e.error instanceof Error) {
|
|
16
|
-
return e.error;
|
|
17
|
-
}
|
|
18
|
-
if (e.message && typeof e.message === 'string') {
|
|
19
|
-
return new Error(e.message);
|
|
20
|
-
}
|
|
21
|
-
return new Error("Unknown error");
|
|
22
|
-
}
|
|
23
|
-
static getInstance() {
|
|
24
|
-
if (!Logger.instance) {
|
|
25
|
-
Logger.instance = new Logger();
|
|
26
|
-
}
|
|
27
|
-
return Logger.instance;
|
|
28
|
-
}
|
|
29
|
-
log(message, severity = "info", context) {
|
|
30
|
-
const item = {
|
|
31
|
-
id: (0, _1.getRandomId)(),
|
|
32
|
-
date: new Date(),
|
|
33
|
-
message,
|
|
34
|
-
severity,
|
|
35
|
-
context,
|
|
36
|
-
dismiss: () => {
|
|
37
|
-
this.setLines(this.lines.value.filter(i => i.id !== item.id));
|
|
38
|
-
},
|
|
39
|
-
trace: (new Error()).stack
|
|
40
|
-
};
|
|
41
|
-
this.setLines([...this.lines.value, item]);
|
|
42
|
-
}
|
|
43
|
-
logError(error) {
|
|
44
|
-
if (error instanceof DOMException ||
|
|
45
|
-
error instanceof Error ||
|
|
46
|
-
false) {
|
|
47
|
-
return this.log(error.message, "error", error.stack);
|
|
48
|
-
}
|
|
49
|
-
console.trace('UNKNOWN ERROR', error);
|
|
50
|
-
return this.log("Unknown error - See Console", "error");
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
exports.default = Logger.getInstance();
|