lixnet 1.0.3 → 1.1.1

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
  }
@@ -25,8 +25,8 @@ export default class LixnetServer {
25
25
  error: "Invalid JSON",
26
26
  }, { status: 400 });
27
27
  }
28
- if (!jsonData.action) {
29
- return this.jsonResponseMaker({ error: "Action not found" }, { status: 400 });
28
+ if (!jsonData.event) {
29
+ return this.jsonResponseMaker({ error: "Event not found" }, { status: 400 });
30
30
  }
31
31
  if (!jsonData.input) {
32
32
  return this.jsonResponseMaker({ error: "Input not found" }, { status: 400 });
@@ -34,7 +34,7 @@ export default class LixnetServer {
34
34
  const event = this.eventCollection[jsonData.event];
35
35
  if (!event) {
36
36
  return this.jsonResponseMaker({
37
- error: "Action not found",
37
+ error: "Event not found",
38
38
  }, { status: 404 });
39
39
  }
40
40
  try {
@@ -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.1",
4
4
  "author": "Nitlix",
5
5
  "license": "MIT",
6
6
  "repository": {