UNPKG

@nimpl/getters

Version:

Implementation of server getters in React Server Components without switching to SSR in next.js

58 lines (57 loc) 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAppParams = void 0; const work_async_storage_external_1 = require("next/dist/server/app-render/work-async-storage.external"); const server_getter_in_client_component_error_1 = require("./server-getter-in-client-component-error"); const utils_1 = require("./utils"); const get_app_pathname_1 = require("./get-app-pathname"); const getAppParams = (options = {}) => { (0, server_getter_in_client_component_error_1.serverGetterInClientComponentError)("getAppParams"); 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(); if (!pageConfigurationStore) return {}; const { route } = pageConfigurationStore; const pagePath = route; const urlPathname = (0, get_app_pathname_1.getAppPathname)(); 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.getAppParams = getAppParams;