lixnet 1.0.3 → 1.1.0

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,14 +1,11 @@
1
- import type { EventCollection, CallableEvent } from "./types";
1
+ import type { EventCollection, ClientCaller } from "./types";
2
2
  export default class LixnetClient<TEvents extends EventCollection> {
3
- private baseUrl;
3
+ private rpcUrl;
4
4
  private events;
5
- constructor({ events, baseUrl, }: {
5
+ constructor({ events, rpcUrl, }: {
6
6
  events: TEvents;
7
- baseUrl: string;
7
+ rpcUrl: string;
8
8
  });
9
- getClient(): {
10
- [K in keyof TEvents]: TEvents[K] extends {
11
- handler: (input: infer TInput) => Promise<infer TOutput>;
12
- } ? CallableEvent<Omit<TInput, "request">, TOutput> : never;
13
- };
9
+ call: ClientCaller<TEvents>;
10
+ getEventCollection(): TEvents;
14
11
  }
@@ -1,29 +1,24 @@
1
1
  export default class LixnetClient {
2
- constructor({ events, baseUrl, }) {
3
- this.baseUrl = baseUrl;
2
+ constructor({ events, rpcUrl, }) {
3
+ this.call = async (event, input) => {
4
+ const response = await fetch(this.rpcUrl, {
5
+ method: "POST",
6
+ headers: {
7
+ "Content-Type": "application/json",
8
+ },
9
+ body: JSON.stringify({ event, input }),
10
+ });
11
+ const json = await response.json();
12
+ if (json.error) {
13
+ throw new Error(json.error);
14
+ }
15
+ return json.data;
16
+ };
17
+ this.rpcUrl = rpcUrl;
4
18
  this.events = events;
5
19
  }
6
- getClient() {
7
- const client = {};
8
- Object.keys(this.events).forEach((key) => {
9
- client[key] = async (input) => {
10
- const response = await fetch(this.baseUrl, {
11
- method: "POST",
12
- body: JSON.stringify({ event: key, input }),
13
- });
14
- try {
15
- const json = (await response.json());
16
- if (json.error) {
17
- throw new Error(json.error);
18
- }
19
- return json.data;
20
- }
21
- catch (error) {
22
- console.error(error);
23
- return null;
24
- }
25
- };
26
- });
27
- return client;
20
+ // Keep this for internal use only
21
+ getEventCollection() {
22
+ return this.events;
28
23
  }
29
24
  }
@@ -14,21 +14,19 @@ export type EventType<TInput = any, TOutput = any> = {
14
14
  output: TOutput;
15
15
  };
16
16
  export type EventCollection = Record<string, Event>;
17
- export type EventTypeCollection = Record<string, EventType>;
18
17
  export type LixnetServerInit<TEvents extends EventCollection> = {
19
18
  debugLog?: boolean;
20
19
  logger?: DebugLogger;
21
20
  jsonResponseMaker?: (data: any, init?: ResponseInit) => Response;
22
21
  events: TEvents;
23
22
  };
24
- export type LixnetClientInit<TEventTypes extends EventTypeCollection> = {
25
- eventTypes: TEventTypes;
26
- baseUrl: string;
27
- };
28
- export type CallableEvent<TInput = any, TOutput = any> = (input: TInput) => Promise<TOutput>;
29
23
  export type ToEventTypes<TEvents extends EventCollection> = {
30
24
  [K in keyof TEvents]: TEvents[K] extends Event<infer TInput, infer TOutput> ? EventType<TInput, TOutput> : never;
31
25
  };
32
- export type ClientEvents<TEventTypes extends EventTypeCollection> = {
33
- [K in keyof TEventTypes]: TEventTypes[K] extends EventType<infer TInput, infer TOutput> ? CallableEvent<TInput, TOutput> : never;
34
- };
26
+ export type ClientEventInput<T> = T extends {
27
+ handler: (input: infer TInput) => any;
28
+ } ? Omit<TInput, "request"> : never;
29
+ export type ClientEventOutput<T> = T extends {
30
+ handler: (input: any) => Promise<infer TOutput>;
31
+ } ? TOutput : never;
32
+ export type ClientCaller<TEvents extends EventCollection> = <K extends keyof TEvents>(event: K, input: ClientEventInput<TEvents[K]>) => Promise<ClientEventOutput<TEvents[K]>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lixnet",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "author": "Nitlix",
5
5
  "license": "MIT",
6
6
  "repository": {