nuqs
Version:
119 lines (114 loc) • 4.61 kB
JavaScript
import { createContext, createElement, useContext } from "react";
//#region src/lib/debug.ts
const debugEnabled = isDebugEnabled();
function debug(message, ...args) {
if (!debugEnabled) return;
const msg = sprintf(message, ...args);
performance.mark(msg);
try {
console.log(message, ...args);
} catch {
console.log(msg);
}
}
function warn(message, ...args) {
if (!debugEnabled) return;
console.warn(message, ...args);
}
function sprintf(base, ...args) {
return base.replace(/%[sfdO]/g, (match) => {
const arg = args.shift();
return match === "%O" && arg ? JSON.stringify(arg).replace(/"([^"]+)":/g, "$1:") : String(arg);
});
}
function isDebugEnabled() {
if (typeof window === "undefined") return (process.env.DEBUG || "").includes("nuqs");
try {
const test = "nuqs-localStorage-test";
if (typeof localStorage === "undefined") return false;
localStorage.setItem(test, test);
const isStorageAvailable = localStorage.getItem(test) === test;
localStorage.removeItem(test);
return isStorageAvailable && (localStorage.getItem("debug") || "").includes("nuqs");
} catch {
return false;
}
}
//#endregion
//#region src/lib/errors.ts
const errors = {
303: "Multiple adapter contexts detected. This might happen in monorepos.",
404: "nuqs requires an adapter to work with your framework.",
409: "Multiple versions of the library are loaded. This may lead to unexpected behavior. Currently using `%s`, but `%s` (via the %s adapter) was about to load on top.",
414: "Max safe URL length exceeded. Some browsers may not be able to accept this URL. Consider limiting the amount of state stored in the URL.",
422: "Invalid options combination: `limitUrlUpdates: debounce` should be used in SSR scenarios, with `shallow: false`",
429: "URL update rate-limited by the browser. Consider increasing `throttleMs` for key(s) `%s`. %O",
500: "Empty search params cache. Search params can't be accessed in Layouts.",
501: "Search params cache already populated. Have you called `parse` twice?"
};
function error(code) {
return `[nuqs] ${errors[code]}
See https://nuqs.dev/NUQS-${code}`;
}
//#endregion
//#region src/lib/url-encoding.ts
function renderQueryString(search) {
if (search.size === 0) return "";
const query = [];
for (const [key, value] of search.entries()) {
const safeKey = key.replace(/#/g, "%23").replace(/&/g, "%26").replace(/\+/g, "%2B").replace(/=/g, "%3D").replace(/\?/g, "%3F");
query.push(`${safeKey}=${encodeQueryValue(value)}`);
}
const queryString = "?" + query.join("&");
warnIfURLIsTooLong(queryString);
return queryString;
}
function encodeQueryValue(input) {
return input.replace(/%/g, "%25").replace(/\+/g, "%2B").replace(/ /g, "+").replace(/#/g, "%23").replace(/&/g, "%26").replace(/"/g, "%22").replace(/'/g, "%27").replace(/`/g, "%60").replace(/</g, "%3C").replace(/>/g, "%3E").replace(/[\x00-\x1F]/g, (char) => encodeURIComponent(char));
}
const URL_MAX_LENGTH = 2e3;
function warnIfURLIsTooLong(queryString) {
if (process.env.NODE_ENV === "production") return;
if (typeof location === "undefined") return;
const url = new URL(location.href);
url.search = queryString;
if (url.href.length > URL_MAX_LENGTH) console.warn(error(414));
}
//#endregion
//#region src/adapters/lib/context.ts
const context = createContext({ useAdapter() {
throw new Error(error(404));
} });
context.displayName = "NuqsAdapterContext";
if (debugEnabled && typeof window !== "undefined") {
if (window.__NuqsAdapterContext && window.__NuqsAdapterContext !== context) console.error(error(303));
window.__NuqsAdapterContext = context;
}
/**
* Create a custom adapter (context provider) for nuqs to work with your framework / router.
*
* Adapters are based on React Context,
*
* @param useAdapter
* @returns
*/
function createAdapterProvider(useAdapter) {
return ({ children, defaultOptions, processUrlSearchParams, ...props }) => createElement(context.Provider, {
...props,
value: {
useAdapter,
defaultOptions,
processUrlSearchParams
}
}, children);
}
function useAdapter(watchKeys) {
const value = useContext(context);
if (!("useAdapter" in value)) throw new Error(error(404));
return value.useAdapter(watchKeys);
}
const useAdapterDefaultOptions = () => useContext(context).defaultOptions;
const useAdapterProcessUrlSearchParams = () => useContext(context).processUrlSearchParams;
//#endregion
export { useAdapterProcessUrlSearchParams as a, debug as c, useAdapterDefaultOptions as i, warn as l, createAdapterProvider as n, renderQueryString as o, useAdapter as r, error as s, context as t };
//# sourceMappingURL=context-C4spomkL.js.map