UNPKG

next-translate-routes-multi-domain

Version:

Flexible and translated routes for Next.js without custom server

152 lines 6.85 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fileUrlToUrl = exports.getTranslatedPathPattern = void 0; var normalize_trailing_slash_1 = require("next/dist/client/normalize-trailing-slash"); var path_to_regexp_1 = require("path-to-regexp"); var url_1 = require("url"); var regex_1 = require("../shared/regex"); var withNtrPrefix_1 = require("../shared/withNtrPrefix"); var ntrData_1 = require("./ntrData"); var urlToUrlObject_1 = require("./urlToUrlObject"); var getPatternFromRoutePaths = function (routeBranch, locale) { var pattern = routeBranch.paths[locale] || routeBranch.paths.default; return regex_1.ignoreSegmentPathRegex.test(pattern) ? '' : pattern; }; /** Add `/` prefix only if `pattern` is not an empty string and does not already have it */ var addSlashPrefix = function (path) { return (path === '' || path.startsWith('/') ? path : "/".concat(path)); }; var getPathPatternPart = function (_a) { var _b; var routeBranch = _a.routeBranch, locale = _a.locale, isLastPathPart = _a.isLastPathPart; var pattern = getPatternFromRoutePaths(routeBranch, locale); if (isLastPathPart) { if ((_b = routeBranch.children) === null || _b === void 0 ? void 0 : _b.length) { var indexChild = routeBranch.children.find(function (child) { var _a; return child.name === 'index' && !((_a = child.children) === null || _a === void 0 ? void 0 : _a.length); }); if (!indexChild) { throw new Error("No index file found in \"".concat(routeBranch.name, "\" folder.")); } var indexChildPattern = getPatternFromRoutePaths(indexChild, locale); if (indexChildPattern && indexChildPattern !== 'index') { pattern = pattern + addSlashPrefix(indexChildPattern); } } else if (pattern === 'index' && routeBranch.name === 'index') { pattern = ''; } } return addSlashPrefix(pattern); }; /** * Get path pattern (path-to-regexp synthax) from file path parts * * Ex: `/[dynamic]/path` => `/:dynamic/path` */ var getTranslatedPathPattern = function (_a) { var e_1, _b; var _c; var routeBranch = _a.routeBranch, pathParts = _a.pathParts, locale = _a.locale; var isLastPathPart = pathParts.length === 0; var currentPathPatternPart = getPathPatternPart({ routeBranch: routeBranch, locale: locale, isLastPathPart: isLastPathPart }); if (isLastPathPart) { return currentPathPatternPart; } var nextPathPart = pathParts[0]; var remainingPathParts = pathParts.slice(1); try { for (var _d = __values(routeBranch.children || []), _e = _d.next(); !_e.done; _e = _d.next()) { var child = _e.value; if ( // child name must match nextPathPart child.name === nextPathPart && // child.children must be coherent with remaining path parts is case a file and and folder share the same name (remainingPathParts.length === 0 || ((_c = child.children) === null || _c === void 0 ? void 0 : _c.length))) { return (currentPathPatternPart + (0, exports.getTranslatedPathPattern)({ routeBranch: child, pathParts: remainingPathParts, locale: locale, })); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_e && !_e.done && (_b = _d.return)) _b.call(_d); } finally { if (e_1) throw e_1.error; } } throw new Error("No \"/".concat(pathParts.join('/'), "\" page found in /").concat(routeBranch.name, " folder.")); }; exports.getTranslatedPathPattern = getTranslatedPathPattern; /** * Translate Next file url into translated string url * * @param url Next default file url * @param locale string */ var fileUrlToUrl = function (url, locale) { var e_2, _a; var _b = (0, ntrData_1.getNtrData)(), routesTree = _b.routesTree, defaultLocale = _b.defaultLocale; var _c = (0, urlToUrlObject_1.urlToUrlObject)(url), pathname = _c.pathname, query = _c.query, hash = _c.hash; if (!routesTree.children) { throw new Error('No page found. You probably need to add the pageDirectory option in your translateRoutes config.'); } try { var pathParts = (pathname || '/') .replace(/^\/|\/$/g, '') .split('/') .filter(Boolean); // Create a new query object that we can mutate var newQuery = __assign({}, query); var pathPattern = (0, exports.getTranslatedPathPattern)({ routeBranch: routesTree, pathParts: pathParts, locale: locale }); var newPathname = (0, normalize_trailing_slash_1.normalizePathTrailingSlash)((0, path_to_regexp_1.compile)(pathPattern)(query)); try { for (var _d = __values((0, path_to_regexp_1.parse)(pathPattern)), _e = _d.next(); !_e.done; _e = _d.next()) { var patterToken = _e.value; if (typeof patterToken === 'object' && patterToken.name) { delete newQuery[patterToken.name]; } } } catch (e_2_1) { e_2 = { error: e_2_1 }; } finally { try { if (_e && !_e.done && (_a = _d.return)) _a.call(_d); } finally { if (e_2) throw e_2.error; } } var hasLocalePrefix = locale !== defaultLocale && process.env.NEXT_PUBLIC_HAS_LOCALE_PREFIX !== 'false'; return "".concat(hasLocalePrefix ? "/".concat(locale) : '').concat((0, url_1.format)({ pathname: newPathname, query: newQuery, hash: hash, })); } catch (error) { throw new Error(withNtrPrefix_1.ntrMessagePrefix + "No page found for pathname ".concat(pathname), { cause: error }); } }; exports.fileUrlToUrl = fileUrlToUrl; //# sourceMappingURL=fileUrlToUrl.js.map