cloudflare-worker-local
cloudflare-worker-local copied to clipboard
Response.redirect is not a function
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));
});
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 ).
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.