@cripty2001/utils 0.0.65 → 0.0.67

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.
@@ -1,4 +1,5 @@
1
- import { Whispr } from "@cripty2001/whispr";
1
+ import { WhisprSetter } from "@cripty2001/whispr";
2
+ import { Dispatcher } from "../Dispatcher";
2
3
  import { AppserverData } from "./common";
3
4
  export type { AppserverData };
4
5
  export declare class ClientError extends Error {
@@ -16,9 +17,10 @@ export declare class ClientValidationError extends ClientError {
16
17
  export declare class Client {
17
18
  private url;
18
19
  private authToken;
19
- loggedIn: Whispr<boolean>;
20
+ setAuthToken: WhisprSetter<string | null>;
21
+ loggedIn: Dispatcher<string | null, boolean>;
20
22
  private constructor();
21
23
  static create(url: string): Client;
22
- setAuthToken(token: string | null): void;
23
24
  exec<I extends AppserverData, O extends AppserverData>(action: string, input: I): Promise<O>;
25
+ private unsafeExec;
24
26
  }
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Client = exports.ClientValidationError = exports.ClientServerError = exports.ClientError = void 0;
4
4
  const whispr_1 = require("@cripty2001/whispr");
5
5
  const msgpack_1 = require("@msgpack/msgpack");
6
+ const Dispatcher_1 = require("../Dispatcher");
6
7
  class ClientError extends Error {
7
8
  constructor(message) {
8
9
  super(message);
@@ -30,20 +31,26 @@ exports.ClientValidationError = ClientValidationError;
30
31
  class Client {
31
32
  url;
32
33
  authToken;
34
+ setAuthToken;
33
35
  loggedIn;
34
36
  constructor(url) {
35
37
  this.url = url;
36
38
  [this.authToken, this.setAuthToken] = whispr_1.Whispr.create(null);
37
- this.loggedIn = whispr_1.Whispr.from({ authToken: this.authToken }, ({ authToken }) => authToken !== null);
39
+ this.loggedIn = new Dispatcher_1.Dispatcher(this.authToken, async (token) => {
40
+ if (token === null)
41
+ return false;
42
+ const { user } = await this.unsafeExec('auth/whoami', {});
43
+ return user !== null;
44
+ }, 200);
38
45
  }
39
46
  static create(url) {
40
47
  return new Client(url);
41
48
  }
42
- setAuthToken(token) {
43
- this.setAuthToken(token ?? null);
44
- }
45
49
  async exec(action, input) {
46
- const res = await fetch(`${this.url}/exec/${action}`, {
50
+ return this.unsafeExec(`/exec/${action}`, input);
51
+ }
52
+ async unsafeExec(action, input) {
53
+ const res = await fetch(`${this.url}${action}`, {
47
54
  method: "POST",
48
55
  headers: {
49
56
  "Content-Type": "application/vnd.msgpack",
@@ -69,9 +69,10 @@ class Appserver {
69
69
  }
70
70
  registerAuth(app) {
71
71
  this.unsafeRegister('/auth/whoami', typebox_1.Type.Object({}), false, async (input, user) => {
72
+ const mappedUser = Object.fromEntries(Object.entries(user ?? {})
73
+ .filter(([key]) => !key.startsWith('_')));
72
74
  return {
73
- user: Object.fromEntries(Object.entries(user ?? {})
74
- .filter(([key]) => !key.startsWith('_')))
75
+ user: user !== null ? mappedUser : null
75
76
  };
76
77
  });
77
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cripty2001/utils",
3
- "version": "0.0.65",
3
+ "version": "0.0.67",
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": {