Skip to content

Commit 495e7d3

Browse files
committed
fix:updated api method implementation
Signed-off-by: Amitkanswal <[email protected]>
1 parent fc6d50e commit 495e7d3

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed

src/types/api.type.ts

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

src/uiLocation.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ import { GenericObjectType, RequestOption } from "./types/common.types";
3333
import { User } from "./types/user.types";
3434
import { formatAppRegion, onData, onError } from "./utils/utils";
3535
import Window from "./window";
36-
import { dispatchApiRequest, dispatchPostRobotRequest } from './utils/adapter';
37-
import {ProxyConfig } from './types/api.type';
36+
import { dispatchApiRequest, dispatchAdapter } from './utils/adapter';
37+
import {ProxyConfig, 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) => dispatchPostRobotRequest(this.postRobot)(config) as Promise<Response>;
485+
createAdapter = (config: ProxyConfig) => dispatchAdapter(this.postRobot)(config) as Promise<ProxyResponse>;
486486

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

src/utils/adapter.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import PostRobot from 'post-robot';
2-
import axios, { AxiosRequestConfig, AxiosResponse} from 'axios';
3-
4-
import { onData, onError, convertHeaders,convertAxiosHeadersToHeadersInit } from './utils';
2+
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
3+
import { onError, convertHeaders, convertAxiosHeadersToHeadersInit } from './utils';
54

65
/**
76
* Dispatches a request using PostRobot.
87
* @param postRobot - The PostRobot instance.
98
* @returns A function that takes AxiosRequestConfig and returns a promise.
109
*/
11-
export const dispatchPostRobotRequest = (postRobot: typeof PostRobot) => (config: AxiosRequestConfig)=> {
10+
export const dispatchAdapter = (postRobot: typeof PostRobot) => (config: AxiosRequestConfig)=> {
1211
return postRobot
1312
.sendToParent("apiAdapter", config )
14-
.then(onData)
13+
.then(({ data }) => ({ ...data, config }))
1514
.catch(onError);
1615
};
1716

@@ -21,7 +20,7 @@ export const dispatchPostRobotRequest = (postRobot: typeof PostRobot) => (config
2120
* @param options - Optional request options.
2221
* @returns A promise that resolves to a partial Response object.
2322
*/
24-
export const dispatchApiRequest = async (url: string, options?: RequestInit):Promise<Partial<Response>> => {
23+
export const dispatchApiRequest = async (url: string, options?: RequestInit): Promise<Partial<Response>> => {
2524
try {
2625
const config: AxiosRequestConfig = {
2726
url,
@@ -30,13 +29,13 @@ export const dispatchApiRequest = async (url: string, options?: RequestInit):Pro
3029
...(options?.body && { data: options?.body })
3130
};
3231

33-
const responseData = await dispatchPostRobotRequest(PostRobot)(config) as AxiosResponse;
34-
32+
const responseData = await dispatchAdapter(PostRobot)(config) as AxiosResponse;
33+
const isCallSuccessful = responseData.status >= 200 && responseData.status < 300;
3534
const fetchResponse: Partial<Response> = {
36-
ok: responseData.status >= 200 && responseData.status < 300,
35+
ok: isCallSuccessful,
3736
status: responseData.status,
3837
statusText: responseData.statusText,
39-
headers: new Headers(convertAxiosHeadersToHeadersInit(responseData.headers || {})),
38+
headers: new Headers(convertAxiosHeadersToHeadersInit(responseData.config.headers || {})),
4039
json: async () => responseData.data,
4140
text: async () => JSON.stringify(responseData.data),
4241
};
@@ -47,8 +46,8 @@ export const dispatchApiRequest = async (url: string, options?: RequestInit):Pro
4746
console.error("API request failed:", error.message);
4847
throw new Error(`API request failed: ${error.message}`);
4948
} else {
50-
console.error("An error occurred:", error);
51-
throw new Error("An error occurred");
49+
console.error("An unexpected error occurred:", error);
50+
throw new Error("An unexpected error occurred");
5251
}
5352
}
5453
};

0 commit comments

Comments
 (0)