@coinbase/cdp-api-client 0.0.32 → 0.0.33
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/esm/index4.js +90 -55
- package/dist/types/index.d.ts +7 -0
- package/package.json +1 -1
package/dist/esm/index4.js
CHANGED
|
@@ -1,53 +1,55 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ERROR_DOCS_PAGE_URL as
|
|
3
|
-
import { UnknownApiError as w, HttpErrorType as
|
|
4
|
-
|
|
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
|
-
}),
|
|
7
|
-
const
|
|
8
|
-
|
|
7
|
+
}), c = null;
|
|
8
|
+
const x = (t) => {
|
|
9
|
+
o = d.create({
|
|
9
10
|
baseURL: t.basePath || "https://api.cdp.coinbase.com/platform",
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
|
|
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
|
|
22
|
-
}),
|
|
23
|
-
const e =
|
|
24
|
-
if (!
|
|
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
|
|
28
|
+
const s = await c.getToken();
|
|
27
29
|
return s && s !== "" && (r.headers.Authorization = `Bearer ${s}`), r;
|
|
28
|
-
}), t.debugging && (
|
|
29
|
-
},
|
|
30
|
-
|
|
31
|
-
},
|
|
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
|
-
}),
|
|
38
|
-
|
|
39
|
+
}), b = async (t, r) => {
|
|
40
|
+
E(t), r && r !== "" && (t = y(t, r));
|
|
39
41
|
try {
|
|
40
|
-
return (await
|
|
42
|
+
return (await o(t)).data;
|
|
41
43
|
} catch (e) {
|
|
42
|
-
if (
|
|
44
|
+
if (d.isAxiosError(e) && !e.response)
|
|
43
45
|
throw new w(
|
|
44
|
-
|
|
46
|
+
a.unknown,
|
|
45
47
|
e.cause instanceof Error ? e.cause.message : e.message,
|
|
46
48
|
e.cause
|
|
47
49
|
);
|
|
48
|
-
if (
|
|
50
|
+
if (d.isAxiosError(e) && e.response) {
|
|
49
51
|
if (m(e.response.data))
|
|
50
|
-
throw new
|
|
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
|
|
64
|
+
throw new i(
|
|
63
65
|
s,
|
|
64
|
-
|
|
66
|
+
a.unauthorized,
|
|
65
67
|
"Unauthorized.",
|
|
66
68
|
void 0,
|
|
67
|
-
`${
|
|
69
|
+
`${u}#unauthorized`,
|
|
68
70
|
e.cause
|
|
69
71
|
);
|
|
70
72
|
case 404:
|
|
71
|
-
throw new
|
|
73
|
+
throw new i(
|
|
72
74
|
s,
|
|
73
|
-
|
|
75
|
+
a.not_found,
|
|
74
76
|
"API not found.",
|
|
75
77
|
void 0,
|
|
76
|
-
`${
|
|
78
|
+
`${u}#not_found`,
|
|
77
79
|
e.cause
|
|
78
80
|
);
|
|
79
81
|
case 502:
|
|
80
|
-
throw new
|
|
82
|
+
throw new i(
|
|
81
83
|
s,
|
|
82
|
-
|
|
84
|
+
a.bad_gateway,
|
|
83
85
|
"Bad gateway.",
|
|
84
86
|
void 0,
|
|
85
|
-
`${
|
|
87
|
+
`${u}`,
|
|
86
88
|
e.cause
|
|
87
89
|
);
|
|
88
90
|
case 503:
|
|
89
|
-
throw new
|
|
91
|
+
throw new i(
|
|
90
92
|
s,
|
|
91
|
-
|
|
93
|
+
a.service_unavailable,
|
|
92
94
|
"Service unavailable. Please try again later.",
|
|
93
95
|
void 0,
|
|
94
|
-
`${
|
|
96
|
+
`${u}`,
|
|
95
97
|
e.cause
|
|
96
98
|
);
|
|
97
99
|
default: {
|
|
98
|
-
let
|
|
100
|
+
let n = "";
|
|
99
101
|
if (e.response.data)
|
|
100
102
|
try {
|
|
101
|
-
|
|
103
|
+
n = JSON.stringify(e.response.data);
|
|
102
104
|
} catch {
|
|
103
|
-
|
|
105
|
+
n = String(e.response.data);
|
|
104
106
|
}
|
|
105
|
-
const
|
|
106
|
-
throw new
|
|
107
|
+
const f = n ? `An unexpected error occurred: ${n}` : "An unexpected error occurred.";
|
|
108
|
+
throw new i(
|
|
107
109
|
s,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
+
a.unexpected_error,
|
|
111
|
+
f,
|
|
110
112
|
void 0,
|
|
111
|
-
`${
|
|
113
|
+
`${u}`,
|
|
112
114
|
e.cause
|
|
113
115
|
);
|
|
114
116
|
}
|
|
115
117
|
}
|
|
116
118
|
}
|
|
117
119
|
}
|
|
118
|
-
throw new
|
|
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
|
-
|
|
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",
|
|
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 =
|
|
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
|
-
|
|
141
|
-
|
|
142
|
-
|
|
175
|
+
b as cdpApiClient,
|
|
176
|
+
x as configureCdpApiClient,
|
|
177
|
+
P as setAuthManager
|
|
143
178
|
};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -100,6 +100,7 @@ declare const cdpApiClient: <T>(config: AxiosRequestConfig, idempotencyKey?: str
|
|
|
100
100
|
export declare type CdpOptions = {
|
|
101
101
|
debugging?: boolean;
|
|
102
102
|
basePath?: string;
|
|
103
|
+
refreshTokenStorage?: RefreshTokenStorage;
|
|
103
104
|
};
|
|
104
105
|
|
|
105
106
|
export declare interface CommonSwapResponse {
|
|
@@ -1014,6 +1015,12 @@ export declare const RefreshAccessTokenBodyGrantType: {
|
|
|
1014
1015
|
|
|
1015
1016
|
export declare type RefreshAccessTokenResult = NonNullable<Awaited<ReturnType<typeof refreshAccessToken>>>;
|
|
1016
1017
|
|
|
1018
|
+
export declare interface RefreshTokenStorage {
|
|
1019
|
+
getRefreshToken(): Promise<string | null>;
|
|
1020
|
+
setRefreshToken(token: string): Promise<void>;
|
|
1021
|
+
removeRefreshToken(): Promise<void>;
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1017
1024
|
export declare const registerTemporaryWalletSecret: (projectId: string, userId: string, registerTemporaryWalletSecretBody: RegisterTemporaryWalletSecretBody, options?: SecondParameter<typeof cdpApiClient>) => Promise<RegisterTemporaryWalletSecret200>;
|
|
1018
1025
|
|
|
1019
1026
|
export declare type RegisterTemporaryWalletSecret200 = {
|