@sdxc/http 0.0.0-pre.1
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/LICENSE.md +21 -0
- package/README.md +549 -0
- package/dist/cache/conditional.d.ts +24 -0
- package/dist/cache/conditional.js +108 -0
- package/dist/cache/etag.d.ts +37 -0
- package/dist/cache/etag.js +33 -0
- package/dist/cache/http-date.d.ts +38 -0
- package/dist/cache/http-date.js +56 -0
- package/dist/cache/if-modified-since.d.ts +36 -0
- package/dist/cache/if-modified-since.js +53 -0
- package/dist/cache/index.d.ts +20 -0
- package/dist/cache/index.js +16 -0
- package/dist/cache/policies.d.ts +73 -0
- package/dist/cache/policies.js +78 -0
- package/dist/cache/policy.d.ts +63 -0
- package/dist/cache/policy.js +54 -0
- package/dist/cache/precondition.d.ts +53 -0
- package/dist/cache/precondition.js +53 -0
- package/dist/cache/vary.d.ts +24 -0
- package/dist/cache/vary.js +33 -0
- package/dist/content-type.d.ts +312 -0
- package/dist/content-type.js +312 -0
- package/dist/middleware/head-requests.d.ts +19 -0
- package/dist/middleware/head-requests.js +30 -0
- package/dist/negotiate.d.ts +97 -0
- package/dist/negotiate.js +156 -0
- package/dist/response/html.d.ts +272 -0
- package/dist/response/html.js +316 -0
- package/dist/response/json.d.ts +272 -0
- package/dist/response/json.js +316 -0
- package/dist/response.d.ts +171 -0
- package/dist/response.js +225 -0
- package/dist/status-code.d.ts +620 -0
- package/dist/status-code.js +486 -0
- package/package.json +33 -0
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML response helpers, one per HTTP status code, pairing an HTML body with
|
|
3
|
+
* its matching status and status text.
|
|
4
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
5
|
+
* @copyright Sergio Xalambrí 2026
|
|
6
|
+
*/
|
|
7
|
+
type Init = Omit<ResponseInit, "status" | "statusText">;
|
|
8
|
+
/**
|
|
9
|
+
* Creates an HTML response with HTTP 200 OK status.
|
|
10
|
+
* Use for successful requests that return HTML content.
|
|
11
|
+
* @param body - The HTML string to send
|
|
12
|
+
* @param init - Optional response options (headers, etc.)
|
|
13
|
+
* @returns A Response with HTML body and 200 status
|
|
14
|
+
* @example
|
|
15
|
+
* return ok("<h1>Welcome</h1>");
|
|
16
|
+
* @example
|
|
17
|
+
* return ok(renderHomePage(), { headers: { "Cache-Control": "max-age=3600" } });
|
|
18
|
+
*/
|
|
19
|
+
export declare function ok(body: string, init?: Init): Response;
|
|
20
|
+
/**
|
|
21
|
+
* Creates an HTML response with HTTP 201 Created status.
|
|
22
|
+
* Use for successful resource creation that returns HTML confirmation.
|
|
23
|
+
* @param body - The HTML string to send
|
|
24
|
+
* @param init - Optional response options (headers, etc.)
|
|
25
|
+
* @returns A Response with HTML body and 201 status
|
|
26
|
+
* @example
|
|
27
|
+
* return created("<h1>Account Created</h1>");
|
|
28
|
+
* @example
|
|
29
|
+
* return created(renderSuccessPage({ message: "Your account has been created" }));
|
|
30
|
+
*/
|
|
31
|
+
export declare function created(body: string, init?: Init): Response;
|
|
32
|
+
/**
|
|
33
|
+
* Creates an HTML response with HTTP 202 Accepted status.
|
|
34
|
+
* Use when a request has been accepted for processing but not yet completed.
|
|
35
|
+
* @param body - The HTML string to send
|
|
36
|
+
* @param init - Optional response options (headers, etc.)
|
|
37
|
+
* @returns A Response with HTML body and 202 status
|
|
38
|
+
* @example
|
|
39
|
+
* return accepted("<h1>Request Accepted</h1>");
|
|
40
|
+
* @example
|
|
41
|
+
* return accepted(renderProcessingPage({ jobId: "123" }));
|
|
42
|
+
*/
|
|
43
|
+
export declare function accepted(body: string, init?: Init): Response;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an HTML response with HTTP 400 Bad Request status.
|
|
46
|
+
* Use when the request is malformed or contains invalid data.
|
|
47
|
+
* @param body - The HTML string to send
|
|
48
|
+
* @param init - Optional response options (headers, etc.)
|
|
49
|
+
* @returns A Response with HTML body and 400 status
|
|
50
|
+
* @example
|
|
51
|
+
* return badRequest("<h1>Bad Request</h1>");
|
|
52
|
+
* @example
|
|
53
|
+
* return badRequest(renderErrorPage({ code: 400, message: "Invalid input" }));
|
|
54
|
+
*/
|
|
55
|
+
export declare function badRequest(body: string, init?: Init): Response;
|
|
56
|
+
/**
|
|
57
|
+
* Creates an HTML response with HTTP 401 Unauthorized status.
|
|
58
|
+
* Use when authentication is required but missing or invalid.
|
|
59
|
+
* @param body - The HTML string to send
|
|
60
|
+
* @param init - Optional response options (headers, etc.)
|
|
61
|
+
* @returns A Response with HTML body and 401 status
|
|
62
|
+
* @example
|
|
63
|
+
* return unauthorized("<h1>Please Log In</h1>");
|
|
64
|
+
* @example
|
|
65
|
+
* return unauthorized(renderLoginPage({ redirect: "/dashboard" }));
|
|
66
|
+
*/
|
|
67
|
+
export declare function unauthorized(body: string, init?: Init): Response;
|
|
68
|
+
/**
|
|
69
|
+
* Creates an HTML response with HTTP 402 Payment Required status.
|
|
70
|
+
* Use when payment is required to access the requested resource.
|
|
71
|
+
* @param body - The HTML string to send
|
|
72
|
+
* @param init - Optional response options (headers, etc.)
|
|
73
|
+
* @returns A Response with HTML body and 402 status
|
|
74
|
+
* @example
|
|
75
|
+
* return paymentRequired("<h1>Payment Required</h1>");
|
|
76
|
+
* @example
|
|
77
|
+
* return paymentRequired(renderPaywallPage({ feature: "premium" }));
|
|
78
|
+
*/
|
|
79
|
+
export declare function paymentRequired(body: string, init?: Init): Response;
|
|
80
|
+
/**
|
|
81
|
+
* Creates an HTML response with HTTP 403 Forbidden status.
|
|
82
|
+
* Use when the user lacks permission to access the resource.
|
|
83
|
+
* @param body - The HTML string to send
|
|
84
|
+
* @param init - Optional response options (headers, etc.)
|
|
85
|
+
* @returns A Response with HTML body and 403 status
|
|
86
|
+
* @example
|
|
87
|
+
* return forbidden("<h1>Access Denied</h1>");
|
|
88
|
+
* @example
|
|
89
|
+
* return forbidden(renderErrorPage({ code: 403, message: "You don't have permission" }));
|
|
90
|
+
*/
|
|
91
|
+
export declare function forbidden(body: string, init?: Init): Response;
|
|
92
|
+
/**
|
|
93
|
+
* Creates an HTML response with HTTP 404 Not Found status.
|
|
94
|
+
* Use for rendering custom "not found" pages.
|
|
95
|
+
* @param body - The HTML string to send
|
|
96
|
+
* @param init - Optional response options (headers, etc.)
|
|
97
|
+
* @returns A Response with HTML body and 404 status
|
|
98
|
+
* @example
|
|
99
|
+
* return notFound("<h1>Page Not Found</h1>");
|
|
100
|
+
* @example
|
|
101
|
+
* return notFound(renderErrorPage({ code: 404, message: "Not Found" }));
|
|
102
|
+
*/
|
|
103
|
+
export declare function notFound(body: string, init?: Init): Response;
|
|
104
|
+
/**
|
|
105
|
+
* Creates an HTML response with HTTP 405 Method Not Allowed status.
|
|
106
|
+
* Use when the HTTP method is not supported for the requested resource.
|
|
107
|
+
* @param body - The HTML string to send
|
|
108
|
+
* @param init - Optional response options (headers, etc.)
|
|
109
|
+
* @returns A Response with HTML body and 405 status
|
|
110
|
+
* @example
|
|
111
|
+
* return methodNotAllowed("<h1>Method Not Allowed</h1>");
|
|
112
|
+
* @example
|
|
113
|
+
* return methodNotAllowed(renderErrorPage({ code: 405, message: "POST not supported" }));
|
|
114
|
+
*/
|
|
115
|
+
export declare function methodNotAllowed(body: string, init?: Init): Response;
|
|
116
|
+
/**
|
|
117
|
+
* Creates an HTML response with HTTP 406 Not Acceptable status.
|
|
118
|
+
* Use when the server cannot produce a response matching acceptable values.
|
|
119
|
+
* @param body - The HTML string to send
|
|
120
|
+
* @param init - Optional response options (headers, etc.)
|
|
121
|
+
* @returns A Response with HTML body and 406 status
|
|
122
|
+
* @example
|
|
123
|
+
* return notAcceptable("<h1>Not Acceptable</h1>");
|
|
124
|
+
* @example
|
|
125
|
+
* return notAcceptable(renderErrorPage({ code: 406, message: "Cannot produce requested format" }));
|
|
126
|
+
*/
|
|
127
|
+
export declare function notAcceptable(body: string, init?: Init): Response;
|
|
128
|
+
/**
|
|
129
|
+
* Creates an HTML response with HTTP 409 Conflict status.
|
|
130
|
+
* Use when the request conflicts with the current state of the resource.
|
|
131
|
+
* @param body - The HTML string to send
|
|
132
|
+
* @param init - Optional response options (headers, etc.)
|
|
133
|
+
* @returns A Response with HTML body and 409 status
|
|
134
|
+
* @example
|
|
135
|
+
* return conflict("<h1>Conflict</h1>");
|
|
136
|
+
* @example
|
|
137
|
+
* return conflict(renderErrorPage({ code: 409, message: "Resource already exists" }));
|
|
138
|
+
*/
|
|
139
|
+
export declare function conflict(body: string, init?: Init): Response;
|
|
140
|
+
/**
|
|
141
|
+
* Creates an HTML response with HTTP 410 Gone status.
|
|
142
|
+
* Use when the resource is no longer available and will not return.
|
|
143
|
+
* @param body - The HTML string to send
|
|
144
|
+
* @param init - Optional response options (headers, etc.)
|
|
145
|
+
* @returns A Response with HTML body and 410 status
|
|
146
|
+
* @example
|
|
147
|
+
* return gone("<h1>Resource Gone</h1>");
|
|
148
|
+
* @example
|
|
149
|
+
* return gone(renderErrorPage({ code: 410, message: "This page has been permanently removed" }));
|
|
150
|
+
*/
|
|
151
|
+
export declare function gone(body: string, init?: Init): Response;
|
|
152
|
+
/**
|
|
153
|
+
* Creates an HTML response with HTTP 412 Precondition Failed status.
|
|
154
|
+
* Use when preconditions in request headers are not met.
|
|
155
|
+
* @param body - The HTML string to send
|
|
156
|
+
* @param init - Optional response options (headers, etc.)
|
|
157
|
+
* @returns A Response with HTML body and 412 status
|
|
158
|
+
* @example
|
|
159
|
+
* return preconditionFailed("<h1>Precondition Failed</h1>");
|
|
160
|
+
* @example
|
|
161
|
+
* return preconditionFailed(renderErrorPage({ code: 412, message: "ETag mismatch" }));
|
|
162
|
+
*/
|
|
163
|
+
export declare function preconditionFailed(body: string, init?: Init): Response;
|
|
164
|
+
/**
|
|
165
|
+
* Creates an HTML response with HTTP 413 Payload Too Large status.
|
|
166
|
+
* Use when the request payload exceeds server limits.
|
|
167
|
+
* @param body - The HTML string to send
|
|
168
|
+
* @param init - Optional response options (headers, etc.)
|
|
169
|
+
* @returns A Response with HTML body and 413 status
|
|
170
|
+
* @example
|
|
171
|
+
* return payloadTooLarge("<h1>File Too Large</h1>");
|
|
172
|
+
* @example
|
|
173
|
+
* return payloadTooLarge(renderErrorPage({ code: 413, message: "Maximum file size is 10MB" }));
|
|
174
|
+
*/
|
|
175
|
+
export declare function payloadTooLarge(body: string, init?: Init): Response;
|
|
176
|
+
/**
|
|
177
|
+
* Creates an HTML response with HTTP 415 Unsupported Media Type status.
|
|
178
|
+
* Use when the request content type is not supported.
|
|
179
|
+
* @param body - The HTML string to send
|
|
180
|
+
* @param init - Optional response options (headers, etc.)
|
|
181
|
+
* @returns A Response with HTML body and 415 status
|
|
182
|
+
* @example
|
|
183
|
+
* return unsupportedMediaType("<h1>Unsupported Media Type</h1>");
|
|
184
|
+
* @example
|
|
185
|
+
* return unsupportedMediaType(renderErrorPage({ code: 415, message: "Only JSON is accepted" }));
|
|
186
|
+
*/
|
|
187
|
+
export declare function unsupportedMediaType(body: string, init?: Init): Response;
|
|
188
|
+
/**
|
|
189
|
+
* Creates an HTML response with HTTP 422 Unprocessable Entity status.
|
|
190
|
+
* Use when the request is well-formed but contains semantic errors.
|
|
191
|
+
* @param body - The HTML string to send
|
|
192
|
+
* @param init - Optional response options (headers, etc.)
|
|
193
|
+
* @returns A Response with HTML body and 422 status
|
|
194
|
+
* @example
|
|
195
|
+
* return unprocessableEntity("<h1>Validation Error</h1>");
|
|
196
|
+
* @example
|
|
197
|
+
* return unprocessableEntity(renderFormErrors({ errors: validationErrors }));
|
|
198
|
+
*/
|
|
199
|
+
export declare function unprocessableEntity(body: string, init?: Init): Response;
|
|
200
|
+
/**
|
|
201
|
+
* Creates an HTML response with HTTP 429 Too Many Requests status.
|
|
202
|
+
* Use when the user has exceeded rate limits.
|
|
203
|
+
* @param body - The HTML string to send
|
|
204
|
+
* @param init - Optional response options (headers, etc.)
|
|
205
|
+
* @returns A Response with HTML body and 429 status
|
|
206
|
+
* @example
|
|
207
|
+
* return tooManyRequests("<h1>Too Many Requests</h1>");
|
|
208
|
+
* @example
|
|
209
|
+
* return tooManyRequests(renderErrorPage({ code: 429, message: "Please try again later" }));
|
|
210
|
+
*/
|
|
211
|
+
export declare function tooManyRequests(body: string, init?: Init): Response;
|
|
212
|
+
/**
|
|
213
|
+
* Creates an HTML response with HTTP 500 Internal Server Error status.
|
|
214
|
+
* Use for unexpected server errors when rendering error pages.
|
|
215
|
+
* @param body - The HTML string to send
|
|
216
|
+
* @param init - Optional response options (headers, etc.)
|
|
217
|
+
* @returns A Response with HTML body and 500 status
|
|
218
|
+
* @example
|
|
219
|
+
* return internalServerError("<h1>Something Went Wrong</h1>");
|
|
220
|
+
* @example
|
|
221
|
+
* return internalServerError(renderErrorPage({ code: 500, message: "Internal Server Error" }));
|
|
222
|
+
*/
|
|
223
|
+
export declare function internalServerError(body: string, init?: Init): Response;
|
|
224
|
+
/**
|
|
225
|
+
* Creates an HTML response with HTTP 501 Not Implemented status.
|
|
226
|
+
* Use when the server does not support the requested functionality.
|
|
227
|
+
* @param body - The HTML string to send
|
|
228
|
+
* @param init - Optional response options (headers, etc.)
|
|
229
|
+
* @returns A Response with HTML body and 501 status
|
|
230
|
+
* @example
|
|
231
|
+
* return notImplemented("<h1>Not Implemented</h1>");
|
|
232
|
+
* @example
|
|
233
|
+
* return notImplemented(renderErrorPage({ code: 501, message: "Feature coming soon" }));
|
|
234
|
+
*/
|
|
235
|
+
export declare function notImplemented(body: string, init?: Init): Response;
|
|
236
|
+
/**
|
|
237
|
+
* Creates an HTML response with HTTP 502 Bad Gateway status.
|
|
238
|
+
* Use when an upstream server returns an invalid response.
|
|
239
|
+
* @param body - The HTML string to send
|
|
240
|
+
* @param init - Optional response options (headers, etc.)
|
|
241
|
+
* @returns A Response with HTML body and 502 status
|
|
242
|
+
* @example
|
|
243
|
+
* return badGateway("<h1>Bad Gateway</h1>");
|
|
244
|
+
* @example
|
|
245
|
+
* return badGateway(renderErrorPage({ code: 502, message: "Upstream server error" }));
|
|
246
|
+
*/
|
|
247
|
+
export declare function badGateway(body: string, init?: Init): Response;
|
|
248
|
+
/**
|
|
249
|
+
* Creates an HTML response with HTTP 503 Service Unavailable status.
|
|
250
|
+
* Use when the server is temporarily unavailable (maintenance, overload).
|
|
251
|
+
* @param body - The HTML string to send
|
|
252
|
+
* @param init - Optional response options (headers, etc.)
|
|
253
|
+
* @returns A Response with HTML body and 503 status
|
|
254
|
+
* @example
|
|
255
|
+
* return serviceUnavailable("<h1>Service Unavailable</h1>");
|
|
256
|
+
* @example
|
|
257
|
+
* return serviceUnavailable(renderMaintenancePage({ estimatedTime: "2 hours" }));
|
|
258
|
+
*/
|
|
259
|
+
export declare function serviceUnavailable(body: string, init?: Init): Response;
|
|
260
|
+
/**
|
|
261
|
+
* Creates an HTML response with HTTP 504 Gateway Timeout status.
|
|
262
|
+
* Use when an upstream server fails to respond in time.
|
|
263
|
+
* @param body - The HTML string to send
|
|
264
|
+
* @param init - Optional response options (headers, etc.)
|
|
265
|
+
* @returns A Response with HTML body and 504 status
|
|
266
|
+
* @example
|
|
267
|
+
* return gatewayTimeout("<h1>Gateway Timeout</h1>");
|
|
268
|
+
* @example
|
|
269
|
+
* return gatewayTimeout(renderErrorPage({ code: 504, message: "Request timed out" }));
|
|
270
|
+
*/
|
|
271
|
+
export declare function gatewayTimeout(body: string, init?: Init): Response;
|
|
272
|
+
export {};
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML response helpers, one per HTTP status code, pairing an HTML body with
|
|
3
|
+
* its matching status and status text.
|
|
4
|
+
* @author [Sergio Xalambrí](https://sergiodxa.com)
|
|
5
|
+
* @copyright Sergio Xalambrí 2026
|
|
6
|
+
*/
|
|
7
|
+
import { html } from "../response.js";
|
|
8
|
+
import * as StatusCode from "../status-code.js";
|
|
9
|
+
/**
|
|
10
|
+
* Creates an HTML response with HTTP 200 OK status.
|
|
11
|
+
* Use for successful requests that return HTML content.
|
|
12
|
+
* @param body - The HTML string to send
|
|
13
|
+
* @param init - Optional response options (headers, etc.)
|
|
14
|
+
* @returns A Response with HTML body and 200 status
|
|
15
|
+
* @example
|
|
16
|
+
* return ok("<h1>Welcome</h1>");
|
|
17
|
+
* @example
|
|
18
|
+
* return ok(renderHomePage(), { headers: { "Cache-Control": "max-age=3600" } });
|
|
19
|
+
*/
|
|
20
|
+
export function ok(body, init) {
|
|
21
|
+
return html(body, { ...init, ...StatusCode.Ok });
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Creates an HTML response with HTTP 201 Created status.
|
|
25
|
+
* Use for successful resource creation that returns HTML confirmation.
|
|
26
|
+
* @param body - The HTML string to send
|
|
27
|
+
* @param init - Optional response options (headers, etc.)
|
|
28
|
+
* @returns A Response with HTML body and 201 status
|
|
29
|
+
* @example
|
|
30
|
+
* return created("<h1>Account Created</h1>");
|
|
31
|
+
* @example
|
|
32
|
+
* return created(renderSuccessPage({ message: "Your account has been created" }));
|
|
33
|
+
*/
|
|
34
|
+
export function created(body, init) {
|
|
35
|
+
return html(body, { ...init, ...StatusCode.Created });
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates an HTML response with HTTP 202 Accepted status.
|
|
39
|
+
* Use when a request has been accepted for processing but not yet completed.
|
|
40
|
+
* @param body - The HTML string to send
|
|
41
|
+
* @param init - Optional response options (headers, etc.)
|
|
42
|
+
* @returns A Response with HTML body and 202 status
|
|
43
|
+
* @example
|
|
44
|
+
* return accepted("<h1>Request Accepted</h1>");
|
|
45
|
+
* @example
|
|
46
|
+
* return accepted(renderProcessingPage({ jobId: "123" }));
|
|
47
|
+
*/
|
|
48
|
+
export function accepted(body, init) {
|
|
49
|
+
return html(body, { ...init, ...StatusCode.Accepted });
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Creates an HTML response with HTTP 400 Bad Request status.
|
|
53
|
+
* Use when the request is malformed or contains invalid data.
|
|
54
|
+
* @param body - The HTML string to send
|
|
55
|
+
* @param init - Optional response options (headers, etc.)
|
|
56
|
+
* @returns A Response with HTML body and 400 status
|
|
57
|
+
* @example
|
|
58
|
+
* return badRequest("<h1>Bad Request</h1>");
|
|
59
|
+
* @example
|
|
60
|
+
* return badRequest(renderErrorPage({ code: 400, message: "Invalid input" }));
|
|
61
|
+
*/
|
|
62
|
+
export function badRequest(body, init) {
|
|
63
|
+
return html(body, { ...init, ...StatusCode.BadRequest });
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Creates an HTML response with HTTP 401 Unauthorized status.
|
|
67
|
+
* Use when authentication is required but missing or invalid.
|
|
68
|
+
* @param body - The HTML string to send
|
|
69
|
+
* @param init - Optional response options (headers, etc.)
|
|
70
|
+
* @returns A Response with HTML body and 401 status
|
|
71
|
+
* @example
|
|
72
|
+
* return unauthorized("<h1>Please Log In</h1>");
|
|
73
|
+
* @example
|
|
74
|
+
* return unauthorized(renderLoginPage({ redirect: "/dashboard" }));
|
|
75
|
+
*/
|
|
76
|
+
export function unauthorized(body, init) {
|
|
77
|
+
return html(body, { ...init, ...StatusCode.Unauthorized });
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Creates an HTML response with HTTP 402 Payment Required status.
|
|
81
|
+
* Use when payment is required to access the requested resource.
|
|
82
|
+
* @param body - The HTML string to send
|
|
83
|
+
* @param init - Optional response options (headers, etc.)
|
|
84
|
+
* @returns A Response with HTML body and 402 status
|
|
85
|
+
* @example
|
|
86
|
+
* return paymentRequired("<h1>Payment Required</h1>");
|
|
87
|
+
* @example
|
|
88
|
+
* return paymentRequired(renderPaywallPage({ feature: "premium" }));
|
|
89
|
+
*/
|
|
90
|
+
export function paymentRequired(body, init) {
|
|
91
|
+
return html(body, { ...init, ...StatusCode.PaymentRequired });
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Creates an HTML response with HTTP 403 Forbidden status.
|
|
95
|
+
* Use when the user lacks permission to access the resource.
|
|
96
|
+
* @param body - The HTML string to send
|
|
97
|
+
* @param init - Optional response options (headers, etc.)
|
|
98
|
+
* @returns A Response with HTML body and 403 status
|
|
99
|
+
* @example
|
|
100
|
+
* return forbidden("<h1>Access Denied</h1>");
|
|
101
|
+
* @example
|
|
102
|
+
* return forbidden(renderErrorPage({ code: 403, message: "You don't have permission" }));
|
|
103
|
+
*/
|
|
104
|
+
export function forbidden(body, init) {
|
|
105
|
+
return html(body, { ...init, ...StatusCode.Forbidden });
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Creates an HTML response with HTTP 404 Not Found status.
|
|
109
|
+
* Use for rendering custom "not found" pages.
|
|
110
|
+
* @param body - The HTML string to send
|
|
111
|
+
* @param init - Optional response options (headers, etc.)
|
|
112
|
+
* @returns A Response with HTML body and 404 status
|
|
113
|
+
* @example
|
|
114
|
+
* return notFound("<h1>Page Not Found</h1>");
|
|
115
|
+
* @example
|
|
116
|
+
* return notFound(renderErrorPage({ code: 404, message: "Not Found" }));
|
|
117
|
+
*/
|
|
118
|
+
export function notFound(body, init) {
|
|
119
|
+
return html(body, { ...init, ...StatusCode.NotFound });
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Creates an HTML response with HTTP 405 Method Not Allowed status.
|
|
123
|
+
* Use when the HTTP method is not supported for the requested resource.
|
|
124
|
+
* @param body - The HTML string to send
|
|
125
|
+
* @param init - Optional response options (headers, etc.)
|
|
126
|
+
* @returns A Response with HTML body and 405 status
|
|
127
|
+
* @example
|
|
128
|
+
* return methodNotAllowed("<h1>Method Not Allowed</h1>");
|
|
129
|
+
* @example
|
|
130
|
+
* return methodNotAllowed(renderErrorPage({ code: 405, message: "POST not supported" }));
|
|
131
|
+
*/
|
|
132
|
+
export function methodNotAllowed(body, init) {
|
|
133
|
+
return html(body, { ...init, ...StatusCode.MethodNotAllowed });
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Creates an HTML response with HTTP 406 Not Acceptable status.
|
|
137
|
+
* Use when the server cannot produce a response matching acceptable values.
|
|
138
|
+
* @param body - The HTML string to send
|
|
139
|
+
* @param init - Optional response options (headers, etc.)
|
|
140
|
+
* @returns A Response with HTML body and 406 status
|
|
141
|
+
* @example
|
|
142
|
+
* return notAcceptable("<h1>Not Acceptable</h1>");
|
|
143
|
+
* @example
|
|
144
|
+
* return notAcceptable(renderErrorPage({ code: 406, message: "Cannot produce requested format" }));
|
|
145
|
+
*/
|
|
146
|
+
export function notAcceptable(body, init) {
|
|
147
|
+
return html(body, { ...init, ...StatusCode.NotAcceptable });
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Creates an HTML response with HTTP 409 Conflict status.
|
|
151
|
+
* Use when the request conflicts with the current state of the resource.
|
|
152
|
+
* @param body - The HTML string to send
|
|
153
|
+
* @param init - Optional response options (headers, etc.)
|
|
154
|
+
* @returns A Response with HTML body and 409 status
|
|
155
|
+
* @example
|
|
156
|
+
* return conflict("<h1>Conflict</h1>");
|
|
157
|
+
* @example
|
|
158
|
+
* return conflict(renderErrorPage({ code: 409, message: "Resource already exists" }));
|
|
159
|
+
*/
|
|
160
|
+
export function conflict(body, init) {
|
|
161
|
+
return html(body, { ...init, ...StatusCode.Conflict });
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Creates an HTML response with HTTP 410 Gone status.
|
|
165
|
+
* Use when the resource is no longer available and will not return.
|
|
166
|
+
* @param body - The HTML string to send
|
|
167
|
+
* @param init - Optional response options (headers, etc.)
|
|
168
|
+
* @returns A Response with HTML body and 410 status
|
|
169
|
+
* @example
|
|
170
|
+
* return gone("<h1>Resource Gone</h1>");
|
|
171
|
+
* @example
|
|
172
|
+
* return gone(renderErrorPage({ code: 410, message: "This page has been permanently removed" }));
|
|
173
|
+
*/
|
|
174
|
+
export function gone(body, init) {
|
|
175
|
+
return html(body, { ...init, ...StatusCode.Gone });
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Creates an HTML response with HTTP 412 Precondition Failed status.
|
|
179
|
+
* Use when preconditions in request headers are not met.
|
|
180
|
+
* @param body - The HTML string to send
|
|
181
|
+
* @param init - Optional response options (headers, etc.)
|
|
182
|
+
* @returns A Response with HTML body and 412 status
|
|
183
|
+
* @example
|
|
184
|
+
* return preconditionFailed("<h1>Precondition Failed</h1>");
|
|
185
|
+
* @example
|
|
186
|
+
* return preconditionFailed(renderErrorPage({ code: 412, message: "ETag mismatch" }));
|
|
187
|
+
*/
|
|
188
|
+
export function preconditionFailed(body, init) {
|
|
189
|
+
return html(body, { ...init, ...StatusCode.PreconditionFailed });
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Creates an HTML response with HTTP 413 Payload Too Large status.
|
|
193
|
+
* Use when the request payload exceeds server limits.
|
|
194
|
+
* @param body - The HTML string to send
|
|
195
|
+
* @param init - Optional response options (headers, etc.)
|
|
196
|
+
* @returns A Response with HTML body and 413 status
|
|
197
|
+
* @example
|
|
198
|
+
* return payloadTooLarge("<h1>File Too Large</h1>");
|
|
199
|
+
* @example
|
|
200
|
+
* return payloadTooLarge(renderErrorPage({ code: 413, message: "Maximum file size is 10MB" }));
|
|
201
|
+
*/
|
|
202
|
+
export function payloadTooLarge(body, init) {
|
|
203
|
+
return html(body, { ...init, ...StatusCode.PayloadTooLarge });
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Creates an HTML response with HTTP 415 Unsupported Media Type status.
|
|
207
|
+
* Use when the request content type is not supported.
|
|
208
|
+
* @param body - The HTML string to send
|
|
209
|
+
* @param init - Optional response options (headers, etc.)
|
|
210
|
+
* @returns A Response with HTML body and 415 status
|
|
211
|
+
* @example
|
|
212
|
+
* return unsupportedMediaType("<h1>Unsupported Media Type</h1>");
|
|
213
|
+
* @example
|
|
214
|
+
* return unsupportedMediaType(renderErrorPage({ code: 415, message: "Only JSON is accepted" }));
|
|
215
|
+
*/
|
|
216
|
+
export function unsupportedMediaType(body, init) {
|
|
217
|
+
return html(body, { ...init, ...StatusCode.UnsupportedMediaType });
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Creates an HTML response with HTTP 422 Unprocessable Entity status.
|
|
221
|
+
* Use when the request is well-formed but contains semantic errors.
|
|
222
|
+
* @param body - The HTML string to send
|
|
223
|
+
* @param init - Optional response options (headers, etc.)
|
|
224
|
+
* @returns A Response with HTML body and 422 status
|
|
225
|
+
* @example
|
|
226
|
+
* return unprocessableEntity("<h1>Validation Error</h1>");
|
|
227
|
+
* @example
|
|
228
|
+
* return unprocessableEntity(renderFormErrors({ errors: validationErrors }));
|
|
229
|
+
*/
|
|
230
|
+
export function unprocessableEntity(body, init) {
|
|
231
|
+
return html(body, { ...init, ...StatusCode.UnprocessableEntity });
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Creates an HTML response with HTTP 429 Too Many Requests status.
|
|
235
|
+
* Use when the user has exceeded rate limits.
|
|
236
|
+
* @param body - The HTML string to send
|
|
237
|
+
* @param init - Optional response options (headers, etc.)
|
|
238
|
+
* @returns A Response with HTML body and 429 status
|
|
239
|
+
* @example
|
|
240
|
+
* return tooManyRequests("<h1>Too Many Requests</h1>");
|
|
241
|
+
* @example
|
|
242
|
+
* return tooManyRequests(renderErrorPage({ code: 429, message: "Please try again later" }));
|
|
243
|
+
*/
|
|
244
|
+
export function tooManyRequests(body, init) {
|
|
245
|
+
return html(body, { ...init, ...StatusCode.TooManyRequests });
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Creates an HTML response with HTTP 500 Internal Server Error status.
|
|
249
|
+
* Use for unexpected server errors when rendering error pages.
|
|
250
|
+
* @param body - The HTML string to send
|
|
251
|
+
* @param init - Optional response options (headers, etc.)
|
|
252
|
+
* @returns A Response with HTML body and 500 status
|
|
253
|
+
* @example
|
|
254
|
+
* return internalServerError("<h1>Something Went Wrong</h1>");
|
|
255
|
+
* @example
|
|
256
|
+
* return internalServerError(renderErrorPage({ code: 500, message: "Internal Server Error" }));
|
|
257
|
+
*/
|
|
258
|
+
export function internalServerError(body, init) {
|
|
259
|
+
return html(body, { ...init, ...StatusCode.InternalServerError });
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Creates an HTML response with HTTP 501 Not Implemented status.
|
|
263
|
+
* Use when the server does not support the requested functionality.
|
|
264
|
+
* @param body - The HTML string to send
|
|
265
|
+
* @param init - Optional response options (headers, etc.)
|
|
266
|
+
* @returns A Response with HTML body and 501 status
|
|
267
|
+
* @example
|
|
268
|
+
* return notImplemented("<h1>Not Implemented</h1>");
|
|
269
|
+
* @example
|
|
270
|
+
* return notImplemented(renderErrorPage({ code: 501, message: "Feature coming soon" }));
|
|
271
|
+
*/
|
|
272
|
+
export function notImplemented(body, init) {
|
|
273
|
+
return html(body, { ...init, ...StatusCode.NotImplemented });
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Creates an HTML response with HTTP 502 Bad Gateway status.
|
|
277
|
+
* Use when an upstream server returns an invalid response.
|
|
278
|
+
* @param body - The HTML string to send
|
|
279
|
+
* @param init - Optional response options (headers, etc.)
|
|
280
|
+
* @returns A Response with HTML body and 502 status
|
|
281
|
+
* @example
|
|
282
|
+
* return badGateway("<h1>Bad Gateway</h1>");
|
|
283
|
+
* @example
|
|
284
|
+
* return badGateway(renderErrorPage({ code: 502, message: "Upstream server error" }));
|
|
285
|
+
*/
|
|
286
|
+
export function badGateway(body, init) {
|
|
287
|
+
return html(body, { ...init, ...StatusCode.BadGateway });
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Creates an HTML response with HTTP 503 Service Unavailable status.
|
|
291
|
+
* Use when the server is temporarily unavailable (maintenance, overload).
|
|
292
|
+
* @param body - The HTML string to send
|
|
293
|
+
* @param init - Optional response options (headers, etc.)
|
|
294
|
+
* @returns A Response with HTML body and 503 status
|
|
295
|
+
* @example
|
|
296
|
+
* return serviceUnavailable("<h1>Service Unavailable</h1>");
|
|
297
|
+
* @example
|
|
298
|
+
* return serviceUnavailable(renderMaintenancePage({ estimatedTime: "2 hours" }));
|
|
299
|
+
*/
|
|
300
|
+
export function serviceUnavailable(body, init) {
|
|
301
|
+
return html(body, { ...init, ...StatusCode.ServiceUnavailable });
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Creates an HTML response with HTTP 504 Gateway Timeout status.
|
|
305
|
+
* Use when an upstream server fails to respond in time.
|
|
306
|
+
* @param body - The HTML string to send
|
|
307
|
+
* @param init - Optional response options (headers, etc.)
|
|
308
|
+
* @returns A Response with HTML body and 504 status
|
|
309
|
+
* @example
|
|
310
|
+
* return gatewayTimeout("<h1>Gateway Timeout</h1>");
|
|
311
|
+
* @example
|
|
312
|
+
* return gatewayTimeout(renderErrorPage({ code: 504, message: "Request timed out" }));
|
|
313
|
+
*/
|
|
314
|
+
export function gatewayTimeout(body, init) {
|
|
315
|
+
return html(body, { ...init, ...StatusCode.GatewayTimeout });
|
|
316
|
+
}
|