express-intlayer 5.5.0-canary.0 → 5.5.1

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.
@@ -29,9 +29,7 @@ var import_chokidar = require("@intlayer/chokidar");
29
29
  var import_config = require("@intlayer/config");
30
30
  var import_core = require("@intlayer/core");
31
31
  var import_cls_hooked = require("cls-hooked");
32
- const { middleware, internationalization } = (0, import_config.getConfiguration)({
33
- verbose: true
34
- });
32
+ const { middleware, internationalization } = (0, import_config.getConfiguration)();
35
33
  const { headerName, cookieName } = middleware;
36
34
  const appNamespace = (0, import_cls_hooked.createNamespace)("app");
37
35
  (0, import_chokidar.createModuleAugmentation)();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration({\n verbose: true,\n});\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n res.locals.t = t;\n res.locals.getIntlayer = getIntlayer;\n res.locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) =>\n appNamespace.get('getIntlayer')(...args);\nexport const getDictionary: typeof getDictionaryFunction = (...args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ACvB;AAEA,MAAI,OAAO,gBAAgB;AAC3B,MAAI,OAAO,gBAAgB;AAC3B,MAAI,OAAO,kBAAkB;AAC7B,MAAI,OAAO,SAAS,gBAAgB,gBAAgB;AACpD,MAAI,OAAO,gBAAgB,qBAAqB;AAEhD,QAAMA,KAAI,kBAAkB,KAAK,KAAK,IAAI;AAE1C,QAAMC,eAA0C,CAC9C,KACA,YAAY,mBACT,cACA,YAAAC,aAAoB,KAAK,WAAW,GAAG,KAAK;AAEjD,QAAMC,iBAA8C,CAClD,KACA,YAAY,mBACT,cACA,YAAAC,eAAsB,KAAK,WAAW,GAAG,KAAK;AAEnD,MAAI,OAAO,IAAIJ;AACf,MAAI,OAAO,cAAcC;AACzB,MAAI,OAAO,gBAAgBE;AAE3B,eAAa,IAAI,MAAM;AACrB,iBAAa,IAAI,KAAKH,EAAC;AACvB,iBAAa,IAAI,eAAeC,YAAW;AAC3C,iBAAa,IAAI,iBAAiBE,cAAa;AAE/C,SAAK;AAAA,EACP,CAAC;AACH;AAEO,MAAM,IAAI,CACf,SACA,WACY,aAAa,IAAI,GAAG,EAAE,SAAS,MAAM;AAE5C,MAAM,cAA0C,IAAI,SACzD,aAAa,IAAI,aAAa,EAAE,GAAG,IAAI;AAClC,MAAM,gBAA8C,IAAI,SAC7D,aAAa,IAAI,eAAe,EAAE,GAAG,IAAI;","names":["t","getIntlayer","getIntlayerFunction","getDictionary","getDictionaryFunction"]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration();\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n res.locals.t = t;\n res.locals.getIntlayer = getIntlayer;\n res.locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) =>\n appNamespace.get('getIntlayer')(...args);\nexport const getDictionary: typeof getDictionaryFunction = (...args) =>\n appNamespace.get('getDictionary')(...args);\n\nexport { LanguageContent };\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,sBAAyC;AACzC,oBAA+C;AAC/C,kBAMO;AACP,wBAAgC;AAGhC,MAAM,EAAE,YAAY,qBAAqB,QAAI,gCAAiB;AAC9D,MAAM,EAAE,YAAY,WAAW,IAAI;AAEnC,MAAM,mBAAe,mCAAgB,KAAK;AAAA,IAE1C,0CAAyB;AAElB,MAAM,oBACX,CAAC,MAAe,KAAe,UAC/B,CACE,SACA,WACM;AACN,QAAM,EAAE,QAAQ,eAAe,cAAc,IAAI,IAAI;AAKrD,QAAM,eAAe,UAAU;AAE/B,MAAI,OAAO,YAAY,aAAa;AAClC,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO;AAAA,EACT;AAEA,MACE,OAAO,UAAU,YAAmD,MACpE,aACA;AACA,QACE,OAAO,UACL,aACF,MAAM,aACN;AACA,aAAO;AAAA,IACT,OAAO;AACL,iBAAO,4BAAe,SAAS,aAAa;AAAA,IAC9C;AAAA,EACF;AAEA,aAAO,4BAAe,SAAS,YAAY;AAC7C;AAOK,MAAM,WAAW,MAAsB,CAAC,KAAK,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AI;","names":["t","getIntlayer","getIntlayerFunction","getDictionary","getDictionaryFunction"]}
@@ -7,9 +7,7 @@ import {
7
7
  localeDetector
8
8
  } from "@intlayer/core";
9
9
  import { createNamespace } from "cls-hooked";
10
- const { middleware, internationalization } = getConfiguration({
11
- verbose: true
12
- });
10
+ const { middleware, internationalization } = getConfiguration();
13
11
  const { headerName, cookieName } = middleware;
14
12
  const appNamespace = createNamespace("app");
15
13
  createModuleAugmentation();
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration({\n verbose: true,\n});\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n res.locals.t = t;\n res.locals.getIntlayer = getIntlayer;\n res.locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) =>\n appNamespace.get('getIntlayer')(...args);\nexport const getDictionary: typeof getDictionaryFunction = (...args) =>\n appNamespace.get('getDictionary')(...args);\n\nexport { LanguageContent };\n"],"mappings":"AAAA,SAAS,gCAAgC;AACzC,SAAS,wBAAsC;AAC/C;AAAA,EACE,iBAAiB;AAAA,EACjB,eAAe;AAAA,EAC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,qBAAqB;AAEhD,QAAMA,KAAI,kBAAkB,KAAK,KAAK,IAAI;AAE1C,QAAMC,eAA0C,CAC9C,KACA,YAAY,mBACT,UACA,oBAAoB,KAAK,WAAW,GAAG,KAAK;AAEjD,QAAMC,iBAA8C,CAClD,KACA,YAAY,mBACT,UACA,sBAAsB,KAAK,WAAW,GAAG,KAAK;AAEnD,MAAI,OAAO,IAAIF;AACf,MAAI,OAAO,cAAcC;AACzB,MAAI,OAAO,gBAAgBC;AAE3B,eAAa,IAAI,MAAM;AACrB,iBAAa,IAAI,KAAKF,EAAC;AACvB,iBAAa,IAAI,eAAeC,YAAW;AAC3C,iBAAa,IAAI,iBAAiBC,cAAa;AAE/C,SAAK;AAAA,EACP,CAAC;AACH;AAEO,MAAM,IAAI,CACf,SACA,WACY,aAAa,IAAI,GAAG,EAAE,SAAS,MAAM;AAE5C,MAAM,cAA0C,IAAI,SACzD,aAAa,IAAI,aAAa,EAAE,GAAG,IAAI;AAClC,MAAM,gBAA8C,IAAI,SAC7D,aAAa,IAAI,eAAe,EAAE,GAAG,IAAI;","names":["t","getIntlayer","getDictionary"]}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { createModuleAugmentation } from '@intlayer/chokidar';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport {\n getDictionary as getDictionaryFunction,\n getIntlayer as getIntlayerFunction,\n getTranslation,\n localeDetector,\n type LanguageContent,\n} from '@intlayer/core';\nimport { createNamespace } from 'cls-hooked';\nimport type { NextFunction, Request, RequestHandler, Response } from 'express';\n\nconst { middleware, internationalization } = getConfiguration();\nconst { headerName, cookieName } = middleware;\n\nconst appNamespace = createNamespace('app');\n\ncreateModuleAugmentation();\n\nexport const translateFunction =\n (_req: Request, res: Response, _next?: NextFunction) =>\n <T extends string>(\n content: LanguageContent<T> | string,\n locale?: Locales\n ): T => {\n const { locale: currentLocale, defaultLocale } = res.locals as {\n locale: Locales;\n defaultLocale: Locales;\n };\n\n const targetLocale = locale ?? currentLocale;\n\n if (typeof content === 'undefined') {\n return '' as unknown as T;\n }\n\n if (typeof content === 'string') {\n return content as unknown as T;\n }\n\n if (\n typeof content?.[targetLocale as unknown as keyof LanguageContent<T>] ===\n 'undefined'\n ) {\n if (\n typeof content?.[\n defaultLocale as unknown as keyof LanguageContent<T>\n ] === 'undefined'\n ) {\n return content as unknown as T;\n } else {\n return getTranslation(content, defaultLocale);\n }\n }\n\n return getTranslation(content, targetLocale);\n };\n\n/**\n * Detect locale used by the user and load it into res locale storage\n *\n * @returns\n */\nexport const intlayer = (): RequestHandler => (req, res, next) => {\n // Detect if locale is set by intlayer frontend lib in the cookies\n const localeCookie = req.cookies?.[cookieName];\n // Detect if locale is set by intlayer frontend lib in the headers\n const localeHeader = req.headers?.[headerName];\n // Interpret browser locale\n\n const negotiatorHeaders: Record<string, string> = {};\n\n // // Check if req.headers exists and is an object\n if (req && typeof req.headers === 'object') {\n // Copy all headers from the request to negotiatorHeaders\n for (const key in req.headers) {\n if (typeof req.headers[key] === 'string') {\n negotiatorHeaders[key] = req.headers[key];\n }\n }\n }\n\n const localeDetected = localeDetector(\n negotiatorHeaders,\n internationalization.locales,\n internationalization.defaultLocale\n );\n\n res.locals.locale_header = localeHeader;\n res.locals.locale_cookie = localeCookie;\n res.locals.locale_detected = localeDetected;\n res.locals.locale = localeCookie ?? localeHeader ?? localeDetected;\n res.locals.defaultLocale = internationalization.defaultLocale;\n\n const t = translateFunction(req, res, next);\n\n const getIntlayer: typeof getIntlayerFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getIntlayerFunction>[1],\n ...props\n ) => getIntlayerFunction(key, localeArg, ...props);\n\n const getDictionary: typeof getDictionaryFunction = (\n key,\n localeArg = localeDetected as Parameters<typeof getDictionaryFunction>[1],\n ...props\n ) => getDictionaryFunction(key, localeArg, ...props);\n\n res.locals.t = t;\n res.locals.getIntlayer = getIntlayer;\n res.locals.getDictionary = getDictionary;\n\n appNamespace.run(() => {\n appNamespace.set('t', t);\n appNamespace.set('getIntlayer', getIntlayer);\n appNamespace.set('getDictionary', getDictionary);\n\n next();\n });\n};\n\nexport const t = <Content = string>(\n content: LanguageContent<Content>,\n locale?: Locales\n): Content => appNamespace.get('t')(content, locale);\n\nexport const getIntlayer: typeof getIntlayerFunction = (...args) =>\n appNamespace.get('getIntlayer')(...args);\nexport const getDictionary: typeof getDictionaryFunction = (...args) =>\n appNamespace.get('getDictionary')(...args);\n\nexport { LanguageContent };\n"],"mappings":"AAAA,SAAS,gCAAgC;AACzC,SAAS,wBAAsC;AAC/C;AAAA,EACE,iBAAiB;AAAA,EACjB,eAAe;AAAA,EACf;AAAA,EACA;AAAA,OAEK;AACP,SAAS,uBAAuB;AAGhC,MAAM,EAAE,YAAY,qBAAqB,IAAI,iBAAiB;AAC9D,MAAM,EAAE,YAAY,WAAW,IAAI;AAEnC,MAAM,eAAe,gBAAgB,KAAK;AAE1C,yBAAyB;AAElB,MAAM,oBACX,CAAC,MAAe,KAAe,UAC/B,CACE,SACA,WACM;AACN,QAAM,EAAE,QAAQ,eAAe,cAAc,IAAI,IAAI;AAKrD,QAAM,eAAe,UAAU;AAE/B,MAAI,OAAO,YAAY,aAAa;AAClC,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,YAAY,UAAU;AAC/B,WAAO;AAAA,EACT;AAEA,MACE,OAAO,UAAU,YAAmD,MACpE,aACA;AACA,QACE,OAAO,UACL,aACF,MAAM,aACN;AACA,aAAO;AAAA,IACT,OAAO;AACL,aAAO,eAAe,SAAS,aAAa;AAAA,IAC9C;AAAA,EACF;AAEA,SAAO,eAAe,SAAS,YAAY;AAC7C;AAOK,MAAM,WAAW,MAAsB,CAAC,KAAK,KAAK,SAAS;AAEhE,QAAM,eAAe,IAAI,UAAU,UAAU;AAE7C,QAAM,eAAe,IAAI,UAAU,UAAU;AAG7C,QAAM,oBAA4C,CAAC;AAGnD,MAAI,OAAO,OAAO,IAAI,YAAY,UAAU;AAE1C,eAAW,OAAO,IAAI,SAAS;AAC7B,UAAI,OAAO,IAAI,QAAQ,GAAG,MAAM,UAAU;AACxC,0BAAkB,GAAG,IAAI,IAAI,QAAQ,GAAG;AAAA,MAC1C;AAAA,IACF;AAAA,EACF;AAEA,QAAM,iBAAiB;AAAA,IACrB;AAAA,IACA,qBAAqB;AAAA,IACrB,qBAAqB;AAAA,EACvB;AAEA,MAAI,OAAO,gBAAgB;AAC3B,MAAI,OAAO,gBAAgB;AAC3B,MAAI,OAAO,kBAAkB;AAC7B,MAAI,OAAO,SAAS,gBAAgB,gBAAgB;AACpD,MAAI,OAAO,gBAAgB,qBAAqB;AAEhD,QAAMA,KAAI,kBAAkB,KAAK,KAAK,IAAI;AAE1C,QAAMC,eAA0C,CAC9C,KACA,YAAY,mBACT,UACA,oBAAoB,KAAK,WAAW,GAAG,KAAK;AAEjD,QAAMC,iBAA8C,CAClD,KACA,YAAY,mBACT,UACA,sBAAsB,KAAK,WAAW,GAAG,KAAK;AAEnD,MAAI,OAAO,IAAIF;AACf,MAAI,OAAO,cAAcC;AACzB,MAAI,OAAO,gBAAgBC;AAE3B,eAAa,IAAI,MAAM;AACrB,iBAAa,IAAI,KAAKF,EAAC;AACvB,iBAAa,IAAI,eAAeC,YAAW;AAC3C,iBAAa,IAAI,iBAAiBC,cAAa;AAE/C,SAAK;AAAA,EACP,CAAC;AACH;AAEO,MAAM,IAAI,CACf,SACA,WACY,aAAa,IAAI,GAAG,EAAE,SAAS,MAAM;AAE5C,MAAM,cAA0C,IAAI,SACzD,aAAa,IAAI,aAAa,EAAE,GAAG,IAAI;AAClC,MAAM,gBAA8C,IAAI,SAC7D,aAAa,IAAI,eAAe,EAAE,GAAG,IAAI;","names":["t","getIntlayer","getDictionary"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClE,OAAO,EACL,aAAa,IAAI,qBAAqB,EACtC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,cAwD3B,CAAC;AAEF,eAAO,MAAM,CAAC,GAAI,OAAO,GAAG,MAAM,EAChC,SAAS,eAAe,CAAC,OAAO,CAAC,EACjC,SAAS,OAAO,KACf,OAAiD,CAAC;AAErD,eAAO,MAAM,WAAW,EAAE,OAAO,mBACS,CAAC;AAC3C,eAAO,MAAM,aAAa,EAAE,OAAO,qBACS,CAAC;AAE7C,OAAO,EAAE,eAAe,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-intlayer",
3
- "version": "5.5.0-canary.0",
3
+ "version": "5.5.1",
4
4
  "private": false,
5
5
  "description": "Manage internationalization i18n in a simple way for express application.",
6
6
  "keywords": [
@@ -62,10 +62,10 @@
62
62
  ],
63
63
  "dependencies": {
64
64
  "cls-hooked": "^4.2.2",
65
- "@intlayer/chokidar": "5.5.0-canary.0",
66
- "@intlayer/core": "5.5.0-canary.0",
67
- "@intlayer/config": "5.5.0-canary.0",
68
- "intlayer": "5.5.0-canary.0"
65
+ "@intlayer/chokidar": "5.5.1",
66
+ "@intlayer/config": "5.5.1",
67
+ "@intlayer/core": "5.5.1",
68
+ "intlayer": "5.5.1"
69
69
  },
70
70
  "devDependencies": {
71
71
  "@changesets/changelog-github": "0.5.1",
@@ -83,15 +83,15 @@
83
83
  "tsup": "^8.4.0",
84
84
  "typescript": "^5.8.2",
85
85
  "@utils/eslint-config": "1.0.4",
86
- "@utils/ts-config-types": "1.0.4",
86
+ "@utils/ts-config": "1.0.4",
87
87
  "@utils/tsup-config": "1.0.4",
88
- "@utils/ts-config": "1.0.4"
88
+ "@utils/ts-config-types": "1.0.4"
89
89
  },
90
90
  "peerDependencies": {
91
- "@intlayer/chokidar": "5.5.0-canary.0",
92
- "@intlayer/config": "5.5.0-canary.0",
93
- "@intlayer/core": "5.5.0-canary.0",
94
- "intlayer": "5.5.0-canary.0"
91
+ "@intlayer/chokidar": "5.5.1",
92
+ "intlayer": "5.5.1",
93
+ "@intlayer/config": "5.5.1",
94
+ "@intlayer/core": "5.5.1"
95
95
  },
96
96
  "engines": {
97
97
  "node": ">=14.18"