@b9g/http-errors 0.1.3 → 0.1.4

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
@@ -1,14 +1,14 @@
1
1
  # @b9g/http-errors
2
2
 
3
- Standard HTTP error responses with proper status codes and web platform Response objects.
3
+ **Standard HTTP error responses for ServiceWorker applications. Returns proper Response objects with status codes, not thrown exceptions.**
4
4
 
5
5
  ## Features
6
6
 
7
- - **Standard HTTP Errors**: Pre-defined error classes for common HTTP status codes
8
- - **Web Platform Response**: Returns proper Response objects, not thrown exceptions
9
- - **Consistent API**: Uniform interface across all error types
7
+ - **ServiceWorker Compatible**: Returns Response objects perfect for `event.respondWith()`
8
+ - **Standard HTTP Status Codes**: Pre-defined functions for all common HTTP errors
9
+ - **No Exceptions**: Functional approach - return errors, don't throw them
10
10
  - **TypeScript Support**: Full type definitions for all error classes
11
- - **Lightweight**: Minimal dependencies, works everywhere
11
+ - **Universal**: Works in browsers, Node.js, Bun, and edge platforms
12
12
 
13
13
  ## Installation
14
14
 
@@ -130,9 +130,9 @@ router.get('/users/:id', async (request, context) => {
130
130
  ### Middleware Error Handling
131
131
 
132
132
  ```javascript
133
- router.use(async (request, context, next) => {
133
+ router.use(async function* (request, context) {
134
134
  try {
135
- return await next();
135
+ return yield request;
136
136
  } catch (error) {
137
137
  console.error('Request failed:', error);
138
138
  return InternalServerError('Something went wrong');
@@ -165,9 +165,48 @@ const duplicateError = Conflict('Email already exists', {
165
165
  });
166
166
  ```
167
167
 
168
+ ## Exports
169
+
170
+ ### Classes
171
+
172
+ - `HTTPError` - Base HTTP error class (extends Error)
173
+ - `NotHandled` - Special error for unhandled requests
174
+
175
+ ### Client Error Classes (4xx)
176
+
177
+ - `BadRequest` (400)
178
+ - `Unauthorized` (401)
179
+ - `Forbidden` (403)
180
+ - `NotFound` (404)
181
+ - `MethodNotAllowed` (405)
182
+ - `Conflict` (409)
183
+ - `UnprocessableEntity` (422)
184
+ - `TooManyRequests` (429)
185
+
186
+ ### Server Error Classes (5xx)
187
+
188
+ - `InternalServerError` (500)
189
+ - `NotImplemented` (501)
190
+ - `BadGateway` (502)
191
+ - `ServiceUnavailable` (503)
192
+ - `GatewayTimeout` (504)
193
+
194
+ ### Functions
195
+
196
+ - `createHTTPError(status, message?, options?)` - Create an HTTPError with a specific status code
197
+ - `isHTTPError(value)` - Type guard to check if a value is an HTTPError
198
+
199
+ ### Types
200
+
201
+ - `HTTPErrorOptions` - Options for HTTPError constructor
202
+
203
+ ### Default Export
204
+
205
+ - `createHTTPError` - Factory function for creating HTTP errors
206
+
168
207
  ## API Reference
169
208
 
170
- ### Error Functions
209
+ ### Error Classes
171
210
 
172
211
  All error functions follow the same signature:
173
212
 
@@ -259,7 +298,7 @@ router.post('/api/users', async (request) => {
259
298
  ```javascript
260
299
  import { Unauthorized, Forbidden } from '@b9g/http-errors';
261
300
 
262
- const authMiddleware = async (request, context, next) => {
301
+ const authMiddleware = async function* (request, context) {
263
302
  const token = request.headers.get('authorization');
264
303
 
265
304
  if (!token) {
@@ -269,7 +308,7 @@ const authMiddleware = async (request, context, next) => {
269
308
  try {
270
309
  const user = await verifyToken(token);
271
310
  context.user = user;
272
- return await next();
311
+ return yield request;
273
312
  } catch (error) {
274
313
  return Forbidden('Invalid or expired token');
275
314
  }
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@b9g/http-errors",
3
- "version": "0.1.3",
4
- "description": "HTTP error classes for web applications",
3
+ "version": "0.1.4",
4
+ "description": "Standard HTTP error responses for ServiceWorker applications. Returns proper Response objects with status codes, not thrown exceptions.",
5
5
  "keywords": [
6
6
  "http",
7
7
  "errors",
8
- "web",
9
- "status-codes"
8
+ "response",
9
+ "serviceworker",
10
+ "status-codes",
11
+ "web-standards",
12
+ "functional",
13
+ "shovel"
10
14
  ],
11
15
  "dependencies": {},
12
16
  "devDependencies": {
package/src/index.d.ts CHANGED
@@ -35,10 +35,6 @@ export declare class HTTPError extends Error {
35
35
  expose: boolean;
36
36
  headers: Record<string, string>;
37
37
  };
38
- /**
39
- * Create a Response object from this error
40
- */
41
- toResponse(): Response;
42
38
  }
43
39
  /**
44
40
  * Special error for middleware fallthrough (not an HTTP error)
package/src/index.js CHANGED
@@ -71,17 +71,6 @@ var HTTPError = class extends Error {
71
71
  headers: this.headers
72
72
  };
73
73
  }
74
- /**
75
- * Create a Response object from this error
76
- */
77
- toResponse() {
78
- const body = this.expose ? this.message : STATUS_CODES[this.status];
79
- return new Response(body, {
80
- status: this.status,
81
- statusText: STATUS_CODES[this.status],
82
- headers: this.headers
83
- });
84
- }
85
74
  };
86
75
  var NotHandled = class extends Error {
87
76
  constructor(message = "Request not handled by middleware") {
@@ -90,7 +79,13 @@ var NotHandled = class extends Error {
90
79
  }
91
80
  };
92
81
  function isHTTPError(value) {
93
- return value instanceof HTTPError || value instanceof Error && typeof value.status === "number" && typeof value.statusCode === "number" && value.status === value.statusCode;
82
+ if (value instanceof HTTPError)
83
+ return true;
84
+ if (!(value instanceof Error))
85
+ return false;
86
+ const hasStatus = "status" in value && typeof value.status === "number";
87
+ const hasStatusCode = "statusCode" in value && typeof value.statusCode === "number";
88
+ return hasStatus && hasStatusCode && value.status === value.statusCode;
94
89
  }
95
90
  function createHTTPError(status, message, options) {
96
91
  return new HTTPError(status, message, options);