import { PikkuFetchHTTPResponse, fetchData } from '@pikku/core/http' import type { APIGatewayEvent, APIGatewayProxyResult } from 'aws-lambda' import { responseToLambdaResult } from '../response-converter.js' import { lambdaEventToRequest } from '../request-converter.js' export const runFetch = async ( event: APIGatewayEvent ): Promise => { const request = lambdaEventToRequest(event) const response = new PikkuFetchHTTPResponse() if (request.method === 'OPTIONS') { response.header( 'Access-Control-Allow-Headers', 'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent' ) response.header( 'Access-Control-Allow-Methods', 'OPTIONS,DELETE,GET,HEAD,PATCH,POST,PUT' ) response.status(200) return responseToLambdaResult(response.toResponse()) } try { await fetchData(request, response) } catch { // Error should have already been handled by fetch } return responseToLambdaResult(response.toResponse()) }