@milkio/stargate 1.0.0-alpha.2 → 1.0.0-alpha.4

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.
Files changed (2) hide show
  1. package/index.ts +60 -4
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -40,6 +40,51 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
40
40
  const $fetch = stargateOptions.fetch ?? fetch;
41
41
  const $abort = stargateOptions.abort ?? AbortController;
42
42
 
43
+ interface $events {
44
+ "milkio:executeBefore": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string }> };
45
+ "milkio:executeError": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string }>; error: Partial<Generated["rejectCode"]> };
46
+ }
47
+
48
+ const __initEventManager = () => {
49
+ const handlers = new Map<(event: any) => void, string>();
50
+ const indexed = new Map<string, Set<(event: any) => void>>();
51
+
52
+ const eventManager = {
53
+ on: <Key extends keyof $events, Handler extends (event: $events[Key]) => void>(key: Key, handler: Handler) => {
54
+ handlers.set(handler, key as string);
55
+ if (indexed.has(key as string) === false) {
56
+ indexed.set(key as string, new Set());
57
+ }
58
+ const set = indexed.get(key as string)!;
59
+ set.add(handler);
60
+ handlers.set(handler, key as string);
61
+
62
+ return () => {
63
+ handlers.delete(handler);
64
+ set.delete(handler);
65
+ };
66
+ },
67
+ off: <Key extends keyof $events, Handler extends (event: $events[Key]) => void>(key: Key, handler: Handler) => {
68
+ const set = indexed.get(key as string);
69
+ if (!set) return;
70
+ handlers.delete(handler);
71
+ set.delete(handler);
72
+ },
73
+ emit: async <Key extends keyof $events, Value extends $events[Key]>(key: Key, value: Value): Promise<void> => {
74
+ const h = indexed.get(key as string);
75
+ if (h) {
76
+ for (const handler of h) {
77
+ await handler(value);
78
+ }
79
+ }
80
+ },
81
+ };
82
+
83
+ return eventManager;
84
+ };
85
+
86
+ const eventManager = __initEventManager();
87
+
43
88
  const bootstrap = async () => {
44
89
  let baseUrl = stargateOptions.baseUrl;
45
90
  if (typeof baseUrl === "function") baseUrl = await baseUrl();
@@ -51,6 +96,7 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
51
96
  const baseUrl: Promise<string> = bootstrap();
52
97
 
53
98
  const stargate = {
99
+ ...eventManager,
54
100
  $types: {
55
101
  generated: void 0 as unknown as Generated,
56
102
  },
@@ -81,6 +127,8 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
81
127
  url = baseUrl + (path as string);
82
128
  } else url = (await baseUrl) + (path as string);
83
129
 
130
+ await eventManager.emit("milkio:executeBefore", { path: path as string, options: options as any });
131
+
84
132
  if (options.type !== "stream") {
85
133
  // action
86
134
  if (options.headers["Accept"] === undefined) options.headers["Accept"] = "application/json";
@@ -106,12 +154,18 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
106
154
  });
107
155
  result = { value: TSON.parse(response) };
108
156
  } catch (error: any) {
109
- if (error?.[0]?.REQUEST_TIMEOUT) return error;
110
- return [{ REQUEST_FAIL: error }, null, { executeId: "unknown" }];
157
+ if (error?.[0]?.REQUEST_TIMEOUT) {
158
+ await eventManager.emit("milkio:executeError", { path: path as string, options: options as any, error: error });
159
+ return error;
160
+ }
161
+ let errorPined = { REQUEST_FAIL: error };
162
+ await eventManager.emit("milkio:executeError", { path: path as string, options: options as any, error: errorPined });
163
+ return [errorPined, null, { executeId: "unknown" }];
111
164
  }
112
165
  if (result.value.success !== true) {
113
166
  const error: any = {};
114
- error[result.value.code] = result.value.reject;
167
+ error[result.value.code] = result.value.reject ?? null;
168
+ await eventManager.emit("milkio:executeError", { path: path as string, options: options as any, error: error[result.value.code] });
115
169
  return [error, null, { executeId: "unknown" }];
116
170
  }
117
171
 
@@ -195,8 +249,10 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
195
249
  await iterator.return();
196
250
  } catch (err) {
197
251
  if (!curRequestController.signal.aborted) curRequestController.abort();
252
+ const error = { REQUEST_FAIL: err };
253
+ await eventManager.emit("milkio:executeError", { path: path as string, options: options as any, error: error });
198
254
  await iterator.throw(err);
199
- streamResultFetched.reject([{ REQUEST_FAIL: err }, null, { executeId: "unknown" }]);
255
+ streamResultFetched.reject([error, null, { executeId: "unknown" }]);
200
256
  }
201
257
  }
202
258
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@milkio/stargate",
3
3
  "module": "index.ts",
4
- "version": "1.0.0-alpha.2",
4
+ "version": "1.0.0-alpha.4",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "@southern-aurora/tson": "^2.0.2"