Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG FIX]: Axios to Fetch Migration #11936

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 36 additions & 16 deletions ecosystem/typescript/aptos-client/src/index.browser.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
import axios, { AxiosRequestConfig, AxiosError } from "axios";
import { AptosClientRequest, AptosClientResponse } from "./types";

export default async function aptosClient<Res>(options: AptosClientRequest): Promise<AptosClientResponse<Res>> {
const { params, method, url, headers, body, overrides } = options;
const requestConfig: AxiosRequestConfig = {
headers,
method,
url,
params,
data: body,
withCredentials: overrides?.WITH_CREDENTIALS ?? true,
const { params, method, url, headers, body } = options;

// Constructing the query string for POST requests
let queryString = '';
if (params && method.toUpperCase() === 'POST') {
queryString = `?address=${params.address}&amount=${params.amount}`
}

// Setting up the Request Configuration for fetch
const requestConfig: RequestInit = {
method: method,
headers: headers,
};
if (body) {
if (body instanceof Uint8Array) {
requestConfig.body = Buffer.from(body);
} else {
requestConfig.body = Buffer.from(JSON.stringify(body));
}
}

try {
const response = await axios(requestConfig);
const response = await fetch(url + queryString, requestConfig);
const responseData = await response.json();

return {
status: response.status,
statusText: response.statusText!,
data: response.data,
statusText: response.statusText,
data: responseData as Res,
headers: response.headers,
config: response.config,
request: requestConfig,
response: response,
};
} catch (error) {
const axiosError = error as AxiosError<Res>;
if (axiosError.response) {
return axiosError.response;
if (error instanceof Response) {
const errorResponse = await error.json();
return {
status: error.status,
statusText: error.statusText,
data: errorResponse,
headers: error.headers,
request: requestConfig,
response: error,
};
}
throw error;
}
Expand Down
Loading