@cycleplatform/api-client-typescript 0.2.5 → 0.3.0

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