@cripty2001/utils 0.0.92 → 0.0.94
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.
|
@@ -4,6 +4,9 @@ export type { AppserverData };
|
|
|
4
4
|
export declare class ClientError extends Error {
|
|
5
5
|
constructor(message: string);
|
|
6
6
|
}
|
|
7
|
+
export declare class ClientAuthError extends ClientError {
|
|
8
|
+
constructor();
|
|
9
|
+
}
|
|
7
10
|
export declare class ClientServerError extends Error {
|
|
8
11
|
code: string;
|
|
9
12
|
payload: AppserverData;
|
|
@@ -22,5 +25,11 @@ export declare class Client {
|
|
|
22
25
|
static create(url: string): Client;
|
|
23
26
|
login(token: string): Promise<boolean>;
|
|
24
27
|
exec<I extends AppserverData, O extends AppserverData>(action: string, input: I): Promise<O>;
|
|
28
|
+
/**
|
|
29
|
+
* Executes an action on the server without invalidating the auth token on auth errors.
|
|
30
|
+
* @param action - The action to execute
|
|
31
|
+
* @param input - The input to the action
|
|
32
|
+
* @returns The result of the action
|
|
33
|
+
*/
|
|
25
34
|
private unsafeExec;
|
|
26
35
|
}
|
package/dist/Appserver/client.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Client = exports.ClientValidationError = exports.ClientServerError = exports.ClientError = void 0;
|
|
3
|
+
exports.Client = exports.ClientValidationError = exports.ClientServerError = exports.ClientAuthError = exports.ClientError = void 0;
|
|
4
4
|
const whispr_1 = require("@cripty2001/whispr");
|
|
5
5
|
const msgpack_1 = require("@msgpack/msgpack");
|
|
6
6
|
const Dispatcher_1 = require("../Dispatcher");
|
|
@@ -10,6 +10,12 @@ class ClientError extends Error {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.ClientError = ClientError;
|
|
13
|
+
class ClientAuthError extends ClientError {
|
|
14
|
+
constructor() {
|
|
15
|
+
super("Authentication error");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
exports.ClientAuthError = ClientAuthError;
|
|
13
19
|
class ClientServerError extends Error {
|
|
14
20
|
code;
|
|
15
21
|
payload;
|
|
@@ -59,12 +65,25 @@ class Client {
|
|
|
59
65
|
return await this.user.data.wait(data => {
|
|
60
66
|
if (data.loading)
|
|
61
67
|
return;
|
|
68
|
+
console.log('Login result in waiter: ', data);
|
|
62
69
|
return data.ok;
|
|
63
70
|
});
|
|
64
71
|
}
|
|
65
72
|
async exec(action, input) {
|
|
66
|
-
return this.unsafeExec(`/exec/${action}`, input)
|
|
73
|
+
return await this.unsafeExec(`/exec/${action}`, input)
|
|
74
|
+
.catch(e => {
|
|
75
|
+
if (e instanceof ClientAuthError) {
|
|
76
|
+
this.setAuthToken(null);
|
|
77
|
+
}
|
|
78
|
+
throw e;
|
|
79
|
+
});
|
|
67
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Executes an action on the server without invalidating the auth token on auth errors.
|
|
83
|
+
* @param action - The action to execute
|
|
84
|
+
* @param input - The input to the action
|
|
85
|
+
* @returns The result of the action
|
|
86
|
+
*/
|
|
68
87
|
async unsafeExec(action, input) {
|
|
69
88
|
const testedToken = this.authToken.value;
|
|
70
89
|
const res = await fetch(`${this.url}${action}`, {
|
|
@@ -75,12 +94,8 @@ class Client {
|
|
|
75
94
|
},
|
|
76
95
|
body: new Blob([new Uint8Array((0, msgpack_1.encode)(input))], { type: 'application/msgpack' }),
|
|
77
96
|
});
|
|
78
|
-
if (res.status === 401 || res.status === 403)
|
|
79
|
-
if (testedToken === this.authToken.value) {
|
|
80
|
-
this.setAuthToken(null);
|
|
81
|
-
}
|
|
97
|
+
if (res.status === 401 || res.status === 403)
|
|
82
98
|
throw new ClientError("Permission denied");
|
|
83
|
-
}
|
|
84
99
|
if (res.status === 404)
|
|
85
100
|
throw new ClientError("Not found");
|
|
86
101
|
const decoded = (0, msgpack_1.decode)(await res.arrayBuffer());
|
package/package.json
CHANGED