@coinbase/cdp-api-client 0.0.43 → 0.0.45

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