I have a AWS Lambda instance that connects to a defined AWS API Gateway. If I enable CORS and give the access-control-allow-origin
a definition of http:/
I did something like this:
const handler: APIGatewayProxyHandler = async (event) => {
const origin = event?.headers?.Origin || event?.headers?.origin;
const allowedOrigins = ['https://example.com'];
const headers = {
'Access-Control-Allow-Origin': allowedOrigins.includes(origin)
? origin
: allowedOrigins[0],
};
return {
headers,
body: JSON.stringify({
myResponse: 'data',
}),
statusCode: 200,
};
};
Can then test via chrome dev tools by going to your client domain and running a fetch in the console:
fetch('https://exampleLambda.com/v1/example', {
method: 'get',
mode: 'cors'
headers: new Headers({
'Authorization': 'Bearer 12345,
}),
})
.then(result => result.json())
.then(console.log)