Skip to content

Instantly share code, notes, and snippets.

@jb510
Created September 26, 2024 05:23
Show Gist options
  • Save jb510/b898c75df7879c38ba052d97cc9a04ff to your computer and use it in GitHub Desktop.
Save jb510/b898c75df7879c38ba052d97cc9a04ff to your computer and use it in GitHub Desktop.
CloudFlare Worker to Proxy WordPress.org API
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