@riftresearch/sdk 0.11.0 → 0.11.1
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 +84 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -156,9 +156,91 @@ function getSupportedModes(from, to) {
|
|
|
156
156
|
import { erc20Abi } from "viem";
|
|
157
157
|
|
|
158
158
|
// src/client.ts
|
|
159
|
-
|
|
159
|
+
async function request(baseUrl, path, init) {
|
|
160
|
+
try {
|
|
161
|
+
const response = await fetch(`${baseUrl}${path}`, init);
|
|
162
|
+
const status = response.status;
|
|
163
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
164
|
+
let value = null;
|
|
165
|
+
if (status !== 204) {
|
|
166
|
+
if (contentType.includes("application/json")) {
|
|
167
|
+
try {
|
|
168
|
+
value = await response.json();
|
|
169
|
+
} catch {
|
|
170
|
+
value = null;
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
const text = await response.text();
|
|
174
|
+
value = text.length > 0 ? text : null;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (response.ok) {
|
|
178
|
+
return {
|
|
179
|
+
data: value,
|
|
180
|
+
error: null,
|
|
181
|
+
status
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
const message = value && typeof value === "object" && "error" in value && typeof value.error === "string" ? value.error : typeof value === "string" && value.length > 0 ? value : `Request failed with status ${status}`;
|
|
185
|
+
return {
|
|
186
|
+
data: null,
|
|
187
|
+
error: {
|
|
188
|
+
status,
|
|
189
|
+
value,
|
|
190
|
+
message
|
|
191
|
+
},
|
|
192
|
+
status
|
|
193
|
+
};
|
|
194
|
+
} catch (error) {
|
|
195
|
+
return {
|
|
196
|
+
data: null,
|
|
197
|
+
error: {
|
|
198
|
+
status: 0,
|
|
199
|
+
message: error instanceof Error ? error.message : String(error)
|
|
200
|
+
},
|
|
201
|
+
status: 0
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function get(baseUrl, path) {
|
|
206
|
+
return request(baseUrl, path, {
|
|
207
|
+
method: "GET",
|
|
208
|
+
headers: {
|
|
209
|
+
accept: "application/json"
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
function postJson(baseUrl, path, body) {
|
|
214
|
+
return request(baseUrl, path, {
|
|
215
|
+
method: "POST",
|
|
216
|
+
headers: {
|
|
217
|
+
accept: "application/json",
|
|
218
|
+
"content-type": "application/json"
|
|
219
|
+
},
|
|
220
|
+
body: JSON.stringify(body)
|
|
221
|
+
});
|
|
222
|
+
}
|
|
160
223
|
function createClient(baseUrl) {
|
|
161
|
-
|
|
224
|
+
const normalizedBaseUrl = baseUrl.replace(/\/$/, "");
|
|
225
|
+
const swap = (params) => {
|
|
226
|
+
const swapId = encodeURIComponent(params.swapId);
|
|
227
|
+
return {
|
|
228
|
+
get: () => get(normalizedBaseUrl, `/swap/${swapId}`),
|
|
229
|
+
tx: {
|
|
230
|
+
post: (body) => postJson(normalizedBaseUrl, `/swap/${swapId}/tx`, body)
|
|
231
|
+
},
|
|
232
|
+
"refresh-step": {
|
|
233
|
+
post: (body) => postJson(normalizedBaseUrl, `/swap/${swapId}/refresh-step`, body)
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
};
|
|
237
|
+
swap.post = (body) => postJson(normalizedBaseUrl, "/swap", body);
|
|
238
|
+
return {
|
|
239
|
+
quote: {
|
|
240
|
+
post: (body) => postJson(normalizedBaseUrl, "/quote", body)
|
|
241
|
+
},
|
|
242
|
+
swap
|
|
243
|
+
};
|
|
162
244
|
}
|
|
163
245
|
|
|
164
246
|
// src/sdk.ts
|