@bluecopa/core 0.1.57 → 0.1.59

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.
package/dist/index.es.js CHANGED
@@ -37,6 +37,8 @@ class ConfigSingleton {
37
37
  workspaceId: "",
38
38
  userId: "",
39
39
  solutionId: void 0,
40
+ solutionBranch: void 0,
41
+ solutionBranchType: void 0,
40
42
  websocketProvider: void 0
41
43
  };
42
44
  }
@@ -54,6 +56,22 @@ const createApiClient = () => {
54
56
  // Increased to 120 seconds for audit log queries
55
57
  headers: {
56
58
  "Content-Type": "application/json"
59
+ },
60
+ // PostgREST requires repeated params for multi-value filters: amount=gt.10&amount=lt.100
61
+ // Axios's default qs serializer uses indices format (amount[0]=gt.10) which PostgREST
62
+ // does not understand. This custom serializer produces the correct repeated-param format.
63
+ paramsSerializer: (params) => {
64
+ const parts = [];
65
+ for (const [key, value] of Object.entries(params)) {
66
+ if (Array.isArray(value)) {
67
+ for (const v of value) {
68
+ parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(v))}`);
69
+ }
70
+ } else if (value !== null && value !== void 0) {
71
+ parts.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
72
+ }
73
+ }
74
+ return parts.join("&");
57
75
  }
58
76
  });
59
77
  client.interceptors.request.use(
@@ -71,11 +89,6 @@ const createApiClient = () => {
71
89
  if (copaConfig.solutionId && config.headers) {
72
90
  config.headers["x-bluecopa-solution-id"] = copaConfig.solutionId;
73
91
  }
74
- console.log("API Request Config:", {
75
- baseURL: config.baseURL,
76
- hasToken: !!copaConfig.accessToken,
77
- hasWorkspaceId: !!copaConfig.workspaceId
78
- });
79
92
  return config;
80
93
  },
81
94
  (error) => {
@@ -125,7 +138,7 @@ async function getAllUsers() {
125
138
  throw { message, status };
126
139
  }
127
140
  }
128
- const index$n = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
141
+ const index$o = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
129
142
  __proto__: null,
130
143
  getAllUsers,
131
144
  getLoggedInUserDetails
@@ -147,7 +160,7 @@ async function triggerHttpWorkflowById({
147
160
  throw { message, status };
148
161
  }
149
162
  }
150
- async function triggerWorkflowById({
163
+ async function triggerWorkflowById$1({
151
164
  parentId,
152
165
  triggerBy
153
166
  }) {
@@ -205,13 +218,13 @@ async function getAllHttpTriggers() {
205
218
  throw { message, status };
206
219
  }
207
220
  }
208
- const index$m = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
221
+ const index$n = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
209
222
  __proto__: null,
210
223
  WorkflowStatus,
211
224
  getAllHttpTriggers,
212
225
  getWorkflowInstanceStatusById,
213
226
  triggerHttpWorkflowById,
214
- triggerWorkflowById
227
+ triggerWorkflowById: triggerWorkflowById$1
215
228
  }, Symbol.toStringTag, { value: "Module" }));
216
229
  async function getFileUrlByFileId({
217
230
  key,
@@ -311,7 +324,7 @@ async function fileDownload({
311
324
  throw { message, status };
312
325
  }
313
326
  }
314
- const index$l = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
327
+ const index$m = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
315
328
  __proto__: null,
316
329
  fileDownload,
317
330
  fileUpload,
@@ -387,7 +400,7 @@ const runDefinition = async (props) => {
387
400
  };
388
401
  }
389
402
  };
390
- const index$k = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
403
+ const index$l = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
391
404
  __proto__: null,
392
405
  runDefinition,
393
406
  runPublishedDefinition,
@@ -425,7 +438,7 @@ const getWorksheetsByType = async (type) => {
425
438
  throw { message, status };
426
439
  }
427
440
  };
428
- const index$j = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
441
+ const index$k = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
429
442
  __proto__: null,
430
443
  getWorksheets,
431
444
  getWorksheetsByType
@@ -9806,7 +9819,7 @@ const getUniqueDuplicateName = (name, collectionToCheckIn, suffix = "Copy", conc
9806
9819
  }
9807
9820
  return newName.trim();
9808
9821
  };
9809
- const index$i = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
9822
+ const index$j = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
9810
9823
  __proto__: null,
9811
9824
  formatDate,
9812
9825
  generatePushID,
@@ -9977,7 +9990,7 @@ const getData$3 = async (metricSheetId, options) => {
9977
9990
  resultData = _.flatten(resultData);
9978
9991
  return { data: resultData };
9979
9992
  };
9980
- const index$h = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
9993
+ const index$i = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
9981
9994
  __proto__: null,
9982
9995
  getData: getData$3
9983
9996
  }, Symbol.toStringTag, { value: "Module" }));
@@ -10132,7 +10145,7 @@ async function checkSubscriptionStatus(userId, threadId) {
10132
10145
  throw { message, status };
10133
10146
  }
10134
10147
  }
10135
- const index$g = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10148
+ const index$h = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10136
10149
  __proto__: null,
10137
10150
  checkSubscriptionStatus,
10138
10151
  createThread,
@@ -10337,7 +10350,7 @@ const getAllDatasets = async () => {
10337
10350
  throw { message, status };
10338
10351
  }
10339
10352
  };
10340
- const index$f = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10353
+ const index$g = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10341
10354
  __proto__: null,
10342
10355
  getAllDatasets,
10343
10356
  getData: getData$2,
@@ -10656,7 +10669,7 @@ const deleteRow = async (tableId, rowId, idField = "_copa_id") => {
10656
10669
  throw { message, status };
10657
10670
  }
10658
10671
  };
10659
- const index$e = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10672
+ const index$f = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10660
10673
  __proto__: null,
10661
10674
  deleteRow,
10662
10675
  getData: getData$1,
@@ -10750,7 +10763,7 @@ async function publishWorkbook({
10750
10763
  throw { message, status };
10751
10764
  }
10752
10765
  }
10753
- const index$d = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10766
+ const index$e = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
10754
10767
  __proto__: null,
10755
10768
  getPublishedWorkbookById,
10756
10769
  getWorkbookDetails,
@@ -11018,7 +11031,7 @@ const getRunResultById = async (runId) => {
11018
11031
  throw { message, status };
11019
11032
  }
11020
11033
  };
11021
- const index$c = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11034
+ const index$d = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11022
11035
  __proto__: null,
11023
11036
  createNewRun,
11024
11037
  getData,
@@ -11048,7 +11061,7 @@ async function getTaskDetails({
11048
11061
  throw { message, status };
11049
11062
  }
11050
11063
  }
11051
- const index$b = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11064
+ const index$c = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11052
11065
  __proto__: null,
11053
11066
  getTaskDetails
11054
11067
  }, Symbol.toStringTag, { value: "Module" }));
@@ -11094,7 +11107,7 @@ async function getAllReconWorkflows() {
11094
11107
  throw { message, status };
11095
11108
  }
11096
11109
  }
11097
- const index$a = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11110
+ const index$b = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11098
11111
  __proto__: null,
11099
11112
  getAllReconWorkflows,
11100
11113
  runRecon
@@ -11195,7 +11208,7 @@ async function createOrUpdateForm({
11195
11208
  throw { message, status: status2 };
11196
11209
  }
11197
11210
  }
11198
- const index$9 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11211
+ const index$a = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11199
11212
  __proto__: null,
11200
11213
  createOrUpdateForm,
11201
11214
  getFormById,
@@ -11236,7 +11249,7 @@ async function createAuditLog(params) {
11236
11249
  throw { message, status };
11237
11250
  }
11238
11251
  }
11239
- const index$8 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11252
+ const index$9 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11240
11253
  __proto__: null,
11241
11254
  createAuditLog,
11242
11255
  getAuditLogs
@@ -11255,7 +11268,7 @@ async function getAllTemplatedPipelines() {
11255
11268
  throw { message, status };
11256
11269
  }
11257
11270
  }
11258
- const index$7 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11271
+ const index$8 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11259
11272
  __proto__: null,
11260
11273
  getAllTemplatedPipelines
11261
11274
  }, Symbol.toStringTag, { value: "Module" }));
@@ -11316,7 +11329,7 @@ async function reassignTask({
11316
11329
  throw { message, status };
11317
11330
  }
11318
11331
  }
11319
- const index$6 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11332
+ const index$7 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11320
11333
  __proto__: null,
11321
11334
  markTaskDone,
11322
11335
  reassignTask
@@ -11393,7 +11406,7 @@ async function markItemAsUnread({
11393
11406
  throw { message, status };
11394
11407
  }
11395
11408
  }
11396
- const index$5 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11409
+ const index$6 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11397
11410
  __proto__: null,
11398
11411
  getAllInboxItems,
11399
11412
  markItemAsRead,
@@ -11420,7 +11433,7 @@ async function getPermissions(params) {
11420
11433
  throw { message, status };
11421
11434
  }
11422
11435
  }
11423
- const index$4 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11436
+ const index$5 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11424
11437
  __proto__: null,
11425
11438
  getPermissions
11426
11439
  }, Symbol.toStringTag, { value: "Module" }));
@@ -11438,7 +11451,7 @@ async function getClientIp() {
11438
11451
  throw { message, status };
11439
11452
  }
11440
11453
  }
11441
- const index$3 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11454
+ const index$4 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11442
11455
  __proto__: null,
11443
11456
  getClientIp
11444
11457
  }, Symbol.toStringTag, { value: "Module" }));
@@ -11573,7 +11586,7 @@ async function getMessageBySenderId(params) {
11573
11586
  throw { message, status };
11574
11587
  }
11575
11588
  }
11576
- const index$2 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11589
+ const index$3 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11577
11590
  __proto__: null,
11578
11591
  createConversation,
11579
11592
  getAllConversations,
@@ -11808,7 +11821,7 @@ async function getCallData(token, callSid) {
11808
11821
  throw { message, status };
11809
11822
  }
11810
11823
  }
11811
- const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11824
+ const index$2 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11812
11825
  __proto__: null,
11813
11826
  agentDisconnect,
11814
11827
  agentGetConnectedParty,
@@ -11828,31 +11841,424 @@ const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.definePrope
11828
11841
  processManualDial,
11829
11842
  refreshToken
11830
11843
  }, Symbol.toStringTag, { value: "Module" }));
11844
+ const BASE$4 = "/api/v1/webcron/webhooks";
11845
+ async function listWebhooks() {
11846
+ var _a, _b, _c, _d;
11847
+ try {
11848
+ const response = await apiClient.get(BASE$4);
11849
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11850
+ } catch (error) {
11851
+ const e = error;
11852
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to list webhooks", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11853
+ }
11854
+ }
11855
+ async function createWebhook(data) {
11856
+ var _a, _b, _c, _d, _e, _f;
11857
+ if (!((_a = data.name) == null ? void 0 : _a.trim())) throw { message: "name is required", status: 400 };
11858
+ if (!((_b = data.url) == null ? void 0 : _b.trim())) throw { message: "url is required", status: 400 };
11859
+ try {
11860
+ const response = await apiClient.post(BASE$4, data);
11861
+ return ((_c = response.data) == null ? void 0 : _c.data) ?? response.data;
11862
+ } catch (error) {
11863
+ const e = error;
11864
+ throw { message: ((_e = (_d = e.response) == null ? void 0 : _d.data) == null ? void 0 : _e.message) || "Failed to create webhook", status: ((_f = e.response) == null ? void 0 : _f.status) || 500 };
11865
+ }
11866
+ }
11867
+ async function getWebhook(id) {
11868
+ var _a, _b, _c, _d;
11869
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11870
+ try {
11871
+ const response = await apiClient.get(`${BASE$4}/${id}`);
11872
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11873
+ } catch (error) {
11874
+ const e = error;
11875
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to get webhook", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11876
+ }
11877
+ }
11878
+ async function updateWebhook(id, data) {
11879
+ var _a, _b, _c, _d;
11880
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11881
+ try {
11882
+ const response = await apiClient.put(`${BASE$4}/${id}`, data);
11883
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11884
+ } catch (error) {
11885
+ const e = error;
11886
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to update webhook", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11887
+ }
11888
+ }
11889
+ async function deleteWebhook(id) {
11890
+ var _a, _b, _c;
11891
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11892
+ try {
11893
+ await apiClient.delete(`${BASE$4}/${id}`);
11894
+ } catch (error) {
11895
+ const e = error;
11896
+ throw { message: ((_b = (_a = e.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message) || "Failed to delete webhook", status: ((_c = e.response) == null ? void 0 : _c.status) || 500 };
11897
+ }
11898
+ }
11899
+ async function testWebhook(id) {
11900
+ var _a, _b, _c, _d;
11901
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11902
+ try {
11903
+ const response = await apiClient.post(`${BASE$4}/${id}/test`);
11904
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11905
+ } catch (error) {
11906
+ const e = error;
11907
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to test webhook", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11908
+ }
11909
+ }
11910
+ async function getWebhookExecutions(id, params) {
11911
+ var _a, _b, _c, _d;
11912
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11913
+ try {
11914
+ const response = await apiClient.get(`${BASE$4}/${id}/executions`, { params });
11915
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11916
+ } catch (error) {
11917
+ const e = error;
11918
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to get webhook executions", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11919
+ }
11920
+ }
11921
+ async function getWebhookSchedules(id) {
11922
+ var _a, _b, _c, _d;
11923
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11924
+ try {
11925
+ const response = await apiClient.get(`${BASE$4}/${id}/schedules`);
11926
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11927
+ } catch (error) {
11928
+ const e = error;
11929
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to get webhook schedules", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11930
+ }
11931
+ }
11932
+ async function createWebhookSchedule(webhookId, data) {
11933
+ var _a, _b, _c, _d;
11934
+ if (!(webhookId == null ? void 0 : webhookId.trim())) throw { message: "webhookId is required", status: 400 };
11935
+ try {
11936
+ const response = await apiClient.post(`${BASE$4}/${webhookId}/schedules`, data);
11937
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11938
+ } catch (error) {
11939
+ const e = error;
11940
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to create schedule", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11941
+ }
11942
+ }
11943
+ const BASE$3 = "/api/v1/webcron/schedules";
11944
+ async function getSchedule(id) {
11945
+ var _a, _b, _c, _d;
11946
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11947
+ try {
11948
+ const response = await apiClient.get(`${BASE$3}/${id}`);
11949
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11950
+ } catch (error) {
11951
+ const e = error;
11952
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to get schedule", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11953
+ }
11954
+ }
11955
+ async function updateSchedule(id, data) {
11956
+ var _a, _b, _c, _d;
11957
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11958
+ try {
11959
+ const response = await apiClient.put(`${BASE$3}/${id}`, data);
11960
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11961
+ } catch (error) {
11962
+ const e = error;
11963
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to update schedule", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11964
+ }
11965
+ }
11966
+ async function deleteSchedule(id) {
11967
+ var _a, _b, _c;
11968
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11969
+ try {
11970
+ await apiClient.delete(`${BASE$3}/${id}`);
11971
+ } catch (error) {
11972
+ const e = error;
11973
+ throw { message: ((_b = (_a = e.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message) || "Failed to delete schedule", status: ((_c = e.response) == null ? void 0 : _c.status) || 500 };
11974
+ }
11975
+ }
11976
+ async function pauseSchedule(id) {
11977
+ var _a, _b, _c, _d;
11978
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11979
+ try {
11980
+ const response = await apiClient.post(`${BASE$3}/${id}/pause`);
11981
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11982
+ } catch (error) {
11983
+ const e = error;
11984
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to pause schedule", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11985
+ }
11986
+ }
11987
+ async function resumeSchedule(id) {
11988
+ var _a, _b, _c, _d;
11989
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
11990
+ try {
11991
+ const response = await apiClient.post(`${BASE$3}/${id}/resume`);
11992
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
11993
+ } catch (error) {
11994
+ const e = error;
11995
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to resume schedule", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
11996
+ }
11997
+ }
11998
+ async function triggerSchedule(id) {
11999
+ var _a, _b, _c, _d;
12000
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12001
+ try {
12002
+ const response = await apiClient.post(`${BASE$3}/${id}/trigger`);
12003
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12004
+ } catch (error) {
12005
+ const e = error;
12006
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to trigger schedule", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12007
+ }
12008
+ }
12009
+ const BASE$2 = "/api/v1/webcron/executions";
12010
+ async function listExecutions(params) {
12011
+ var _a, _b, _c, _d;
12012
+ try {
12013
+ const response = await apiClient.get(BASE$2, { params });
12014
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12015
+ } catch (error) {
12016
+ const e = error;
12017
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to list executions", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12018
+ }
12019
+ }
12020
+ async function getExecution(id) {
12021
+ var _a, _b, _c, _d;
12022
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12023
+ try {
12024
+ const response = await apiClient.get(`${BASE$2}/${id}`);
12025
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12026
+ } catch (error) {
12027
+ const e = error;
12028
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to get execution", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12029
+ }
12030
+ }
12031
+ async function retryExecution(id) {
12032
+ var _a, _b, _c, _d;
12033
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12034
+ try {
12035
+ const response = await apiClient.post(`${BASE$2}/${id}/retry`);
12036
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12037
+ } catch (error) {
12038
+ const e = error;
12039
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to retry execution", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12040
+ }
12041
+ }
12042
+ const BASE$1 = "/api/v1/webcron/tasks";
12043
+ async function listTasks() {
12044
+ var _a, _b, _c, _d;
12045
+ try {
12046
+ const response = await apiClient.get(BASE$1);
12047
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12048
+ } catch (error) {
12049
+ const e = error;
12050
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to list tasks", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12051
+ }
12052
+ }
12053
+ async function createTask(data) {
12054
+ var _a, _b, _c, _d, _e;
12055
+ if (!((_a = data.webhookId) == null ? void 0 : _a.trim())) throw { message: "webhookId is required", status: 400 };
12056
+ try {
12057
+ const response = await apiClient.post(BASE$1, data);
12058
+ return ((_b = response.data) == null ? void 0 : _b.data) ?? response.data;
12059
+ } catch (error) {
12060
+ const e = error;
12061
+ throw { message: ((_d = (_c = e.response) == null ? void 0 : _c.data) == null ? void 0 : _d.message) || "Failed to create task", status: ((_e = e.response) == null ? void 0 : _e.status) || 500 };
12062
+ }
12063
+ }
12064
+ async function getTask(id) {
12065
+ var _a, _b, _c, _d;
12066
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12067
+ try {
12068
+ const response = await apiClient.get(`${BASE$1}/${id}`);
12069
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12070
+ } catch (error) {
12071
+ const e = error;
12072
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to get task", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12073
+ }
12074
+ }
12075
+ async function cancelTask(id) {
12076
+ var _a, _b, _c, _d;
12077
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12078
+ try {
12079
+ const response = await apiClient.post(`${BASE$1}/${id}/cancel`);
12080
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12081
+ } catch (error) {
12082
+ const e = error;
12083
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to cancel task", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12084
+ }
12085
+ }
12086
+ async function retryTask(id) {
12087
+ var _a, _b, _c, _d;
12088
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12089
+ try {
12090
+ const response = await apiClient.post(`${BASE$1}/${id}/retry`);
12091
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12092
+ } catch (error) {
12093
+ const e = error;
12094
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to retry task", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12095
+ }
12096
+ }
12097
+ async function progressTask(id, data) {
12098
+ var _a, _b, _c;
12099
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12100
+ try {
12101
+ await apiClient.post(`${BASE$1}/${id}/progress`, data);
12102
+ } catch (error) {
12103
+ const e = error;
12104
+ throw { message: ((_b = (_a = e.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message) || "Failed to log task progress", status: ((_c = e.response) == null ? void 0 : _c.status) || 500 };
12105
+ }
12106
+ }
12107
+ async function signalTask(id, data, branchId) {
12108
+ var _a, _b, _c, _d;
12109
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12110
+ try {
12111
+ const params = branchId ? { branchId } : void 0;
12112
+ const response = await apiClient.post(`${BASE$1}/${id}/signal`, data, { params });
12113
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12114
+ } catch (error) {
12115
+ const e = error;
12116
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to signal task", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12117
+ }
12118
+ }
12119
+ const BASE = "/api/v1/webcron/inspection/bins";
12120
+ async function listBins() {
12121
+ var _a, _b, _c, _d;
12122
+ try {
12123
+ const response = await apiClient.get(BASE);
12124
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12125
+ } catch (error) {
12126
+ const e = error;
12127
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to list bins", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12128
+ }
12129
+ }
12130
+ async function createBin(data) {
12131
+ var _a, _b, _c, _d, _e;
12132
+ if (!((_a = data.name) == null ? void 0 : _a.trim())) throw { message: "name is required", status: 400 };
12133
+ try {
12134
+ const response = await apiClient.post(BASE, data);
12135
+ return ((_b = response.data) == null ? void 0 : _b.data) ?? response.data;
12136
+ } catch (error) {
12137
+ const e = error;
12138
+ throw { message: ((_d = (_c = e.response) == null ? void 0 : _c.data) == null ? void 0 : _d.message) || "Failed to create bin", status: ((_e = e.response) == null ? void 0 : _e.status) || 500 };
12139
+ }
12140
+ }
12141
+ async function getBin(id) {
12142
+ var _a, _b, _c, _d;
12143
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12144
+ try {
12145
+ const response = await apiClient.get(`${BASE}/${id}`);
12146
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12147
+ } catch (error) {
12148
+ const e = error;
12149
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to get bin", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12150
+ }
12151
+ }
12152
+ async function deleteBin(id) {
12153
+ var _a, _b, _c;
12154
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12155
+ try {
12156
+ await apiClient.delete(`${BASE}/${id}`);
12157
+ } catch (error) {
12158
+ const e = error;
12159
+ throw { message: ((_b = (_a = e.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message) || "Failed to delete bin", status: ((_c = e.response) == null ? void 0 : _c.status) || 500 };
12160
+ }
12161
+ }
12162
+ async function getBinRequests(id, params) {
12163
+ var _a, _b, _c, _d;
12164
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12165
+ try {
12166
+ const response = await apiClient.get(`${BASE}/${id}/requests`, { params });
12167
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12168
+ } catch (error) {
12169
+ const e = error;
12170
+ throw { message: ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || "Failed to get bin requests", status: ((_d = e.response) == null ? void 0 : _d.status) || 500 };
12171
+ }
12172
+ }
12173
+ async function clearBinRequests(id) {
12174
+ var _a, _b, _c;
12175
+ if (!(id == null ? void 0 : id.trim())) throw { message: "id is required", status: 400 };
12176
+ try {
12177
+ await apiClient.delete(`${BASE}/${id}/requests`);
12178
+ } catch (error) {
12179
+ const e = error;
12180
+ throw { message: ((_b = (_a = e.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message) || "Failed to clear bin requests", status: ((_c = e.response) == null ? void 0 : _c.status) || 500 };
12181
+ }
12182
+ }
12183
+ async function triggerWorkflowById(id, data) {
12184
+ var _a, _b, _c, _d;
12185
+ if (!(id == null ? void 0 : id.trim())) {
12186
+ throw { message: "id is required", status: 400 };
12187
+ }
12188
+ try {
12189
+ const response = await apiClient.post(
12190
+ `/api/v1/workflow/trigger/${id.trim()}`,
12191
+ data ?? {}
12192
+ );
12193
+ return ((_a = response.data) == null ? void 0 : _a.data) ?? response.data;
12194
+ } catch (error) {
12195
+ const e = error;
12196
+ const message = ((_c = (_b = e.response) == null ? void 0 : _b.data) == null ? void 0 : _c.message) || e.message || "Failed to trigger workflow";
12197
+ const status = ((_d = e.response) == null ? void 0 : _d.status) || 500;
12198
+ throw { message, status };
12199
+ }
12200
+ }
12201
+ const index$1 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
12202
+ __proto__: null,
12203
+ cancelTask,
12204
+ clearBinRequests,
12205
+ createBin,
12206
+ createTask,
12207
+ createWebhook,
12208
+ createWebhookSchedule,
12209
+ deleteBin,
12210
+ deleteSchedule,
12211
+ deleteWebhook,
12212
+ getBin,
12213
+ getBinRequests,
12214
+ getExecution,
12215
+ getSchedule,
12216
+ getTask,
12217
+ getWebhook,
12218
+ getWebhookExecutions,
12219
+ getWebhookSchedules,
12220
+ listBins,
12221
+ listExecutions,
12222
+ listTasks,
12223
+ listWebhooks,
12224
+ pauseSchedule,
12225
+ progressTask,
12226
+ resumeSchedule,
12227
+ retryExecution,
12228
+ retryTask,
12229
+ signalTask,
12230
+ testWebhook,
12231
+ triggerSchedule,
12232
+ triggerWorkflowById,
12233
+ updateSchedule,
12234
+ updateWebhook
12235
+ }, Symbol.toStringTag, { value: "Module" }));
11831
12236
  const index = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
11832
12237
  __proto__: null,
11833
12238
  apiClient,
11834
- audit: index$8,
11835
- chat: index$g,
11836
- clientIp: index$3,
11837
- dataset: index$f,
11838
- definition: index$k,
11839
- emailEngine: index$2,
11840
- files: index$l,
11841
- form: index$9,
11842
- inboxItems: index$5,
11843
- inputTable: index$e,
11844
- metric: index$h,
11845
- permissions: index$4,
11846
- process: index$6,
11847
- recon: index$a,
11848
- statement: index$c,
11849
- task: index$b,
11850
- tcn: index$1,
11851
- templatedPipeline: index$7,
11852
- user: index$n,
11853
- workbook: index$d,
11854
- workflow: index$m,
11855
- worksheet: index$j
12239
+ audit: index$9,
12240
+ chat: index$h,
12241
+ clientIp: index$4,
12242
+ dataset: index$g,
12243
+ definition: index$l,
12244
+ emailEngine: index$3,
12245
+ files: index$m,
12246
+ form: index$a,
12247
+ inboxItems: index$6,
12248
+ inputTable: index$f,
12249
+ metric: index$i,
12250
+ permissions: index$5,
12251
+ process: index$7,
12252
+ recon: index$b,
12253
+ statement: index$d,
12254
+ task: index$c,
12255
+ tcn: index$2,
12256
+ templatedPipeline: index$8,
12257
+ user: index$o,
12258
+ webcron: index$1,
12259
+ workbook: index$e,
12260
+ workflow: index$n,
12261
+ worksheet: index$k
11856
12262
  }, Symbol.toStringTag, { value: "Module" }));
11857
12263
  var InputTableColumnType = /* @__PURE__ */ ((InputTableColumnType2) => {
11858
12264
  InputTableColumnType2["UUID"] = "uuid";
@@ -11867,6 +12273,75 @@ var InputTableColumnType = /* @__PURE__ */ ((InputTableColumnType2) => {
11867
12273
  InputTableColumnType2["JSONB"] = "jsonb";
11868
12274
  return InputTableColumnType2;
11869
12275
  })(InputTableColumnType || {});
12276
+ const IDENTIFIER_PATTERN = /^[a-zA-Z0-9_-]+$/;
12277
+ const SOLUTION_BRANCH_COOKIE_NAME = "x-bluecopa-solution-branch";
12278
+ const SOLUTION_BRANCH_TYPE_COOKIE_NAME = "x-bluecopa-solution-branch-type";
12279
+ function readCookie(name) {
12280
+ try {
12281
+ const cookie = document.cookie.split(";").map((c) => c.trim()).find((c) => c.startsWith(`${name}=`));
12282
+ return cookie ? decodeURIComponent(cookie.split("=").slice(1).join("=")) : null;
12283
+ } catch {
12284
+ return null;
12285
+ }
12286
+ }
12287
+ function getSolutionBranchHeaders() {
12288
+ const config = getConfig();
12289
+ const branch = config.solutionBranch ?? readCookie(SOLUTION_BRANCH_COOKIE_NAME);
12290
+ const branchType = config.solutionBranchType ?? readCookie(SOLUTION_BRANCH_TYPE_COOKIE_NAME);
12291
+ const headers = {};
12292
+ if (branch) headers["x-bluecopa-solution-branch"] = branch;
12293
+ if (branchType) headers["x-bluecopa-solution-branch-type"] = branchType;
12294
+ return headers;
12295
+ }
12296
+ const ISO_TIMESTAMP_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;
12297
+ class InputTableError extends Error {
12298
+ constructor(message, status) {
12299
+ super(message);
12300
+ this.status = status;
12301
+ this.name = "InputTableError";
12302
+ }
12303
+ }
12304
+ function validateIdentifier(value, label) {
12305
+ const trimmed = value.trim();
12306
+ if (!trimmed || !IDENTIFIER_PATTERN.test(trimmed)) {
12307
+ throw new InputTableError(`Invalid ${label}: "${value}"`, 400);
12308
+ }
12309
+ return trimmed;
12310
+ }
12311
+ function validateCheckpointTimestamp(value) {
12312
+ if (!ISO_TIMESTAMP_PATTERN.test(value)) {
12313
+ throw new InputTableError(`Invalid checkpoint timestamp: "${value}"`, 400);
12314
+ }
12315
+ return value;
12316
+ }
12317
+ function validateCheckpointId(value) {
12318
+ return validateIdentifier(value, "checkpoint id");
12319
+ }
12320
+ function stripNullsAndCoercePk(row, primaryKeyField) {
12321
+ const doc = {};
12322
+ for (const [key, value] of Object.entries(row)) {
12323
+ if (value === null) continue;
12324
+ doc[key] = value;
12325
+ }
12326
+ doc[primaryKeyField] = String(row[primaryKeyField]);
12327
+ return doc;
12328
+ }
12329
+ function deferSubscription(resolve, callback, fallback) {
12330
+ let innerUnsub = null;
12331
+ let cancelled = false;
12332
+ resolve().then((subscribeFn) => {
12333
+ if (!cancelled) {
12334
+ innerUnsub = subscribeFn(callback);
12335
+ }
12336
+ }).catch((err) => {
12337
+ console.error("[copaInputTableDb]", err);
12338
+ if (!cancelled) callback(fallback);
12339
+ });
12340
+ return () => {
12341
+ cancelled = true;
12342
+ innerUnsub == null ? void 0 : innerUnsub();
12343
+ };
12344
+ }
11870
12345
  const OP_MAP = {
11871
12346
  "==": "$eq",
11872
12347
  "!=": "$ne",
@@ -11878,8 +12353,9 @@ const OP_MAP = {
11878
12353
  "not-in": "$nin"
11879
12354
  };
11880
12355
  class QueryBuilderImpl {
11881
- constructor(collection, initialWhere, initialOrder, initialLimit, initialSkip) {
12356
+ constructor(collection, initialWhere, initialOrder, initialLimit, initialSkip, aggregateExecutor) {
11882
12357
  this.collection = collection;
12358
+ this.aggregateExecutor = aggregateExecutor;
11883
12359
  this.wheres = [];
11884
12360
  this.orders = [];
11885
12361
  if (initialWhere) this.wheres.push(initialWhere);
@@ -11915,6 +12391,22 @@ class QueryBuilderImpl {
11915
12391
  const docs = await query.exec();
11916
12392
  return docs.map((d) => d.toJSON());
11917
12393
  }
12394
+ async aggregate(spec, options) {
12395
+ if (!this.aggregateExecutor) {
12396
+ throw new InputTableError(
12397
+ "[copaInputTableDb] aggregate() is not available on this QueryBuilder instance",
12398
+ 500
12399
+ );
12400
+ }
12401
+ return this.aggregateExecutor(
12402
+ this.wheres,
12403
+ this.orders,
12404
+ this.limitCount,
12405
+ this.skipCount,
12406
+ spec,
12407
+ options == null ? void 0 : options.groupBy
12408
+ );
12409
+ }
11918
12410
  buildQuery() {
11919
12411
  const selector = {};
11920
12412
  for (const { field, op, value } of this.wheres) {
@@ -11940,56 +12432,6 @@ const MAX_COLLECTIONS = 14;
11940
12432
  const REPLICATION_RETRY_TIME = 5e3;
11941
12433
  const PULL_BATCH_SIZE = 100;
11942
12434
  const PUSH_BATCH_SIZE = 10;
11943
- const IDENTIFIER_PATTERN = /^[a-zA-Z0-9_-]+$/;
11944
- const ISO_TIMESTAMP_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/;
11945
- class InputTableError extends Error {
11946
- constructor(message, status) {
11947
- super(message);
11948
- this.status = status;
11949
- this.name = "InputTableError";
11950
- }
11951
- }
11952
- function validateIdentifier(value, label) {
11953
- const trimmed = value.trim();
11954
- if (!trimmed || !IDENTIFIER_PATTERN.test(trimmed)) {
11955
- throw new InputTableError(`Invalid ${label}: "${value}"`, 400);
11956
- }
11957
- return trimmed;
11958
- }
11959
- function validateCheckpointTimestamp(value) {
11960
- if (!ISO_TIMESTAMP_PATTERN.test(value)) {
11961
- throw new InputTableError(`Invalid checkpoint timestamp: "${value}"`, 400);
11962
- }
11963
- return value;
11964
- }
11965
- function validateCheckpointId(value) {
11966
- return validateIdentifier(value, "checkpoint id");
11967
- }
11968
- function stripNullsAndCoercePk(row, primaryKeyField) {
11969
- const doc = {};
11970
- for (const [key, value] of Object.entries(row)) {
11971
- if (value === null) continue;
11972
- doc[key] = value;
11973
- }
11974
- doc[primaryKeyField] = String(row[primaryKeyField]);
11975
- return doc;
11976
- }
11977
- function deferSubscription(resolve, callback, fallback) {
11978
- let innerUnsub = null;
11979
- let cancelled = false;
11980
- resolve().then((subscribeFn) => {
11981
- if (!cancelled) {
11982
- innerUnsub = subscribeFn(callback);
11983
- }
11984
- }).catch((err) => {
11985
- console.error("[copaInputTableDb]", err);
11986
- if (!cancelled) callback(fallback);
11987
- });
11988
- return () => {
11989
- cancelled = true;
11990
- innerUnsub == null ? void 0 : innerUnsub();
11991
- };
11992
- }
11993
12435
  class DocRefImpl {
11994
12436
  constructor(collection, id, solutionId, tableName, primaryKeyField, websocketProvider) {
11995
12437
  this.collection = collection;
@@ -12029,6 +12471,168 @@ class DocRefImpl {
12029
12471
  return () => rxSub.unsubscribe();
12030
12472
  }
12031
12473
  }
12474
+ const POSTGREST_OP_MAP = {
12475
+ "==": "eq",
12476
+ "!=": "neq",
12477
+ "<": "lt",
12478
+ "<=": "lte",
12479
+ ">": "gt",
12480
+ ">=": "gte",
12481
+ in: "in",
12482
+ "not-in": "not.in"
12483
+ };
12484
+ const VALID_AGGREGATE_FUNCTIONS = /* @__PURE__ */ new Set([
12485
+ "sum",
12486
+ "avg",
12487
+ "count",
12488
+ "min",
12489
+ "max"
12490
+ ]);
12491
+ function validateAggregateSpec(spec) {
12492
+ for (const [key, value] of Object.entries(spec)) {
12493
+ if (key === "_count") {
12494
+ if (value !== true) {
12495
+ throw new InputTableError(
12496
+ `"_count" must be exactly \`true\`, got: ${JSON.stringify(value)}`,
12497
+ 400
12498
+ );
12499
+ }
12500
+ continue;
12501
+ }
12502
+ validateIdentifier(key, `aggregate column "${key}"`);
12503
+ if (!Array.isArray(value) || value.length === 0) {
12504
+ throw new InputTableError(
12505
+ `Aggregate spec column "${key}" must have a non-empty array of functions`,
12506
+ 400
12507
+ );
12508
+ }
12509
+ for (const fn of value) {
12510
+ if (!VALID_AGGREGATE_FUNCTIONS.has(fn)) {
12511
+ throw new InputTableError(
12512
+ `Unknown aggregate function "${fn}" for column "${key}". Allowed: sum, avg, count, min, max`,
12513
+ 400
12514
+ );
12515
+ }
12516
+ }
12517
+ }
12518
+ }
12519
+ function validateGroupBySpec(groupBy, spec) {
12520
+ const specKeys = new Set(Object.keys(spec).filter((k) => k !== "_count"));
12521
+ if (specKeys.size === 0 && !spec._count) {
12522
+ throw new InputTableError(
12523
+ "groupBy requires at least one aggregate function or `_count: true` in the spec",
12524
+ 400
12525
+ );
12526
+ }
12527
+ for (const col of groupBy) {
12528
+ validateIdentifier(col, `groupBy column "${col}"`);
12529
+ if (specKeys.has(col)) {
12530
+ throw new InputTableError(
12531
+ `groupBy column "${col}" overlaps with aggregate spec key. Use a different column name or separate the queries.`,
12532
+ 400
12533
+ );
12534
+ }
12535
+ }
12536
+ }
12537
+ function buildAggregateSelect(spec, groupBy) {
12538
+ const parts = [];
12539
+ if (groupBy && groupBy.length > 0) {
12540
+ for (const col of groupBy) {
12541
+ parts.push(col);
12542
+ }
12543
+ }
12544
+ for (const [key, value] of Object.entries(spec)) {
12545
+ if (key === "_count") continue;
12546
+ for (const fn of value) {
12547
+ parts.push(`${key}_${fn}:${key}.${fn}()`);
12548
+ }
12549
+ }
12550
+ if (spec._count) {
12551
+ parts.push("count()");
12552
+ }
12553
+ return parts.join(",");
12554
+ }
12555
+ function buildPostgrestOrder(orders) {
12556
+ for (const { field } of orders) {
12557
+ validateIdentifier(field, `order field "${field}"`);
12558
+ }
12559
+ return orders.map(({ field, direction }) => `${field}.${direction}`).join(",");
12560
+ }
12561
+ const RESERVED_PARAMS = /* @__PURE__ */ new Set(["select", "order", "limit", "offset", "on_conflict", "columns", "and", "or", "not"]);
12562
+ function buildPostgrestFilters(wheres) {
12563
+ const params = {};
12564
+ for (const { field, op, value } of wheres) {
12565
+ validateIdentifier(field, `where field "${field}"`);
12566
+ if (RESERVED_PARAMS.has(field)) {
12567
+ throw new InputTableError(
12568
+ `Cannot filter on reserved PostgREST param name "${field}"`,
12569
+ 400
12570
+ );
12571
+ }
12572
+ const pgOp = POSTGREST_OP_MAP[op];
12573
+ let filterStr;
12574
+ if (op === "in" || op === "not-in") {
12575
+ const arr = Array.isArray(value) ? value : [value];
12576
+ filterStr = `${pgOp}.(${arr.map(quotePostgrestValue).join(",")})`;
12577
+ } else {
12578
+ filterStr = `${pgOp}.${quotePostgrestValue(value)}`;
12579
+ }
12580
+ const existing = params[field];
12581
+ if (existing === void 0) {
12582
+ params[field] = filterStr;
12583
+ } else if (Array.isArray(existing)) {
12584
+ existing.push(filterStr);
12585
+ } else {
12586
+ params[field] = [existing, filterStr];
12587
+ }
12588
+ }
12589
+ return params;
12590
+ }
12591
+ function quotePostgrestValue(value) {
12592
+ if (value === null || value === void 0) {
12593
+ throw new InputTableError("Filter value must not be null or undefined", 400);
12594
+ }
12595
+ if (typeof value === "object") {
12596
+ throw new InputTableError(
12597
+ `Filter value must be a primitive, got: ${Array.isArray(value) ? "array" : "object"}`,
12598
+ 400
12599
+ );
12600
+ }
12601
+ const str = String(value);
12602
+ if (/[,.:()\\"']/.test(str)) {
12603
+ const escaped = str.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
12604
+ return `"${escaped}"`;
12605
+ }
12606
+ return str;
12607
+ }
12608
+ function parseRowAggregates(spec, row) {
12609
+ const result = {};
12610
+ for (const [key, value] of Object.entries(spec)) {
12611
+ if (key === "_count") continue;
12612
+ const nested = {};
12613
+ for (const fn of value) {
12614
+ const alias = `${key}_${fn}`;
12615
+ nested[fn] = alias in row ? row[alias] : null;
12616
+ }
12617
+ result[key] = nested;
12618
+ }
12619
+ if (spec._count) {
12620
+ result["_count"] = row["count"] ?? 0;
12621
+ }
12622
+ return result;
12623
+ }
12624
+ function parseAggregateResponse(spec, data) {
12625
+ return parseRowAggregates(spec, data[0] ?? {});
12626
+ }
12627
+ function parseGroupedAggregateResponse(spec, groupBy, data) {
12628
+ return data.map((row) => {
12629
+ const result = parseRowAggregates(spec, row);
12630
+ for (const col of groupBy) {
12631
+ result[col] = row[col];
12632
+ }
12633
+ return result;
12634
+ });
12635
+ }
12032
12636
  class CollectionRefImpl {
12033
12637
  constructor(collection, solutionId, tableName, primaryKeyField, pkColumn, replicationState, getWebsocketProvider) {
12034
12638
  this.collection = collection;
@@ -12039,17 +12643,27 @@ class CollectionRefImpl {
12039
12643
  this.replicationState = replicationState;
12040
12644
  this.getWebsocketProvider = getWebsocketProvider;
12041
12645
  }
12646
+ makeQueryBuilder(initialWhere, initialOrder, initialLimit, initialSkip) {
12647
+ return new QueryBuilderImpl(
12648
+ this.collection,
12649
+ initialWhere,
12650
+ initialOrder,
12651
+ initialLimit,
12652
+ initialSkip,
12653
+ (wheres, orders, limit, skip, spec, groupBy) => this.executeAggregate(wheres, orders, limit, skip, spec, groupBy)
12654
+ );
12655
+ }
12042
12656
  where(field, op, value) {
12043
- return new QueryBuilderImpl(this.collection, { field, op, value });
12657
+ return this.makeQueryBuilder({ field, op, value });
12044
12658
  }
12045
12659
  orderBy(field, direction = "asc") {
12046
- return new QueryBuilderImpl(this.collection, void 0, { field, direction });
12660
+ return this.makeQueryBuilder(void 0, { field, direction });
12047
12661
  }
12048
12662
  limit(count) {
12049
- return new QueryBuilderImpl(this.collection, void 0, void 0, count);
12663
+ return this.makeQueryBuilder(void 0, void 0, count);
12050
12664
  }
12051
12665
  skip(count) {
12052
- return new QueryBuilderImpl(this.collection, void 0, void 0, void 0, count);
12666
+ return this.makeQueryBuilder(void 0, void 0, void 0, count);
12053
12667
  }
12054
12668
  doc(id) {
12055
12669
  return new DocRefImpl(
@@ -12105,6 +12719,58 @@ class CollectionRefImpl {
12105
12719
  });
12106
12720
  return () => rxSub.unsubscribe();
12107
12721
  }
12722
+ async aggregate(spec, options) {
12723
+ return this.executeAggregate([], [], void 0, void 0, spec, options == null ? void 0 : options.groupBy);
12724
+ }
12725
+ async executeAggregate(wheres, orders, limit, skip, spec, groupBy) {
12726
+ var _a, _b, _c;
12727
+ validateAggregateSpec(spec);
12728
+ const activeGroupBy = groupBy && groupBy.length > 0 ? groupBy : void 0;
12729
+ if (activeGroupBy) {
12730
+ validateGroupBySpec(activeGroupBy, spec);
12731
+ }
12732
+ const selectParam = buildAggregateSelect(spec, activeGroupBy);
12733
+ if (!selectParam) {
12734
+ return activeGroupBy ? [] : {};
12735
+ }
12736
+ const filterParams = buildPostgrestFilters(wheres);
12737
+ const params = {
12738
+ select: selectParam,
12739
+ ...filterParams
12740
+ };
12741
+ if (limit !== void 0) params["limit"] = limit;
12742
+ if (skip !== void 0) params["offset"] = skip;
12743
+ if (activeGroupBy && orders.length > 0) {
12744
+ const groupBySet = new Set(activeGroupBy);
12745
+ for (const { field } of orders) {
12746
+ if (!groupBySet.has(field)) {
12747
+ throw new InputTableError(
12748
+ `orderBy field "${field}" must be one of the groupBy columns: [${activeGroupBy.join(", ")}]`,
12749
+ 400
12750
+ );
12751
+ }
12752
+ }
12753
+ params["order"] = buildPostgrestOrder(orders);
12754
+ }
12755
+ let response;
12756
+ try {
12757
+ response = await apiClient.get(
12758
+ `/input-table-v2/${this.solutionId}/rows/${this.tableName}`,
12759
+ { params, headers: getSolutionBranchHeaders() }
12760
+ );
12761
+ } catch (err) {
12762
+ const axiosErr = err;
12763
+ throw new InputTableError(
12764
+ ((_b = (_a = axiosErr.response) == null ? void 0 : _a.data) == null ? void 0 : _b.message) ?? axiosErr.message ?? "Aggregate query failed",
12765
+ ((_c = axiosErr.response) == null ? void 0 : _c.status) ?? 500
12766
+ );
12767
+ }
12768
+ const data = Array.isArray(response.data) ? response.data : [response.data];
12769
+ if (activeGroupBy) {
12770
+ return parseGroupedAggregateResponse(spec, activeGroupBy, data);
12771
+ }
12772
+ return parseAggregateResponse(spec, data);
12773
+ }
12108
12774
  }
12109
12775
  let dbPromise = null;
12110
12776
  function getInputTableDatabase() {
@@ -12457,18 +13123,22 @@ async function evictLeastRecentlyUsed() {
12457
13123
  }
12458
13124
  }
12459
13125
  const SOLUTION_COOKIE_NAME = "x-bluecopa-solution-id";
13126
+ function getCookieValue(name) {
13127
+ try {
13128
+ const cookie = document.cookie.split(";").map((c) => c.trim()).find((c) => c.startsWith(`${name}=`));
13129
+ return cookie ? decodeURIComponent(cookie.split("=").slice(1).join("=")) : null;
13130
+ } catch {
13131
+ return null;
13132
+ }
13133
+ }
12460
13134
  function getSolutionId() {
12461
13135
  const config = getConfig();
12462
13136
  if (config.solutionId) {
12463
13137
  return validateIdentifier(config.solutionId, "solutionId");
12464
13138
  }
12465
- try {
12466
- const cookie = document.cookie.split(";").map((c) => c.trim()).find((c) => c.startsWith(`${SOLUTION_COOKIE_NAME}=`));
12467
- if (cookie) {
12468
- const value = decodeURIComponent(cookie.split("=").slice(1).join("="));
12469
- return validateIdentifier(value, "solutionId (cookie)");
12470
- }
12471
- } catch {
13139
+ const cookieValue = getCookieValue(SOLUTION_COOKIE_NAME);
13140
+ if (cookieValue) {
13141
+ return validateIdentifier(cookieValue, "solutionId (cookie)");
12472
13142
  }
12473
13143
  throw new InputTableError(
12474
13144
  "[copaInputTableDb] No solutionId configured. Set via copaSetConfig({ solutionId }) or cookie.",
@@ -12540,7 +13210,8 @@ const _InputTableDBImpl = class _InputTableDBImpl {
12540
13210
  if (this.schemaPending.has(solutionId)) {
12541
13211
  return this.schemaPending.get(solutionId);
12542
13212
  }
12543
- const pending = apiClient.get(`/input-table-v2/${solutionId}/tables`).then((res) => {
13213
+ const headers = getSolutionBranchHeaders();
13214
+ const pending = apiClient.get(`/input-table-v2/${solutionId}/tables`, { headers }).then((res) => {
12544
13215
  const tables = res.data ?? [];
12545
13216
  this.schemaCache.set(solutionId, { tables, fetchedAt: Date.now() });
12546
13217
  this.schemaPending.delete(solutionId);
@@ -12620,6 +13291,13 @@ class LazyCollectionRef {
12620
13291
  0
12621
13292
  );
12622
13293
  }
13294
+ async aggregate(spec, options) {
13295
+ const ref = await this.getRef();
13296
+ if (options) {
13297
+ return ref.aggregate(spec, options);
13298
+ }
13299
+ return ref.aggregate(spec);
13300
+ }
12623
13301
  }
12624
13302
  class LazyQueryBuilder {
12625
13303
  constructor(resolveBase) {
@@ -12665,6 +13343,13 @@ class LazyQueryBuilder {
12665
13343
  return [];
12666
13344
  }
12667
13345
  }
13346
+ async aggregate(spec, options) {
13347
+ const qb = await this.resolveWithOps();
13348
+ if (options) {
13349
+ return qb.aggregate(spec, options);
13350
+ }
13351
+ return qb.aggregate(spec);
13352
+ }
12668
13353
  }
12669
13354
  class LazyDocRef {
12670
13355
  constructor(id, resolve) {
@@ -12857,6 +13542,6 @@ export {
12857
13542
  copaInputTableDb,
12858
13543
  setConfig as copaSetConfig,
12859
13544
  bluecopaTailwindConfig as copaTailwindConfig,
12860
- index$i as copaUtils
13545
+ index$j as copaUtils
12861
13546
  };
12862
13547
  //# sourceMappingURL=index.es.js.map