@markwharton/pwa-core 3.0.0 → 3.1.0

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/client.d.ts CHANGED
@@ -83,6 +83,20 @@ export declare class ApiError extends Error {
83
83
  * });
84
84
  */
85
85
  export declare function initApiClient(config: ApiClientConfig): void;
86
+ /**
87
+ * Extract a user-friendly error message from an API error.
88
+ * Use in catch blocks to convert errors to displayable strings.
89
+ * @param error - The caught error (typically ApiError or Error)
90
+ * @param fallback - Message to use if error is not recognized
91
+ * @returns A user-friendly error message
92
+ * @example
93
+ * try {
94
+ * await apiGet('/users');
95
+ * } catch (error) {
96
+ * showToast(getApiErrorMessage(error, 'Failed to load users'), 'error');
97
+ * }
98
+ */
99
+ export declare function getApiErrorMessage(error: unknown, fallback: string): string;
86
100
  /**
87
101
  * Makes an authenticated API call. Throws ApiError on non-2xx responses.
88
102
  * @typeParam T - The expected response data type
package/dist/client.js CHANGED
@@ -7,6 +7,7 @@
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.ApiError = void 0;
9
9
  exports.initApiClient = initApiClient;
10
+ exports.getApiErrorMessage = getApiErrorMessage;
10
11
  exports.apiCall = apiCall;
11
12
  exports.apiGet = apiGet;
12
13
  exports.apiPost = apiPost;
@@ -149,6 +150,28 @@ function initApiClient(config) {
149
150
  onUnauthorized = config.onUnauthorized ?? null;
150
151
  requestTimeout = config.timeout ?? 30000;
151
152
  }
153
+ /**
154
+ * Extract a user-friendly error message from an API error.
155
+ * Use in catch blocks to convert errors to displayable strings.
156
+ * @param error - The caught error (typically ApiError or Error)
157
+ * @param fallback - Message to use if error is not recognized
158
+ * @returns A user-friendly error message
159
+ * @example
160
+ * try {
161
+ * await apiGet('/users');
162
+ * } catch (error) {
163
+ * showToast(getApiErrorMessage(error, 'Failed to load users'), 'error');
164
+ * }
165
+ */
166
+ function getApiErrorMessage(error, fallback) {
167
+ if (error instanceof ApiError) {
168
+ return error.message || `${fallback} (${error.status})`;
169
+ }
170
+ if (error instanceof Error) {
171
+ return error.message;
172
+ }
173
+ return fallback;
174
+ }
152
175
  /**
153
176
  * Makes an authenticated API call. Throws ApiError on non-2xx responses.
154
177
  * @typeParam T - The expected response data type
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markwharton/pwa-core",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Shared patterns for Azure PWA projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",