@rb2bv/cache-handler
Version:
Next.js self-hosting simplified.
100 lines (98 loc) • 3.23 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/handlers/server.ts
var server_exports = {};
__export(server_exports, {
default: () => createHandler
});
module.exports = __toCommonJS(server_exports);
function createHandler({ baseUrl, timeoutMs }) {
return {
name: "server",
async get(key, { implicitTags }) {
const url = new URL("/get", baseUrl);
url.searchParams.set("key", key);
url.searchParams.set("implicitTags", JSON.stringify(implicitTags));
const response = await fetch(url, {
signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : void 0,
// @ts-expect-error -- act as an internal fetch call
next: {
internal: true
}
});
if (response.status === 404) {
return null;
}
if (!response.ok) {
throw new Error(`get error: ${response.status}`);
}
const string = await response.text();
return JSON.parse(string);
},
async set(key, cacheHandlerValue) {
const url = new URL("/set", baseUrl);
const response = await fetch(url, {
method: "POST",
body: JSON.stringify([key, JSON.stringify(cacheHandlerValue)]),
headers: {
"Content-Type": "application/json"
},
signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : void 0,
// @ts-expect-error -- act as an internal fetch call
next: {
internal: true
}
});
if (!response.ok) {
throw new Error(`set error: ${response.status}`);
}
},
async revalidateTag(tag) {
const url = new URL("/revalidateTag", baseUrl);
const response = await fetch(url, {
method: "POST",
body: JSON.stringify([tag]),
headers: {
"Content-Type": "application/json"
},
signal: timeoutMs ? AbortSignal.timeout(timeoutMs) : void 0,
// @ts-expect-error -- act as an internal fetch call
next: {
internal: true
}
});
if (!response.ok) {
throw new Error(`revalidateTag error: ${response.status}`);
}
},
async delete(key) {
const url = new URL(`/${key}`, baseUrl);
const response = await fetch(url, {
method: "DELETE",
// @ts-expect-error -- act as an internal fetch call
next: {
internal: true
}
});
if (!response.ok) {
throw new Error(`delete error: ${response.status}`);
}
}
};
}