Check if window.fetch exists before polyfilling it#362
Check if window.fetch exists before polyfilling it#362bobziroll wants to merge 1 commit intopretenderjs:masterfrom
window.fetch exists before polyfilling it#362Conversation
|
Please accept this PR as it will solve the Pretender Error in node.js 18+ |
|
@bobziroll Thank you for your help. I got error in other line that iterating |
Yes indeed, at the top of the constructor we need to initialize it to an empty array. I would also propose to validate the incoming verbs: const Verbs: Record<string, Verb> = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE',
PATCH: 'PATCH',
HEAD: 'HEAD',
OPTIONS: 'OPTIONS',
} as const;
const validatedVerb = (str: string): Verb => {
str = str.toUpperCase();
if (!Verbs[str]) {
const validVerbs = Object.values(Verbs).join(', ');
throw new Error(
`${str} is not a valid verb. Expected value one of [${validVerbs}]`
);
}
return Verbs[str];
};I also believe the map(fn: (pretender: Pretender) => void) {
n.call(this);
}I believe this should be: map(fn: (this: Pretender) => void) {
fn.call(this);
}This way we tie what context the function can be called with. A better API would be: map(fn: (this: Pretender) => void): Pretender {
fn.call(this);
return this;
}This way we could nicely chain our operations together: const myPretender = new Pretender()
.map(authenticationRoutes);
.map(songsRoutes);Now I can also see another Type error in the following line: requiresManualResolution(verbStr: string, path: string) {
let verb = validatedVerb(verbStr);
let handler = this._handlerFor(verb, path, {}); <-- object is not a valid argument
// etc...
}
Sorry @bobziroll if I spammed your PR here. 😅 |
Sorry if this is presumptuous of me, I haven't done much by way of contributing code to OS before, so please let me know if I need to do something before submitting this.
I'm running into an issue where
whatwg-fetchis hijacking the Response object that React Router checks when redirecting. React Router ≥v6.4.5 duck-type checks the Response object, checking specifically if there is abodyproperty included. (Since it's required by W3 Spec).I guess
whatwg-fetchdoesn't support the body property, but instead injects a_bodyInitproperty in its place. Something to do with ReadableStreams or whatnot, I'm not sure 😝That said, I was curious why pretender is using the polyfill in the first place, since my browser is a modern,
fetch-enabled browser.Please let me know if there's another, better way I could contribute to this, or help me better understand why a PR like this doesn't make sense. Thanks in advance! 🙏