11import PostRobot from 'post-robot' ;
2- import { onData , onError } from './utils' ;
3- import { ApiRequestParams } from '../types/api.type' ;
4- import { RequestOption , GenericObjectType } from '../types/common.types' ;
2+ import axios , { AxiosRequestConfig , AxiosResponse } from 'axios' ;
53
6- export const dispatchPostRobotRequest = ( postRobot : typeof PostRobot ) => ( url :string , opts ?: RequestOption ) : Promise < GenericObjectType > => {
4+ import { onData , onError , convertHeaders , convertAxiosHeadersToHeadersInit } from './utils' ;
5+
6+ /**
7+ * Dispatches a request using PostRobot.
8+ * @param postRobot - The PostRobot instance.
9+ * @returns A function that takes AxiosRequestConfig and returns a promise.
10+ */
11+ export const dispatchPostRobotRequest = ( postRobot : typeof PostRobot ) => ( config : AxiosRequestConfig ) => {
712 return postRobot
8- . sendToParent ( "apiAdapter" , { url , option : opts } )
13+ . sendToParent ( "apiAdapter" , config )
914 . then ( onData )
1015 . catch ( onError ) ;
1116} ;
1217
13- export const createSDKAdapter = ( postRobot : typeof PostRobot ) => async ( config : ApiRequestParams ) => {
18+ /**
19+ * Dispatches an API request using axios and PostRobot.
20+ * @param url - The URL of the API endpoint.
21+ * @param options - Optional request options.
22+ * @returns A promise that resolves to a partial Response object.
23+ */
24+ export const dispatchApiRequest = async ( url : string , options ?: RequestInit ) :Promise < Partial < Response > > => {
1425 try {
15- const data = await dispatchPostRobotRequest ( postRobot ) ( config . url , {
16- baseURL : config . baseURL ,
17- url : config . url ,
18- method : config . method ,
19- headers : config . headers ,
20- body : config . data as BodyInit ,
21- } ) ;
22- return {
23- data,
24- status : data ?. status || 200 ,
25- statusText : 'OK' ,
26- headers : config . headers || { } ,
26+ const config : AxiosRequestConfig = {
27+ url,
28+ method : options ?. method || "GET" ,
29+ ...( options ?. headers && { headers : convertHeaders ( options . headers ) } ) ,
30+ ...( options ?. body && { data : options ?. body } )
2731 } ;
28- } catch ( error ) {
29- const typedError = error as GenericObjectType & { status ?: number ; statusText ?: string ; headers ?: Record < string , string > ; body ?: any ; message ?: string } ;
30- return {
31- data : typedError . body || typedError . message || typedError . data ,
32- status : typedError . status || 500 ,
33- statusText : typedError . statusText || 'Internal Server Error' ,
34- headers : typedError . headers || { } ,
32+
33+ const responseData = await dispatchPostRobotRequest ( PostRobot ) ( config ) as AxiosResponse ;
34+
35+ const fetchResponse : Partial < Response > = {
36+ ok : responseData . status >= 200 && responseData . status < 300 ,
37+ status : responseData . status ,
38+ statusText : responseData . statusText ,
39+ headers : new Headers ( convertAxiosHeadersToHeadersInit ( responseData . headers || { } ) ) ,
40+ json : async ( ) => responseData . data ,
41+ text : async ( ) => JSON . stringify ( responseData . data ) ,
3542 } ;
43+
44+ return fetchResponse ;
45+ } catch ( error ) {
46+ if ( axios . isAxiosError ( error ) ) {
47+ console . error ( "API request failed:" , error . message ) ;
48+ throw new Error ( `API request failed: ${ error . message } ` ) ;
49+ } else {
50+ console . error ( "An error occurred:" , error ) ;
51+ throw new Error ( "An error occurred" ) ;
52+ }
3653 }
37- } ;
54+ } ;
0 commit comments