@cycleplatform/api-client-typescript 0.3.0 → 0.3.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.
package/dist/index.js CHANGED
@@ -1,316 +1,455 @@
1
- const H = {
2
- "Content-Type": "application/json"
3
- }, D = /\{[^{}]+\}/g;
4
- class I extends Request {
5
- constructor(t, e) {
6
- super(t, e);
7
- for (const n in e)
8
- n in this || (this[n] = e[n]);
9
- }
10
- }
11
- function P() {
1
+ const B = /\{[^{}]+\}/g, F = () => {
2
+ var r, t;
3
+ return typeof process == "object" && Number.parseInt((t = (r = process == null ? void 0 : process.versions) == null ? void 0 : r.node) == null ? void 0 : t.substring(0, 2)) >= 18 && process.versions.undici;
4
+ };
5
+ function M() {
12
6
  return Math.random().toString(36).slice(2, 11);
13
7
  }
14
- function L(s) {
8
+ function W(r) {
15
9
  let {
16
10
  baseUrl: t = "",
17
- fetch: e = globalThis.fetch,
11
+ Request: e = globalThis.Request,
12
+ fetch: s = globalThis.fetch,
18
13
  querySerializer: n,
19
- bodySerializer: r,
14
+ bodySerializer: o,
20
15
  headers: i,
21
- ...a
22
- } = { ...s };
23
- t.endsWith("/") && (t = t.substring(0, t.length - 1)), i = x(H, i);
24
- const l = [];
25
- async function u(c, o) {
16
+ requestInitExt: l = void 0,
17
+ ...y
18
+ } = { ...r };
19
+ l = F() ? l : void 0, t = U(t);
20
+ const h = [];
21
+ async function p(c, a) {
26
22
  const {
27
- fetch: b = e,
28
- headers: O,
29
- params: m = {},
30
- parseAs: j = "json",
31
- querySerializer: R,
32
- bodySerializer: z = r ?? F,
33
- ...U
34
- } = o || {};
35
- let g = typeof n == "function" ? n : T(n);
36
- R && (g = typeof R == "function" ? R : T({
23
+ baseUrl: m,
24
+ fetch: I = s,
25
+ Request: Y = e,
26
+ headers: J,
27
+ params: g = {},
28
+ parseAs: A = "json",
29
+ querySerializer: j,
30
+ bodySerializer: k = o ?? Q,
31
+ body: v,
32
+ ...x
33
+ } = a || {};
34
+ m && (t = U(m));
35
+ let z = typeof n == "function" ? n : O(n);
36
+ j && (z = typeof j == "function" ? j : O({
37
37
  ...typeof n == "object" ? n : {},
38
- ...R
38
+ ...j
39
39
  }));
40
- const h = {
40
+ const q = v === void 0 ? void 0 : k(v), L = (
41
+ // with no body, we should not to set Content-Type
42
+ q === void 0 || // if serialized body is FormData; browser will correctly set Content-Type & boundary expression
43
+ q instanceof FormData ? {} : {
44
+ "Content-Type": "application/json"
45
+ }
46
+ ), N = {
41
47
  redirect: "follow",
42
- ...a,
43
- ...U,
44
- headers: x(i, O, m.header)
48
+ ...y,
49
+ ...x,
50
+ body: q,
51
+ headers: X(L, i, J, g.header)
45
52
  };
46
- h.body && (h.body = z(h.body)), h.body instanceof FormData && h.headers.delete("Content-Type");
47
- let $, A, w = new I(N(c, { baseUrl: t, params: m, querySerializer: g }), h);
48
- if (l.length) {
49
- $ = P(), A = Object.freeze({
53
+ let E, S, b = new e(_(c, { baseUrl: t, params: g, querySerializer: z }), N);
54
+ for (const u in x)
55
+ u in b || (b[u] = x[u]);
56
+ if (h.length) {
57
+ E = M(), S = Object.freeze({
50
58
  baseUrl: t,
51
- fetch: b,
52
- parseAs: j,
53
- querySerializer: g,
54
- bodySerializer: z
59
+ fetch: I,
60
+ parseAs: A,
61
+ querySerializer: z,
62
+ bodySerializer: k
55
63
  });
56
- for (const d of l)
57
- if (d && typeof d == "object" && typeof d.onRequest == "function") {
58
- const y = await d.onRequest({
59
- request: w,
64
+ for (const u of h)
65
+ if (u && typeof u == "object" && typeof u.onRequest == "function") {
66
+ const d = await u.onRequest({
67
+ request: b,
60
68
  schemaPath: c,
61
- params: m,
62
- options: A,
63
- id: $
69
+ params: g,
70
+ options: S,
71
+ id: E
64
72
  });
65
- if (y) {
66
- if (!(y instanceof Request))
73
+ if (d) {
74
+ if (!(d instanceof e))
67
75
  throw new Error("onRequest: must return new Request() when modifying the request");
68
- w = y;
76
+ b = d;
77
+ }
78
+ }
79
+ }
80
+ let f;
81
+ try {
82
+ f = await I(b, l);
83
+ } catch (u) {
84
+ let d = u;
85
+ if (h.length)
86
+ for (let w = h.length - 1; w >= 0; w--) {
87
+ const T = h[w];
88
+ if (T && typeof T == "object" && typeof T.onError == "function") {
89
+ const R = await T.onError({
90
+ request: b,
91
+ error: d,
92
+ schemaPath: c,
93
+ params: g,
94
+ options: S,
95
+ id: E
96
+ });
97
+ if (R) {
98
+ if (R instanceof Response) {
99
+ d = void 0, f = R;
100
+ break;
101
+ }
102
+ if (R instanceof Error) {
103
+ d = R;
104
+ continue;
105
+ }
106
+ throw new Error("onError: must return new Response() or instance of Error");
107
+ }
69
108
  }
70
109
  }
110
+ if (d)
111
+ throw d;
71
112
  }
72
- let f = await b(w);
73
- if (l.length)
74
- for (let d = l.length - 1; d >= 0; d--) {
75
- const y = l[d];
76
- if (y && typeof y == "object" && typeof y.onResponse == "function") {
77
- const E = await y.onResponse({
78
- request: w,
113
+ if (h.length)
114
+ for (let u = h.length - 1; u >= 0; u--) {
115
+ const d = h[u];
116
+ if (d && typeof d == "object" && typeof d.onResponse == "function") {
117
+ const w = await d.onResponse({
118
+ request: b,
79
119
  response: f,
80
120
  schemaPath: c,
81
- params: m,
82
- options: A,
83
- id: $
121
+ params: g,
122
+ options: S,
123
+ id: E
84
124
  });
85
- if (E) {
86
- if (!(E instanceof Response))
125
+ if (w) {
126
+ if (!(w instanceof Response))
87
127
  throw new Error("onResponse: must return new Response() when modifying the response");
88
- f = E;
128
+ f = w;
89
129
  }
90
130
  }
91
131
  }
92
132
  if (f.status === 204 || f.headers.get("Content-Length") === "0")
93
- return f.ok ? { data: {}, response: f } : { error: {}, response: f };
133
+ return f.ok ? { data: void 0, response: f } : { error: void 0, response: f };
94
134
  if (f.ok)
95
- return j === "stream" ? { data: f.body, response: f } : { data: await f[j](), response: f };
96
- let S = await f.text();
135
+ return A === "stream" ? { data: f.body, response: f } : { data: await f[A](), response: f };
136
+ let C = await f.text();
97
137
  try {
98
- S = JSON.parse(S);
138
+ C = JSON.parse(C);
99
139
  } catch {
100
140
  }
101
- return { error: S, response: f };
141
+ return { error: C, response: f };
102
142
  }
103
143
  return {
144
+ request(c, a, m) {
145
+ return p(a, { ...m, method: c.toUpperCase() });
146
+ },
104
147
  /** Call a GET endpoint */
105
- async GET(c, o) {
106
- return u(c, { ...o, method: "GET" });
148
+ GET(c, a) {
149
+ return p(c, { ...a, method: "GET" });
107
150
  },
108
151
  /** Call a PUT endpoint */
109
- async PUT(c, o) {
110
- return u(c, { ...o, method: "PUT" });
152
+ PUT(c, a) {
153
+ return p(c, { ...a, method: "PUT" });
111
154
  },
112
155
  /** Call a POST endpoint */
113
- async POST(c, o) {
114
- return u(c, { ...o, method: "POST" });
156
+ POST(c, a) {
157
+ return p(c, { ...a, method: "POST" });
115
158
  },
116
159
  /** Call a DELETE endpoint */
117
- async DELETE(c, o) {
118
- return u(c, { ...o, method: "DELETE" });
160
+ DELETE(c, a) {
161
+ return p(c, { ...a, method: "DELETE" });
119
162
  },
120
163
  /** Call a OPTIONS endpoint */
121
- async OPTIONS(c, o) {
122
- return u(c, { ...o, method: "OPTIONS" });
164
+ OPTIONS(c, a) {
165
+ return p(c, { ...a, method: "OPTIONS" });
123
166
  },
124
167
  /** Call a HEAD endpoint */
125
- async HEAD(c, o) {
126
- return u(c, { ...o, method: "HEAD" });
168
+ HEAD(c, a) {
169
+ return p(c, { ...a, method: "HEAD" });
127
170
  },
128
171
  /** Call a PATCH endpoint */
129
- async PATCH(c, o) {
130
- return u(c, { ...o, method: "PATCH" });
172
+ PATCH(c, a) {
173
+ return p(c, { ...a, method: "PATCH" });
131
174
  },
132
175
  /** Call a TRACE endpoint */
133
- async TRACE(c, o) {
134
- return u(c, { ...o, method: "TRACE" });
176
+ TRACE(c, a) {
177
+ return p(c, { ...a, method: "TRACE" });
135
178
  },
136
179
  /** Register middleware */
137
180
  use(...c) {
138
- for (const o of c)
139
- if (o) {
140
- if (typeof o != "object" || !("onRequest" in o || "onResponse" in o))
141
- throw new Error("Middleware must be an object with one of `onRequest()` or `onResponse()`");
142
- l.push(o);
181
+ for (const a of c)
182
+ if (a) {
183
+ if (typeof a != "object" || !("onRequest" in a || "onResponse" in a || "onError" in a))
184
+ throw new Error("Middleware must be an object with one of `onRequest()`, `onResponse() or `onError()`");
185
+ h.push(a);
143
186
  }
144
187
  },
145
188
  /** Unregister middleware */
146
189
  eject(...c) {
147
- for (const o of c) {
148
- const b = l.indexOf(o);
149
- b !== -1 && l.splice(b, 1);
190
+ for (const a of c) {
191
+ const m = h.indexOf(a);
192
+ m !== -1 && h.splice(m, 1);
150
193
  }
151
194
  }
152
195
  };
153
196
  }
154
- function p(s, t, e) {
197
+ function $(r, t, e) {
155
198
  if (t == null)
156
199
  return "";
157
200
  if (typeof t == "object")
158
201
  throw new Error(
159
202
  "Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these."
160
203
  );
161
- return `${s}=${(e == null ? void 0 : e.allowReserved) === !0 ? t : encodeURIComponent(t)}`;
204
+ return `${r}=${(e == null ? void 0 : e.allowReserved) === !0 ? t : encodeURIComponent(t)}`;
162
205
  }
163
- function q(s, t, e) {
206
+ function H(r, t, e) {
164
207
  if (!t || typeof t != "object")
165
208
  return "";
166
- const n = [], r = {
209
+ const s = [], n = {
167
210
  simple: ",",
168
211
  label: ".",
169
212
  matrix: ";"
170
213
  }[e.style] || "&";
171
214
  if (e.style !== "deepObject" && e.explode === !1) {
172
215
  for (const l in t)
173
- n.push(l, e.allowReserved === !0 ? t[l] : encodeURIComponent(t[l]));
174
- const a = n.join(",");
216
+ s.push(l, e.allowReserved === !0 ? t[l] : encodeURIComponent(t[l]));
217
+ const i = s.join(",");
175
218
  switch (e.style) {
176
219
  case "form":
177
- return `${s}=${a}`;
220
+ return `${r}=${i}`;
178
221
  case "label":
179
- return `.${a}`;
222
+ return `.${i}`;
180
223
  case "matrix":
181
- return `;${s}=${a}`;
224
+ return `;${r}=${i}`;
182
225
  default:
183
- return a;
226
+ return i;
184
227
  }
185
228
  }
186
- for (const a in t) {
187
- const l = e.style === "deepObject" ? `${s}[${a}]` : a;
188
- n.push(p(l, t[a], e));
229
+ for (const i in t) {
230
+ const l = e.style === "deepObject" ? `${r}[${i}]` : i;
231
+ s.push($(l, t[i], e));
189
232
  }
190
- const i = n.join(r);
191
- return e.style === "label" || e.style === "matrix" ? `${r}${i}` : i;
233
+ const o = s.join(n);
234
+ return e.style === "label" || e.style === "matrix" ? `${n}${o}` : o;
192
235
  }
193
- function C(s, t, e) {
236
+ function P(r, t, e) {
194
237
  if (!Array.isArray(t))
195
238
  return "";
196
239
  if (e.explode === !1) {
197
- const i = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[e.style] || ",", a = (e.allowReserved === !0 ? t : t.map((l) => encodeURIComponent(l))).join(i);
240
+ const o = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[e.style] || ",", i = (e.allowReserved === !0 ? t : t.map((l) => encodeURIComponent(l))).join(o);
198
241
  switch (e.style) {
199
242
  case "simple":
200
- return a;
243
+ return i;
201
244
  case "label":
202
- return `.${a}`;
245
+ return `.${i}`;
203
246
  case "matrix":
204
- return `;${s}=${a}`;
247
+ return `;${r}=${i}`;
248
+ // case "spaceDelimited":
249
+ // case "pipeDelimited":
205
250
  default:
206
- return `${s}=${a}`;
251
+ return `${r}=${i}`;
207
252
  }
208
253
  }
209
- const n = { simple: ",", label: ".", matrix: ";" }[e.style] || "&", r = [];
210
- for (const i of t)
211
- e.style === "simple" || e.style === "label" ? r.push(e.allowReserved === !0 ? i : encodeURIComponent(i)) : r.push(p(s, i, e));
212
- return e.style === "label" || e.style === "matrix" ? `${n}${r.join(n)}` : r.join(n);
254
+ const s = { simple: ",", label: ".", matrix: ";" }[e.style] || "&", n = [];
255
+ for (const o of t)
256
+ e.style === "simple" || e.style === "label" ? n.push(e.allowReserved === !0 ? o : encodeURIComponent(o)) : n.push($(r, o, e));
257
+ return e.style === "label" || e.style === "matrix" ? `${s}${n.join(s)}` : n.join(s);
213
258
  }
214
- function T(s) {
259
+ function O(r) {
215
260
  return function(e) {
216
- const n = [];
261
+ const s = [];
217
262
  if (e && typeof e == "object")
218
- for (const r in e) {
219
- const i = e[r];
220
- if (i != null) {
221
- if (Array.isArray(i)) {
222
- n.push(
223
- C(r, i, {
263
+ for (const n in e) {
264
+ const o = e[n];
265
+ if (o != null) {
266
+ if (Array.isArray(o)) {
267
+ if (o.length === 0)
268
+ continue;
269
+ s.push(
270
+ P(n, o, {
224
271
  style: "form",
225
272
  explode: !0,
226
- ...s == null ? void 0 : s.array,
227
- allowReserved: (s == null ? void 0 : s.allowReserved) || !1
273
+ ...r == null ? void 0 : r.array,
274
+ allowReserved: (r == null ? void 0 : r.allowReserved) || !1
228
275
  })
229
276
  );
230
277
  continue;
231
278
  }
232
- if (typeof i == "object") {
233
- n.push(
234
- q(r, i, {
279
+ if (typeof o == "object") {
280
+ s.push(
281
+ H(n, o, {
235
282
  style: "deepObject",
236
283
  explode: !0,
237
- ...s == null ? void 0 : s.object,
238
- allowReserved: (s == null ? void 0 : s.allowReserved) || !1
284
+ ...r == null ? void 0 : r.object,
285
+ allowReserved: (r == null ? void 0 : r.allowReserved) || !1
239
286
  })
240
287
  );
241
288
  continue;
242
289
  }
243
- n.push(p(r, i, s));
290
+ s.push($(n, o, r));
244
291
  }
245
292
  }
246
- return n.join("&");
293
+ return s.join("&");
247
294
  };
248
295
  }
249
- function k(s, t) {
250
- let e = s;
251
- for (const n of s.match(D) ?? []) {
252
- let r = n.substring(1, n.length - 1), i = !1, a = "simple";
253
- if (r.endsWith("*") && (i = !0, r = r.substring(0, r.length - 1)), r.startsWith(".") ? (a = "label", r = r.substring(1)) : r.startsWith(";") && (a = "matrix", r = r.substring(1)), !t || t[r] === void 0 || t[r] === null)
296
+ function G(r, t) {
297
+ let e = r;
298
+ for (const s of r.match(B) ?? []) {
299
+ let n = s.substring(1, s.length - 1), o = !1, i = "simple";
300
+ if (n.endsWith("*") && (o = !0, n = n.substring(0, n.length - 1)), n.startsWith(".") ? (i = "label", n = n.substring(1)) : n.startsWith(";") && (i = "matrix", n = n.substring(1)), !t || t[n] === void 0 || t[n] === null)
254
301
  continue;
255
- const l = t[r];
302
+ const l = t[n];
256
303
  if (Array.isArray(l)) {
257
- e = e.replace(n, C(r, l, { style: a, explode: i }));
304
+ e = e.replace(s, P(n, l, { style: i, explode: o }));
258
305
  continue;
259
306
  }
260
307
  if (typeof l == "object") {
261
- e = e.replace(n, q(r, l, { style: a, explode: i }));
308
+ e = e.replace(s, H(n, l, { style: i, explode: o }));
262
309
  continue;
263
310
  }
264
- if (a === "matrix") {
265
- e = e.replace(n, `;${p(r, l)}`);
311
+ if (i === "matrix") {
312
+ e = e.replace(s, `;${$(n, l)}`);
266
313
  continue;
267
314
  }
268
- e = e.replace(n, a === "label" ? `.${encodeURIComponent(l)}` : encodeURIComponent(l));
315
+ e = e.replace(s, i === "label" ? `.${encodeURIComponent(l)}` : encodeURIComponent(l));
269
316
  }
270
317
  return e;
271
318
  }
272
- function F(s) {
273
- return JSON.stringify(s);
319
+ function Q(r) {
320
+ return r instanceof FormData ? r : JSON.stringify(r);
274
321
  }
275
- function N(s, t) {
276
- var r;
277
- let e = `${t.baseUrl}${s}`;
278
- (r = t.params) != null && r.path && (e = k(e, t.params.path));
279
- let n = t.querySerializer(t.params.query ?? {});
280
- return n.startsWith("?") && (n = n.substring(1)), n && (e += `?${n}`), e;
322
+ function _(r, t) {
323
+ var n;
324
+ let e = `${t.baseUrl}${r}`;
325
+ (n = t.params) != null && n.path && (e = G(e, t.params.path));
326
+ let s = t.querySerializer(t.params.query ?? {});
327
+ return s.startsWith("?") && (s = s.substring(1)), s && (e += `?${s}`), e;
281
328
  }
282
- function x(...s) {
329
+ function X(...r) {
283
330
  const t = new Headers();
284
- for (const e of s) {
331
+ for (const e of r) {
285
332
  if (!e || typeof e != "object")
286
333
  continue;
287
- const n = e instanceof Headers ? e.entries() : Object.entries(e);
288
- for (const [r, i] of n)
289
- if (i === null)
290
- t.delete(r);
291
- else if (Array.isArray(i))
292
- for (const a of i)
293
- t.append(r, a);
294
- else i !== void 0 && t.set(r, i);
334
+ const s = e instanceof Headers ? e.entries() : Object.entries(e);
335
+ for (const [n, o] of s)
336
+ if (o === null)
337
+ t.delete(n);
338
+ else if (Array.isArray(o))
339
+ for (const i of o)
340
+ t.append(n, i);
341
+ else o !== void 0 && t.set(n, o);
295
342
  }
296
343
  return t;
297
344
  }
298
- function W({
299
- apiKey: s,
345
+ function U(r) {
346
+ return r.endsWith("/") ? r.substring(0, r.length - 1) : r;
347
+ }
348
+ class Z extends EventTarget {
349
+ constructor(t, e, s) {
350
+ super(), this.promise = this.track(t, e, s);
351
+ }
352
+ async track(t, e, s) {
353
+ const n = typeof e == "object" ? e.id : e, o = {
354
+ pollingInterval: 2e3,
355
+ ...s
356
+ };
357
+ let i = 0;
358
+ for (; ; ) {
359
+ const l = await t.GET("/v1/jobs/{jobId}", {
360
+ params: { path: { jobId: n } }
361
+ });
362
+ if (l.error) {
363
+ if (i < 5 && l.error.error.code === "404.job") {
364
+ i++, await this.delay(o.pollingInterval);
365
+ continue;
366
+ }
367
+ throw l.error;
368
+ }
369
+ const y = l.data.data, h = V(y);
370
+ if (this.dispatchEvent(
371
+ new CustomEvent("progress", {
372
+ detail: { ...h, state: y.state.current }
373
+ })
374
+ ), K(y)) {
375
+ if (y.state.current === "error")
376
+ throw y;
377
+ return this.dispatchEvent(
378
+ new CustomEvent("done", {
379
+ detail: y
380
+ })
381
+ ), y;
382
+ }
383
+ await this.delay(o.pollingInterval);
384
+ }
385
+ }
386
+ delay(t = 2e3) {
387
+ return new Promise((e) => setTimeout(e, t));
388
+ }
389
+ }
390
+ function K(r) {
391
+ return [
392
+ "completed",
393
+ "error",
394
+ "expired"
395
+ ].includes(r.state.current);
396
+ }
397
+ const D = "0001-01-01T00:00:00Z";
398
+ function V(r) {
399
+ if (!r.tasks || r.tasks.length === 0)
400
+ return { total: 0, completed: 0, failed: 0, percent: 100 };
401
+ const t = r.tasks.reduce(
402
+ (e, s) => {
403
+ var o, i, l;
404
+ const n = ((o = s.steps) == null ? void 0 : o.length) || 1;
405
+ if (e.total += n, s.state.current === "pending")
406
+ return e;
407
+ if (!((i = s.steps) != null && i.length)) {
408
+ switch (s.state.current) {
409
+ case "completed":
410
+ e.completed += 1;
411
+ break;
412
+ case "error":
413
+ e.failed += 1;
414
+ }
415
+ return e;
416
+ }
417
+ return (l = s.steps) == null || l.forEach((y) => {
418
+ if (y.completed !== D) {
419
+ e.completed += 1;
420
+ return;
421
+ }
422
+ ["error", "completed"].includes(s.state.current || "") && y.started !== D && (e.failed += 1);
423
+ }), e;
424
+ },
425
+ { total: 0, completed: 0, failed: 0 }
426
+ );
427
+ return {
428
+ ...t,
429
+ percent: t.completed / t.total * 100
430
+ };
431
+ }
432
+ function ee(r, t, e) {
433
+ return new Z(r, t, e);
434
+ }
435
+ function te({
436
+ apiKey: r,
300
437
  baseUrl: t = "https://api.cycle.io",
301
438
  hubId: e,
302
- fetch: n
439
+ fetch: s
303
440
  }) {
304
- const r = L({
441
+ const n = W({
305
442
  baseUrl: t,
306
- fetch: n || fetch
307
- }), i = {
308
- async onRequest({ request: a }) {
309
- return a.headers.set("Authorization", `Bearer ${s}`), a.headers.set("X-Hub-Id", e), a;
443
+ fetch: s || fetch
444
+ }), o = {
445
+ async onRequest({ request: i }) {
446
+ return i.headers.set("Authorization", `Bearer ${r}`), i.headers.set("X-Hub-Id", e), i;
310
447
  }
311
448
  };
312
- return r.use(i), r;
449
+ return n.use(o), n;
313
450
  }
314
451
  export {
315
- W as getClient
452
+ te as getClient,
453
+ V as getJobProgress,
454
+ ee as trackJob
316
455
  };