@milkio/stargate 1.0.0-alpha.7 → 1.0.0-alpha.9

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 +18 -6
  2. package/package.json +1 -1
package/index.ts CHANGED
@@ -42,8 +42,20 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
42
42
 
43
43
  type StargateEvents = {
44
44
  "milkio:executeBefore": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string }> };
45
- "milkio:fetchBefore": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string; body: string }> };
46
- "milkio:executeError": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string }>; error: Partial<Generated["rejectCode"]> };
45
+ "milkio:fetchBefore": { path: string; options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string }>; body: string };
46
+ "milkio:executeError": {
47
+ path: string;
48
+ options: Mixin<ExecuteOptions, { headers: Record<string, string>; baseUrl: string }>;
49
+ error: Partial<Generated["rejectCode"]>;
50
+ handleError: <K extends keyof Partial<Generated["rejectCode"]>>(error: any, key: K, handler: (error: Partial<Generated["rejectCode"][K]>) => void | boolean | Promise<void | boolean>) => Promise<void>;
51
+ };
52
+ };
53
+
54
+ const handleError: any = async (error: any, key: string, handler: (error: any) => void | boolean | Promise<void | boolean>) => {
55
+ if (key in error) {
56
+ const handled = await handler(error[key]);
57
+ if (handled) delete error[key];
58
+ }
47
59
  };
48
60
 
49
61
  const __initEventManager = () => {
@@ -157,17 +169,17 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
157
169
  result = { value: TSON.parse(response) };
158
170
  } catch (error: any) {
159
171
  if (error?.[0]?.REQUEST_TIMEOUT) {
160
- await eventManager.emit("milkio:executeError", { path: path as string, options: options as any, error: error });
172
+ await eventManager.emit("milkio:executeError", { handleError, path: path as string, options: options as any, error: error });
161
173
  return error;
162
174
  }
163
175
  let errorPined = { REQUEST_FAIL: error };
164
- await eventManager.emit("milkio:executeError", { path: path as string, options: options as any, error: errorPined });
176
+ await eventManager.emit("milkio:executeError", { handleError, path: path as string, options: options as any, error: errorPined });
165
177
  return [errorPined, null, { executeId: "unknown" }];
166
178
  }
167
179
  if (result.value.success !== true) {
168
180
  const error: any = {};
169
181
  error[result.value.code] = result.value.reject ?? null;
170
- await eventManager.emit("milkio:executeError", { path: path as string, options: options as any, error: error[result.value.code] });
182
+ await eventManager.emit("milkio:executeError", { handleError, path: path as string, options: options as any, error: error });
171
183
  return [error, null, { executeId: "unknown" }];
172
184
  }
173
185
 
@@ -252,7 +264,7 @@ export async function createStargate<Generated extends { routeSchema: any; rejec
252
264
  } catch (err) {
253
265
  if (!curRequestController.signal.aborted) curRequestController.abort();
254
266
  const error = { REQUEST_FAIL: err };
255
- await eventManager.emit("milkio:executeError", { path: path as string, options: options as any, error: error });
267
+ await eventManager.emit("milkio:executeError", { handleError, path: path as string, options: options as any, error: error });
256
268
  await iterator.throw(err);
257
269
  streamResultFetched.reject([error, null, { executeId: "unknown" }]);
258
270
  }
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.7",
4
+ "version": "1.0.0-alpha.9",
5
5
  "type": "module",
6
6
  "dependencies": {
7
7
  "@southern-aurora/tson": "^2.0.2"