@intlayer/core 3.5.7 → 3.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/checkIsURLAbsolute.cjs +29 -0
- package/dist/cjs/checkIsURLAbsolute.cjs.map +1 -0
- package/dist/cjs/getMultilingualUrls.cjs +3 -3
- package/dist/cjs/getMultilingualUrls.cjs.map +1 -1
- package/dist/cjs/getPathWithoutLocale.cjs +8 -17
- package/dist/cjs/getPathWithoutLocale.cjs.map +1 -1
- package/dist/esm/checkIsURLAbsolute.mjs +5 -0
- package/dist/esm/checkIsURLAbsolute.mjs.map +1 -0
- package/dist/esm/getMultilingualUrls.mjs +4 -7
- package/dist/esm/getMultilingualUrls.mjs.map +1 -1
- package/dist/esm/getPathWithoutLocale.mjs +7 -14
- package/dist/esm/getPathWithoutLocale.mjs.map +1 -1
- package/dist/types/checkIsURLAbsolute.d.ts +2 -0
- package/dist/types/checkIsURLAbsolute.d.ts.map +1 -0
- package/dist/types/getMultilingualUrls.d.ts.map +1 -1
- package/dist/types/getPathWithoutLocale.d.ts +0 -19
- package/dist/types/getPathWithoutLocale.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var checkIsURLAbsolute_exports = {};
|
|
20
|
+
__export(checkIsURLAbsolute_exports, {
|
|
21
|
+
checkIsURLAbsolute: () => checkIsURLAbsolute
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(checkIsURLAbsolute_exports);
|
|
24
|
+
const checkIsURLAbsolute = (url) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
|
|
25
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
26
|
+
0 && (module.exports = {
|
|
27
|
+
checkIsURLAbsolute
|
|
28
|
+
});
|
|
29
|
+
//# sourceMappingURL=checkIsURLAbsolute.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/checkIsURLAbsolute.ts"],"sourcesContent":["export const checkIsURLAbsolute = (url: string): boolean =>\n /^[a-zA-Z][a-zA-Z\\d+\\-.]*:/.test(url);\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAO,MAAM,qBAAqB,CAAC,QACjC,4BAA4B,KAAK,GAAG;","names":[]}
|
|
@@ -22,15 +22,15 @@ __export(getMultilingualUrls_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(getMultilingualUrls_exports);
|
|
24
24
|
var import_client = require("@intlayer/config/client");
|
|
25
|
+
var import_checkIsURLAbsolute = require('./checkIsURLAbsolute.cjs');
|
|
25
26
|
var import_getPathWithoutLocale = require('./getPathWithoutLocale.cjs');
|
|
26
27
|
const { internationalization, middleware } = (0, import_client.getConfiguration)();
|
|
27
28
|
const { locales: localesDefault, defaultLocale: defaultLocaleDefault } = internationalization;
|
|
28
29
|
const { prefixDefault: prefixDefaultDefault } = middleware;
|
|
29
30
|
const getMultilingualUrls = (url, locales = localesDefault, defaultLocale = defaultLocaleDefault, prefixDefault = prefixDefaultDefault) => {
|
|
30
31
|
const urlWithoutLocale = (0, import_getPathWithoutLocale.getPathWithoutLocale)(url, locales);
|
|
31
|
-
const isAbsoluteUrl = (0,
|
|
32
|
-
const
|
|
33
|
-
const parsedUrl = new URL(urlWithoutLocale, base);
|
|
32
|
+
const isAbsoluteUrl = (0, import_checkIsURLAbsolute.checkIsURLAbsolute)(urlWithoutLocale);
|
|
33
|
+
const parsedUrl = isAbsoluteUrl ? new URL(urlWithoutLocale) : new URL(urlWithoutLocale, "http://example.com");
|
|
34
34
|
let pathname = parsedUrl.pathname;
|
|
35
35
|
if (!pathname.startsWith("/")) {
|
|
36
36
|
pathname = `/${pathname}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/getMultilingualUrls.ts"],"sourcesContent":["import { getConfiguration, Locales } from '@intlayer/config/client';\n// @ts-ignore intlayer declared for module augmentation\nimport type { IConfigLocales } from 'intlayer';\nimport {
|
|
1
|
+
{"version":3,"sources":["../../src/getMultilingualUrls.ts"],"sourcesContent":["import { getConfiguration, Locales } from '@intlayer/config/client';\n// @ts-ignore intlayer declared for module augmentation\nimport type { IConfigLocales } from 'intlayer';\nimport { checkIsURLAbsolute } from './checkIsURLAbsolute';\nimport { getPathWithoutLocale } from './getPathWithoutLocale';\n\n// Destructure necessary configurations\nconst { internationalization, middleware } = getConfiguration();\nconst { locales: localesDefault, defaultLocale: defaultLocaleDefault } =\n internationalization;\nconst { prefixDefault: prefixDefaultDefault } = middleware;\n\n/**\n * Generates multilingual URLs by prefixing the given URL with each supported locale.\n * Handles both absolute and relative URLs appropriately.\n *\n * This function get the locales, default locale, and prefix default from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', false)\n * // Returns { en: '/dashboard', fr: '/fr/dashboard' }\n * getMultilingualUrls('https://example.com/dashboard', ['en', 'fr'], 'en', true)\n * // Returns { en: 'https://example.com/en/dashboard', fr: 'https://example.com/fr/dashboard' }\n * ```\n *\n * @param url - The original URL string to be prefixed with locales.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @param defaultLocale - The default locale. Defaults to `defaultLocaleDefault`.\n * @param prefixDefault - Whether to prefix the default locale. Defaults to `prefixDefaultDefault`.\n * @returns An object mapping each locale to its corresponding multilingual URL.\n */\nexport const getMultilingualUrls = (\n url: string,\n locales: Locales[] = localesDefault,\n defaultLocale: Locales = defaultLocaleDefault,\n prefixDefault: boolean = prefixDefaultDefault\n): IConfigLocales<string> => {\n // Remove any existing locale segment from the URL\n const urlWithoutLocale = getPathWithoutLocale(url, locales);\n\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsURLAbsolute(urlWithoutLocale);\n\n // Initialize a URL object if the URL is absolute\n // For relative URLs, use a dummy base to leverage the URL API\n const parsedUrl = isAbsoluteUrl\n ? new URL(urlWithoutLocale)\n : new URL(urlWithoutLocale, 'http://example.com');\n\n // Extract the pathname from the parsed URL\n let pathname = parsedUrl.pathname;\n\n // Ensure the pathname starts with a '/'\n if (!pathname.startsWith('/')) {\n pathname = `/${pathname}`;\n }\n\n // Prepare the base URL (protocol + host) if it's absolute\n const baseUrl = isAbsoluteUrl\n ? `${parsedUrl.protocol}//${parsedUrl.host}`\n : '';\n\n // Generate multilingual URLs by iterating over each locale\n const multilingualUrls = locales.reduce<IConfigLocales<string>>(\n (acc, locale) => {\n // Determine if the current locale is the default locale\n const isDefaultLocale = locale.toString() === defaultLocale.toString();\n\n // Decide whether to prefix the default locale based on `prefixDefault`\n const shouldPrefix = prefixDefault || !isDefaultLocale;\n\n // Construct the new pathname with or without the locale prefix\n let localizedPath = shouldPrefix ? `/${locale}${pathname}` : pathname;\n\n if (localizedPath.length > 1 && localizedPath.endsWith('/')) {\n localizedPath = localizedPath.slice(0, -1);\n }\n\n // Combine with the base URL if the original URL was absolute\n const localizedUrl = isAbsoluteUrl\n ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}`\n : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;\n\n // Assign the constructed URL to the corresponding locale key\n acc[locale as unknown as keyof IConfigLocales<string>] = localizedUrl;\n\n return acc;\n },\n {} as IConfigLocales<string> // Initialize an empty object\n );\n\n return multilingualUrls;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA0C;AAG1C,gCAAmC;AACnC,kCAAqC;AAGrC,MAAM,EAAE,sBAAsB,WAAW,QAAI,gCAAiB;AAC9D,MAAM,EAAE,SAAS,gBAAgB,eAAe,qBAAqB,IACnE;AACF,MAAM,EAAE,eAAe,qBAAqB,IAAI;AAuBzC,MAAM,sBAAsB,CACjC,KACA,UAAqB,gBACrB,gBAAyB,sBACzB,gBAAyB,yBACE;AAE3B,QAAM,uBAAmB,kDAAqB,KAAK,OAAO;AAG1D,QAAM,oBAAgB,8CAAmB,gBAAgB;AAIzD,QAAM,YAAY,gBACd,IAAI,IAAI,gBAAgB,IACxB,IAAI,IAAI,kBAAkB,oBAAoB;AAGlD,MAAI,WAAW,UAAU;AAGzB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAC7B,eAAW,IAAI,QAAQ;AAAA,EACzB;AAGA,QAAM,UAAU,gBACZ,GAAG,UAAU,QAAQ,KAAK,UAAU,IAAI,KACxC;AAGJ,QAAM,mBAAmB,QAAQ;AAAA,IAC/B,CAAC,KAAK,WAAW;AAEf,YAAM,kBAAkB,OAAO,SAAS,MAAM,cAAc,SAAS;AAGrE,YAAM,eAAe,iBAAiB,CAAC;AAGvC,UAAI,gBAAgB,eAAe,IAAI,MAAM,GAAG,QAAQ,KAAK;AAE7D,UAAI,cAAc,SAAS,KAAK,cAAc,SAAS,GAAG,GAAG;AAC3D,wBAAgB,cAAc,MAAM,GAAG,EAAE;AAAA,MAC3C;AAGA,YAAM,eAAe,gBACjB,GAAG,OAAO,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI,KAC9D,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI;AAGxD,UAAI,MAAiD,IAAI;AAEzD,aAAO;AAAA,IACT;AAAA,IACA,CAAC;AAAA;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -18,16 +18,16 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
var getPathWithoutLocale_exports = {};
|
|
20
20
|
__export(getPathWithoutLocale_exports, {
|
|
21
|
-
|
|
22
|
-
getPathWithoutLocale: () => getPathWithoutLocale,
|
|
23
|
-
removeLocaleFromUrl: () => removeLocaleFromUrl
|
|
21
|
+
getPathWithoutLocale: () => getPathWithoutLocale
|
|
24
22
|
});
|
|
25
23
|
module.exports = __toCommonJS(getPathWithoutLocale_exports);
|
|
26
24
|
var import_client = require("@intlayer/config/client");
|
|
25
|
+
var import_checkIsURLAbsolute = require('./checkIsURLAbsolute.cjs');
|
|
27
26
|
const { internationalization } = (0, import_client.getConfiguration)();
|
|
28
27
|
const { locales: localesDefault } = internationalization;
|
|
29
|
-
const
|
|
30
|
-
const
|
|
28
|
+
const getPathWithoutLocale = (inputUrl, locales = localesDefault) => {
|
|
29
|
+
const isAbsoluteUrl = (0, import_checkIsURLAbsolute.checkIsURLAbsolute)(inputUrl);
|
|
30
|
+
const url = isAbsoluteUrl ? new URL(inputUrl) : new URL(inputUrl, "http://example.com");
|
|
31
31
|
const pathname = url.pathname;
|
|
32
32
|
if (!pathname.startsWith("/")) {
|
|
33
33
|
url.pathname = `/${pathname}`;
|
|
@@ -39,22 +39,13 @@ const removeLocaleFromUrl = (inputUrl, locales = localesDefault) => {
|
|
|
39
39
|
const newPathname = pathSegments.join("/") ?? "/";
|
|
40
40
|
url.pathname = newPathname;
|
|
41
41
|
}
|
|
42
|
-
return url.toString();
|
|
43
|
-
};
|
|
44
|
-
const getPathWithoutLocale = (inputUrl, locales = localesDefault) => {
|
|
45
|
-
const isAbsoluteUrl = checkIsAbsoluteUrl(inputUrl);
|
|
46
|
-
const url = isAbsoluteUrl ? new URL(inputUrl) : new URL(inputUrl, "http://example.com");
|
|
47
|
-
const urlWithoutLocale = removeLocaleFromUrl(url.toString(), locales);
|
|
48
42
|
if (isAbsoluteUrl) {
|
|
49
|
-
return
|
|
43
|
+
return url.toString();
|
|
50
44
|
}
|
|
51
|
-
return
|
|
45
|
+
return url.toString().replace("http://example.com", "");
|
|
52
46
|
};
|
|
53
|
-
const checkIsAbsoluteUrl = (url) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
|
|
54
47
|
// Annotate the CommonJS export names for ESM import in node:
|
|
55
48
|
0 && (module.exports = {
|
|
56
|
-
|
|
57
|
-
getPathWithoutLocale,
|
|
58
|
-
removeLocaleFromUrl
|
|
49
|
+
getPathWithoutLocale
|
|
59
50
|
});
|
|
60
51
|
//# sourceMappingURL=getPathWithoutLocale.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/getPathWithoutLocale.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config/client';\n\nconst { internationalization } = getConfiguration();\nconst { locales: localesDefault } = internationalization;\n\n/**\n * Removes the locale segment from the given URL if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n *
|
|
1
|
+
{"version":3,"sources":["../../src/getPathWithoutLocale.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config/client';\nimport { checkIsURLAbsolute } from './checkIsURLAbsolute';\n\nconst { internationalization } = getConfiguration();\nconst { locales: localesDefault } = internationalization;\n\n/**\n * Removes the locale segment from the given URL or pathname if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getPathWithoutLocale('/en/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/fr/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('dashboard') // Returns 'dashboard'\n * getPathWithoutLocale('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/fr/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/dashboard') // Returns 'https://example.com/dashboard'\n * ```\n *\n * @param inputUrl - The complete URL string or pathname to process.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @returns The URL string or pathname without the locale segment.\n */\nexport const getPathWithoutLocale = (\n inputUrl: string,\n locales: Locales[] = localesDefault\n): string => {\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);\n\n // Initialize a URL object if the URL is absolute\n // For relative URLs, use a dummy base to leverage the URL API\n const url = isAbsoluteUrl\n ? new URL(inputUrl)\n : new URL(inputUrl, 'http://example.com');\n\n const pathname = url.pathname;\n\n // Ensure the pathname starts with '/'\n if (!pathname.startsWith('/')) {\n // If not, return the URL as is\n url.pathname = `/${pathname}`;\n }\n\n // Split the pathname to extract the first segment\n const pathSegments = pathname.split('/');\n const firstSegment = pathSegments[1]; // The segment after the first '/'\n\n // Check if the first segment is a supported locale\n if (locales.includes(firstSegment as Locales)) {\n // Remove the locale segment from the pathname\n pathSegments.splice(1, 1); // Remove the first segment\n\n // Reconstruct the pathname\n const newPathname = pathSegments.join('/') ?? '/';\n url.pathname = newPathname;\n }\n\n if (isAbsoluteUrl) {\n // Return the modified URL as a string\n return url.toString();\n }\n\n // Return the modified URL as a string\n return url.toString().replace('http://example.com', '');\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,gCAAmC;AAEnC,MAAM,EAAE,qBAAqB,QAAI,gCAAiB;AAClD,MAAM,EAAE,SAAS,eAAe,IAAI;AAuB7B,MAAM,uBAAuB,CAClC,UACA,UAAqB,mBACV;AAEX,QAAM,oBAAgB,8CAAmB,QAAQ;AAIjD,QAAM,MAAM,gBACR,IAAI,IAAI,QAAQ,IAChB,IAAI,IAAI,UAAU,oBAAoB;AAE1C,QAAM,WAAW,IAAI;AAGrB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAE7B,QAAI,WAAW,IAAI,QAAQ;AAAA,EAC7B;AAGA,QAAM,eAAe,SAAS,MAAM,GAAG;AACvC,QAAM,eAAe,aAAa,CAAC;AAGnC,MAAI,QAAQ,SAAS,YAAuB,GAAG;AAE7C,iBAAa,OAAO,GAAG,CAAC;AAGxB,UAAM,cAAc,aAAa,KAAK,GAAG,KAAK;AAC9C,QAAI,WAAW;AAAA,EACjB;AAEA,MAAI,eAAe;AAEjB,WAAO,IAAI,SAAS;AAAA,EACtB;AAGA,SAAO,IAAI,SAAS,EAAE,QAAQ,sBAAsB,EAAE;AACxD;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/checkIsURLAbsolute.ts"],"sourcesContent":["export const checkIsURLAbsolute = (url: string): boolean =>\n /^[a-zA-Z][a-zA-Z\\d+\\-.]*:/.test(url);\n"],"mappings":"AAAO,MAAM,qBAAqB,CAAC,QACjC,4BAA4B,KAAK,GAAG;","names":[]}
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import { getConfiguration } from "@intlayer/config/client";
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
getPathWithoutLocale
|
|
5
|
-
} from './getPathWithoutLocale.mjs';
|
|
2
|
+
import { checkIsURLAbsolute } from './checkIsURLAbsolute.mjs';
|
|
3
|
+
import { getPathWithoutLocale } from './getPathWithoutLocale.mjs';
|
|
6
4
|
const { internationalization, middleware } = getConfiguration();
|
|
7
5
|
const { locales: localesDefault, defaultLocale: defaultLocaleDefault } = internationalization;
|
|
8
6
|
const { prefixDefault: prefixDefaultDefault } = middleware;
|
|
9
7
|
const getMultilingualUrls = (url, locales = localesDefault, defaultLocale = defaultLocaleDefault, prefixDefault = prefixDefaultDefault) => {
|
|
10
8
|
const urlWithoutLocale = getPathWithoutLocale(url, locales);
|
|
11
|
-
const isAbsoluteUrl =
|
|
12
|
-
const
|
|
13
|
-
const parsedUrl = new URL(urlWithoutLocale, base);
|
|
9
|
+
const isAbsoluteUrl = checkIsURLAbsolute(urlWithoutLocale);
|
|
10
|
+
const parsedUrl = isAbsoluteUrl ? new URL(urlWithoutLocale) : new URL(urlWithoutLocale, "http://example.com");
|
|
14
11
|
let pathname = parsedUrl.pathname;
|
|
15
12
|
if (!pathname.startsWith("/")) {
|
|
16
13
|
pathname = `/${pathname}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/getMultilingualUrls.ts"],"sourcesContent":["import { getConfiguration, Locales } from '@intlayer/config/client';\n// @ts-ignore intlayer declared for module augmentation\nimport type { IConfigLocales } from 'intlayer';\nimport {
|
|
1
|
+
{"version":3,"sources":["../../src/getMultilingualUrls.ts"],"sourcesContent":["import { getConfiguration, Locales } from '@intlayer/config/client';\n// @ts-ignore intlayer declared for module augmentation\nimport type { IConfigLocales } from 'intlayer';\nimport { checkIsURLAbsolute } from './checkIsURLAbsolute';\nimport { getPathWithoutLocale } from './getPathWithoutLocale';\n\n// Destructure necessary configurations\nconst { internationalization, middleware } = getConfiguration();\nconst { locales: localesDefault, defaultLocale: defaultLocaleDefault } =\n internationalization;\nconst { prefixDefault: prefixDefaultDefault } = middleware;\n\n/**\n * Generates multilingual URLs by prefixing the given URL with each supported locale.\n * Handles both absolute and relative URLs appropriately.\n *\n * This function get the locales, default locale, and prefix default from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getMultilingualUrls('/dashboard', ['en', 'fr'], 'en', false)\n * // Returns { en: '/dashboard', fr: '/fr/dashboard' }\n * getMultilingualUrls('https://example.com/dashboard', ['en', 'fr'], 'en', true)\n * // Returns { en: 'https://example.com/en/dashboard', fr: 'https://example.com/fr/dashboard' }\n * ```\n *\n * @param url - The original URL string to be prefixed with locales.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @param defaultLocale - The default locale. Defaults to `defaultLocaleDefault`.\n * @param prefixDefault - Whether to prefix the default locale. Defaults to `prefixDefaultDefault`.\n * @returns An object mapping each locale to its corresponding multilingual URL.\n */\nexport const getMultilingualUrls = (\n url: string,\n locales: Locales[] = localesDefault,\n defaultLocale: Locales = defaultLocaleDefault,\n prefixDefault: boolean = prefixDefaultDefault\n): IConfigLocales<string> => {\n // Remove any existing locale segment from the URL\n const urlWithoutLocale = getPathWithoutLocale(url, locales);\n\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsURLAbsolute(urlWithoutLocale);\n\n // Initialize a URL object if the URL is absolute\n // For relative URLs, use a dummy base to leverage the URL API\n const parsedUrl = isAbsoluteUrl\n ? new URL(urlWithoutLocale)\n : new URL(urlWithoutLocale, 'http://example.com');\n\n // Extract the pathname from the parsed URL\n let pathname = parsedUrl.pathname;\n\n // Ensure the pathname starts with a '/'\n if (!pathname.startsWith('/')) {\n pathname = `/${pathname}`;\n }\n\n // Prepare the base URL (protocol + host) if it's absolute\n const baseUrl = isAbsoluteUrl\n ? `${parsedUrl.protocol}//${parsedUrl.host}`\n : '';\n\n // Generate multilingual URLs by iterating over each locale\n const multilingualUrls = locales.reduce<IConfigLocales<string>>(\n (acc, locale) => {\n // Determine if the current locale is the default locale\n const isDefaultLocale = locale.toString() === defaultLocale.toString();\n\n // Decide whether to prefix the default locale based on `prefixDefault`\n const shouldPrefix = prefixDefault || !isDefaultLocale;\n\n // Construct the new pathname with or without the locale prefix\n let localizedPath = shouldPrefix ? `/${locale}${pathname}` : pathname;\n\n if (localizedPath.length > 1 && localizedPath.endsWith('/')) {\n localizedPath = localizedPath.slice(0, -1);\n }\n\n // Combine with the base URL if the original URL was absolute\n const localizedUrl = isAbsoluteUrl\n ? `${baseUrl}${localizedPath}${parsedUrl.search}${parsedUrl.hash}`\n : `${localizedPath}${parsedUrl.search}${parsedUrl.hash}`;\n\n // Assign the constructed URL to the corresponding locale key\n acc[locale as unknown as keyof IConfigLocales<string>] = localizedUrl;\n\n return acc;\n },\n {} as IConfigLocales<string> // Initialize an empty object\n );\n\n return multilingualUrls;\n};\n"],"mappings":"AAAA,SAAS,wBAAiC;AAG1C,SAAS,0BAA0B;AACnC,SAAS,4BAA4B;AAGrC,MAAM,EAAE,sBAAsB,WAAW,IAAI,iBAAiB;AAC9D,MAAM,EAAE,SAAS,gBAAgB,eAAe,qBAAqB,IACnE;AACF,MAAM,EAAE,eAAe,qBAAqB,IAAI;AAuBzC,MAAM,sBAAsB,CACjC,KACA,UAAqB,gBACrB,gBAAyB,sBACzB,gBAAyB,yBACE;AAE3B,QAAM,mBAAmB,qBAAqB,KAAK,OAAO;AAG1D,QAAM,gBAAgB,mBAAmB,gBAAgB;AAIzD,QAAM,YAAY,gBACd,IAAI,IAAI,gBAAgB,IACxB,IAAI,IAAI,kBAAkB,oBAAoB;AAGlD,MAAI,WAAW,UAAU;AAGzB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAC7B,eAAW,IAAI,QAAQ;AAAA,EACzB;AAGA,QAAM,UAAU,gBACZ,GAAG,UAAU,QAAQ,KAAK,UAAU,IAAI,KACxC;AAGJ,QAAM,mBAAmB,QAAQ;AAAA,IAC/B,CAAC,KAAK,WAAW;AAEf,YAAM,kBAAkB,OAAO,SAAS,MAAM,cAAc,SAAS;AAGrE,YAAM,eAAe,iBAAiB,CAAC;AAGvC,UAAI,gBAAgB,eAAe,IAAI,MAAM,GAAG,QAAQ,KAAK;AAE7D,UAAI,cAAc,SAAS,KAAK,cAAc,SAAS,GAAG,GAAG;AAC3D,wBAAgB,cAAc,MAAM,GAAG,EAAE;AAAA,MAC3C;AAGA,YAAM,eAAe,gBACjB,GAAG,OAAO,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI,KAC9D,GAAG,aAAa,GAAG,UAAU,MAAM,GAAG,UAAU,IAAI;AAGxD,UAAI,MAAiD,IAAI;AAEzD,aAAO;AAAA,IACT;AAAA,IACA,CAAC;AAAA;AAAA,EACH;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { getConfiguration } from "@intlayer/config/client";
|
|
2
|
+
import { checkIsURLAbsolute } from './checkIsURLAbsolute.mjs';
|
|
2
3
|
const { internationalization } = getConfiguration();
|
|
3
4
|
const { locales: localesDefault } = internationalization;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
5
|
+
const getPathWithoutLocale = (inputUrl, locales = localesDefault) => {
|
|
6
|
+
const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);
|
|
7
|
+
const url = isAbsoluteUrl ? new URL(inputUrl) : new URL(inputUrl, "http://example.com");
|
|
6
8
|
const pathname = url.pathname;
|
|
7
9
|
if (!pathname.startsWith("/")) {
|
|
8
10
|
url.pathname = `/${pathname}`;
|
|
@@ -14,21 +16,12 @@ const removeLocaleFromUrl = (inputUrl, locales = localesDefault) => {
|
|
|
14
16
|
const newPathname = pathSegments.join("/") ?? "/";
|
|
15
17
|
url.pathname = newPathname;
|
|
16
18
|
}
|
|
17
|
-
return url.toString();
|
|
18
|
-
};
|
|
19
|
-
const getPathWithoutLocale = (inputUrl, locales = localesDefault) => {
|
|
20
|
-
const isAbsoluteUrl = checkIsAbsoluteUrl(inputUrl);
|
|
21
|
-
const url = isAbsoluteUrl ? new URL(inputUrl) : new URL(inputUrl, "http://example.com");
|
|
22
|
-
const urlWithoutLocale = removeLocaleFromUrl(url.toString(), locales);
|
|
23
19
|
if (isAbsoluteUrl) {
|
|
24
|
-
return
|
|
20
|
+
return url.toString();
|
|
25
21
|
}
|
|
26
|
-
return
|
|
22
|
+
return url.toString().replace("http://example.com", "");
|
|
27
23
|
};
|
|
28
|
-
const checkIsAbsoluteUrl = (url) => /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(url);
|
|
29
24
|
export {
|
|
30
|
-
|
|
31
|
-
getPathWithoutLocale,
|
|
32
|
-
removeLocaleFromUrl
|
|
25
|
+
getPathWithoutLocale
|
|
33
26
|
};
|
|
34
27
|
//# sourceMappingURL=getPathWithoutLocale.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/getPathWithoutLocale.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config/client';\n\nconst { internationalization } = getConfiguration();\nconst { locales: localesDefault } = internationalization;\n\n/**\n * Removes the locale segment from the given URL if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n *
|
|
1
|
+
{"version":3,"sources":["../../src/getPathWithoutLocale.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config/client';\nimport { checkIsURLAbsolute } from './checkIsURLAbsolute';\n\nconst { internationalization } = getConfiguration();\nconst { locales: localesDefault } = internationalization;\n\n/**\n * Removes the locale segment from the given URL or pathname if present.\n *\n * This function get the locales from the configuration if not provided.\n *\n * Example:\n *\n * ```ts\n * getPathWithoutLocale('/en/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/fr/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('/dashboard') // Returns '/dashboard'\n * getPathWithoutLocale('dashboard') // Returns 'dashboard'\n * getPathWithoutLocale('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/fr/dashboard') // Returns 'https://example.com/dashboard'\n * getPathWithoutLocale('https://example.com/dashboard') // Returns 'https://example.com/dashboard'\n * ```\n *\n * @param inputUrl - The complete URL string or pathname to process.\n * @param locales - Optional array of supported locales. Defaults to `localesDefault`.\n * @returns The URL string or pathname without the locale segment.\n */\nexport const getPathWithoutLocale = (\n inputUrl: string,\n locales: Locales[] = localesDefault\n): string => {\n // Determine if the original URL is absolute (includes protocol)\n const isAbsoluteUrl = checkIsURLAbsolute(inputUrl);\n\n // Initialize a URL object if the URL is absolute\n // For relative URLs, use a dummy base to leverage the URL API\n const url = isAbsoluteUrl\n ? new URL(inputUrl)\n : new URL(inputUrl, 'http://example.com');\n\n const pathname = url.pathname;\n\n // Ensure the pathname starts with '/'\n if (!pathname.startsWith('/')) {\n // If not, return the URL as is\n url.pathname = `/${pathname}`;\n }\n\n // Split the pathname to extract the first segment\n const pathSegments = pathname.split('/');\n const firstSegment = pathSegments[1]; // The segment after the first '/'\n\n // Check if the first segment is a supported locale\n if (locales.includes(firstSegment as Locales)) {\n // Remove the locale segment from the pathname\n pathSegments.splice(1, 1); // Remove the first segment\n\n // Reconstruct the pathname\n const newPathname = pathSegments.join('/') ?? '/';\n url.pathname = newPathname;\n }\n\n if (isAbsoluteUrl) {\n // Return the modified URL as a string\n return url.toString();\n }\n\n // Return the modified URL as a string\n return url.toString().replace('http://example.com', '');\n};\n"],"mappings":"AAAA,SAAS,wBAAsC;AAC/C,SAAS,0BAA0B;AAEnC,MAAM,EAAE,qBAAqB,IAAI,iBAAiB;AAClD,MAAM,EAAE,SAAS,eAAe,IAAI;AAuB7B,MAAM,uBAAuB,CAClC,UACA,UAAqB,mBACV;AAEX,QAAM,gBAAgB,mBAAmB,QAAQ;AAIjD,QAAM,MAAM,gBACR,IAAI,IAAI,QAAQ,IAChB,IAAI,IAAI,UAAU,oBAAoB;AAE1C,QAAM,WAAW,IAAI;AAGrB,MAAI,CAAC,SAAS,WAAW,GAAG,GAAG;AAE7B,QAAI,WAAW,IAAI,QAAQ;AAAA,EAC7B;AAGA,QAAM,eAAe,SAAS,MAAM,GAAG;AACvC,QAAM,eAAe,aAAa,CAAC;AAGnC,MAAI,QAAQ,SAAS,YAAuB,GAAG;AAE7C,iBAAa,OAAO,GAAG,CAAC;AAGxB,UAAM,cAAc,aAAa,KAAK,GAAG,KAAK;AAC9C,QAAI,WAAW;AAAA,EACjB;AAEA,MAAI,eAAe;AAEjB,WAAO,IAAI,SAAS;AAAA,EACtB;AAGA,SAAO,IAAI,SAAS,EAAE,QAAQ,sBAAsB,EAAE;AACxD;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checkIsURLAbsolute.d.ts","sourceRoot":"","sources":["../../src/checkIsURLAbsolute.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,QAAS,MAAM,KAAG,OACV,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getMultilingualUrls.d.ts","sourceRoot":"","sources":["../../src/getMultilingualUrls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAEpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"getMultilingualUrls.d.ts","sourceRoot":"","sources":["../../src/getMultilingualUrls.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAEpE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAU/C;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,mBAAmB,QACzB,MAAM,YACF,OAAO,EAAE,kBACH,OAAO,kBACP,OAAO,KACrB,cAAc,CAAC,MAAM,CAwDvB,CAAC"}
|
|
@@ -1,22 +1,4 @@
|
|
|
1
1
|
import { type Locales } from '@intlayer/config/client';
|
|
2
|
-
/**
|
|
3
|
-
* Removes the locale segment from the given URL if present.
|
|
4
|
-
*
|
|
5
|
-
* This function get the locales from the configuration if not provided.
|
|
6
|
-
*
|
|
7
|
-
* Example:
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* removeLocaleFromUrl('https://example.com/en/dashboard') // Returns 'https://example.com/dashboard'
|
|
11
|
-
* removeLocaleFromUrl('https://example.com/fr/dashboard') // Returns 'https://example.com/fr/dashboard'
|
|
12
|
-
* removeLocaleFromUrl('https://example.com/dashboard') // Returns 'https://example.com/dashboard'
|
|
13
|
-
* ```
|
|
14
|
-
*
|
|
15
|
-
* @param inputUrl - The complete URL string to process.
|
|
16
|
-
* @param locales - Optional array of supported locales. Defaults to `localesDefault`.
|
|
17
|
-
* @returns The URL string without the locale segment.
|
|
18
|
-
*/
|
|
19
|
-
export declare const removeLocaleFromUrl: (inputUrl: string, locales?: Locales[]) => string;
|
|
20
2
|
/**
|
|
21
3
|
* Removes the locale segment from the given URL or pathname if present.
|
|
22
4
|
*
|
|
@@ -39,5 +21,4 @@ export declare const removeLocaleFromUrl: (inputUrl: string, locales?: Locales[]
|
|
|
39
21
|
* @returns The URL string or pathname without the locale segment.
|
|
40
22
|
*/
|
|
41
23
|
export declare const getPathWithoutLocale: (inputUrl: string, locales?: Locales[]) => string;
|
|
42
|
-
export declare const checkIsAbsoluteUrl: (url: string) => boolean;
|
|
43
24
|
//# sourceMappingURL=getPathWithoutLocale.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPathWithoutLocale.d.ts","sourceRoot":"","sources":["../../src/getPathWithoutLocale.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,OAAO,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"getPathWithoutLocale.d.ts","sourceRoot":"","sources":["../../src/getPathWithoutLocale.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAMzE;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,oBAAoB,aACrB,MAAM,YACP,OAAO,EAAE,KACjB,MAuCF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@intlayer/core",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.8",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "IntLayer - Layer of abstraction between the business logic and the data access layer. Manage internationalization in a simple way, through TypeScript, JavaScript or JSON declaration file.",
|
|
6
6
|
"keywords": [
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"@formatjs/intl-localematcher": "^0.5.4",
|
|
62
62
|
"chokidar": "^3.6.0",
|
|
63
63
|
"negotiator": "^0.6.3",
|
|
64
|
-
"@intlayer/config": "3.5.
|
|
64
|
+
"@intlayer/config": "3.5.8"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@types/negotiator": "^0.6.3",
|
|
@@ -74,14 +74,14 @@
|
|
|
74
74
|
"tsc-alias": "^1.8.10",
|
|
75
75
|
"tsup": "^8.3.5",
|
|
76
76
|
"typescript": "^5.7.2",
|
|
77
|
-
"@utils/
|
|
77
|
+
"@utils/eslint-config": "1.0.4",
|
|
78
78
|
"@utils/ts-config-types": "1.0.4",
|
|
79
79
|
"@utils/tsup-config": "1.0.4",
|
|
80
|
-
"@utils/
|
|
80
|
+
"@utils/ts-config": "1.0.4"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
83
83
|
"react": ">=16.0.0",
|
|
84
|
-
"@intlayer/config": "3.5.
|
|
84
|
+
"@intlayer/config": "3.5.8"
|
|
85
85
|
},
|
|
86
86
|
"engines": {
|
|
87
87
|
"node": ">=14.18"
|