@nimpl/getters
Version:
Implementation of server getters in React Server Components without switching to SSR in next.js
60 lines (59 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getParams = void 0;
const work_async_storage_external_1 = require("next/dist/server/app-render/work-async-storage.external");
const work_unit_async_storage_external_1 = require("next/dist/server/app-render/work-unit-async-storage.external");
const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error");
const utils_1 = require("./utils");
const getParams = (options = {}) => {
(0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getParams");
const store = work_async_storage_external_1.workAsyncStorage.getStore();
if (!store)
return {};
const { ignoreDifferenceError, pagePaths, pathname } = options;
const pageConfigurationStore = work_async_storage_external_1.workAsyncStorage.getStore();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pageStore = work_unit_async_storage_external_1.workUnitAsyncStorage.getStore();
if (!pageConfigurationStore)
return {};
const { route } = pageConfigurationStore;
const pagePath = route;
const urlPathname = pageStore?.url?.pathname;
const targetUrlPathname = pathname || urlPathname;
if (!targetUrlPathname)
return {};
let isInvalid = false;
try {
if (pagePaths) {
for (const userPagePath of pagePaths) {
const params = (0, utils_1.parseParams)(targetUrlPathname, userPagePath);
if (params === utils_1.INVALID_PARSE) {
isInvalid ||= true;
continue;
}
return params || {};
}
}
else {
const params = (0, utils_1.parseParams)(targetUrlPathname, pagePath);
if (params === utils_1.INVALID_PARSE) {
isInvalid ||= true;
}
else {
return params || {};
}
}
}
catch {
isInvalid = true;
}
if (isInvalid && !ignoreDifferenceError) {
const createIssueUrl = new URL("https://github.com/alexdln/nimpl-getters/issues/new");
createIssueUrl.searchParams.set("title", "Error parsing segments in get-params");
createIssueUrl.searchParams.set("body", `urlPathname: \`${urlPathname}\`;\n\npagePath(s): \`${pagePaths || pagePath}\`;`);
createIssueUrl.searchParams.append("labels", "bug");
throw new Error(`Something went wrong. Please create an issue on Github: ${createIssueUrl}`);
}
return {};
};
exports.getParams = getParams;