@cripty2001/utils 0.0.93 → 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
  }
@@ -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;
@@ -64,8 +70,20 @@ class Client {
64
70
  });
65
71
  }
66
72
  async exec(action, input) {
67
- 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
+ });
68
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
+ */
69
87
  async unsafeExec(action, input) {
70
88
  const testedToken = this.authToken.value;
71
89
  const res = await fetch(`${this.url}${action}`, {
@@ -76,12 +94,8 @@ class Client {
76
94
  },
77
95
  body: new Blob([new Uint8Array((0, msgpack_1.encode)(input))], { type: 'application/msgpack' }),
78
96
  });
79
- if (res.status === 401 || res.status === 403) {
80
- if (testedToken === this.authToken.value) {
81
- this.setAuthToken(null);
82
- }
97
+ if (res.status === 401 || res.status === 403)
83
98
  throw new ClientError("Permission denied");
84
- }
85
99
  if (res.status === 404)
86
100
  throw new ClientError("Not found");
87
101
  const decoded = (0, msgpack_1.decode)(await res.arrayBuffer());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.93",
3
+ "version": "0.0.94",
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": {