Skip to content

Commit 00a0ca1

Browse files
committed
fix:addressed review changes
Signed-off-by: Amitkanswal <[email protected]>
1 parent 495e7d3 commit 00a0ca1

File tree

4 files changed

+37
-36
lines changed

4 files changed

+37
-36
lines changed

src/types/api.type.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { AxiosRequestConfig, AxiosResponse } from 'axios'
2-
export type ProxyConfig = AxiosRequestConfig
1+
import { AxiosRequestConfig, AxiosResponse, } from 'axios'
2+
export type RequestConfig = AxiosRequestConfig
33
export type ProxyResponse = AxiosResponse

src/uiLocation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { User } from "./types/user.types";
3434
import { formatAppRegion, onData, onError } from "./utils/utils";
3535
import Window from "./window";
3636
import { dispatchApiRequest, dispatchAdapter } from './utils/adapter';
37-
import {ProxyConfig, ProxyResponse } from './types/api.type';
37+
import {RequestConfig, ProxyResponse } from './types/api.type';
3838

3939
const emitter = new EventEmitter();
4040

@@ -482,7 +482,7 @@ class UiLocation {
482482
* Method used to create an adapter for management sdk.
483483
*/
484484

485-
createAdapter = (config: ProxyConfig) => dispatchAdapter(this.postRobot)(config) as Promise<ProxyResponse>;
485+
createAdapter = (config: RequestConfig) => dispatchAdapter(this.postRobot)(config) as Promise<ProxyResponse>;
486486

487487
/**
488488
* Method used to initialize the App SDK.

src/utils/adapter.ts

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PostRobot from 'post-robot';
2-
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
3-
import { onError, convertHeaders, convertAxiosHeadersToHeadersInit } from './utils';
2+
import { AxiosRequestConfig, AxiosResponse } from 'axios';
3+
import { onError, fetchToAxiosConfig } from './utils';
44

55
/**
66
* Dispatches a request using PostRobot.
@@ -20,34 +20,28 @@ export const dispatchAdapter = (postRobot: typeof PostRobot) => (config: AxiosRe
2020
* @param options - Optional request options.
2121
* @returns A promise that resolves to a partial Response object.
2222
*/
23-
export const dispatchApiRequest = async (url: string, options?: RequestInit): Promise<Partial<Response>> => {
23+
export const dispatchApiRequest = async (url: string, options?: RequestInit): Promise<Response> => {
2424
try {
25-
const config: AxiosRequestConfig = {
26-
url,
27-
method: options?.method || "GET",
28-
...(options?.headers && { headers: convertHeaders(options.headers) }),
29-
...(options?.body && { data: options?.body })
30-
};
31-
25+
const config = fetchToAxiosConfig(url, options);
3226
const responseData = await dispatchAdapter(PostRobot)(config) as AxiosResponse;
33-
const isCallSuccessful = responseData.status >= 200 && responseData.status < 300;
34-
const fetchResponse: Partial<Response> = {
35-
ok: isCallSuccessful,
27+
return new Response(responseData.data,{
3628
status: responseData.status,
3729
statusText: responseData.statusText,
38-
headers: new Headers(convertAxiosHeadersToHeadersInit(responseData.config.headers || {})),
39-
json: async () => responseData.data,
40-
text: async () => JSON.stringify(responseData.data),
41-
};
30+
headers: new Headers(responseData.config.headers || {}),
31+
});
4232

43-
return fetchResponse;
44-
} catch (error) {
45-
if (axios.isAxiosError(error)) {
46-
console.error("API request failed:", error.message);
47-
throw new Error(`API request failed: ${error.message}`);
33+
} catch (error: any) {
34+
if (error.response) {
35+
const fetchResponse = new Response(error.response.data, {
36+
status: error.response.status,
37+
statusText: error.response.statusText,
38+
headers: new Headers(error.response.headers)
39+
});
40+
return Promise.reject(fetchResponse);
41+
} else if (error.request) {
42+
return Promise.reject(new Response(null, { status: 0, statusText: 'Network Error' }));
4843
} else {
49-
console.error("An unexpected error occurred:", error);
50-
throw new Error("An unexpected error occurred");
44+
return Promise.reject(new Response(null, { status: 0, statusText: error.message }));
5145
}
5246
}
5347
};

src/utils/utils.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Region } from "../types";
2-
import { AxiosHeaders } from "axios";
2+
import { AxiosHeaders, AxiosRequestConfig } from "axios";
33

44
export function onData<Data extends Record<string, any>>(data: { data: Data }) {
55
if (typeof data.data === "string") {
@@ -69,10 +69,17 @@ export const convertHeaders = (headers: HeadersInit): AxiosHeaders => {
6969
return axiosHeaders;
7070
};
7171

72-
export const convertAxiosHeadersToHeadersInit = (axiosHeaders: any): HeadersInit => {
73-
const headers: HeadersInit = {};
74-
Object.keys(axiosHeaders).forEach(key => {
75-
headers[key] = axiosHeaders[key];
76-
});
77-
return headers;
78-
};
72+
export const fetchToAxiosConfig = (url: string, options: RequestInit = {}): AxiosRequestConfig => {
73+
const axiosConfig: AxiosRequestConfig = {
74+
url,
75+
method: options.method || 'GET',
76+
headers: options.headers ? convertHeaders({...options.headers}) : {},
77+
data: options.body,
78+
};
79+
80+
if (options.credentials === 'include') {
81+
axiosConfig.withCredentials = true;
82+
}
83+
84+
return axiosConfig;
85+
}

0 commit comments

Comments
 (0)