11import 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