@markwharton/pwa-core 3.0.0 → 3.2.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/dist/server.d.ts CHANGED
@@ -217,15 +217,28 @@ export declare function validateRequired(value: string | undefined, paramName: s
217
217
  */
218
218
  export type ErrorCallback = (operation: string, message: string) => void;
219
219
  /**
220
- * Sets a callback to be invoked when handleFunctionError is called.
220
+ * Initializes error handling configuration. Call once at application startup.
221
221
  * Use this to integrate with alerting or monitoring systems.
222
- * @param callback - The callback to invoke (fire-and-forget)
222
+ * @param config - Configuration object
223
+ * @param config.callback - Optional callback invoked when handleFunctionError is called (fire-and-forget)
223
224
  * @example
224
- * setErrorCallback((operation, message) => {
225
- * sendAlert(operation, message); // Fire-and-forget
225
+ * initErrorHandling({
226
+ * callback: (operation, message) => {
227
+ * sendAlert(operation, message); // Fire-and-forget
228
+ * }
226
229
  * });
227
230
  */
228
- export declare function setErrorCallback(callback: ErrorCallback | null): void;
231
+ export declare function initErrorHandling(config?: {
232
+ callback?: ErrorCallback;
233
+ }): void;
234
+ /**
235
+ * Initializes error handling from environment variables.
236
+ * Currently a no-op but provides consistent API for future error config
237
+ * (e.g., ERROR_LOG_LEVEL, ERROR_INCLUDE_STACK).
238
+ * @example
239
+ * initErrorHandlingFromEnv(); // Uses process.env automatically
240
+ */
241
+ export declare function initErrorHandlingFromEnv(): void;
229
242
  /**
230
243
  * Handles unexpected errors safely by logging details and returning a generic message.
231
244
  * Use in catch blocks to avoid exposing internal error details to clients.
@@ -287,6 +300,7 @@ export declare function isConflictError(error: unknown): boolean;
287
300
  * @param config - Storage configuration options
288
301
  * @param config.accountName - Azure Storage account name (for managed identity)
289
302
  * @param config.connectionString - Connection string (for local development)
303
+ * @throws Error if neither accountName nor connectionString is provided
290
304
  * @example
291
305
  * // Production (Azure with managed identity)
292
306
  * initStorage({ accountName: 'mystorageaccount' });
package/dist/server.js CHANGED
@@ -28,7 +28,8 @@ exports.forbiddenResponse = forbiddenResponse;
28
28
  exports.notFoundResponse = notFoundResponse;
29
29
  exports.conflictResponse = conflictResponse;
30
30
  exports.validateRequired = validateRequired;
31
- exports.setErrorCallback = setErrorCallback;
31
+ exports.initErrorHandling = initErrorHandling;
32
+ exports.initErrorHandlingFromEnv = initErrorHandlingFromEnv;
32
33
  exports.handleFunctionError = handleFunctionError;
33
34
  exports.isTableStorageError = isTableStorageError;
34
35
  exports.isNotFoundError = isNotFoundError;
@@ -341,16 +342,30 @@ function validateRequired(value, paramName) {
341
342
  }
342
343
  let errorCallback = null;
343
344
  /**
344
- * Sets a callback to be invoked when handleFunctionError is called.
345
+ * Initializes error handling configuration. Call once at application startup.
345
346
  * Use this to integrate with alerting or monitoring systems.
346
- * @param callback - The callback to invoke (fire-and-forget)
347
+ * @param config - Configuration object
348
+ * @param config.callback - Optional callback invoked when handleFunctionError is called (fire-and-forget)
347
349
  * @example
348
- * setErrorCallback((operation, message) => {
349
- * sendAlert(operation, message); // Fire-and-forget
350
+ * initErrorHandling({
351
+ * callback: (operation, message) => {
352
+ * sendAlert(operation, message); // Fire-and-forget
353
+ * }
350
354
  * });
351
355
  */
352
- function setErrorCallback(callback) {
353
- errorCallback = callback;
356
+ function initErrorHandling(config = {}) {
357
+ errorCallback = config.callback ?? null;
358
+ }
359
+ /**
360
+ * Initializes error handling from environment variables.
361
+ * Currently a no-op but provides consistent API for future error config
362
+ * (e.g., ERROR_LOG_LEVEL, ERROR_INCLUDE_STACK).
363
+ * @example
364
+ * initErrorHandlingFromEnv(); // Uses process.env automatically
365
+ */
366
+ function initErrorHandlingFromEnv() {
367
+ // Future: read ERROR_LOG_LEVEL, ERROR_INCLUDE_STACK, etc.
368
+ initErrorHandling({});
354
369
  }
355
370
  /**
356
371
  * Handles unexpected errors safely by logging details and returning a generic message.
@@ -453,6 +468,7 @@ let storageConnectionString;
453
468
  * @param config - Storage configuration options
454
469
  * @param config.accountName - Azure Storage account name (for managed identity)
455
470
  * @param config.connectionString - Connection string (for local development)
471
+ * @throws Error if neither accountName nor connectionString is provided
456
472
  * @example
457
473
  * // Production (Azure with managed identity)
458
474
  * initStorage({ accountName: 'mystorageaccount' });
@@ -461,8 +477,12 @@ let storageConnectionString;
461
477
  * initStorage({ connectionString: 'UseDevelopmentStorage=true' });
462
478
  */
463
479
  function initStorage(config) {
464
- storageAccountName = config.accountName;
465
- storageConnectionString = config.connectionString;
480
+ const { accountName, connectionString } = config;
481
+ if (!accountName && !connectionString) {
482
+ throw new Error('Storage requires either accountName (for managed identity) or connectionString');
483
+ }
484
+ storageAccountName = accountName;
485
+ storageConnectionString = connectionString;
466
486
  }
467
487
  /**
468
488
  * Initializes storage from environment variables.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markwharton/pwa-core",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "description": "Shared patterns for Azure PWA projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",