import { APIGatewayEvent , APIGatewayProxyResult } from "aws-lambda" ; import axios , { AxiosError } from "axios" ; import { headers } from "../lambda-helpers" ; import charset from "charset" ; export const handler = async ( event ) : Promise < APIGatewayProxyResult > => { const { url } = JSON . parse ( event . body ) ; const fetch = ( requestHeaders ) => axios . get ( url , { headers : requestHeaders , responseType : "document" , responseEncoding : "base64" , } ) . then ( ( r ) => { const enc = charset ( r . headers ) || "utf8" ; return { statusCode : 200 , body : JSON . stringify ( r . data ) , headers : { ... headers ( event ) , "Content-Type" : `application/json;charset=${ enc }` , } , } ; } ) ; const handleError = ( e ) => ( { statusCode : e . response ?. status || 500 , body : e . response ?. data ? JSON . stringify ( e . response . data ) : e . message , headers : headers ( event ) , } ) ; const mostHeaders = { "Content-type" : "text/html" , accept : "*/*" , pragma : "no-cache" , } ; return fetch ( { "user-agent" : "Mozilla/5.0" , ... mostHeaders , } ) . catch ( ( e ) => [ 406 , 502 ] . includes ( e . response . status ) ? fetch ( mostHeaders ) : handleError ( e ) ) . catch ( handleError ) ; } ;