aboutsummaryrefslogtreecommitdiff
path: root/src/js/thirdparty/vercel_fetch.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/thirdparty/vercel_fetch.js')
-rw-r--r--src/js/thirdparty/vercel_fetch.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/js/thirdparty/vercel_fetch.js b/src/js/thirdparty/vercel_fetch.js
new file mode 100644
index 000000000..9d9be6c17
--- /dev/null
+++ b/src/js/thirdparty/vercel_fetch.js
@@ -0,0 +1,32 @@
+// This is just a no-op. Intent is to prevent importing a bunch of stuff that isn't relevant.
+export default (wrapper = Bun.fetch) => {
+ async function vercelFetch(url, opts = {}) {
+ // Convert Object bodies to JSON if they are JS objects
+ if (
+ opts.body &&
+ typeof opts.body === "object" &&
+ (!("buffer" in opts.body) || typeof opts.body.buffer !== "object" || !(opts.body.buffer instanceof ArrayBuffer))
+ ) {
+ opts.body = JSON.stringify(opts.body);
+ // Content length will automatically be set
+ if (!opts.headers) opts.headers = new Headers();
+
+ opts.headers.set("Content-Type", "application/json");
+ }
+
+ try {
+ return await wrapper(url, opts);
+ } catch (err) {
+ if (typeof err === "string") {
+ err = new Error(err);
+ }
+
+ err.url = url;
+ err.opts = opts;
+ throw err;
+ }
+ }
+
+ vercelFetch.default = vercelFetch;
+ return vercelFetch;
+};