Skip to content

Instantly share code, notes, and snippets.

@jb510
Created September 26, 2024 05:23
Show Gist options
  • Select an option

  • Save jb510/b898c75df7879c38ba052d97cc9a04ff to your computer and use it in GitHub Desktop.

Select an option

Save jb510/b898c75df7879c38ba052d97cc9a04ff to your computer and use it in GitHub Desktop.

Revisions

  1. jb510 created this gist Sep 26, 2024.
    26 changes: 26 additions & 0 deletions wporg-proxy.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    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));
    });