@cripty2001/utils 0.0.34 → 0.0.36

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.
@@ -0,0 +1,18 @@
1
+ import { AppserverData } from "./common";
2
+ export declare class ClientError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ export declare class ClientServerError extends Error {
6
+ code: string;
7
+ payload: AppserverData;
8
+ constructor(code: string, message: string, payload?: AppserverData);
9
+ }
10
+ export declare class ClientValidationError extends ClientError {
11
+ errors: any[];
12
+ constructor(errors: any[]);
13
+ }
14
+ export declare class Client {
15
+ private url;
16
+ constructor(url: string);
17
+ exec<I extends AppserverData, O extends AppserverData>(action: string, input: I): Promise<O>;
18
+ }
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Client = exports.ClientValidationError = exports.ClientServerError = exports.ClientError = void 0;
4
+ const msgpack_1 = require("@msgpack/msgpack");
5
+ class ClientError extends Error {
6
+ constructor(message) {
7
+ super(message);
8
+ }
9
+ }
10
+ exports.ClientError = ClientError;
11
+ class ClientServerError extends Error {
12
+ code;
13
+ payload;
14
+ constructor(code, message, payload = {}) {
15
+ super(message);
16
+ this.code = code;
17
+ this.payload = payload;
18
+ }
19
+ }
20
+ exports.ClientServerError = ClientServerError;
21
+ class ClientValidationError extends ClientError {
22
+ errors;
23
+ constructor(errors) {
24
+ super("Validation Error");
25
+ this.errors = errors;
26
+ }
27
+ }
28
+ exports.ClientValidationError = ClientValidationError;
29
+ class Client {
30
+ url;
31
+ constructor(url) {
32
+ this.url = url;
33
+ }
34
+ async exec(action, input) {
35
+ const res = await fetch(`${this.url}/exec/${action}`, {
36
+ method: "POST",
37
+ headers: {
38
+ "Content-Type": "application/vnd.msgpack",
39
+ },
40
+ body: new Blob([new Uint8Array((0, msgpack_1.encode)(input))], { type: 'application/msgpack' }),
41
+ });
42
+ const decoded = (0, msgpack_1.decode)(await res.arrayBuffer());
43
+ let responseData;
44
+ switch (res.status) {
45
+ case 200:
46
+ responseData = decoded;
47
+ return responseData;
48
+ case 422:
49
+ responseData = decoded;
50
+ throw new ClientValidationError(responseData.errors);
51
+ case 400:
52
+ case 500:
53
+ responseData = decoded;
54
+ throw new ClientServerError(responseData.code, responseData.error, responseData.payload);
55
+ default:
56
+ throw new ClientError(`Unexpected server response: ${res.status}`);
57
+ }
58
+ }
59
+ }
60
+ exports.Client = Client;
@@ -0,0 +1,3 @@
1
+ export type AppserverData = null | boolean | number | string | Uint8Array | AppserverData[] | {
2
+ [key: string]: AppserverData;
3
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,21 @@
1
+ import { Static, TSchema } from '@sinclair/typebox';
2
+ import { AppserverData } from './common';
3
+ export type AppserverHandler<I extends AppserverData, U extends AppserverData, O extends AppserverData> = (input: I, user: U | null) => Promise<O> | O;
4
+ export type AppserverUsergetter<U extends AppserverData> = (token: string) => Promise<U | null>;
5
+ declare class AppserverError extends Error {
6
+ code: string;
7
+ payload: AppserverData;
8
+ status: number;
9
+ constructor(code: string, message: string, payload?: AppserverData, status?: number);
10
+ }
11
+ export declare class AppserverHandledError extends AppserverError {
12
+ constructor(code: string, message: string, payload?: AppserverData);
13
+ }
14
+ export declare class Appserver<U extends AppserverData> {
15
+ private app;
16
+ private parseUser;
17
+ constructor(port: number, parseUser: AppserverUsergetter<U>);
18
+ private parseInput;
19
+ register<ISchema extends TSchema, O extends AppserverData, I extends Static<ISchema> & AppserverData = Static<ISchema> & AppserverData>(action: string, inputSchema: ISchema, handler: AppserverHandler<I, U, O>): void;
20
+ }
21
+ export {};
@@ -0,0 +1,89 @@
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
+ const msgpack_1 = require("@msgpack/msgpack");
10
+ (0, msgpack_1.encode)({}); // Fixes issue with msgpack not being included in build
11
+ class AppserverError extends Error {
12
+ code;
13
+ payload;
14
+ status;
15
+ constructor(code, message, payload = {}, status = 500) {
16
+ super(message);
17
+ this.code = code;
18
+ this.payload = payload;
19
+ this.status = status;
20
+ }
21
+ }
22
+ class AppserverHandledError extends AppserverError {
23
+ constructor(code, message, payload = {}) {
24
+ super(code, message, payload);
25
+ }
26
+ }
27
+ exports.AppserverHandledError = AppserverHandledError;
28
+ class Appserver {
29
+ app;
30
+ parseUser;
31
+ constructor(port, parseUser) {
32
+ this.parseUser = parseUser;
33
+ this.app = (0, express_1.default)();
34
+ this.app.listen(port);
35
+ }
36
+ async parseInput(req) {
37
+ if (req.headers['content-type'] !== 'application/vnd.msgpack')
38
+ throw new AppserverError('REQUEST_INVALID_TYPE_HEADER', 'Content-Type must be a messagepack (application/vnd.msgpack)', 400);
39
+ const data = (() => {
40
+ try {
41
+ return JSON.parse(req.body);
42
+ }
43
+ catch {
44
+ throw new AppserverError('REQUEST_INVALID_BODY', 'Request body is not valid JSON', 400);
45
+ }
46
+ })();
47
+ const authHeader = req.headers['authorization'];
48
+ const token = authHeader?.startsWith('Bearer ') ? authHeader.slice(7) : null;
49
+ return {
50
+ data,
51
+ user: token ? await this.parseUser(token) : null
52
+ };
53
+ }
54
+ register(action, inputSchema, handler) {
55
+ this.app.post(`/exec/${action}`, async (req, res) => {
56
+ const { status, data } = await (async () => {
57
+ try {
58
+ const { data: unsafeData, user } = await this.parseInput(req);
59
+ if (!value_1.Value.Check(inputSchema, unsafeData))
60
+ return {
61
+ status: 422,
62
+ data: {
63
+ errors: [...value_1.Value.Errors(inputSchema, unsafeData)]
64
+ }
65
+ };
66
+ return {
67
+ status: 200,
68
+ data: await handler(unsafeData, user)
69
+ };
70
+ }
71
+ catch (e) {
72
+ if (e instanceof AppserverError)
73
+ return {
74
+ status: e.status,
75
+ data: { error: e.message, code: e.code, payload: e.payload }
76
+ };
77
+ return {
78
+ status: 500,
79
+ data: { error: 'Internal server error', code: 'INTERNAL_SERVERERROR' }
80
+ };
81
+ }
82
+ })();
83
+ res
84
+ .status(status)
85
+ .send((0, msgpack_1.encode)(data));
86
+ });
87
+ }
88
+ }
89
+ exports.Appserver = Appserver;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.34",
3
+ "version": "0.0.36",
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": {
@@ -34,10 +34,11 @@
34
34
  "lodash": "^4.17.21"
35
35
  },
36
36
  "peerDependencies": {
37
+ "@sinclair/typebox": "^0.34.41",
37
38
  "dotenv": "^17.2.3",
38
39
  "express": "^5.1.0",
39
40
  "react": "^19",
40
- "@sinclair/typebox": "^0.34.41"
41
+ "@msgpack/msgpack": "^3.1.2"
41
42
  },
42
43
  "peerDependenciesMeta": {
43
44
  "react": {
@@ -61,9 +62,13 @@
61
62
  "import": "./dist/Logger.js",
62
63
  "types": "./dist/Logger.d.ts"
63
64
  },
64
- "./appserver": {
65
- "import": "./dist/Appserver.js",
66
- "types": "./dist/Appserver.d.ts"
65
+ "./appserver/server": {
66
+ "import": "./dist/Appserver/server.js",
67
+ "types": "./dist/Appserver/server.d.ts"
68
+ },
69
+ "./appserver/client": {
70
+ "import": "./dist/Appserver/client.js",
71
+ "types": "./dist/Appserver/client.d.ts"
67
72
  }
68
73
  }
69
74
  }