Skip to content

Commit

Permalink
feat(fetch): implement Response.error, Response.redirect and Response…
Browse files Browse the repository at this point in the history
….error
  • Loading branch information
ardatan committed Sep 12, 2022
1 parent 85f86d4 commit c9f05f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-games-impress.md
@@ -0,0 +1,5 @@
---
'@whatwg-node/fetch': patch
---

Add ponyfills for Response.redirect, Response.json and Response.error
29 changes: 29 additions & 0 deletions packages/fetch/dist/create-node-ponyfill.js
Expand Up @@ -319,5 +319,34 @@ module.exports = function createNodePonyfill(opts = {}) {

}
}

if (!ponyfills.Response.redirect) {
ponyfills.Response.redirect = function (url, status = 302) {
return new ponyfills.Response(null, {
status,
headers: {
Location: url,
},
});
};
}
if (!ponyfills.Response.json) {
ponyfills.Response.json = function (data, init = {}) {
return new ponyfills.Response(JSON.stringify(data), {
...init,
headers: {
"Content-Type": "application/json",
...init.headers,
},
});
};
}
if (!ponyfills.Response.error) {
ponyfills.Response.error = function () {
return new ponyfills.Response(null, {
status: 500,
});
};
}
return ponyfills;
}

0 comments on commit c9f05f2

Please sign in to comment.