Created
September 26, 2024 05:23
-
-
Save jb510/b898c75df7879c38ba052d97cc9a04ff to your computer and use it in GitHub Desktop.
CloudFlare Worker to Proxy WordPress.org API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function handleRequest(request) { | |
const url = new URL(request.url); | |
// Modify the URL to target WordPress.org | |
if (url.pathname.startsWith('/plugins/')) { | |
url.hostname = 'api.wordpress.org'; | |
url.pathname = url.pathname.replace('/plugins/', '/plugins/info/1.2/'); | |
} else if (url.pathname.startsWith('/themes/')) { | |
url.hostname = 'api.wordpress.org'; | |
url.pathname = url.pathname.replace('/themes/', '/themes/info/1.2/'); | |
} else { | |
return new Response('Invalid Request', { status: 400 }); | |
} | |
// Fetch the request from the WordPress API | |
const response = await fetch(url.toString(), { | |
method: request.method, | |
headers: request.headers, | |
}); | |
return response; | |
} | |
addEventListener('fetch', event => { | |
event.respondWith(handleRequest(event.request)); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment