@ptx-showcase/utils 1.1.0 → 1.1.1-beta.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.
package/README.md CHANGED
@@ -215,6 +215,13 @@ try {
215
215
  }
216
216
  ```
217
217
 
218
+ If the backend returns messages as a per-language object (`{ "message": { "en": "…", "fr": "…", "it": "…" } }`), pass the language as the second argument. It falls back to `en` when the requested language is missing, and to `Something went wrong` when there is no usable text at all.
219
+
220
+ ```ts
221
+ handleApiError(error, 'fr') // → "Email ou mot de passe incorrect"
222
+ handleApiError(error) // → "Incorrect email or password"
223
+ ```
224
+
218
225
  ---
219
226
 
220
227
  ### isAdminToken
@@ -0,0 +1 @@
1
+ let e=require("ky");var t=`Something went wrong`,n=new Set([`message`,`detail`,`error`,`msg`]),r=new Set([`en`,`fr`,`it`]),i=e=>typeof e==`object`&&!!e&&!Array.isArray(e),a=e=>{if(!i(e))return!1;let t=Object.keys(e);return t.length>0&&t.every(e=>r.has(e))},o=(e,t)=>{for(let n of[t,`en`])if(Object.hasOwn(e,n)&&typeof e[n]==`string`&&e[n].trim())return e[n];return null},s=(e,t=`en`)=>a(e)?o(e,t):typeof e==`string`&&e.trim()?e:null,c=(e,t,n=`en`)=>{if(a(e)){let r=o(e,n);return r?[t?`${t}: ${r}`:r]:[]}return typeof e==`string`?e.trim()?[t?`${t}: ${e}`:e]:[]:Array.isArray(e)?e.flatMap(e=>c(e,t,n)):i(e)?Object.entries(e).flatMap(([e,t])=>c(t,e===`non_field_errors`?void 0:e,n)):[]},l=(e,t=`en`)=>{if(!e)return[];if(typeof e==`string`)return[e];if(Array.isArray(e))return e.filter(e=>typeof e==`string`&&!!e.trim());let r=s(e.detail,t)??s(e.message,t)??s(e.error,t)??s(e.msg,t);return[...Object.entries(e).flatMap(([e,r])=>n.has(e)?[]:e===`non_field_errors`||e===`errors`?c(r,void 0,t):c(r,e,t)),...r?[r]:[]]},u=e=>{console.error(e)},d=e=>{u=e},f=()=>u,p=e=>{d(e.notify)},m=(n,r)=>{let i=f();if((0,e.isHTTPError)(n)){let e=n.data,a=[...new Set(l(e,r))];if(a.length>0){a.forEach(e=>i(e));return}i(t);return}if(n instanceof Error){i(n.message||`Something went wrong`);return}i(t)};Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return p}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return m}});
@@ -0,0 +1,55 @@
1
+ import { isHTTPError as e } from "ky";
2
+ //#region src/core/handle-api-error/constants/handle-api-error.constant.ts
3
+ var t = "Something went wrong", n = /* @__PURE__ */ new Set([
4
+ "message",
5
+ "detail",
6
+ "error",
7
+ "msg"
8
+ ]), r = /* @__PURE__ */ new Set([
9
+ "en",
10
+ "fr",
11
+ "it"
12
+ ]), i = (e) => typeof e == "object" && !!e && !Array.isArray(e), a = (e) => {
13
+ if (!i(e)) return !1;
14
+ let t = Object.keys(e);
15
+ return t.length > 0 && t.every((e) => r.has(e));
16
+ }, o = (e, t) => {
17
+ for (let n of [t, "en"]) if (Object.hasOwn(e, n) && typeof e[n] == "string" && e[n].trim()) return e[n];
18
+ return null;
19
+ }, s = (e, t = "en") => a(e) ? o(e, t) : typeof e == "string" && e.trim() ? e : null, c = (e, t, n = "en") => {
20
+ if (a(e)) {
21
+ let r = o(e, n);
22
+ return r ? [t ? `${t}: ${r}` : r] : [];
23
+ }
24
+ return typeof e == "string" ? e.trim() ? [t ? `${t}: ${e}` : e] : [] : Array.isArray(e) ? e.flatMap((e) => c(e, t, n)) : i(e) ? Object.entries(e).flatMap(([e, t]) => c(t, e === "non_field_errors" ? void 0 : e, n)) : [];
25
+ }, l = (e, t = "en") => {
26
+ if (!e) return [];
27
+ if (typeof e == "string") return [e];
28
+ if (Array.isArray(e)) return e.filter((e) => typeof e == "string" && !!e.trim());
29
+ let r = s(e.detail, t) ?? s(e.message, t) ?? s(e.error, t) ?? s(e.msg, t);
30
+ return [...Object.entries(e).flatMap(([e, r]) => n.has(e) ? [] : e === "non_field_errors" || e === "errors" ? c(r, void 0, t) : c(r, e, t)), ...r ? [r] : []];
31
+ }, u = (e) => {
32
+ console.error(e);
33
+ }, d = (e) => {
34
+ u = e;
35
+ }, f = () => u, p = (e) => {
36
+ d(e.notify);
37
+ }, m = (n, r) => {
38
+ let i = f();
39
+ if (e(n)) {
40
+ let e = n.data, a = [...new Set(l(e, r))];
41
+ if (a.length > 0) {
42
+ a.forEach((e) => i(e));
43
+ return;
44
+ }
45
+ i(t);
46
+ return;
47
+ }
48
+ if (n instanceof Error) {
49
+ i(n.message || "Something went wrong");
50
+ return;
51
+ }
52
+ i(t);
53
+ };
54
+ //#endregion
55
+ export { p as n, m as t };
@@ -1,2 +1,4 @@
1
1
  export declare const DEFAULT_ERROR_MESSAGE: "Something went wrong";
2
2
  export declare const SKIPPED_KEYS: Set<string>;
3
+ export declare const DEFAULT_LANGUAGE: "en";
4
+ export declare const LANGUAGES: Set<string>;
@@ -1,3 +1,12 @@
1
- import { HandleApiErrorConfig } from './types/handle-api-error.type';
1
+ import { HandleApiErrorConfig, Languages } from './types/handle-api-error.type';
2
2
  export declare const initHandleApiError: (config: HandleApiErrorConfig) => void;
3
- export declare const handleApiError: (error: unknown) => void;
3
+ export type { Languages };
4
+ /**
5
+ * Parses an API error and reports each unique message through the `notify` function set by `initHandleApiError`.
6
+ * Messages delivered as a per-language object (`{ en, fr, it }`) are shown in `language`, falling back to English;
7
+ * if nothing usable is found, a default error message is shown.
8
+ *
9
+ * @param error - Anything caught from a request, typically ky's `HTTPError`.
10
+ * @param language - Preferred language for localized messages. Defaults to `'en'`.
11
+ */
12
+ export declare const handleApiError: (error: unknown, language?: Languages) => void;
@@ -1,5 +1,6 @@
1
- import { ApiErrorData, ApiErrorPrimitive } from '../types/handle-api-error.type';
1
+ import { ApiErrorData, ApiErrorPrimitive, Languages, LocalizedMessage } from '../types/handle-api-error.type';
2
2
  export declare const isPlainObject: (value: unknown) => value is Record<string, unknown>;
3
- export declare const toMessage: (value: unknown) => string | null;
4
- export declare const extractMessages: (value: unknown, parentKey?: string) => string[];
5
- export declare const getHttpErrorMessages: (errorData: ApiErrorData | ApiErrorPrimitive | undefined) => string[];
3
+ export declare const isLocalizedMessage: (value: unknown) => value is LocalizedMessage;
4
+ export declare const toMessage: (value: unknown, language?: Languages) => string | null;
5
+ export declare const extractMessages: (value: unknown, parentKey?: string, language?: Languages) => string[];
6
+ export declare const getHttpErrorMessages: (errorData: ApiErrorData | ApiErrorPrimitive | undefined, language?: Languages) => string[];
@@ -2,6 +2,8 @@ export type NotifyFn = (message: string) => void;
2
2
  export interface HandleApiErrorConfig {
3
3
  notify: NotifyFn;
4
4
  }
5
+ export type Languages = 'en' | 'fr' | 'it';
6
+ export type LocalizedMessage = Partial<Record<Languages, string>>;
5
7
  export type ApiErrorPrimitive = string | string[];
6
8
  export interface ApiErrorData {
7
9
  detail?: string;
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../build-query-params/index.cjs"),t=require("../clean-object/index.cjs"),n=require("../cookies/index.cjs"),r=require("../build-sort-param/index.cjs"),i=require("../chunks/currency-symbol-gb63Q_9Q.cjs"),a=require("../download-file/index.cjs"),o=require("../chunks/flags-BRQ9Jims.cjs"),s=require("../chunks/handle-api-error-D714W9rp.cjs"),c=require("../normalize-input/index.cjs"),l=require("../chunks/truncate-decimals-CArL-IeO.cjs");exports.buildQueryParams=e.buildQueryParams,exports.buildSortParam=r.buildSortParam,exports.cleanObject=t.cleanObject,exports.defaultCookieOptions=n.defaultCookieOptions,exports.deleteCookie=n.deleteCookie,exports.downloadBlob=a.downloadBlob,exports.getCookie=n.getCookie,exports.getCurrencySymbol=i.t,exports.getFlagFallbackUrl=o.t,exports.getFlagUnknownUrl=o.n,exports.getFlagUrl=o.r,exports.handleApiError=s.t,exports.handleFlagImageError=o.i,exports.initHandleApiError=s.n,exports.normalizeDecimalInput=c.normalizeDecimalInput,exports.normalizePhoneNumberInput=c.normalizePhoneNumberInput,exports.setCookie=n.setCookie,exports.truncateDecimals=l.t;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../build-query-params/index.cjs"),t=require("../clean-object/index.cjs"),n=require("../cookies/index.cjs"),r=require("../build-sort-param/index.cjs"),i=require("../chunks/currency-symbol-gb63Q_9Q.cjs"),a=require("../download-file/index.cjs"),o=require("../chunks/flags-BRQ9Jims.cjs"),s=require("../chunks/handle-api-error-B-2TPzB6.cjs"),c=require("../normalize-input/index.cjs"),l=require("../chunks/truncate-decimals-CArL-IeO.cjs");exports.buildQueryParams=e.buildQueryParams,exports.buildSortParam=r.buildSortParam,exports.cleanObject=t.cleanObject,exports.defaultCookieOptions=n.defaultCookieOptions,exports.deleteCookie=n.deleteCookie,exports.downloadBlob=a.downloadBlob,exports.getCookie=n.getCookie,exports.getCurrencySymbol=i.t,exports.getFlagFallbackUrl=o.t,exports.getFlagUnknownUrl=o.n,exports.getFlagUrl=o.r,exports.handleApiError=s.t,exports.handleFlagImageError=o.i,exports.initHandleApiError=s.n,exports.normalizeDecimalInput=c.normalizeDecimalInput,exports.normalizePhoneNumberInput=c.normalizePhoneNumberInput,exports.setCookie=n.setCookie,exports.truncateDecimals=l.t;
@@ -5,7 +5,7 @@ import { buildSortParam as o } from "../build-sort-param/index.js";
5
5
  import { t as s } from "../chunks/currency-symbol-CJp1EcR5.es";
6
6
  import { downloadBlob as c } from "../download-file/index.js";
7
7
  import { i as l, n as u, r as d, t as f } from "../chunks/flags-C5E6W9lm.es";
8
- import { n as p, t as m } from "../chunks/handle-api-error-BYygTKYM.es";
8
+ import { n as p, t as m } from "../chunks/handle-api-error-CoTTGH3m.es";
9
9
  import { normalizeDecimalInput as h, normalizePhoneNumberInput as g } from "../normalize-input/index.js";
10
10
  import { t as _ } from "../chunks/truncate-decimals-BPaTWeHm.es";
11
11
  export { e as buildQueryParams, o as buildSortParam, t as cleanObject, n as defaultCookieOptions, r as deleteCookie, c as downloadBlob, i as getCookie, s as getCurrencySymbol, f as getFlagFallbackUrl, u as getFlagUnknownUrl, d as getFlagUrl, m as handleApiError, l as handleFlagImageError, p as initHandleApiError, h as normalizeDecimalInput, g as normalizePhoneNumberInput, a as setCookie, _ as truncateDecimals };
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../chunks/handle-api-error-D714W9rp.cjs");exports.handleApiError=e.t,exports.initHandleApiError=e.n;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("../chunks/handle-api-error-B-2TPzB6.cjs");exports.handleApiError=e.t,exports.initHandleApiError=e.n;
@@ -1,2 +1,2 @@
1
- import { n as e, t } from "../chunks/handle-api-error-BYygTKYM.es";
1
+ import { n as e, t } from "../chunks/handle-api-error-CoTTGH3m.es";
2
2
  export { t as handleApiError, e as initHandleApiError };
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./chunks/base-DLHy3wps.cjs"),t=require("./chunks/admin-DR1tBkNV.cjs"),n=require("./build-query-params/index.cjs"),r=require("./clean-object/index.cjs"),i=require("./cookies/index.cjs"),a=require("./build-sort-param/index.cjs"),o=require("./chunks/currency-symbol-gb63Q_9Q.cjs"),s=require("./download-file/index.cjs"),c=require("./chunks/flags-BRQ9Jims.cjs"),l=require("./chunks/handle-api-error-D714W9rp.cjs"),u=require("./normalize-input/index.cjs"),d=require("./chunks/truncate-decimals-CArL-IeO.cjs");require("./core/index.cjs"),exports.ADMIN_QUERY_KEYS=t.n,exports.DISCOUNT_TYPES=e.t,exports.ORDER_STATUSES=e.n,exports.SUPPORT_STATUSES=e.r,exports.buildQueryParams=n.buildQueryParams,exports.buildSortParam=a.buildSortParam,exports.cleanObject=r.cleanObject,exports.defaultCookieOptions=i.defaultCookieOptions,exports.deleteCookie=i.deleteCookie,exports.downloadBlob=s.downloadBlob,exports.getCookie=i.getCookie,exports.getCurrencySymbol=o.t,exports.getFlagFallbackUrl=c.t,exports.getFlagUnknownUrl=c.n,exports.getFlagUrl=c.r,exports.handleApiError=l.t,exports.handleFlagImageError=c.i,exports.initHandleApiError=l.n,exports.isAdminToken=t.t,exports.normalizeDecimalInput=u.normalizeDecimalInput,exports.normalizePhoneNumberInput=u.normalizePhoneNumberInput,exports.setCookie=i.setCookie,exports.truncateDecimals=d.t;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});const e=require("./chunks/base-DLHy3wps.cjs"),t=require("./chunks/admin-DR1tBkNV.cjs"),n=require("./build-query-params/index.cjs"),r=require("./clean-object/index.cjs"),i=require("./cookies/index.cjs"),a=require("./build-sort-param/index.cjs"),o=require("./chunks/currency-symbol-gb63Q_9Q.cjs"),s=require("./download-file/index.cjs"),c=require("./chunks/flags-BRQ9Jims.cjs"),l=require("./chunks/handle-api-error-B-2TPzB6.cjs"),u=require("./normalize-input/index.cjs"),d=require("./chunks/truncate-decimals-CArL-IeO.cjs");require("./core/index.cjs"),exports.ADMIN_QUERY_KEYS=t.n,exports.DISCOUNT_TYPES=e.t,exports.ORDER_STATUSES=e.n,exports.SUPPORT_STATUSES=e.r,exports.buildQueryParams=n.buildQueryParams,exports.buildSortParam=a.buildSortParam,exports.cleanObject=r.cleanObject,exports.defaultCookieOptions=i.defaultCookieOptions,exports.deleteCookie=i.deleteCookie,exports.downloadBlob=s.downloadBlob,exports.getCookie=i.getCookie,exports.getCurrencySymbol=o.t,exports.getFlagFallbackUrl=c.t,exports.getFlagUnknownUrl=c.n,exports.getFlagUrl=c.r,exports.handleApiError=l.t,exports.handleFlagImageError=c.i,exports.initHandleApiError=l.n,exports.isAdminToken=t.t,exports.normalizeDecimalInput=u.normalizeDecimalInput,exports.normalizePhoneNumberInput=u.normalizePhoneNumberInput,exports.setCookie=i.setCookie,exports.truncateDecimals=d.t;
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ import { buildSortParam as d } from "./build-sort-param/index.js";
7
7
  import { t as f } from "./chunks/currency-symbol-CJp1EcR5.es";
8
8
  import { downloadBlob as p } from "./download-file/index.js";
9
9
  import { i as m, n as h, r as g, t as _ } from "./chunks/flags-C5E6W9lm.es";
10
- import { n as v, t as y } from "./chunks/handle-api-error-BYygTKYM.es";
10
+ import { n as v, t as y } from "./chunks/handle-api-error-CoTTGH3m.es";
11
11
  import { normalizeDecimalInput as b, normalizePhoneNumberInput as x } from "./normalize-input/index.js";
12
12
  import { t as S } from "./chunks/truncate-decimals-BPaTWeHm.es";
13
13
  import "./core/index.js";
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@ptx-showcase/utils",
3
3
  "author": "ptx-showcase",
4
4
  "description": "Shared utilities for PTX Showcase projects",
5
- "version": "1.1.0",
5
+ "version": "1.1.1-beta.1",
6
6
  "private": false,
7
7
  "type": "module",
8
8
  "license": "MIT",
@@ -113,13 +113,13 @@
113
113
  "access": "public"
114
114
  },
115
115
  "devDependencies": {
116
- "@types/node": "^26.6.1",
116
+ "@types/node": "^26.6.2",
117
117
  "bumpp": "^12.3.0",
118
118
  "oxfmt": "^0.68.0",
119
119
  "oxlint": "^1.83.0",
120
120
  "typescript": "~6.0.3",
121
121
  "vite": "^8.3.0",
122
- "vite-plugin-dts": "^5.1.0",
122
+ "vite-plugin-dts": "^5.1.1",
123
123
  "vitest": "^5.0.1"
124
124
  },
125
125
  "dependencies": {
@@ -1,38 +0,0 @@
1
- import { isHTTPError as e } from "ky";
2
- //#region src/core/handle-api-error/constants/handle-api-error.constant.ts
3
- var t = "Something went wrong", n = /* @__PURE__ */ new Set([
4
- "message",
5
- "detail",
6
- "error",
7
- "msg"
8
- ]), r = (e) => typeof e == "object" && !!e && !Array.isArray(e), i = (e) => typeof e == "string" && e.trim() ? e : null, a = (e, t) => typeof e == "string" ? e.trim() ? [t ? `${t}: ${e}` : e] : [] : Array.isArray(e) ? e.flatMap((e) => a(e, t)) : r(e) ? Object.entries(e).flatMap(([e, t]) => a(t, e === "non_field_errors" ? void 0 : e)) : [], o = (e) => {
9
- if (!e) return [];
10
- if (typeof e == "string") return [e];
11
- if (Array.isArray(e)) return e.filter((e) => typeof e == "string" && !!e.trim());
12
- let t = i(e.detail) ?? i(e.message) ?? i(e.error) ?? i(e.msg);
13
- return [...Object.entries(e).flatMap(([e, t]) => n.has(e) ? [] : e === "non_field_errors" || e === "errors" ? a(t) : a(t, e)), ...t ? [t] : []];
14
- }, s = (e) => {
15
- console.error(e);
16
- }, c = (e) => {
17
- s = e;
18
- }, l = () => s, u = (e) => {
19
- c(e.notify);
20
- }, d = (n) => {
21
- let r = l();
22
- if (e(n)) {
23
- let e = n.data, i = [...new Set(o(e))];
24
- if (i.length > 0) {
25
- i.forEach((e) => r(e));
26
- return;
27
- }
28
- r(t);
29
- return;
30
- }
31
- if (n instanceof Error) {
32
- r(n.message || "Something went wrong");
33
- return;
34
- }
35
- r(t);
36
- };
37
- //#endregion
38
- export { u as n, d as t };
@@ -1 +0,0 @@
1
- let e=require("ky");var t=`Something went wrong`,n=new Set([`message`,`detail`,`error`,`msg`]),r=e=>typeof e==`object`&&!!e&&!Array.isArray(e),i=e=>typeof e==`string`&&e.trim()?e:null,a=(e,t)=>typeof e==`string`?e.trim()?[t?`${t}: ${e}`:e]:[]:Array.isArray(e)?e.flatMap(e=>a(e,t)):r(e)?Object.entries(e).flatMap(([e,t])=>a(t,e===`non_field_errors`?void 0:e)):[],o=e=>{if(!e)return[];if(typeof e==`string`)return[e];if(Array.isArray(e))return e.filter(e=>typeof e==`string`&&!!e.trim());let t=i(e.detail)??i(e.message)??i(e.error)??i(e.msg);return[...Object.entries(e).flatMap(([e,t])=>n.has(e)?[]:e===`non_field_errors`||e===`errors`?a(t):a(t,e)),...t?[t]:[]]},s=e=>{console.error(e)},c=e=>{s=e},l=()=>s,u=e=>{c(e.notify)},d=n=>{let r=l();if((0,e.isHTTPError)(n)){let e=n.data,i=[...new Set(o(e))];if(i.length>0){i.forEach(e=>r(e));return}r(t);return}if(n instanceof Error){r(n.message||`Something went wrong`);return}r(t)};Object.defineProperty(exports,"n",{enumerable:!0,get:function(){return u}}),Object.defineProperty(exports,"t",{enumerable:!0,get:function(){return d}});