@coinbase/cdp-api-client 0.0.32 → 0.0.34

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.
@@ -1,53 +1,55 @@
1
- import c from "axios";
2
- import { ERROR_DOCS_PAGE_URL as i } from "./index6.js";
3
- import { UnknownApiError as w, HttpErrorType as n, isOpenAPIError as m, APIError as u, UnknownError as f } from "./index5.js";
4
- let a = c.create({
1
+ import d from "axios";
2
+ import { ERROR_DOCS_PAGE_URL as u } from "./index6.js";
3
+ import { UnknownApiError as w, HttpErrorType as a, isOpenAPIError as m, APIError as i, UnknownError as k } from "./index5.js";
4
+ const h = () => typeof window < "u" && typeof document < "u";
5
+ let o = d.create({
5
6
  baseURL: "https://api.cdp.coinbase.com/platform"
6
- }), d = null;
7
- const R = (t) => {
8
- a = c.create({
7
+ }), c = null;
8
+ const x = (t) => {
9
+ o = d.create({
9
10
  baseURL: t.basePath || "https://api.cdp.coinbase.com/platform",
10
- withCredentials: !0
11
- }), a.interceptors.request.use(async (r) => {
12
- const e = U(r), s = h(r);
13
- if (!d || p(s.pathname))
11
+ // Only enable cookies in browser environments where they're supported
12
+ withCredentials: h()
13
+ }), o.interceptors.request.use(async (r) => {
14
+ const e = U(r), s = l(r);
15
+ if (!c || p(s.pathname))
14
16
  return r;
15
- const o = await d.getXWalletAuth?.({
17
+ const n = await c.getXWalletAuth?.({
16
18
  requestMethod: e,
17
19
  requestHost: s.host,
18
20
  requestPath: s.pathname,
19
21
  requestData: r.data
20
22
  });
21
- return o && o !== "" && (r.headers["X-Wallet-Auth"] = o), r;
22
- }), a.interceptors.request.use(async (r) => {
23
- const e = h(r);
24
- if (!d || p(e.pathname))
23
+ return n && n !== "" && (r.headers["X-Wallet-Auth"] = n), r;
24
+ }), o.interceptors.request.use(async (r) => {
25
+ const e = l(r);
26
+ if (!c || p(e.pathname))
25
27
  return r;
26
- const s = await d.getToken();
28
+ const s = await c.getToken();
27
29
  return s && s !== "" && (r.headers.Authorization = `Bearer ${s}`), r;
28
- }), t.debugging && (a.interceptors.request.use((r) => (console.log("Request:", r), r)), a.interceptors.response.use((r) => (console.log("Response:", r), r)));
29
- }, x = (t) => {
30
- d = t;
31
- }, A = (t, r) => ({
30
+ }), !h() && t.refreshTokenStorage && R(t.refreshTokenStorage), t.debugging && (o.interceptors.request.use((r) => (console.log("Request:", r), r)), o.interceptors.response.use((r) => (console.log("Response:", r), r)));
31
+ }, P = (t) => {
32
+ c = t;
33
+ }, y = (t, r) => ({
32
34
  ...t,
33
35
  headers: {
34
36
  ...t.headers || {},
35
37
  "X-Idempotency-Key": r
36
38
  }
37
- }), C = async (t, r) => {
38
- v(t), r && r !== "" && (t = A(t, r));
39
+ }), b = async (t, r) => {
40
+ E(t), r && r !== "" && (t = y(t, r));
39
41
  try {
40
- return (await a(t)).data;
42
+ return (await o(t)).data;
41
43
  } catch (e) {
42
- if (c.isAxiosError(e) && !e.response)
44
+ if (d.isAxiosError(e) && !e.response)
43
45
  throw new w(
44
- n.unknown,
46
+ a.unknown,
45
47
  e.cause instanceof Error ? e.cause.message : e.message,
46
48
  e.cause
47
49
  );
48
- if (c.isAxiosError(e) && e.response) {
50
+ if (d.isAxiosError(e) && e.response) {
49
51
  if (m(e.response.data))
50
- throw new u(
52
+ throw new i(
51
53
  e.response.status,
52
54
  e.response.data.errorType,
53
55
  e.response.data.errorMessage,
@@ -59,85 +61,118 @@ const R = (t) => {
59
61
  const s = e.response.status;
60
62
  switch (s) {
61
63
  case 401:
62
- throw new u(
64
+ throw new i(
63
65
  s,
64
- n.unauthorized,
66
+ a.unauthorized,
65
67
  "Unauthorized.",
66
68
  void 0,
67
- `${i}#unauthorized`,
69
+ `${u}#unauthorized`,
68
70
  e.cause
69
71
  );
70
72
  case 404:
71
- throw new u(
73
+ throw new i(
72
74
  s,
73
- n.not_found,
75
+ a.not_found,
74
76
  "API not found.",
75
77
  void 0,
76
- `${i}#not_found`,
78
+ `${u}#not_found`,
77
79
  e.cause
78
80
  );
79
81
  case 502:
80
- throw new u(
82
+ throw new i(
81
83
  s,
82
- n.bad_gateway,
84
+ a.bad_gateway,
83
85
  "Bad gateway.",
84
86
  void 0,
85
- `${i}`,
87
+ `${u}`,
86
88
  e.cause
87
89
  );
88
90
  case 503:
89
- throw new u(
91
+ throw new i(
90
92
  s,
91
- n.service_unavailable,
93
+ a.service_unavailable,
92
94
  "Service unavailable. Please try again later.",
93
95
  void 0,
94
- `${i}`,
96
+ `${u}`,
95
97
  e.cause
96
98
  );
97
99
  default: {
98
- let o = "";
100
+ let n = "";
99
101
  if (e.response.data)
100
102
  try {
101
- o = JSON.stringify(e.response.data);
103
+ n = JSON.stringify(e.response.data);
102
104
  } catch {
103
- o = String(e.response.data);
105
+ n = String(e.response.data);
104
106
  }
105
- const l = o ? `An unexpected error occurred: ${o}` : "An unexpected error occurred.";
106
- throw new u(
107
+ const f = n ? `An unexpected error occurred: ${n}` : "An unexpected error occurred.";
108
+ throw new i(
107
109
  s,
108
- n.unexpected_error,
109
- l,
110
+ a.unexpected_error,
111
+ f,
110
112
  void 0,
111
- `${i}`,
113
+ `${u}`,
112
114
  e.cause
113
115
  );
114
116
  }
115
117
  }
116
118
  }
117
119
  }
118
- throw new f(
120
+ throw new k(
119
121
  "Something went wrong. Please reach out at https://discord.com/channels/1220414409550336183/1271495764580896789 for help.",
120
122
  e instanceof Error ? e : void 0
121
123
  );
122
124
  }
125
+ }, R = (t) => {
126
+ o.interceptors.response.use(
127
+ async (r) => {
128
+ try {
129
+ const e = r.headers["set-cookie"];
130
+ if (e) {
131
+ const s = v(e);
132
+ s && await t.setRefreshToken(s);
133
+ }
134
+ } catch (e) {
135
+ console.warn("Failed to store refresh token:", e);
136
+ }
137
+ return r;
138
+ },
139
+ (r) => Promise.reject(r)
140
+ ), o.interceptors.request.use(async (r) => {
141
+ try {
142
+ if (A(r.url)) {
143
+ const e = await t.getRefreshToken();
144
+ e && (r.headers.Cookie = `cdp_refresh_token=${e}`);
145
+ }
146
+ } catch (e) {
147
+ console.warn("Failed to retrieve refresh token:", e);
148
+ }
149
+ return r;
150
+ });
123
151
  }, v = (t) => {
124
- if (!a.getUri() || a.getUri() === "")
152
+ for (const r of t) {
153
+ const e = r.match(/cdp_refresh_token=([^;]+)/);
154
+ if (e)
155
+ return e[1];
156
+ }
157
+ return null;
158
+ }, A = (t) => t ? /^\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/refresh$/.test(t) : !1, E = (t) => {
159
+ if (!o.getUri() || o.getUri() === "")
125
160
  throw new Error("CDP client URI not configured. Call configure() first.");
126
161
  if (!t.url || t.url === "")
127
162
  throw new Error("AxiosRequestConfig URL is empty. This should never happen.");
128
163
  if (!t.method || t.method === "")
129
164
  throw new Error("AxiosRequestConfig method is empty. This should never happen.");
130
- }, U = (t) => t.method?.toString().toUpperCase() || "GET", h = (t) => {
165
+ }, U = (t) => t.method?.toString().toUpperCase() || "GET", l = (t) => {
131
166
  if (!t.url)
132
167
  throw new Error("URL is required for authentication");
133
- const r = a.getUri() + t.url;
168
+ const r = o.getUri() + t.url;
134
169
  return new URL(r);
135
170
  }, p = (t) => {
136
171
  const r = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/(init|refresh|logout)$/, e = /^\/platform\/v2\/embedded-wallet-api\/projects\/[^/]+\/auth\/verify\//;
137
172
  return r.test(t) || e.test(t);
138
173
  };
139
174
  export {
140
- C as cdpApiClient,
141
- R as configureCdpApiClient,
142
- x as setAuthManager
175
+ b as cdpApiClient,
176
+ x as configureCdpApiClient,
177
+ P as setAuthManager
143
178
  };