1- import PostRobot from 'post-robot' ;
2- import { AxiosRequestConfig , AxiosResponse } from 'axios' ;
3- import { onError , fetchToAxiosConfig } from './utils' ;
1+ import PostRobot from "post-robot" ;
2+ import { Response } from 'node-fetch' ;
3+ import { AxiosRequestConfig , AxiosResponse } from "axios" ;
4+
5+ import { onError , fetchToAxiosConfig , serializeAxiosResponse , handleApiError , sanitizeResponseHeader } from "./utils" ;
46
57/**
68 * Dispatches a request using PostRobot.
79 * @param postRobot - The PostRobot instance.
810 * @returns A function that takes AxiosRequestConfig and returns a promise.
911 */
10- export const dispatchAdapter = ( postRobot : typeof PostRobot ) => ( config : AxiosRequestConfig ) => {
12+ export const dispatchAdapter = ( postRobot : typeof PostRobot ) => ( config : AxiosRequestConfig ) : Promise < AxiosResponse > => {
1113 return postRobot
12- . sendToParent ( "apiAdapter" , config )
13- . then ( ( { data } ) => ( { ...data , config } ) )
14+ . sendToParent ( "apiAdapter" , config )
15+ . then ( ( event :unknown ) => {
16+ const { data } = event as { data : AxiosResponse } ;
17+ if ( data . status >= 400 ) {
18+ throw serializeAxiosResponse ( data , config ) ;
19+ }
20+ return serializeAxiosResponse ( data , config ) ;
21+ } )
1422 . catch ( onError ) ;
1523} ;
16-
1724/**
1825 * Dispatches an API request using axios and PostRobot.
1926 * @param url - The URL of the API endpoint.
2027 * @param options - Optional request options.
2128 * @returns A promise that resolves to a partial Response object.
2229 */
23- export const dispatchApiRequest = async ( url : string , options ?: RequestInit ) : Promise < Response > => {
24- try {
25- const config = fetchToAxiosConfig ( url , options ) ;
26- const responseData = await dispatchAdapter ( PostRobot ) ( config ) as AxiosResponse ;
27- return new Response ( responseData . data , {
28- status : responseData . status ,
29- statusText : responseData . statusText ,
30- headers : new Headers ( responseData . config . headers || { } ) ,
31- } ) ;
32-
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' } ) ) ;
43- } else {
44- return Promise . reject ( new Response ( null , { status : 0 , statusText : error . message } ) ) ;
45- }
30+ export const dispatchApiRequest = async (
31+ url : string ,
32+ options ?: RequestInit ,
33+ ) : Promise < Response > => {
34+ try {
35+ const config = fetchToAxiosConfig ( url , options ) ;
36+ const responseData = ( await dispatchAdapter ( PostRobot ) (
37+ config
38+ ) ) as AxiosResponse ;
39+
40+ return new Response ( responseData ?. data , {
41+ status : responseData . status ,
42+ statusText : responseData . statusText ,
43+ url : responseData . config . url ,
44+ headers : new Headers ( sanitizeResponseHeader ( responseData . config . headers || { } ) ) ,
45+ } ) ;
46+
47+ } catch ( error ) {
48+ return handleApiError ( error ) ;
4649 }
47- } ;
50+ } ;
0 commit comments