cloudflare-worker-local icon indicating copy to clipboard operation
cloudflare-worker-local copied to clipboard

Response.redirect is not a function

Open stelescuraul opened this issue 4 years ago • 2 comments

I`d like to be able to test the redirect functionality locally (as dummy test at least), but the Response.redirect function is not defined.

Do you have any plans on supporting it ?

const handler = async function(request) {
  return Response.redirect('some_url', 200);
};

addEventListener('fetch', async function(event) {
  event.respondWith(handler(event.request));
});

stelescuraul avatar Mar 03 '20 09:03 stelescuraul

Thanks for the bug report. Give me a day or two to think about this. Intuitively, this should be supported. However, I just expose Request / Response as it’s provided by node-fetch, who has explicitly mentioned that it isn’t supported.

Would you mind opening the ticket in that repo / post back with why they aren’t supporting it?

If for whatever reason, node-fetch won’t support it, then I’ll accept a pr for the same

On Tue, Mar 3 2020 at 3:23 PM, Raul Stelescu < [email protected] > wrote:

I`d like to be able to test the redirect functionality locally (as dummy test at least), but the Response.redirect function is not defined.

Do you have any plans on supporting it ?

const handler = async function ( request ) { return Response. redirect ( ' some_url ' , 200 ); }; addEventListener ( ' fetch ' , async function ( event ) { event. respondWith ( handler ( event. request )); });

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub ( https://github.com/gja/cloudflare-worker-local/issues/39?email_source=notifications&email_token=AAAIPOVGRJSWJTILJLOJDYLRFTHS3A5CNFSM4LAGUYXKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IR63K4A ) , or unsubscribe ( https://github.com/notifications/unsubscribe-auth/AAAIPOUNB6R5R5MM7TAD5ILRFTHS3ANCNFSM4LAGUYXA ).

gja avatar Mar 04 '20 14:03 gja

node-fetch doesn't have it supported but isomorphic-fetch does. I just copy pasted the shim from isomorphic-fetch into my project:

const redirectStatuses = [301, 302, 303, 307, 308];

if (!Response.redirect) {
  Response.redirect = function(url, status= 302) {
    if (redirectStatuses.indexOf(status) === -1) {
      throw new RangeError(`Invalid status code: ${status}`);
    }

    return new Response(null, { status: status, headers: { location: url } });
  }
}

I think an upstream PR would be more appropriate.

innovate-invent avatar Mar 19 '20 17:03 innovate-invent