mirror of
https://github.com/deployflare/WorkerTemplate.git
synced 2026-01-13 16:18:01 +00:00
Update index.js
This commit is contained in:
28
index.js
28
index.js
@@ -1,22 +1,12 @@
|
||||
// A simple, secure proxy for the Cloudflare API
|
||||
export default {
|
||||
async fetch(request) {
|
||||
const corsHeaders = {
|
||||
const corsHeaders = {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Authorization, Content-Type',
|
||||
};
|
||||
if (request.method === 'OPTIONS') {
|
||||
return new Response(null, { headers: corsHeaders });
|
||||
}
|
||||
const url = new URL(request.url);
|
||||
const apiUrl = `https://api.cloudflare.com${url.pathname}${url.search}`;
|
||||
const newRequest = new Request(apiUrl, request);
|
||||
const response = await fetch(newRequest);
|
||||
const newResponse = new Response(response.body, response);
|
||||
Object.entries(corsHeaders).forEach(([key, value]) => {
|
||||
newResponse.headers.set(key, value);
|
||||
});
|
||||
return newResponse;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function fetch(request) {
|
||||
if (request.method === 'OPTIONS') return new Response(null, { headers: corsHeaders })
|
||||
return new Response('OK', { headers: corsHeaders })
|
||||
}
|
||||
|
||||
export default { fetch }
|
||||
|
||||
Reference in New Issue
Block a user