@ancon/wildcat-utils 1.44.0-terminal-refactor.2 → 1.44.0-terminal-refactor.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.
@@ -6,6 +6,10 @@ declare type HangfirePollerConfig = {
6
6
  maxGroupsSize: number;
7
7
  /** Max number of simultaneous workers per group */
8
8
  maxWorkersSize: number;
9
+ /** Successful response interceptor */
10
+ responseInterceptor?: (response: any) => any;
11
+ /** Failed response interceptor */
12
+ responseRejectionInterceptor?: (error: any) => any;
9
13
  /**
10
14
  * Function returning a boolean indicating whether connected
11
15
  * @example () => isNetworkOnlineSelector(getStore().getState())
@@ -27,6 +31,7 @@ declare class HangfirePoller {
27
31
  private workerByIdMap;
28
32
  private timer;
29
33
  private config;
34
+ private axiosInstance;
30
35
  constructor(options?: Partial<HangfirePollerConfig>);
31
36
  private stopTimer;
32
37
  private startTimer;
@@ -36,7 +41,7 @@ declare class HangfirePoller {
36
41
  createWorker(groupId: string, { workerId, url, headers, }: {
37
42
  workerId: string;
38
43
  url: string;
39
- headers?: APIHeaders;
44
+ headers: APIHeaders;
40
45
  }): Promise<unknown>;
41
46
  /** Trigger a worker to execute immediately, ignoring busy state */
42
47
  triggerWorker(workerId: string): void;
@@ -1 +1 @@
1
- "use strict";var f=Object.defineProperty;var p=(i,e,r)=>e in i?f(i,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):i[e]=r;var o=(i,e,r)=>(p(i,typeof e!="symbol"?e+"":e,r),r);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const b=require("axios"),u=require("@ancon/wildcat-types"),a=require("../error/createCodedError.js"),k=require("../error/isNotFoundError.js"),w=require("../error/serializeError.js"),W=require("../error/isErrorWithStatusCode.js"),m=require("../api/isNoContentResponse.js"),x=require("../shared/wait.js");require("../error/isCodedError.js");require("../error/isAPIError.js");class g{constructor(e){o(this,"busyWorkers",new Set);o(this,"enabledGroups",new Set);o(this,"groups",new Map);o(this,"workerByIdMap",new Map);o(this,"timer",null);o(this,"config",{pollerInterval:5e3,maxGroupsSize:50,maxWorkersSize:20,getIsNetworkOnline(){return!0},async getRequestHeaders(){return{}},debug(...e){console.log(...e)}});this.config={...this.config,...e??null}}stopTimer(){this.timer!=null&&(this.config.debug("*stop timer*"),clearInterval(this.timer),this.timer=null)}startTimer(){if(this.timer!=null)throw a(new Error("Timer already started"),u.ErrorCode.GenericUnexpectedHangfireError);this.config.debug("*start timer*"),this.timer=setInterval(()=>{this.config.debug("*tick*",{busyWorkers:this.busyWorkers.size,enabledGroups:this.enabledGroups.size,groups:this.groups}),this.enabledGroups.forEach(e=>{const r=this.groups.get(e);r&&r.forEach(t=>this.executeWorkerRequest(t))}),this.groups.size<1&&this.stopTimer()},this.config.pollerInterval)}async waitForNetworkOnline(e=5){let r=0;for(;!this.config.getIsNetworkOnline()&&r<e+1;)r+=1,await x(1e3)}async executeWorkerRequest(e,r=!1){const t=r!==!0&&this.busyWorkers.has(e.workerId);this.config.debug("*executeWorkerRequest*",{workerId:e.workerId,isBusy:t,force:r}),t?this.config.debug("*executeWorkerRequest* worker is busy",e.workerId):(this.busyWorkers.add(e.workerId),await this.waitForNetworkOnline(),this.config.debug("*executeWorkerRequest* request",{isNetworkOnline:this.config.getIsNetworkOnline()}),b({url:e.url,method:"get",timeout:this.config.pollerInterval,headers:{...await this.config.getRequestHeaders(),...e.headers}}).then(s=>{s&&!m(s)?(e.resolve(s),this.deleteWorker(e.groupId,e.workerId)):this.busyWorkers.delete(e.workerId)}).catch(s=>{if(W(s)){if(k(s)){this.config.debug("*executeWorkerRequest* worker is expired or invalid",e.workerId);const n=w(a(new Error("The Hangfire worker is expired or invalid"),u.ErrorCode.HangfireWorkerExpiredOrInvalid));e.reject(n)}else e.reject(s);this.deleteWorker(e.groupId,e.workerId)}else this.config.debug("*executeWorkerRequest* not an api error"),this.busyWorkers.delete(e.workerId)}))}deleteWorker(e,r){const t=this.groups.get(e);t&&(this.config.debug("*delete worker*",t.size,t),t.delete(r),this.workerByIdMap.delete(r),this.busyWorkers.delete(r),t.size<1&&(this.groups.delete(e),this.enabledGroups.delete(e),this.enabledGroups.size<1&&this.stopTimer()))}createWorker(e,{workerId:r,url:t,headers:s}){if(!this.groups.has(e)){if(this.groups.size>=this.config.maxGroupsSize){const l=a(new Error("Max number of Hangfire groups exceeded"),u.ErrorCode.GenericUnexpectedHangfireError,{count:this.groups.size});throw this.destroy(),l}this.groups.set(e,new Map)}const n=this.groups.get(e);return new Promise((l,h)=>{if(n.has(r))throw a(new Error("Duplicate Hangfire worker ID"),u.ErrorCode.GenericUnexpectedHangfireError);if(n.size>=this.config.maxWorkersSize){const c=a(new Error("Max number of Hangfire workers in group exceeded"),u.ErrorCode.GenericUnexpectedHangfireError,{groupId:e,count:n.size});throw h(c),this.destroy(),c}const d={workerId:r,groupId:e,url:t,resolve:l,reject:h,headers:s};n.set(r,d),this.workerByIdMap.set(r,d),this.enabledGroups.has(e)||this.enabledGroups.add(e),this.timer==null&&this.enabledGroups.size>0&&this.startTimer()})}triggerWorker(e){this.config.debug("*triggerWorker*");const r=this.workerByIdMap.get(e);r?this.executeWorkerRequest(r,!0):this.config.debug("*triggerWorker* worker not found",e)}triggerAllWorkers(){this.config.debug("*triggerAllWorkers*");for(const e of this.workerByIdMap.values())this.executeWorkerRequest(e)}enableWorkers(e){this.config.debug("*enable*",e),this.enabledGroups.add(e),this.groups.has(e)&&this.timer==null&&this.startTimer()}disableWorkers(e){this.config.debug("*disable*",e),this.enabledGroups.delete(e),this.enabledGroups.size<1&&this.stopTimer()}destroy(){this.config.debug("*destroy*"),this.groups.clear(),this.stopTimer()}}function y(i){return new g(i)}exports.HangfirePoller=g;exports.default=y;
1
+ "use strict";var f=Object.defineProperty;var p=(i,e,r)=>e in i?f(i,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):i[e]=r;var o=(i,e,r)=>(p(i,typeof e!="symbol"?e+"":e,r),r);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const b=require("axios"),u=require("@ancon/wildcat-types"),a=require("../error/createCodedError.js"),k=require("../error/isNotFoundError.js"),w=require("../error/serializeError.js"),W=require("../error/isErrorWithStatusCode.js"),m=require("../api/isNoContentResponse.js"),x=require("../shared/wait.js");require("../error/isCodedError.js");require("../error/isAPIError.js");class g{constructor(e){o(this,"busyWorkers",new Set);o(this,"enabledGroups",new Set);o(this,"groups",new Map);o(this,"workerByIdMap",new Map);o(this,"timer",null);o(this,"config",{pollerInterval:5e3,maxGroupsSize:50,maxWorkersSize:20,responseInterceptor:void 0,responseRejectionInterceptor:void 0,getIsNetworkOnline(){return!0},async getRequestHeaders(){return{}},debug(...e){console.log(...e)}});o(this,"axiosInstance");this.config={...this.config,...e??null},this.axiosInstance=b.create(),(this.config.responseInterceptor||this.config.responseRejectionInterceptor)&&this.axiosInstance.interceptors.response.use(this.config.responseInterceptor,this.config.responseRejectionInterceptor)}stopTimer(){this.timer!=null&&(this.config.debug("*stop timer*"),clearInterval(this.timer),this.timer=null)}startTimer(){if(this.timer!=null)throw a(new Error("Timer already started"),u.ErrorCode.GenericUnexpectedHangfireError);this.config.debug("*start timer*"),this.timer=setInterval(()=>{this.config.debug("*tick*",{busyWorkers:this.busyWorkers.size,enabledGroups:this.enabledGroups.size,groups:this.groups}),this.enabledGroups.forEach(e=>{const r=this.groups.get(e);r&&r.forEach(t=>this.executeWorkerRequest(t))}),this.groups.size<1&&this.stopTimer()},this.config.pollerInterval)}async waitForNetworkOnline(e=5){let r=0;for(;!this.config.getIsNetworkOnline()&&r<e+1;)r+=1,await x(1e3)}async executeWorkerRequest(e,r=!1){const t=r!==!0&&this.busyWorkers.has(e.workerId);this.config.debug("*executeWorkerRequest*",{workerId:e.workerId,isBusy:t,force:r}),t?this.config.debug("*executeWorkerRequest* worker is busy",e.workerId):(this.busyWorkers.add(e.workerId),await this.waitForNetworkOnline(),this.config.debug("*executeWorkerRequest* request",{isNetworkOnline:this.config.getIsNetworkOnline()}),this.axiosInstance({url:e.url,method:"get",timeout:this.config.pollerInterval,headers:{...await this.config.getRequestHeaders(),...e.headers}}).then(s=>{s&&!m(s)?(e.resolve(s),this.deleteWorker(e.groupId,e.workerId)):this.busyWorkers.delete(e.workerId)}).catch(s=>{if(W(s)){if(k(s)){this.config.debug("*executeWorkerRequest* worker is expired or invalid",e.workerId);const n=w(a(new Error("The Hangfire worker is expired or invalid"),u.ErrorCode.HangfireWorkerExpiredOrInvalid));e.reject(n)}else e.reject(s);this.deleteWorker(e.groupId,e.workerId)}else this.config.debug("*executeWorkerRequest* not an api error"),this.busyWorkers.delete(e.workerId)}))}deleteWorker(e,r){const t=this.groups.get(e);t&&(this.config.debug("*delete worker*",t.size,t),t.delete(r),this.workerByIdMap.delete(r),this.busyWorkers.delete(r),t.size<1&&(this.groups.delete(e),this.enabledGroups.delete(e),this.enabledGroups.size<1&&this.stopTimer()))}createWorker(e,{workerId:r,url:t,headers:s}){if(!this.groups.has(e)){if(this.groups.size>=this.config.maxGroupsSize){const c=a(new Error("Max number of Hangfire groups exceeded"),u.ErrorCode.GenericUnexpectedHangfireError,{count:this.groups.size});throw this.destroy(),c}this.groups.set(e,new Map)}const n=this.groups.get(e);return new Promise((c,h)=>{if(n.has(r))throw a(new Error("Duplicate Hangfire worker ID"),u.ErrorCode.GenericUnexpectedHangfireError);if(n.size>=this.config.maxWorkersSize){const d=a(new Error("Max number of Hangfire workers in group exceeded"),u.ErrorCode.GenericUnexpectedHangfireError,{groupId:e,count:n.size});throw h(d),this.destroy(),d}const l={workerId:r,groupId:e,url:t,resolve:c,reject:h,headers:s};n.set(r,l),this.workerByIdMap.set(r,l),this.enabledGroups.has(e)||this.enabledGroups.add(e),this.timer==null&&this.enabledGroups.size>0&&this.startTimer()})}triggerWorker(e){this.config.debug("*triggerWorker*");const r=this.workerByIdMap.get(e);r?this.executeWorkerRequest(r,!0):this.config.debug("*triggerWorker* worker not found",e)}triggerAllWorkers(){this.config.debug("*triggerAllWorkers*");for(const e of this.workerByIdMap.values())this.executeWorkerRequest(e)}enableWorkers(e){this.config.debug("*enable*",e),this.enabledGroups.add(e),this.groups.has(e)&&this.timer==null&&this.startTimer()}disableWorkers(e){this.config.debug("*disable*",e),this.enabledGroups.delete(e),this.enabledGroups.size<1&&this.stopTimer()}destroy(){this.config.debug("*destroy*"),this.groups.clear(),this.stopTimer()}}function y(i){return new g(i)}exports.HangfirePoller=g;exports.default=y;
@@ -1,9 +1,9 @@
1
- var c = Object.defineProperty;
2
- var f = (s, e, r) => e in s ? c(s, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : s[e] = r;
3
- var o = (s, e, r) => (f(s, typeof e != "symbol" ? e + "" : e, r), r);
1
+ var d = Object.defineProperty;
2
+ var f = (i, e, r) => e in i ? d(i, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : i[e] = r;
3
+ var o = (i, e, r) => (f(i, typeof e != "symbol" ? e + "" : e, r), r);
4
4
  import p from "axios";
5
5
  import { ErrorCode as a } from "@ancon/wildcat-types";
6
- import u from "../error/createCodedError.mjs";
6
+ import h from "../error/createCodedError.mjs";
7
7
  import m from "../error/isNotFoundError.mjs";
8
8
  import b from "../error/serializeError.mjs";
9
9
  import k from "../error/isErrorWithStatusCode.mjs";
@@ -26,6 +26,8 @@ class x {
26
26
  pollerInterval: 5e3,
27
27
  maxGroupsSize: 50,
28
28
  maxWorkersSize: 20,
29
+ responseInterceptor: void 0,
30
+ responseRejectionInterceptor: void 0,
29
31
  getIsNetworkOnline() {
30
32
  return !0;
31
33
  },
@@ -36,17 +38,21 @@ class x {
36
38
  console.log(...e);
37
39
  }
38
40
  });
41
+ o(this, "axiosInstance");
39
42
  this.config = {
40
43
  ...this.config,
41
44
  ...e ?? null
42
- };
45
+ }, this.axiosInstance = p.create(), (this.config.responseInterceptor || this.config.responseRejectionInterceptor) && this.axiosInstance.interceptors.response.use(
46
+ this.config.responseInterceptor,
47
+ this.config.responseRejectionInterceptor
48
+ );
43
49
  }
44
50
  stopTimer() {
45
51
  this.timer != null && (this.config.debug("*stop timer*"), clearInterval(this.timer), this.timer = null);
46
52
  }
47
53
  startTimer() {
48
54
  if (this.timer != null)
49
- throw u(
55
+ throw h(
50
56
  new Error("Timer already started"),
51
57
  a.GenericUnexpectedHangfireError
52
58
  );
@@ -77,7 +83,7 @@ class x {
77
83
  e.workerId
78
84
  ) : (this.busyWorkers.add(e.workerId), await this.waitForNetworkOnline(), this.config.debug("*executeWorkerRequest* request", {
79
85
  isNetworkOnline: this.config.getIsNetworkOnline()
80
- }), p({
86
+ }), this.axiosInstance({
81
87
  url: e.url,
82
88
  method: "get",
83
89
  timeout: this.config.pollerInterval,
@@ -85,24 +91,24 @@ class x {
85
91
  ...await this.config.getRequestHeaders(),
86
92
  ...e.headers
87
93
  }
88
- }).then((i) => {
89
- i && !w(i) ? (e.resolve(i), this.deleteWorker(e.groupId, e.workerId)) : this.busyWorkers.delete(e.workerId);
90
- }).catch((i) => {
91
- if (k(i)) {
92
- if (m(i)) {
94
+ }).then((s) => {
95
+ s && !w(s) ? (e.resolve(s), this.deleteWorker(e.groupId, e.workerId)) : this.busyWorkers.delete(e.workerId);
96
+ }).catch((s) => {
97
+ if (k(s)) {
98
+ if (m(s)) {
93
99
  this.config.debug(
94
100
  "*executeWorkerRequest* worker is expired or invalid",
95
101
  e.workerId
96
102
  );
97
103
  const n = b(
98
- u(
104
+ h(
99
105
  new Error("The Hangfire worker is expired or invalid"),
100
106
  a.HangfireWorkerExpiredOrInvalid
101
107
  )
102
108
  );
103
109
  e.reject(n);
104
110
  } else
105
- e.reject(i);
111
+ e.reject(s);
106
112
  this.deleteWorker(e.groupId, e.workerId);
107
113
  } else
108
114
  this.config.debug("*executeWorkerRequest* not an api error"), this.busyWorkers.delete(e.workerId);
@@ -115,43 +121,43 @@ class x {
115
121
  createWorker(e, {
116
122
  workerId: r,
117
123
  url: t,
118
- headers: i
124
+ headers: s
119
125
  }) {
120
126
  if (!this.groups.has(e)) {
121
127
  if (this.groups.size >= this.config.maxGroupsSize) {
122
- const h = u(
128
+ const u = h(
123
129
  new Error("Max number of Hangfire groups exceeded"),
124
130
  a.GenericUnexpectedHangfireError,
125
131
  { count: this.groups.size }
126
132
  );
127
- throw this.destroy(), h;
133
+ throw this.destroy(), u;
128
134
  }
129
135
  this.groups.set(e, /* @__PURE__ */ new Map());
130
136
  }
131
137
  const n = this.groups.get(e);
132
- return new Promise((h, l) => {
138
+ return new Promise((u, c) => {
133
139
  if (n.has(r))
134
- throw u(
140
+ throw h(
135
141
  new Error("Duplicate Hangfire worker ID"),
136
142
  a.GenericUnexpectedHangfireError
137
143
  );
138
144
  if (n.size >= this.config.maxWorkersSize) {
139
- const d = u(
145
+ const g = h(
140
146
  new Error("Max number of Hangfire workers in group exceeded"),
141
147
  a.GenericUnexpectedHangfireError,
142
148
  { groupId: e, count: n.size }
143
149
  );
144
- throw l(d), this.destroy(), d;
150
+ throw c(g), this.destroy(), g;
145
151
  }
146
- const g = {
152
+ const l = {
147
153
  workerId: r,
148
154
  groupId: e,
149
155
  url: t,
150
- resolve: h,
151
- reject: l,
152
- headers: i
156
+ resolve: u,
157
+ reject: c,
158
+ headers: s
153
159
  };
154
- n.set(r, g), this.workerByIdMap.set(r, g), this.enabledGroups.has(e) || this.enabledGroups.add(e), this.timer == null && this.enabledGroups.size > 0 && this.startTimer();
160
+ n.set(r, l), this.workerByIdMap.set(r, l), this.enabledGroups.has(e) || this.enabledGroups.add(e), this.timer == null && this.enabledGroups.size > 0 && this.startTimer();
155
161
  });
156
162
  }
157
163
  /** Trigger a worker to execute immediately, ignoring busy state */
@@ -177,8 +183,8 @@ class x {
177
183
  this.config.debug("*destroy*"), this.groups.clear(), this.stopTimer();
178
184
  }
179
185
  }
180
- function N(s) {
181
- return new x(s);
186
+ function N(i) {
187
+ return new x(i);
182
188
  }
183
189
  export {
184
190
  x as HangfirePoller,
@@ -6,5 +6,5 @@ import { HangfirePoller } from './createHangfirePoller';
6
6
  * @param hangfirePoller `HangfirePoller` instance from `createHangfirePoller()`
7
7
  * @returns `makeHangfireRequest()` callback function
8
8
  */
9
- declare function createMakeHangfireRequest(hangfirePoller: HangfirePoller): <TData extends Record<string, any>, TParams extends APIParams>(request: APIPostRequest<unknown, TData, TParams> | APIPutRequest<unknown, TData, TParams>, data: TData, params: TParams, groupId: string, headers?: APIHeaders, axiosConfig?: Record<string, unknown>, onResponse?: () => void | Promise<void>) => Promise<AxiosResponse>;
9
+ declare function createMakeHangfireRequest(hangfirePoller: HangfirePoller): <TData extends Record<string, any>, TParams extends APIParams>(request: APIPostRequest<unknown, TData, TParams> | APIPutRequest<unknown, TData, TParams>, data: TData, params: TParams, groupId: string, headers?: APIHeaders, onResponse?: () => void | Promise<void>) => Promise<AxiosResponse>;
10
10
  export default createMakeHangfireRequest;
@@ -1 +1 @@
1
- "use strict";const k=require("./isHangfireResponse.js");require("../api/isAcceptedResponse.js");function o(r,i){return`${r.config.baseURL??""}/${r.config.url??""}/status/${i}`}function q(r){return async function(n,c,f,g,a,s,e){const t=await n(c,f,a,s);if(await(e==null?void 0:e()),k(t)){const u=t.data.id;return r.createWorker(g,{workerId:u,url:o(t,u),headers:a})}return t}}module.exports=q;
1
+ "use strict";const s=require("./isHangfireResponse.js");require("../api/isAcceptedResponse.js");function k(r,i){return`${r.config.baseURL??""}/${r.config.url??""}/status/${i}`}function o(r){return async function(n,c,f,g,a={},e){const t=await n(c,f,a);if(await(e==null?void 0:e()),s(t)){const u=t.data.id;return r.createWorker(g,{workerId:u,url:k(t,u),headers:a})}return t}}module.exports=o;
@@ -1,16 +1,16 @@
1
- import k from "./isHangfireResponse.mjs";
1
+ import g from "./isHangfireResponse.mjs";
2
2
  import "../api/isAcceptedResponse.mjs";
3
- function l(r, e) {
3
+ function k(r, e) {
4
4
  return `${r.config.baseURL ?? ""}/${r.config.url ?? ""}/status/${e}`;
5
5
  }
6
- function s(r) {
7
- return async function(f, n, c, o, i, g, a) {
8
- const t = await f(n, c, i, g);
9
- if (await (a == null ? void 0 : a()), k(t)) {
6
+ function d(r) {
7
+ return async function(f, n, c, o, i = {}, a) {
8
+ const t = await f(n, c, i);
9
+ if (await (a == null ? void 0 : a()), g(t)) {
10
10
  const u = t.data.id;
11
11
  return r.createWorker(o, {
12
12
  workerId: u,
13
- url: l(t, u),
13
+ url: k(t, u),
14
14
  headers: i
15
15
  });
16
16
  }
@@ -18,5 +18,5 @@ function s(r) {
18
18
  };
19
19
  }
20
20
  export {
21
- s as default
21
+ d as default
22
22
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ancon/wildcat-utils",
3
- "version": "1.44.0-terminal-refactor.2",
3
+ "version": "1.44.0-terminal-refactor.4",
4
4
  "private": false,
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",