@cycleplatform/api-client-typescript 0.1.7 → 0.1.9

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,94 +1,129 @@
1
- const T = {
1
+ const O = {
2
2
  "Content-Type": "application/json"
3
- };
4
- function S(n) {
5
- const o = new Headers({
6
- ...T,
7
- ...(n == null ? void 0 : n.headers) ?? {}
3
+ }, j = /\/*$/;
4
+ function E(t = {}) {
5
+ const { fetch: r = globalThis.fetch, querySerializer: e, bodySerializer: a, ...i } = t, c = new Headers({
6
+ ...O,
7
+ ...i.headers ?? {}
8
8
  });
9
- async function r(t, e) {
10
- const { headers: u, body: h, params: c = {}, querySerializer: p = (a) => new URLSearchParams(a).toString(), ...g } = e || {};
11
- let d = `${(n == null ? void 0 : n.baseUrl) ?? ""}${t}`;
12
- if (c.path)
13
- for (const [a, i] of Object.entries(c.path))
14
- d = d.replace(`{${a}}`, encodeURIComponent(String(i)));
15
- c.query && Object.keys(c.query).length && (d += `?${p(c.query)}`);
16
- const f = new Headers(o), w = new Headers(u);
17
- for (const [a, i] of w.entries())
18
- i == null ? f.delete(a) : f.set(a, i);
19
- const s = await fetch(d, {
20
- redirect: "follow",
21
- ...n,
22
- ...g,
23
- headers: f,
24
- body: typeof h == "string" ? h : JSON.stringify(h)
25
- }), b = s.status === 204 || s.headers.get("Content-Length") === "0" ? {} : await s.json();
26
- return s.ok ? { data: b, response: s } : { error: b, response: s };
9
+ async function u(n, o) {
10
+ const { headers: H, body: S, params: p = {}, parseAs: f = "json", querySerializer: w = e ?? z, bodySerializer: A = a ?? L, ...g } = o || {}, C = P(n, { baseUrl: i.baseUrl, params: p, querySerializer: w }), T = R(c, H, p.header), l = { redirect: "follow", ...i, ...g, headers: T };
11
+ S && (l.body = A(S)), l.body instanceof FormData && T.delete("Content-Type");
12
+ const s = await r(C, l);
13
+ if (s.status === 204 || s.headers.get("Content-Length") === "0")
14
+ return s.ok ? { data: {}, response: s } : { error: {}, response: s };
15
+ if (s.ok) {
16
+ let m = s.body;
17
+ if (f !== "stream") {
18
+ const y = s.clone();
19
+ m = typeof y[f] == "function" ? await y[f]() : await y.text();
20
+ }
21
+ return { data: m, response: s };
22
+ }
23
+ let d = {};
24
+ try {
25
+ d = await s.clone().json();
26
+ } catch {
27
+ d = await s.clone().text();
28
+ }
29
+ return { error: d, response: s };
27
30
  }
28
31
  return {
29
32
  /** Call a GET endpoint */
30
- async get(t, e) {
31
- return r(t, { ...e, method: "GET" });
33
+ async GET(n, o) {
34
+ return u(n, { ...o, method: "GET" });
32
35
  },
33
36
  /** Call a PUT endpoint */
34
- async put(t, e) {
35
- return r(t, { ...e, method: "PUT" });
37
+ async PUT(n, o) {
38
+ return u(n, { ...o, method: "PUT" });
36
39
  },
37
40
  /** Call a POST endpoint */
38
- async post(t, e) {
39
- return r(t, { ...e, method: "POST" });
41
+ async POST(n, o) {
42
+ return u(n, { ...o, method: "POST" });
40
43
  },
41
44
  /** Call a DELETE endpoint */
42
- async del(t, e) {
43
- return r(t, { ...e, method: "DELETE" });
45
+ async DELETE(n, o) {
46
+ return u(n, { ...o, method: "DELETE" });
44
47
  },
45
48
  /** Call a OPTIONS endpoint */
46
- async options(t, e) {
47
- return r(t, { ...e, method: "OPTIONS" });
49
+ async OPTIONS(n, o) {
50
+ return u(n, { ...o, method: "OPTIONS" });
48
51
  },
49
52
  /** Call a HEAD endpoint */
50
- async head(t, e) {
51
- return r(t, { ...e, method: "HEAD" });
53
+ async HEAD(n, o) {
54
+ return u(n, { ...o, method: "HEAD" });
52
55
  },
53
56
  /** Call a PATCH endpoint */
54
- async patch(t, e) {
55
- return r(t, { ...e, method: "PATCH" });
57
+ async PATCH(n, o) {
58
+ return u(n, { ...o, method: "PATCH" });
56
59
  },
57
60
  /** Call a TRACE endpoint */
58
- async trace(t, e) {
59
- return r(t, { ...e, method: "TRACE" });
61
+ async TRACE(n, o) {
62
+ return u(n, { ...o, method: "TRACE" });
60
63
  }
61
64
  };
62
65
  }
63
- let l, y, m = "https://api.cycle.io";
64
- function H(n) {
65
- l = n;
66
+ function z(t) {
67
+ const r = new URLSearchParams();
68
+ if (t && typeof t == "object")
69
+ for (const [e, a] of Object.entries(t))
70
+ a != null && r.set(e, a);
71
+ return r.toString();
72
+ }
73
+ function L(t) {
74
+ return JSON.stringify(t);
75
+ }
76
+ function P(t, r) {
77
+ let e = `${r.baseUrl ? r.baseUrl.replace(j, "") : ""}${t}`;
78
+ if (r.params.path)
79
+ for (const [a, i] of Object.entries(r.params.path))
80
+ e = e.replace(`{${a}}`, encodeURIComponent(String(i)));
81
+ if (r.params.query) {
82
+ const a = r.querySerializer(r.params.query);
83
+ a && (e += `?${a}`);
84
+ }
85
+ return e;
86
+ }
87
+ function R(...t) {
88
+ const r = new Headers();
89
+ for (const e of t) {
90
+ if (!e || typeof e != "object")
91
+ continue;
92
+ const a = e instanceof Headers ? e.entries() : Object.entries(e);
93
+ for (const [i, c] of a)
94
+ c != null && r.set(i, c);
95
+ }
96
+ return r;
97
+ }
98
+ let h, b, U = "https://api.cycle.io";
99
+ function D(t) {
100
+ h = t;
66
101
  }
67
- function C(n) {
68
- y = n;
102
+ function I(t) {
103
+ b = t;
69
104
  }
70
- function O(n) {
71
- m = n;
105
+ function k(t) {
106
+ U = t;
72
107
  }
73
- const E = S(), U = new Proxy(E, {
74
- get(n, o) {
75
- const r = {};
76
- return l && (r.Authorization = `Bearer ${l}`), y && (r["X-Hub-Id"] = y), S({
77
- headers: r,
78
- baseUrl: m
79
- })[o];
108
+ const $ = E(), q = new Proxy($, {
109
+ get(t, r) {
110
+ const e = {};
111
+ return h && (e.Authorization = `Bearer ${h}`), b && (e["X-Hub-Id"] = b), E({
112
+ headers: e,
113
+ baseUrl: U
114
+ })[r];
80
115
  }
81
116
  });
82
- function j(n) {
83
- const o = Object.entries(n).reduce((r, [t, e]) => typeof e == "string" ? (r[t] = e, r) : (e && typeof e == "object" && Object.keys(e).forEach(
84
- (u) => r[`${t}[${u}]`] = e[u]
85
- ), r), {});
86
- return new URLSearchParams(o).toString();
117
+ function B(t) {
118
+ const r = Object.entries(t).reduce((e, [a, i]) => typeof i == "string" ? (e[a] = i, e) : (i && typeof i == "object" && Object.keys(i).forEach(
119
+ (c) => e[`${a}[${c}]`] = i[c]
120
+ ), e), {});
121
+ return new URLSearchParams(r).toString();
87
122
  }
88
123
  export {
89
- U as client,
90
- j as querySerializer,
91
- H as setAuthToken,
92
- O as setBaseUrl,
93
- C as setHubScope
124
+ q as client,
125
+ B as querySerializer,
126
+ D as setAuthToken,
127
+ k as setBaseUrl,
128
+ I as setHubScope
94
129
  };
@@ -1 +1 @@
1
- (function(s,i){typeof exports=="object"&&typeof module<"u"?i(exports):typeof define=="function"&&define.amd?define(["exports"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s["Cycle API Client"]={}))})(this,function(s){"use strict";const i={"Content-Type":"application/json"};function m(r){const c=new Headers({...i,...(r==null?void 0:r.headers)??{}});async function n(t,e){const{headers:f,body:b,params:u={},querySerializer:U=a=>new URLSearchParams(a).toString(),...j}=e||{};let l=`${(r==null?void 0:r.baseUrl)??""}${t}`;if(u.path)for(const[a,d]of Object.entries(u.path))l=l.replace(`{${a}}`,encodeURIComponent(String(d)));u.query&&Object.keys(u.query).length&&(l+=`?${U(u.query)}`);const S=new Headers(c),A=new Headers(f);for(const[a,d]of A.entries())d==null?S.delete(a):S.set(a,d);const o=await fetch(l,{redirect:"follow",...r,...j,headers:S,body:typeof b=="string"?b:JSON.stringify(b)}),T=o.status===204||o.headers.get("Content-Length")==="0"?{}:await o.json();return o.ok?{data:T,response:o}:{error:T,response:o}}return{async get(t,e){return n(t,{...e,method:"GET"})},async put(t,e){return n(t,{...e,method:"PUT"})},async post(t,e){return n(t,{...e,method:"POST"})},async del(t,e){return n(t,{...e,method:"DELETE"})},async options(t,e){return n(t,{...e,method:"OPTIONS"})},async head(t,e){return n(t,{...e,method:"HEAD"})},async patch(t,e){return n(t,{...e,method:"PATCH"})},async trace(t,e){return n(t,{...e,method:"TRACE"})}}}let h,y,p="https://api.cycle.io";function g(r){h=r}function w(r){y=r}function C(r){p=r}const H=m(),E=new Proxy(H,{get(r,c){const n={};return h&&(n.Authorization=`Bearer ${h}`),y&&(n["X-Hub-Id"]=y),m({headers:n,baseUrl:p})[c]}});function O(r){const c=Object.entries(r).reduce((n,[t,e])=>typeof e=="string"?(n[t]=e,n):(e&&typeof e=="object"&&Object.keys(e).forEach(f=>n[`${t}[${f}]`]=e[f]),n),{});return new URLSearchParams(c).toString()}s.client=E,s.querySerializer=O,s.setAuthToken=g,s.setBaseUrl=C,s.setHubScope=w,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});
1
+ (function(c,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(c=typeof globalThis<"u"?globalThis:c||self,l(c["Cycle API Client"]={}))})(this,function(c){"use strict";const l={"Content-Type":"application/json"},g=/\/*$/;function p(t={}){const{fetch:n=globalThis.fetch,querySerializer:e,bodySerializer:i,...a}=t,u=new Headers({...l,...a.headers??{}});async function f(r,o){const{headers:k,body:E,params:U={},parseAs:h="json",querySerializer:q=e??C,bodySerializer:D=i??j,...B}=o||{},F=w(r,{baseUrl:a.baseUrl,params:U,querySerializer:q}),A=O(u,k,U.header),b={redirect:"follow",...a,...B,headers:A};E&&(b.body=D(E)),b.body instanceof FormData&&A.delete("Content-Type");const s=await n(F,b);if(s.status===204||s.headers.get("Content-Length")==="0")return s.ok?{data:{},response:s}:{error:{},response:s};if(s.ok){let H=s.body;if(h!=="stream"){const T=s.clone();H=typeof T[h]=="function"?await T[h]():await T.text()}return{data:H,response:s}}let S={};try{S=await s.clone().json()}catch{S=await s.clone().text()}return{error:S,response:s}}return{async GET(r,o){return f(r,{...o,method:"GET"})},async PUT(r,o){return f(r,{...o,method:"PUT"})},async POST(r,o){return f(r,{...o,method:"POST"})},async DELETE(r,o){return f(r,{...o,method:"DELETE"})},async OPTIONS(r,o){return f(r,{...o,method:"OPTIONS"})},async HEAD(r,o){return f(r,{...o,method:"HEAD"})},async PATCH(r,o){return f(r,{...o,method:"PATCH"})},async TRACE(r,o){return f(r,{...o,method:"TRACE"})}}}function C(t){const n=new URLSearchParams;if(t&&typeof t=="object")for(const[e,i]of Object.entries(t))i!=null&&n.set(e,i);return n.toString()}function j(t){return JSON.stringify(t)}function w(t,n){let e=`${n.baseUrl?n.baseUrl.replace(g,""):""}${t}`;if(n.params.path)for(const[i,a]of Object.entries(n.params.path))e=e.replace(`{${i}}`,encodeURIComponent(String(a)));if(n.params.query){const i=n.querySerializer(n.params.query);i&&(e+=`?${i}`)}return e}function O(...t){const n=new Headers;for(const e of t){if(!e||typeof e!="object")continue;const i=e instanceof Headers?e.entries():Object.entries(e);for(const[a,u]of i)u!=null&&n.set(a,u)}return n}let d,y,m="https://api.cycle.io";function P(t){d=t}function z(t){y=t}function L(t){m=t}const R=p(),I=new Proxy(R,{get(t,n){const e={};return d&&(e.Authorization=`Bearer ${d}`),y&&(e["X-Hub-Id"]=y),p({headers:e,baseUrl:m})[n]}});function $(t){const n=Object.entries(t).reduce((e,[i,a])=>typeof a=="string"?(e[i]=a,e):(a&&typeof a=="object"&&Object.keys(a).forEach(u=>e[`${i}[${u}]`]=a[u]),e),{});return new URLSearchParams(n).toString()}c.client=I,c.querySerializer=$,c.setAuthToken=P,c.setBaseUrl=L,c.setHubScope=z,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cycleplatform/api-client-typescript",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "A Cycle API client for the web/nodejs",
5
5
  "main": "./dist/index.umd.cjs",
6
6
  "module": "./dist/index.js",
@@ -36,13 +36,13 @@
36
36
  },
37
37
  "homepage": "https://github.com/cycleplatform/api-client-typescript#readme",
38
38
  "dependencies": {
39
- "openapi-fetch": "^0.1.4"
39
+ "openapi-fetch": "0.7.1"
40
40
  },
41
41
  "devDependencies": {
42
- "msw": "^1.2.1",
43
- "openapi-typescript": "^6.2.4",
42
+ "msw": "1.2.3",
43
+ "openapi-typescript": "6.3.9",
44
44
  "typescript": "^5.0.4",
45
- "vite": "^4.3.7",
46
- "vitest": "^0.31.0"
45
+ "vite": "4.4.7",
46
+ "vitest": "0.33.0"
47
47
  }
48
48
  }