@shipstatic/types 0.9.4 → 0.9.5

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
@@ -52,13 +52,23 @@ if (error.isAuthError()) { /* handle auth */ }
52
52
  // Producer side (API workers): serialize a ShipError to JSON
53
53
  return c.json(error.toResponse(), error.status ?? 500);
54
54
 
55
- // Consumer side (SDK, web app): rehydrate from any error Response
55
+ // Consumer side two symmetric helpers cover both HTTP error modes:
56
+
57
+ // 1. Server returned a non-OK response
56
58
  if (!response.ok) {
57
- throw await ShipError.fromHttpResponse(response, 'Get account failed');
59
+ throw await ShipError.fromHttpResponse(response, 'Get account');
58
60
  }
61
+
62
+ // 2. fetch itself threw (offline, abort, CORS, ...)
63
+ try { response = await fetch(url); }
64
+ catch (cause) { throw ShipError.fromFetchError(cause, 'Get account'); }
59
65
  ```
60
66
 
61
- `fromHttpResponse` trusts the body's `error` field when it's a known `ErrorType` — so a server's `ShipError.validation(...)` round-trips back to `ErrorType.Validation` on the client. For non-API responses (CDN errors, intermediaries) or malformed bodies it falls back to status-derived (401 → `Authentication`, 429 → `RateLimit`, else → `Api`). Body's `message` and `details` are preserved best-effort. The optional second arg is a fallback message used when the body has nothing usable.
67
+ `fromHttpResponse` trusts the body's `error` field when it's a known server-producible `ErrorType` — so a server's `ShipError.validation(...)` round-trips back to `ErrorType.Validation` on the client. For non-API responses (CDN errors, intermediaries) or malformed bodies it falls back to status-derived (401 → `Authentication`, 429 → `RateLimit`, else → `Api`). Body's `message` and `details` are preserved best-effort.
68
+
69
+ `fromFetchError` routes by the thrown cause: an existing `ShipError` is returned unchanged, `AbortError` becomes `Cancelled`, a fetch `TypeError` becomes `Network`, anything else becomes `Api` (with no HTTP status — the request never reached the server).
70
+
71
+ Both helpers accept an optional operation-name string for contextual messages (`"Get account was cancelled"`, `"Get account failed: ..."`).
62
72
 
63
73
  ### Status Constants
64
74
 
package/dist/index.d.ts CHANGED
@@ -367,6 +367,25 @@ export declare class ShipError extends Error {
367
367
  * callers can compose; most will `throw await ShipError.fromHttpResponse(...)`.
368
368
  */
369
369
  static fromHttpResponse(response: Response, fallbackMessage?: string): Promise<ShipError>;
370
+ /**
371
+ * Construct a `ShipError` from an error caught around a `fetch()` call.
372
+ *
373
+ * The mirror of `fromHttpResponse` for the *other* side of the HTTP error
374
+ * story — the network layer failing (offline, CORS, abort) rather than the
375
+ * server returning a non-OK response.
376
+ *
377
+ * Routing:
378
+ * - Already a `ShipError` → returned as-is (caller's intent preserved)
379
+ * - `AbortError` → `ShipError.cancelled(...)`
380
+ * - `TypeError` whose message mentions "fetch" → `ShipError.network(...)`
381
+ * - Any other `Error` → `ShipError(Api, ...)` (no HTTP status — fetch never reached the server)
382
+ * - Anything else (string, undefined, etc.) → `ShipError(Api, ...)`
383
+ *
384
+ * The optional `operationName` is composed into the message for context:
385
+ * `"Get account was cancelled"`, `"Get account failed: ..."`. Defaults to
386
+ * `"Request"` when omitted.
387
+ */
388
+ static fromFetchError(cause: unknown, operationName?: string): ShipError;
370
389
  static validation(message: string, details?: any): ShipError;
371
390
  static notFound(resource: string, id?: string): ShipError;
372
391
  static rateLimit(message?: string): ShipError;
package/dist/index.js CHANGED
@@ -179,6 +179,39 @@ export class ShipError extends Error {
179
179
  ErrorType.Api);
180
180
  return new ShipError(type, message, response.status, details);
181
181
  }
182
+ /**
183
+ * Construct a `ShipError` from an error caught around a `fetch()` call.
184
+ *
185
+ * The mirror of `fromHttpResponse` for the *other* side of the HTTP error
186
+ * story — the network layer failing (offline, CORS, abort) rather than the
187
+ * server returning a non-OK response.
188
+ *
189
+ * Routing:
190
+ * - Already a `ShipError` → returned as-is (caller's intent preserved)
191
+ * - `AbortError` → `ShipError.cancelled(...)`
192
+ * - `TypeError` whose message mentions "fetch" → `ShipError.network(...)`
193
+ * - Any other `Error` → `ShipError(Api, ...)` (no HTTP status — fetch never reached the server)
194
+ * - Anything else (string, undefined, etc.) → `ShipError(Api, ...)`
195
+ *
196
+ * The optional `operationName` is composed into the message for context:
197
+ * `"Get account was cancelled"`, `"Get account failed: ..."`. Defaults to
198
+ * `"Request"` when omitted.
199
+ */
200
+ static fromFetchError(cause, operationName) {
201
+ if (isShipError(cause))
202
+ return cause;
203
+ const op = operationName || 'Request';
204
+ if (cause instanceof Error) {
205
+ if (cause.name === 'AbortError') {
206
+ return ShipError.cancelled(`${op} was cancelled`);
207
+ }
208
+ if (cause instanceof TypeError && cause.message.includes('fetch')) {
209
+ return ShipError.network(`${op} failed: ${cause.message}`, cause);
210
+ }
211
+ return new ShipError(ErrorType.Api, `${op} failed: ${cause.message}`);
212
+ }
213
+ return new ShipError(ErrorType.Api, `${op} failed: Unknown error`);
214
+ }
182
215
  // Factory methods for common errors
183
216
  static validation(message, details) {
184
217
  return new ShipError(ErrorType.Validation, message, 400, details);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shipstatic/types",
3
- "version": "0.9.4",
3
+ "version": "0.9.5",
4
4
  "description": "Shared types for ShipStatic platform",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/index.ts CHANGED
@@ -500,6 +500,42 @@ export class ShipError extends Error {
500
500
  return new ShipError(type, message, response.status, details);
501
501
  }
502
502
 
503
+ /**
504
+ * Construct a `ShipError` from an error caught around a `fetch()` call.
505
+ *
506
+ * The mirror of `fromHttpResponse` for the *other* side of the HTTP error
507
+ * story — the network layer failing (offline, CORS, abort) rather than the
508
+ * server returning a non-OK response.
509
+ *
510
+ * Routing:
511
+ * - Already a `ShipError` → returned as-is (caller's intent preserved)
512
+ * - `AbortError` → `ShipError.cancelled(...)`
513
+ * - `TypeError` whose message mentions "fetch" → `ShipError.network(...)`
514
+ * - Any other `Error` → `ShipError(Api, ...)` (no HTTP status — fetch never reached the server)
515
+ * - Anything else (string, undefined, etc.) → `ShipError(Api, ...)`
516
+ *
517
+ * The optional `operationName` is composed into the message for context:
518
+ * `"Get account was cancelled"`, `"Get account failed: ..."`. Defaults to
519
+ * `"Request"` when omitted.
520
+ */
521
+ static fromFetchError(cause: unknown, operationName?: string): ShipError {
522
+ if (isShipError(cause)) return cause;
523
+
524
+ const op = operationName || 'Request';
525
+
526
+ if (cause instanceof Error) {
527
+ if (cause.name === 'AbortError') {
528
+ return ShipError.cancelled(`${op} was cancelled`);
529
+ }
530
+ if (cause instanceof TypeError && cause.message.includes('fetch')) {
531
+ return ShipError.network(`${op} failed: ${cause.message}`, cause);
532
+ }
533
+ return new ShipError(ErrorType.Api, `${op} failed: ${cause.message}`);
534
+ }
535
+
536
+ return new ShipError(ErrorType.Api, `${op} failed: Unknown error`);
537
+ }
538
+
503
539
  // Factory methods for common errors
504
540
  static validation(message: string, details?: any): ShipError {
505
541
  return new ShipError(ErrorType.Validation, message, 400, details);