@b9g/http-errors 0.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/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@b9g/http-errors",
3
+ "version": "0.1.0",
4
+ "description": "HTTP error classes for web applications",
5
+ "keywords": [
6
+ "http",
7
+ "errors",
8
+ "web",
9
+ "status-codes"
10
+ ],
11
+ "dependencies": {},
12
+ "devDependencies": {
13
+ "@b9g/libuild": "^0.1.10",
14
+ "bun-types": "latest"
15
+ },
16
+ "type": "module",
17
+ "types": "src/index.d.ts",
18
+ "module": "src/index.js",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./src/index.d.ts",
22
+ "import": "./src/index.js"
23
+ },
24
+ "./package.json": "./package.json",
25
+ "./index": {
26
+ "types": "./src/index.d.ts",
27
+ "import": "./src/index.js"
28
+ },
29
+ "./index.js": {
30
+ "types": "./src/index.d.ts",
31
+ "import": "./src/index.js"
32
+ }
33
+ }
34
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,96 @@
1
+ /**
2
+ * Modern HTTP error classes for Shovel
3
+ * Lightweight alternative to http-errors with native Error cause support
4
+ */
5
+ /**
6
+ * Options for creating HTTP errors
7
+ */
8
+ export interface HTTPErrorOptions {
9
+ /** Original error that caused this HTTP error */
10
+ cause?: Error;
11
+ /** Custom headers to include in the error */
12
+ headers?: Record<string, string>;
13
+ /** Whether the error details should be exposed to clients (defaults based on status) */
14
+ expose?: boolean;
15
+ /** Additional properties to attach to the error */
16
+ [key: string]: any;
17
+ }
18
+ /**
19
+ * Base HTTP error class
20
+ */
21
+ export declare class HTTPError extends Error {
22
+ readonly status: number;
23
+ readonly statusCode: number;
24
+ readonly expose: boolean;
25
+ readonly headers?: Record<string, string>;
26
+ constructor(status: number, message?: string, options?: HTTPErrorOptions);
27
+ /**
28
+ * Convert error to a plain object for serialization
29
+ */
30
+ toJSON(): {
31
+ name: string;
32
+ message: string;
33
+ status: number;
34
+ statusCode: number;
35
+ expose: boolean;
36
+ headers: Record<string, string>;
37
+ };
38
+ /**
39
+ * Create a Response object from this error
40
+ */
41
+ toResponse(): Response;
42
+ }
43
+ /**
44
+ * Special error for middleware fallthrough (not an HTTP error)
45
+ */
46
+ export declare class NotHandled extends Error {
47
+ constructor(message?: string);
48
+ }
49
+ /**
50
+ * Check if a value is an HTTP error
51
+ */
52
+ export declare function isHTTPError(value: any): value is HTTPError;
53
+ /**
54
+ * Create an HTTP error with the given status code
55
+ */
56
+ export declare function createHTTPError(status: number, message?: string, options?: HTTPErrorOptions): HTTPError;
57
+ export declare class BadRequest extends HTTPError {
58
+ constructor(message?: string, options?: HTTPErrorOptions);
59
+ }
60
+ export declare class Unauthorized extends HTTPError {
61
+ constructor(message?: string, options?: HTTPErrorOptions);
62
+ }
63
+ export declare class Forbidden extends HTTPError {
64
+ constructor(message?: string, options?: HTTPErrorOptions);
65
+ }
66
+ export declare class NotFound extends HTTPError {
67
+ constructor(message?: string, options?: HTTPErrorOptions);
68
+ }
69
+ export declare class MethodNotAllowed extends HTTPError {
70
+ constructor(message?: string, options?: HTTPErrorOptions);
71
+ }
72
+ export declare class Conflict extends HTTPError {
73
+ constructor(message?: string, options?: HTTPErrorOptions);
74
+ }
75
+ export declare class UnprocessableEntity extends HTTPError {
76
+ constructor(message?: string, options?: HTTPErrorOptions);
77
+ }
78
+ export declare class TooManyRequests extends HTTPError {
79
+ constructor(message?: string, options?: HTTPErrorOptions);
80
+ }
81
+ export declare class InternalServerError extends HTTPError {
82
+ constructor(message?: string, options?: HTTPErrorOptions);
83
+ }
84
+ export declare class NotImplemented extends HTTPError {
85
+ constructor(message?: string, options?: HTTPErrorOptions);
86
+ }
87
+ export declare class BadGateway extends HTTPError {
88
+ constructor(message?: string, options?: HTTPErrorOptions);
89
+ }
90
+ export declare class ServiceUnavailable extends HTTPError {
91
+ constructor(message?: string, options?: HTTPErrorOptions);
92
+ }
93
+ export declare class GatewayTimeout extends HTTPError {
94
+ constructor(message?: string, options?: HTTPErrorOptions);
95
+ }
96
+ export default createHTTPError;
package/src/index.js ADDED
@@ -0,0 +1,183 @@
1
+ /// <reference types="./index.d.ts" />
2
+ // src/index.ts
3
+ var STATUS_CODES = {
4
+ // 4xx Client Errors
5
+ 400: "Bad Request",
6
+ 401: "Unauthorized",
7
+ 402: "Payment Required",
8
+ 403: "Forbidden",
9
+ 404: "Not Found",
10
+ 405: "Method Not Allowed",
11
+ 406: "Not Acceptable",
12
+ 407: "Proxy Authentication Required",
13
+ 408: "Request Timeout",
14
+ 409: "Conflict",
15
+ 410: "Gone",
16
+ 411: "Length Required",
17
+ 412: "Precondition Failed",
18
+ 413: "Payload Too Large",
19
+ 414: "URI Too Long",
20
+ 415: "Unsupported Media Type",
21
+ 416: "Range Not Satisfiable",
22
+ 417: "Expectation Failed",
23
+ 418: "I'm a Teapot",
24
+ 421: "Misdirected Request",
25
+ 422: "Unprocessable Entity",
26
+ 423: "Locked",
27
+ 424: "Failed Dependency",
28
+ 425: "Too Early",
29
+ 426: "Upgrade Required",
30
+ 428: "Precondition Required",
31
+ 429: "Too Many Requests",
32
+ 431: "Request Header Fields Too Large",
33
+ 451: "Unavailable For Legal Reasons",
34
+ // 5xx Server Errors
35
+ 500: "Internal Server Error",
36
+ 501: "Not Implemented",
37
+ 502: "Bad Gateway",
38
+ 503: "Service Unavailable",
39
+ 504: "Gateway Timeout",
40
+ 505: "HTTP Version Not Supported",
41
+ 506: "Variant Also Negotiates",
42
+ 507: "Insufficient Storage",
43
+ 508: "Loop Detected",
44
+ 510: "Not Extended",
45
+ 511: "Network Authentication Required"
46
+ };
47
+ var HTTPError = class extends Error {
48
+ status;
49
+ statusCode;
50
+ expose;
51
+ headers;
52
+ constructor(status, message, options = {}) {
53
+ const defaultMessage = STATUS_CODES[status] || "Unknown Error";
54
+ super(message || defaultMessage, { cause: options.cause });
55
+ this.name = this.constructor.name;
56
+ this.status = this.statusCode = status;
57
+ this.expose = options.expose ?? status < 500;
58
+ this.headers = options.headers;
59
+ Object.assign(this, options);
60
+ }
61
+ /**
62
+ * Convert error to a plain object for serialization
63
+ */
64
+ toJSON() {
65
+ return {
66
+ name: this.name,
67
+ message: this.message,
68
+ status: this.status,
69
+ statusCode: this.statusCode,
70
+ expose: this.expose,
71
+ headers: this.headers
72
+ };
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
+ };
86
+ var NotHandled = class extends Error {
87
+ constructor(message = "Request not handled by middleware") {
88
+ super(message);
89
+ this.name = "NotHandled";
90
+ }
91
+ };
92
+ function isHTTPError(value) {
93
+ return value instanceof HTTPError || value instanceof Error && typeof value.status === "number" && typeof value.statusCode === "number" && value.status === value.statusCode;
94
+ }
95
+ function createHTTPError(status, message, options) {
96
+ return new HTTPError(status, message, options);
97
+ }
98
+ var BadRequest = class extends HTTPError {
99
+ constructor(message, options) {
100
+ super(400, message, options);
101
+ }
102
+ };
103
+ var Unauthorized = class extends HTTPError {
104
+ constructor(message, options) {
105
+ super(401, message, options);
106
+ }
107
+ };
108
+ var Forbidden = class extends HTTPError {
109
+ constructor(message, options) {
110
+ super(403, message, options);
111
+ }
112
+ };
113
+ var NotFound = class extends HTTPError {
114
+ constructor(message, options) {
115
+ super(404, message, options);
116
+ }
117
+ };
118
+ var MethodNotAllowed = class extends HTTPError {
119
+ constructor(message, options) {
120
+ super(405, message, options);
121
+ }
122
+ };
123
+ var Conflict = class extends HTTPError {
124
+ constructor(message, options) {
125
+ super(409, message, options);
126
+ }
127
+ };
128
+ var UnprocessableEntity = class extends HTTPError {
129
+ constructor(message, options) {
130
+ super(422, message, options);
131
+ }
132
+ };
133
+ var TooManyRequests = class extends HTTPError {
134
+ constructor(message, options) {
135
+ super(429, message, options);
136
+ }
137
+ };
138
+ var InternalServerError = class extends HTTPError {
139
+ constructor(message, options) {
140
+ super(500, message, options);
141
+ }
142
+ };
143
+ var NotImplemented = class extends HTTPError {
144
+ constructor(message, options) {
145
+ super(501, message, options);
146
+ }
147
+ };
148
+ var BadGateway = class extends HTTPError {
149
+ constructor(message, options) {
150
+ super(502, message, options);
151
+ }
152
+ };
153
+ var ServiceUnavailable = class extends HTTPError {
154
+ constructor(message, options) {
155
+ super(503, message, options);
156
+ }
157
+ };
158
+ var GatewayTimeout = class extends HTTPError {
159
+ constructor(message, options) {
160
+ super(504, message, options);
161
+ }
162
+ };
163
+ var src_default = createHTTPError;
164
+ export {
165
+ BadGateway,
166
+ BadRequest,
167
+ Conflict,
168
+ Forbidden,
169
+ GatewayTimeout,
170
+ HTTPError,
171
+ InternalServerError,
172
+ MethodNotAllowed,
173
+ NotFound,
174
+ NotHandled,
175
+ NotImplemented,
176
+ ServiceUnavailable,
177
+ TooManyRequests,
178
+ Unauthorized,
179
+ UnprocessableEntity,
180
+ createHTTPError,
181
+ src_default as default,
182
+ isHTTPError
183
+ };