@cycleplatform/api-client-typescript 0.2.5 → 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,314 +1,455 @@
1
- const D = {
2
- "Content-Type": "application/json"
3
- }, H = /\{[^{}]+\}/g;
4
- function U(n) {
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() {
6
+ return Math.random().toString(36).slice(2, 11);
7
+ }
8
+ function W(r) {
5
9
  let {
6
10
  baseUrl: t = "",
7
- fetch: e = globalThis.fetch,
8
- querySerializer: s,
9
- bodySerializer: r,
11
+ Request: e = globalThis.Request,
12
+ fetch: s = globalThis.fetch,
13
+ querySerializer: n,
14
+ bodySerializer: o,
10
15
  headers: i,
11
- ...a
12
- } = { ...n };
13
- t.endsWith("/") && (t = t.substring(0, t.length - 1)), i = z(D, i);
14
- const c = [];
15
- async function u(o, l) {
16
- let {
17
- fetch: m = e,
18
- headers: q,
19
- params: p = {},
20
- parseAs: j = "json",
21
- querySerializer: w,
22
- bodySerializer: S = r ?? I,
23
- ...C
24
- } = l || {}, $ = typeof s == "function" ? s : T(s);
25
- w && ($ = typeof w == "function" ? w : T({
26
- ...typeof s == "object" ? s : {},
27
- ...w
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) {
22
+ const {
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
+ ...typeof n == "object" ? n : {},
38
+ ...j
28
39
  }));
29
- 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 = {
30
47
  redirect: "follow",
31
- ...a,
32
- ...C,
33
- headers: z(i, q, p.header)
34
- };
35
- h.body && (h.body = S(h.body)), h.body instanceof FormData && h.headers.delete("Content-Type");
36
- let b = new Request(
37
- L(o, { baseUrl: t, params: p, querySerializer: $ }),
38
- h
39
- );
40
- const E = {
41
- baseUrl: t,
42
- fetch: m,
43
- parseAs: j,
44
- querySerializer: $,
45
- bodySerializer: S
48
+ ...y,
49
+ ...x,
50
+ body: q,
51
+ headers: X(L, i, J, g.header)
46
52
  };
47
- for (const d of c)
48
- if (d && typeof d == "object" && typeof d.onRequest == "function") {
49
- b.schemaPath = o, b.params = p;
50
- const y = await d.onRequest(b, E);
51
- if (y) {
52
- if (!(y instanceof Request))
53
- throw new Error(
54
- "Middleware must return new Request() when modifying the request"
55
- );
56
- b = y;
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({
58
+ baseUrl: t,
59
+ fetch: I,
60
+ parseAs: A,
61
+ querySerializer: z,
62
+ bodySerializer: k
63
+ });
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,
68
+ schemaPath: c,
69
+ params: g,
70
+ options: S,
71
+ id: E
72
+ });
73
+ if (d) {
74
+ if (!(d instanceof e))
75
+ throw new Error("onRequest: must return new Request() when modifying the request");
76
+ b = d;
77
+ }
57
78
  }
58
- }
59
- let f = await m(b);
60
- for (let d = c.length - 1; d >= 0; d--) {
61
- const y = c[d];
62
- if (y && typeof y == "object" && typeof y.onResponse == "function") {
63
- const A = await y.onResponse(f, E);
64
- if (A) {
65
- if (!(A instanceof Response))
66
- throw new Error(
67
- "Middleware must return new Response() when modifying the response"
68
- );
69
- f = A;
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
+ }
108
+ }
70
109
  }
71
- }
110
+ if (d)
111
+ throw d;
72
112
  }
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,
119
+ response: f,
120
+ schemaPath: c,
121
+ params: g,
122
+ options: S,
123
+ id: E
124
+ });
125
+ if (w) {
126
+ if (!(w instanceof Response))
127
+ throw new Error("onResponse: must return new Response() when modifying the response");
128
+ f = w;
129
+ }
130
+ }
131
+ }
73
132
  if (f.status === 204 || f.headers.get("Content-Length") === "0")
74
- return f.ok ? { data: {}, response: f } : { error: {}, response: f };
133
+ return f.ok ? { data: void 0, response: f } : { error: void 0, response: f };
75
134
  if (f.ok)
76
- return j === "stream" ? { data: f.body, response: f } : { data: await f[j](), response: f };
77
- let g = await f.text();
135
+ return A === "stream" ? { data: f.body, response: f } : { data: await f[A](), response: f };
136
+ let C = await f.text();
78
137
  try {
79
- g = JSON.parse(g);
138
+ C = JSON.parse(C);
80
139
  } catch {
81
140
  }
82
- return { error: g, response: f };
141
+ return { error: C, response: f };
83
142
  }
84
143
  return {
144
+ request(c, a, m) {
145
+ return p(a, { ...m, method: c.toUpperCase() });
146
+ },
85
147
  /** Call a GET endpoint */
86
- async GET(o, l) {
87
- return u(o, { ...l, method: "GET" });
148
+ GET(c, a) {
149
+ return p(c, { ...a, method: "GET" });
88
150
  },
89
151
  /** Call a PUT endpoint */
90
- async PUT(o, l) {
91
- return u(o, { ...l, method: "PUT" });
152
+ PUT(c, a) {
153
+ return p(c, { ...a, method: "PUT" });
92
154
  },
93
155
  /** Call a POST endpoint */
94
- async POST(o, l) {
95
- return u(o, { ...l, method: "POST" });
156
+ POST(c, a) {
157
+ return p(c, { ...a, method: "POST" });
96
158
  },
97
159
  /** Call a DELETE endpoint */
98
- async DELETE(o, l) {
99
- return u(o, { ...l, method: "DELETE" });
160
+ DELETE(c, a) {
161
+ return p(c, { ...a, method: "DELETE" });
100
162
  },
101
163
  /** Call a OPTIONS endpoint */
102
- async OPTIONS(o, l) {
103
- return u(o, { ...l, method: "OPTIONS" });
164
+ OPTIONS(c, a) {
165
+ return p(c, { ...a, method: "OPTIONS" });
104
166
  },
105
167
  /** Call a HEAD endpoint */
106
- async HEAD(o, l) {
107
- return u(o, { ...l, method: "HEAD" });
168
+ HEAD(c, a) {
169
+ return p(c, { ...a, method: "HEAD" });
108
170
  },
109
171
  /** Call a PATCH endpoint */
110
- async PATCH(o, l) {
111
- return u(o, { ...l, method: "PATCH" });
172
+ PATCH(c, a) {
173
+ return p(c, { ...a, method: "PATCH" });
112
174
  },
113
175
  /** Call a TRACE endpoint */
114
- async TRACE(o, l) {
115
- return u(o, { ...l, method: "TRACE" });
176
+ TRACE(c, a) {
177
+ return p(c, { ...a, method: "TRACE" });
116
178
  },
117
179
  /** Register middleware */
118
- use(...o) {
119
- for (const l of o)
120
- if (l) {
121
- if (typeof l != "object" || !("onRequest" in l || "onResponse" in l))
122
- throw new Error(
123
- "Middleware must be an object with one of `onRequest()` or `onResponse()`"
124
- );
125
- c.push(l);
180
+ use(...c) {
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);
126
186
  }
127
187
  },
128
188
  /** Unregister middleware */
129
- eject(...o) {
130
- for (const l of o) {
131
- const m = c.indexOf(l);
132
- m !== -1 && c.splice(m, 1);
189
+ eject(...c) {
190
+ for (const a of c) {
191
+ const m = h.indexOf(a);
192
+ m !== -1 && h.splice(m, 1);
133
193
  }
134
194
  }
135
195
  };
136
196
  }
137
- function R(n, t, e) {
197
+ function $(r, t, e) {
138
198
  if (t == null)
139
199
  return "";
140
200
  if (typeof t == "object")
141
201
  throw new Error(
142
202
  "Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these."
143
203
  );
144
- return `${n}=${(e == null ? void 0 : e.allowReserved) === !0 ? t : encodeURIComponent(t)}`;
204
+ return `${r}=${(e == null ? void 0 : e.allowReserved) === !0 ? t : encodeURIComponent(t)}`;
145
205
  }
146
- function x(n, t, e) {
206
+ function H(r, t, e) {
147
207
  if (!t || typeof t != "object")
148
208
  return "";
149
- const s = [], r = {
209
+ const s = [], n = {
150
210
  simple: ",",
151
211
  label: ".",
152
212
  matrix: ";"
153
213
  }[e.style] || "&";
154
214
  if (e.style !== "deepObject" && e.explode === !1) {
155
- for (const c in t)
156
- s.push(
157
- c,
158
- e.allowReserved === !0 ? t[c] : encodeURIComponent(t[c])
159
- );
160
- const a = s.join(",");
215
+ for (const l in t)
216
+ s.push(l, e.allowReserved === !0 ? t[l] : encodeURIComponent(t[l]));
217
+ const i = s.join(",");
161
218
  switch (e.style) {
162
219
  case "form":
163
- return `${n}=${a}`;
220
+ return `${r}=${i}`;
164
221
  case "label":
165
- return `.${a}`;
222
+ return `.${i}`;
166
223
  case "matrix":
167
- return `;${n}=${a}`;
224
+ return `;${r}=${i}`;
168
225
  default:
169
- return a;
226
+ return i;
170
227
  }
171
228
  }
172
- for (const a in t) {
173
- const c = e.style === "deepObject" ? `${n}[${a}]` : a;
174
- s.push(R(c, 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));
175
232
  }
176
- const i = s.join(r);
177
- 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;
178
235
  }
179
- function O(n, t, e) {
236
+ function P(r, t, e) {
180
237
  if (!Array.isArray(t))
181
238
  return "";
182
239
  if (e.explode === !1) {
183
- const i = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[e.style] || ",", a = (e.allowReserved === !0 ? t : t.map((c) => encodeURIComponent(c))).join(i);
240
+ const o = { form: ",", spaceDelimited: "%20", pipeDelimited: "|" }[e.style] || ",", i = (e.allowReserved === !0 ? t : t.map((l) => encodeURIComponent(l))).join(o);
184
241
  switch (e.style) {
185
242
  case "simple":
186
- return a;
243
+ return i;
187
244
  case "label":
188
- return `.${a}`;
245
+ return `.${i}`;
189
246
  case "matrix":
190
- return `;${n}=${a}`;
191
- case "spaceDelimited":
192
- case "pipeDelimited":
247
+ return `;${r}=${i}`;
248
+ // case "spaceDelimited":
249
+ // case "pipeDelimited":
193
250
  default:
194
- return `${n}=${a}`;
251
+ return `${r}=${i}`;
195
252
  }
196
253
  }
197
- const s = { simple: ",", label: ".", matrix: ";" }[e.style] || "&", r = [];
198
- for (const i of t)
199
- e.style === "simple" || e.style === "label" ? r.push(e.allowReserved === !0 ? i : encodeURIComponent(i)) : r.push(R(n, i, e));
200
- return e.style === "label" || e.style === "matrix" ? `${s}${r.join(s)}` : r.join(s);
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);
201
258
  }
202
- function T(n) {
259
+ function O(r) {
203
260
  return function(e) {
204
261
  const s = [];
205
262
  if (e && typeof e == "object")
206
- for (const r in e) {
207
- const i = e[r];
208
- if (i != null) {
209
- if (Array.isArray(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;
210
269
  s.push(
211
- O(r, i, {
270
+ P(n, o, {
212
271
  style: "form",
213
272
  explode: !0,
214
- ...n == null ? void 0 : n.array,
215
- allowReserved: (n == null ? void 0 : n.allowReserved) || !1
273
+ ...r == null ? void 0 : r.array,
274
+ allowReserved: (r == null ? void 0 : r.allowReserved) || !1
216
275
  })
217
276
  );
218
277
  continue;
219
278
  }
220
- if (typeof i == "object") {
279
+ if (typeof o == "object") {
221
280
  s.push(
222
- x(r, i, {
281
+ H(n, o, {
223
282
  style: "deepObject",
224
283
  explode: !0,
225
- ...n == null ? void 0 : n.object,
226
- allowReserved: (n == null ? void 0 : n.allowReserved) || !1
284
+ ...r == null ? void 0 : r.object,
285
+ allowReserved: (r == null ? void 0 : r.allowReserved) || !1
227
286
  })
228
287
  );
229
288
  continue;
230
289
  }
231
- s.push(R(r, i, n));
290
+ s.push($(n, o, r));
232
291
  }
233
292
  }
234
293
  return s.join("&");
235
294
  };
236
295
  }
237
- function P(n, t) {
238
- let e = n;
239
- for (const s of n.match(H) ?? []) {
240
- let r = s.substring(1, s.length - 1), i = !1, a = "simple";
241
- 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)
242
301
  continue;
243
- const c = t[r];
244
- if (Array.isArray(c)) {
245
- e = e.replace(
246
- s,
247
- O(r, c, { style: a, explode: i })
248
- );
302
+ const l = t[n];
303
+ if (Array.isArray(l)) {
304
+ e = e.replace(s, P(n, l, { style: i, explode: o }));
249
305
  continue;
250
306
  }
251
- if (typeof c == "object") {
252
- e = e.replace(
253
- s,
254
- x(r, c, { style: a, explode: i })
255
- );
307
+ if (typeof l == "object") {
308
+ e = e.replace(s, H(n, l, { style: i, explode: o }));
256
309
  continue;
257
310
  }
258
- if (a === "matrix") {
259
- e = e.replace(
260
- s,
261
- `;${R(r, c)}`
262
- );
311
+ if (i === "matrix") {
312
+ e = e.replace(s, `;${$(n, l)}`);
263
313
  continue;
264
314
  }
265
- e = e.replace(s, a === "label" ? `.${c}` : c);
315
+ e = e.replace(s, i === "label" ? `.${encodeURIComponent(l)}` : encodeURIComponent(l));
266
316
  }
267
317
  return e;
268
318
  }
269
- function I(n) {
270
- return JSON.stringify(n);
319
+ function Q(r) {
320
+ return r instanceof FormData ? r : JSON.stringify(r);
271
321
  }
272
- function L(n, t) {
273
- var r;
274
- let e = `${t.baseUrl}${n}`;
275
- (r = t.params) != null && r.path && (e = P(e, t.params.path));
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));
276
326
  let s = t.querySerializer(t.params.query ?? {});
277
327
  return s.startsWith("?") && (s = s.substring(1)), s && (e += `?${s}`), e;
278
328
  }
279
- function z(...n) {
329
+ function X(...r) {
280
330
  const t = new Headers();
281
- for (const e of n) {
331
+ for (const e of r) {
282
332
  if (!e || typeof e != "object")
283
333
  continue;
284
334
  const s = e instanceof Headers ? e.entries() : Object.entries(e);
285
- for (const [r, i] of s)
286
- if (i === null)
287
- t.delete(r);
288
- else if (Array.isArray(i))
289
- for (const a of i)
290
- t.append(r, a);
291
- else
292
- i !== void 0 && t.set(r, i);
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);
293
342
  }
294
343
  return t;
295
344
  }
296
- function k({
297
- apiKey: n,
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,
298
437
  baseUrl: t = "https://api.cycle.io",
299
438
  hubId: e,
300
439
  fetch: s
301
440
  }) {
302
- const r = U({
441
+ const n = W({
303
442
  baseUrl: t,
304
443
  fetch: s || fetch
305
- }), i = {
306
- async onRequest(a) {
307
- return a.headers.set("Authorization", `Bearer ${n}`), a.headers.set("X-Hub-Id", e), a;
444
+ }), o = {
445
+ async onRequest({ request: i }) {
446
+ return i.headers.set("Authorization", `Bearer ${r}`), i.headers.set("X-Hub-Id", e), i;
308
447
  }
309
448
  };
310
- return r.use(i), r;
449
+ return n.use(o), n;
311
450
  }
312
451
  export {
313
- k as getClient
452
+ te as getClient,
453
+ V as getJobProgress,
454
+ ee as trackJob
314
455
  };