@cycleplatform/api-client-typescript 0.1.3 → 0.1.5

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/src/index.ts CHANGED
@@ -34,3 +34,28 @@ export const client = new Proxy(baseClient, {
34
34
  return newClient[key];
35
35
  },
36
36
  });
37
+
38
+ type ParamVal = string | number | Record<string, string | number> | string[];
39
+ type Params<T extends string, K extends ParamVal> = Partial<Record<T, K>>;
40
+
41
+ /** Converts any Cycle query parameters into a URL search string */
42
+ export function querySerializer<T extends string, K extends ParamVal>(
43
+ params: Params<T, K>
44
+ ): string {
45
+ const nv = Object.entries(params).reduce((acc, [k, v]) => {
46
+ if (typeof v === "string") {
47
+ acc[k] = v;
48
+ return acc;
49
+ }
50
+
51
+ if (v && typeof v === "object") {
52
+ Object.keys(v).forEach(
53
+ (sk) => (acc[`${k}[${sk}]`] = (v as Record<string, string>)[sk])
54
+ );
55
+ }
56
+
57
+ return acc;
58
+ }, {} as Record<string, string>);
59
+
60
+ return new URLSearchParams(nv).toString();
61
+ }
package/dist/index.mjs DELETED
@@ -1,87 +0,0 @@
1
- const S = {
2
- "Content-Type": "application/json"
3
- };
4
- function b(n) {
5
- const u = new Headers({
6
- ...S,
7
- ...(n == null ? void 0 : n.headers) ?? {}
8
- });
9
- async function r(e, t) {
10
- const { headers: T, body: i, params: c = {}, querySerializer: p = (s) => new URLSearchParams(s).toString(), ...w } = t || {};
11
- let d = `${(n == null ? void 0 : n.baseUrl) ?? ""}${e}`;
12
- if (c.path)
13
- for (const [s, o] of Object.entries(c.path))
14
- d = d.replace(`{${s}}`, encodeURIComponent(String(o)));
15
- c.query && Object.keys(c.query).length && (d += `?${p(c.query)}`);
16
- const h = new Headers(u), H = new Headers(T);
17
- for (const [s, o] of H.entries())
18
- o == null ? h.delete(s) : h.set(s, o);
19
- const a = await fetch(d, {
20
- redirect: "follow",
21
- ...n,
22
- ...w,
23
- headers: h,
24
- body: typeof i == "string" ? i : JSON.stringify(i)
25
- }), f = a.status === 204 || a.headers.get("Content-Length") === "0" ? {} : await a.json();
26
- return a.ok ? { data: f, response: a } : { error: f, response: a };
27
- }
28
- return {
29
- /** Call a GET endpoint */
30
- async get(e, t) {
31
- return r(e, { ...t, method: "GET" });
32
- },
33
- /** Call a PUT endpoint */
34
- async put(e, t) {
35
- return r(e, { ...t, method: "PUT" });
36
- },
37
- /** Call a POST endpoint */
38
- async post(e, t) {
39
- return r(e, { ...t, method: "POST" });
40
- },
41
- /** Call a DELETE endpoint */
42
- async del(e, t) {
43
- return r(e, { ...t, method: "DELETE" });
44
- },
45
- /** Call a OPTIONS endpoint */
46
- async options(e, t) {
47
- return r(e, { ...t, method: "OPTIONS" });
48
- },
49
- /** Call a HEAD endpoint */
50
- async head(e, t) {
51
- return r(e, { ...t, method: "HEAD" });
52
- },
53
- /** Call a PATCH endpoint */
54
- async patch(e, t) {
55
- return r(e, { ...t, method: "PATCH" });
56
- },
57
- /** Call a TRACE endpoint */
58
- async trace(e, t) {
59
- return r(e, { ...t, method: "TRACE" });
60
- }
61
- };
62
- }
63
- let l, y, m = "https://api.cycle.io";
64
- function C(n) {
65
- l = n;
66
- }
67
- function E(n) {
68
- y = n;
69
- }
70
- function U(n) {
71
- m = n;
72
- }
73
- const g = b(), A = new Proxy(g, {
74
- get(n, u) {
75
- const r = {};
76
- return l && (r.Authorization = `Bearer ${l}`), y && (r["X-Hub-Id"] = y), b({
77
- headers: r,
78
- baseUrl: m
79
- })[u];
80
- }
81
- });
82
- export {
83
- A as client,
84
- C as setAuthToken,
85
- U as setBaseUrl,
86
- E as setHubScope
87
- };
package/dist/index.umd.js DELETED
@@ -1 +0,0 @@
1
- (function(s,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(s=typeof globalThis<"u"?globalThis:s||self,c(s["Cycle API Client"]={}))})(this,function(s){"use strict";const c={"Content-Type":"application/json"};function T(n){const h=new Headers({...c,...(n==null?void 0:n.headers)??{}});async function r(e,t){const{headers:A,body:y,params:d={},querySerializer:E=a=>new URLSearchParams(a).toString(),...U}=t||{};let u=`${(n==null?void 0:n.baseUrl)??""}${e}`;if(d.path)for(const[a,i]of Object.entries(d.path))u=u.replace(`{${a}}`,encodeURIComponent(String(i)));d.query&&Object.keys(d.query).length&&(u+=`?${E(d.query)}`);const b=new Headers(h),O=new Headers(A);for(const[a,i]of O.entries())i==null?b.delete(a):b.set(a,i);const o=await fetch(u,{redirect:"follow",...n,...U,headers:b,body:typeof y=="string"?y:JSON.stringify(y)}),p=o.status===204||o.headers.get("Content-Length")==="0"?{}:await o.json();return o.ok?{data:p,response:o}:{error:p,response:o}}return{async get(e,t){return r(e,{...t,method:"GET"})},async put(e,t){return r(e,{...t,method:"PUT"})},async post(e,t){return r(e,{...t,method:"POST"})},async del(e,t){return r(e,{...t,method:"DELETE"})},async options(e,t){return r(e,{...t,method:"OPTIONS"})},async head(e,t){return r(e,{...t,method:"HEAD"})},async patch(e,t){return r(e,{...t,method:"PATCH"})},async trace(e,t){return r(e,{...t,method:"TRACE"})}}}let l,f,m="https://api.cycle.io";function S(n){l=n}function g(n){f=n}function C(n){m=n}const H=T(),w=new Proxy(H,{get(n,h){const r={};return l&&(r.Authorization=`Bearer ${l}`),f&&(r["X-Hub-Id"]=f),T({headers:r,baseUrl:m})[h]}});s.client=w,s.setAuthToken=S,s.setBaseUrl=C,s.setHubScope=g,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})});