@duongtrungnguyen/next-helper
Version:
Helper library for Next.js 15
120 lines • 3.9 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 query_client_exports = {};
__export(query_client_exports, {
queryClient: () => queryClient
});
module.exports = __toCommonJS(query_client_exports);
var import_cache = require("./cache");
class QueryClientImpl {
constructor() {
/**
* A set to keep track of currently fetching queries.
*/
this.fetchingQueries = /* @__PURE__ */ new Set();
}
/**
* Converts a query key to a string.
* @param queryKey - The key of the query.
* @returns The string representation of the query key.
*/
getKeyString(queryKey) {
return JSON.stringify(queryKey);
}
/**
* Retrieves the data for a given query key.
* @param queryKey - The key of the query.
* @returns The data associated with the query key, or undefined if not found.
*/
getQueryData(queryKey) {
return import_cache.queryCache.get(queryKey);
}
/**
* Sets the data for a given query key.
* @param queryKey - The key of the query.
* @param data - The new data or a function that returns the new data based on the old data.
*/
setQueryData(queryKey, data) {
const currentData = import_cache.queryCache.get(queryKey);
const newData = typeof data === "function" ? data(currentData) : data;
import_cache.queryCache.set(queryKey, newData);
}
/**
* Invalidates all queries that match the given query key.
* @param queryKey - The key of the query.
*/
async invalidateQueries(queryKey) {
const matchingKeys = import_cache.queryCache.getMatchingKeys(queryKey);
matchingKeys.forEach((key) => {
import_cache.queryCache.invalidate(key);
});
}
/**
* Refetches all queries that match the given query key.
* @param queryKey - The key of the query.
*/
async refetchQueries(queryKey) {
const matchingKeys = import_cache.queryCache.getMatchingKeys(queryKey);
matchingKeys.forEach((key) => {
import_cache.queryCache.invalidate(key);
});
}
/**
* Removes all queries that match the given query key.
* @param queryKey - The key of the query.
*/
removeQueries(queryKey) {
const matchingKeys = import_cache.queryCache.getMatchingKeys(queryKey);
matchingKeys.forEach((key) => {
import_cache.queryCache.remove(key);
});
}
/**
* Clears all queries from the cache.
*/
clear() {
import_cache.queryCache.clear();
}
/**
* Returns the number of currently fetching queries.
* @returns The number of fetching queries.
*/
isFetching() {
return this.fetchingQueries.size;
}
/**
* Sets the fetching state for a given query key.
* @param queryKey - The key of the query.
* @param isFetching - Whether the query is currently fetching.
*/
setFetching(queryKey, isFetching) {
const keyString = this.getKeyString(queryKey);
if (isFetching) {
this.fetchingQueries.add(keyString);
} else {
this.fetchingQueries.delete(keyString);
}
}
}
const queryClient = new QueryClientImpl();
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
queryClient
});
//# sourceMappingURL=query-client.js.map