@duongtrungnguyen/next-helper
Version:
Helper library for Next.js 15
111 lines • 3.85 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);
var server_exports = {};
__export(server_exports, {
createQueryFn: () => createQueryFn,
fetchQuery: () => fetchQuery,
getQueryData: () => getQueryData,
prefetchQuery: () => prefetchQuery,
setQueryData: () => setQueryData
});
module.exports = __toCommonJS(server_exports);
var import_cache = require("./cache");
var import_http = require("../query/http");
var import_configs = require("../configs");
async function fetchQuery(options) {
const {
queryKey,
queryFn,
staleTime = 5 * 60 * 1e3,
// 5 minutes
cacheTime = 30 * 60 * 1e3,
// 30 minutes
retry = 3,
retryDelay = (attempt) => Math.min(1e3 * 2 ** attempt, 3e4),
initialData,
select = (data) => data
} = options;
const cachedData = import_cache.queryCache.get(queryKey);
if (cachedData !== void 0 && !import_cache.queryCache.isStale(queryKey)) {
return select(cachedData);
}
if (cachedData !== void 0) {
const result = select(cachedData);
executeQueryAndCache().catch((error) => {
console.error("Background fetch error:", error);
});
return result;
}
if (initialData !== void 0 && cachedData === void 0) {
const initialValue = typeof initialData === "function" ? initialData() : initialData;
if (initialValue !== void 0) {
import_cache.queryCache.set(queryKey, initialValue, staleTime, cacheTime);
return select(initialValue);
}
}
return executeQueryAndCache();
async function executeQueryAndCache() {
let attempts = 0;
const maxRetries = typeof retry === "number" ? retry : retry === true ? 3 : 0;
const executeQuery = async () => {
try {
const result = await queryFn({}, { queryKey });
import_cache.queryCache.set(queryKey, result, staleTime, cacheTime);
return select(result);
} catch (error) {
if (attempts >= maxRetries) {
throw error;
}
attempts++;
const delay = typeof retryDelay === "function" ? retryDelay(attempts) : typeof retryDelay === "number" ? retryDelay : Math.min(1e3 * 2 ** attempts, 3e4);
await new Promise((resolve) => setTimeout(resolve, delay));
return executeQuery();
}
};
return executeQuery();
}
}
function createQueryFn(url, params) {
return async () => {
const http = new import_http.HttpClient(url.includes("http") ? "" : import_configs.libConfig.baseUrl);
return http.get(url, { query: params });
};
}
async function prefetchQuery(options) {
try {
await fetchQuery(options);
} catch (error) {
console.error("Error prefetching query:", error);
}
}
function getQueryData(queryKey) {
return import_cache.queryCache.get(queryKey);
}
function setQueryData(queryKey, data, staleTime = 5 * 60 * 1e3, cacheTime = 30 * 60 * 1e3) {
import_cache.queryCache.set(queryKey, data, staleTime, cacheTime);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createQueryFn,
fetchQuery,
getQueryData,
prefetchQuery,
setQueryData
});
//# sourceMappingURL=server.js.map