@daloyjs/core 0.7.5 → 0.8.2
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 +21 -10
- package/dist/adapters/bun.d.ts +26 -4
- package/dist/adapters/bun.d.ts.map +1 -1
- package/dist/adapters/bun.js +16 -4
- package/dist/adapters/bun.js.map +1 -1
- package/dist/adapters/cloudflare.d.ts +21 -4
- package/dist/adapters/cloudflare.d.ts.map +1 -1
- package/dist/adapters/cloudflare.js.map +1 -1
- package/dist/adapters/deno.d.ts +25 -2
- package/dist/adapters/deno.d.ts.map +1 -1
- package/dist/adapters/deno.js +39 -7
- package/dist/adapters/deno.js.map +1 -1
- package/dist/adapters/fastly.d.ts +23 -0
- package/dist/adapters/fastly.d.ts.map +1 -0
- package/dist/adapters/fastly.js +11 -0
- package/dist/adapters/fastly.js.map +1 -0
- package/dist/adapters/lambda.d.ts +67 -0
- package/dist/adapters/lambda.d.ts.map +1 -0
- package/dist/adapters/lambda.js +117 -0
- package/dist/adapters/lambda.js.map +1 -0
- package/dist/adapters/node.d.ts +7 -0
- package/dist/adapters/node.d.ts.map +1 -1
- package/dist/adapters/node.js +17 -4
- package/dist/adapters/node.js.map +1 -1
- package/dist/adapters/vercel.d.ts +35 -7
- package/dist/adapters/vercel.d.ts.map +1 -1
- package/dist/adapters/vercel.js +22 -1
- package/dist/adapters/vercel.js.map +1 -1
- package/dist/app.d.ts +235 -8
- package/dist/app.d.ts.map +1 -1
- package/dist/app.js +222 -8
- package/dist/app.js.map +1 -1
- package/dist/client.d.ts +27 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +27 -0
- package/dist/client.js.map +1 -1
- package/dist/errors.d.ts +169 -3
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +143 -0
- package/dist/errors.js.map +1 -1
- package/dist/logger.d.ts +26 -0
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +26 -0
- package/dist/logger.js.map +1 -1
- package/dist/middleware.d.ts +143 -0
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +147 -4
- package/dist/middleware.js.map +1 -1
- package/dist/openapi.d.ts +32 -0
- package/dist/openapi.d.ts.map +1 -1
- package/dist/openapi.js +33 -2
- package/dist/openapi.js.map +1 -1
- package/dist/schema.d.ts +33 -2
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +33 -2
- package/dist/schema.js.map +1 -1
- package/dist/security-schemes.d.ts +75 -5
- package/dist/security-schemes.d.ts.map +1 -1
- package/dist/security-schemes.js +75 -5
- package/dist/security-schemes.js.map +1 -1
- package/dist/security.d.ts +73 -4
- package/dist/security.d.ts.map +1 -1
- package/dist/security.js +73 -4
- package/dist/security.js.map +1 -1
- package/dist/types.d.ts +195 -5
- package/dist/types.d.ts.map +1 -1
- package/package.json +9 -1
package/dist/errors.d.ts
CHANGED
|
@@ -8,25 +8,81 @@
|
|
|
8
8
|
* Production-mode rendering scrubs `detail` for 5xx responses so internal
|
|
9
9
|
* messages never leak to clients (security: information disclosure).
|
|
10
10
|
*/
|
|
11
|
+
/**
|
|
12
|
+
* RFC 9457 Problem Details document. This is the on-the-wire shape DaloyJS
|
|
13
|
+
* uses for **every** error response (the `Content-Type` is
|
|
14
|
+
* `application/problem+json`). Clients in any language can decode the same
|
|
15
|
+
* structure.
|
|
16
|
+
*
|
|
17
|
+
* Custom fields are allowed and pass through the JSON serialization
|
|
18
|
+
* unchanged — handy for correlation ids, retry hints, or business codes.
|
|
19
|
+
*
|
|
20
|
+
* @see https://www.rfc-editor.org/rfc/rfc9457.html
|
|
21
|
+
* @since 0.1.0
|
|
22
|
+
*/
|
|
11
23
|
export interface ProblemDetails {
|
|
24
|
+
/** URI reference identifying the problem type (dereferenceable when possible). */
|
|
12
25
|
type: string;
|
|
26
|
+
/** Short, human-readable summary of the problem (stable across occurrences). */
|
|
13
27
|
title: string;
|
|
28
|
+
/** HTTP status code generated by the origin server. */
|
|
14
29
|
status: number;
|
|
30
|
+
/** Human-readable explanation specific to this occurrence. Scrubbed for 5xx in production. */
|
|
15
31
|
detail?: string;
|
|
32
|
+
/** URI reference identifying the specific occurrence (e.g. `urn:request:abc123`). */
|
|
16
33
|
instance?: string;
|
|
17
|
-
/**
|
|
34
|
+
/** DaloyJS extension: structured validation issues with JSON Pointer-style paths. */
|
|
18
35
|
errors?: Array<{
|
|
19
36
|
path: string;
|
|
20
37
|
message: string;
|
|
21
38
|
}>;
|
|
39
|
+
/** Arbitrary additional members (RFC 9457 §3.2). */
|
|
22
40
|
[key: string]: unknown;
|
|
23
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Options for {@link HttpError.toResponse}.
|
|
44
|
+
*
|
|
45
|
+
* @since 0.1.0
|
|
46
|
+
*/
|
|
24
47
|
export interface ProblemRenderOptions {
|
|
25
|
-
/**
|
|
48
|
+
/** Scrub `detail` from 5xx responses. Defaults to `process.env.NODE_ENV === "production"`. */
|
|
26
49
|
production?: boolean;
|
|
27
|
-
/**
|
|
50
|
+
/** Request id stamped into `instance` as `urn:request:<id>` for log correlation. */
|
|
28
51
|
requestId?: string;
|
|
29
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Base class for every DaloyJS HTTP error.
|
|
55
|
+
*
|
|
56
|
+
* Throwing an `HttpError` (or any subclass) anywhere inside a handler, hook,
|
|
57
|
+
* or middleware causes DaloyJS to serialize it as RFC 9457 problem+json with
|
|
58
|
+
* the appropriate status code and (optionally) extra response headers such as
|
|
59
|
+
* `Retry-After` or `Allow`. In production mode, 5xx `detail` is scrubbed to
|
|
60
|
+
* avoid information disclosure.
|
|
61
|
+
*
|
|
62
|
+
* Prefer the dedicated subclasses (`BadRequestError`, `NotFoundError`, ...)
|
|
63
|
+
* for common statuses; instantiate `HttpError` directly only for unusual
|
|
64
|
+
* status codes or fully-custom problem documents.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* import { HttpError } from "@daloyjs/core";
|
|
69
|
+
*
|
|
70
|
+
* app.route({
|
|
71
|
+
* method: "GET",
|
|
72
|
+
* path: "/teapot",
|
|
73
|
+
* responses: { 418: { description: "I'm a teapot" } },
|
|
74
|
+
* handler: () => {
|
|
75
|
+
* throw new HttpError(418, {
|
|
76
|
+
* type: "https://example.com/errors/teapot",
|
|
77
|
+
* title: "I'm a teapot",
|
|
78
|
+
* detail: "Short and stout",
|
|
79
|
+
* });
|
|
80
|
+
* },
|
|
81
|
+
* });
|
|
82
|
+
* ```
|
|
83
|
+
*
|
|
84
|
+
* @since 0.1.0
|
|
85
|
+
*/
|
|
30
86
|
export declare class HttpError extends Error {
|
|
31
87
|
readonly status: number;
|
|
32
88
|
readonly problem: ProblemDetails;
|
|
@@ -34,41 +90,151 @@ export declare class HttpError extends Error {
|
|
|
34
90
|
constructor(status: number, problem: Partial<ProblemDetails> & {
|
|
35
91
|
title: string;
|
|
36
92
|
}, headers?: Record<string, string>);
|
|
93
|
+
/**
|
|
94
|
+
* Render this error as a `Response` whose body is the problem+json document.
|
|
95
|
+
* Used internally by the framework; safe to call from custom `onError` hooks
|
|
96
|
+
* that want to forward additional headers.
|
|
97
|
+
*
|
|
98
|
+
* @param opts - Rendering options (production scrubbing, correlation id).
|
|
99
|
+
* @returns A `Response` with `Content-Type: application/problem+json`.
|
|
100
|
+
*/
|
|
37
101
|
toResponse(opts?: ProblemRenderOptions): Response;
|
|
38
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* `400 Bad Request` — the request is syntactically invalid or otherwise
|
|
105
|
+
* malformed in a way the client should be able to fix without retrying.
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* throw new BadRequestError("`since` must be an ISO-8601 timestamp");
|
|
110
|
+
* ```
|
|
111
|
+
*
|
|
112
|
+
* @param detail - Optional human-readable explanation surfaced to the client.
|
|
113
|
+
* @since 0.1.0
|
|
114
|
+
*/
|
|
39
115
|
export declare class BadRequestError extends HttpError {
|
|
40
116
|
constructor(detail?: string);
|
|
41
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* `422 Unprocessable Entity` — raised automatically when DaloyJS' request
|
|
120
|
+
* validator rejects `params`, `query`, `headers`, or `body`. The reported
|
|
121
|
+
* issues are surfaced in the `errors` member of the problem document so
|
|
122
|
+
* frontends can render field-level feedback without reparsing the message.
|
|
123
|
+
*
|
|
124
|
+
* Throw this directly only when implementing custom validation outside the
|
|
125
|
+
* declared schema.
|
|
126
|
+
*
|
|
127
|
+
* @param where - Which part of the request failed validation.
|
|
128
|
+
* @param issues - Array of `{ path, message }` records (one per failure).
|
|
129
|
+
* @since 0.1.0
|
|
130
|
+
*/
|
|
42
131
|
export declare class ValidationError extends HttpError {
|
|
43
132
|
constructor(where: "params" | "query" | "headers" | "body", issues: Array<{
|
|
44
133
|
path: string;
|
|
45
134
|
message: string;
|
|
46
135
|
}>);
|
|
47
136
|
}
|
|
137
|
+
/**
|
|
138
|
+
* `404 Not Found` — the requested resource does not exist.
|
|
139
|
+
* DaloyJS also throws this internally for routes the router did not match.
|
|
140
|
+
*
|
|
141
|
+
* @param detail - Optional human-readable explanation (e.g. resource id).
|
|
142
|
+
* @since 0.1.0
|
|
143
|
+
*/
|
|
48
144
|
export declare class NotFoundError extends HttpError {
|
|
49
145
|
constructor(detail?: string);
|
|
50
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* `401 Unauthorized` — authentication is required and missing or invalid.
|
|
149
|
+
* Pair with a `WWW-Authenticate` header on the response when issuing a
|
|
150
|
+
* challenge (the built-in `bearerAuth` middleware does this for you).
|
|
151
|
+
*
|
|
152
|
+
* @param detail - Optional human-readable explanation surfaced to the client.
|
|
153
|
+
* @since 0.1.0
|
|
154
|
+
*/
|
|
51
155
|
export declare class UnauthorizedError extends HttpError {
|
|
52
156
|
constructor(detail?: string);
|
|
53
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* `403 Forbidden` — the request is authenticated but not permitted.
|
|
160
|
+
* Use for authorization failures ("who you are" is fine, "what you're asking
|
|
161
|
+
* for" is not). Used by built-in CSRF and bearer-token middleware.
|
|
162
|
+
*
|
|
163
|
+
* @param detail - Optional human-readable explanation surfaced to the client.
|
|
164
|
+
* @since 0.1.0
|
|
165
|
+
*/
|
|
54
166
|
export declare class ForbiddenError extends HttpError {
|
|
55
167
|
constructor(detail?: string);
|
|
56
168
|
}
|
|
169
|
+
/**
|
|
170
|
+
* `405 Method Not Allowed` — thrown automatically by the router when a path
|
|
171
|
+
* matches but no route is registered for the request method. The response
|
|
172
|
+
* carries an `Allow` header enumerating the supported methods, as required by
|
|
173
|
+
* the HTTP spec.
|
|
174
|
+
*
|
|
175
|
+
* @param allow - List of HTTP method names supported on the matched path.
|
|
176
|
+
* @since 0.1.0
|
|
177
|
+
*/
|
|
57
178
|
export declare class MethodNotAllowedError extends HttpError {
|
|
58
179
|
constructor(allow: string[]);
|
|
59
180
|
}
|
|
181
|
+
/**
|
|
182
|
+
* `413 Payload Too Large` — thrown by `readBodyLimited` when an incoming
|
|
183
|
+
* request body exceeds {@link AppOptions.bodyLimitBytes} (or a tighter
|
|
184
|
+
* per-route limit). Trips both on the declared `Content-Length` (fail-fast)
|
|
185
|
+
* and on the actual streamed byte count (defence in depth).
|
|
186
|
+
*
|
|
187
|
+
* @param limit - The configured byte cap that was exceeded.
|
|
188
|
+
* @since 0.1.0
|
|
189
|
+
*/
|
|
60
190
|
export declare class PayloadTooLargeError extends HttpError {
|
|
61
191
|
constructor(limit: number);
|
|
62
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* `415 Unsupported Media Type` — thrown when the request `Content-Type` is
|
|
195
|
+
* not in {@link AppOptions.allowedContentTypes} for a route that declares a
|
|
196
|
+
* body schema.
|
|
197
|
+
*
|
|
198
|
+
* @param got - The `Content-Type` value the client sent.
|
|
199
|
+
* @param expected - Allowed media types for the route.
|
|
200
|
+
* @since 0.1.0
|
|
201
|
+
*/
|
|
63
202
|
export declare class UnsupportedMediaTypeError extends HttpError {
|
|
64
203
|
constructor(got: string, expected: string[]);
|
|
65
204
|
}
|
|
205
|
+
/**
|
|
206
|
+
* `429 Too Many Requests` — thrown by the built-in `rateLimit` middleware
|
|
207
|
+
* when the caller exceeds its quota. When `retryAfterSeconds` is provided the
|
|
208
|
+
* response carries a `Retry-After` header; clients should back off for that
|
|
209
|
+
* many seconds before retrying.
|
|
210
|
+
*
|
|
211
|
+
* @param retryAfterSeconds - Seconds the client should wait before retrying.
|
|
212
|
+
* @since 0.1.0
|
|
213
|
+
*/
|
|
66
214
|
export declare class TooManyRequestsError extends HttpError {
|
|
67
215
|
constructor(retryAfterSeconds?: number);
|
|
68
216
|
}
|
|
217
|
+
/**
|
|
218
|
+
* `408 Request Timeout` — thrown when a handler exceeds
|
|
219
|
+
* {@link AppOptions.requestTimeoutMs}. The framework aborts the in-flight
|
|
220
|
+
* handler when this fires (handlers should respect the `AbortSignal` on
|
|
221
|
+
* `ctx.request.signal` to clean up).
|
|
222
|
+
*
|
|
223
|
+
* @param ms - The configured timeout that was exceeded.
|
|
224
|
+
* @since 0.1.0
|
|
225
|
+
*/
|
|
69
226
|
export declare class RequestTimeoutError extends HttpError {
|
|
70
227
|
constructor(ms: number);
|
|
71
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* `500 Internal Server Error` — generic fallback used by the framework when
|
|
231
|
+
* a handler throws something that isn't an `HttpError`. The original error is
|
|
232
|
+
* logged at `error` level with the request id for correlation. The `detail`
|
|
233
|
+
* is automatically scrubbed in production mode.
|
|
234
|
+
*
|
|
235
|
+
* @param detail - Optional explanation; suppressed in production output.
|
|
236
|
+
* @since 0.1.0
|
|
237
|
+
*/
|
|
72
238
|
export declare class InternalError extends HttpError {
|
|
73
239
|
constructor(detail?: string);
|
|
74
240
|
}
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,WAAW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ED;;;;;;;;GAQG;AACH,qBAAa,aAAc,SAAQ,SAAS;gBAC9B,MAAM,CAAC,EAAE,MAAM;CAQ5B"}
|
package/dist/errors.js
CHANGED
|
@@ -8,6 +8,39 @@
|
|
|
8
8
|
* Production-mode rendering scrubs `detail` for 5xx responses so internal
|
|
9
9
|
* messages never leak to clients (security: information disclosure).
|
|
10
10
|
*/
|
|
11
|
+
/**
|
|
12
|
+
* Base class for every DaloyJS HTTP error.
|
|
13
|
+
*
|
|
14
|
+
* Throwing an `HttpError` (or any subclass) anywhere inside a handler, hook,
|
|
15
|
+
* or middleware causes DaloyJS to serialize it as RFC 9457 problem+json with
|
|
16
|
+
* the appropriate status code and (optionally) extra response headers such as
|
|
17
|
+
* `Retry-After` or `Allow`. In production mode, 5xx `detail` is scrubbed to
|
|
18
|
+
* avoid information disclosure.
|
|
19
|
+
*
|
|
20
|
+
* Prefer the dedicated subclasses (`BadRequestError`, `NotFoundError`, ...)
|
|
21
|
+
* for common statuses; instantiate `HttpError` directly only for unusual
|
|
22
|
+
* status codes or fully-custom problem documents.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { HttpError } from "@daloyjs/core";
|
|
27
|
+
*
|
|
28
|
+
* app.route({
|
|
29
|
+
* method: "GET",
|
|
30
|
+
* path: "/teapot",
|
|
31
|
+
* responses: { 418: { description: "I'm a teapot" } },
|
|
32
|
+
* handler: () => {
|
|
33
|
+
* throw new HttpError(418, {
|
|
34
|
+
* type: "https://example.com/errors/teapot",
|
|
35
|
+
* title: "I'm a teapot",
|
|
36
|
+
* detail: "Short and stout",
|
|
37
|
+
* });
|
|
38
|
+
* },
|
|
39
|
+
* });
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @since 0.1.0
|
|
43
|
+
*/
|
|
11
44
|
export class HttpError extends Error {
|
|
12
45
|
status;
|
|
13
46
|
problem;
|
|
@@ -25,6 +58,14 @@ export class HttpError extends Error {
|
|
|
25
58
|
if (headers)
|
|
26
59
|
this.headers = headers;
|
|
27
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* Render this error as a `Response` whose body is the problem+json document.
|
|
63
|
+
* Used internally by the framework; safe to call from custom `onError` hooks
|
|
64
|
+
* that want to forward additional headers.
|
|
65
|
+
*
|
|
66
|
+
* @param opts - Rendering options (production scrubbing, correlation id).
|
|
67
|
+
* @returns A `Response` with `Content-Type: application/problem+json`.
|
|
68
|
+
*/
|
|
28
69
|
toResponse(opts = {}) {
|
|
29
70
|
const isProd = opts.production ??
|
|
30
71
|
(typeof process !== "undefined" && process.env?.NODE_ENV === "production");
|
|
@@ -41,6 +82,18 @@ export class HttpError extends Error {
|
|
|
41
82
|
return new Response(JSON.stringify(out), { status: this.status, headers });
|
|
42
83
|
}
|
|
43
84
|
}
|
|
85
|
+
/**
|
|
86
|
+
* `400 Bad Request` — the request is syntactically invalid or otherwise
|
|
87
|
+
* malformed in a way the client should be able to fix without retrying.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* throw new BadRequestError("`since` must be an ISO-8601 timestamp");
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* @param detail - Optional human-readable explanation surfaced to the client.
|
|
95
|
+
* @since 0.1.0
|
|
96
|
+
*/
|
|
44
97
|
export class BadRequestError extends HttpError {
|
|
45
98
|
constructor(detail) {
|
|
46
99
|
super(400, {
|
|
@@ -51,6 +104,19 @@ export class BadRequestError extends HttpError {
|
|
|
51
104
|
this.name = "BadRequestError";
|
|
52
105
|
}
|
|
53
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* `422 Unprocessable Entity` — raised automatically when DaloyJS' request
|
|
109
|
+
* validator rejects `params`, `query`, `headers`, or `body`. The reported
|
|
110
|
+
* issues are surfaced in the `errors` member of the problem document so
|
|
111
|
+
* frontends can render field-level feedback without reparsing the message.
|
|
112
|
+
*
|
|
113
|
+
* Throw this directly only when implementing custom validation outside the
|
|
114
|
+
* declared schema.
|
|
115
|
+
*
|
|
116
|
+
* @param where - Which part of the request failed validation.
|
|
117
|
+
* @param issues - Array of `{ path, message }` records (one per failure).
|
|
118
|
+
* @since 0.1.0
|
|
119
|
+
*/
|
|
54
120
|
export class ValidationError extends HttpError {
|
|
55
121
|
constructor(where, issues) {
|
|
56
122
|
super(422, {
|
|
@@ -62,6 +128,13 @@ export class ValidationError extends HttpError {
|
|
|
62
128
|
this.name = "ValidationError";
|
|
63
129
|
}
|
|
64
130
|
}
|
|
131
|
+
/**
|
|
132
|
+
* `404 Not Found` — the requested resource does not exist.
|
|
133
|
+
* DaloyJS also throws this internally for routes the router did not match.
|
|
134
|
+
*
|
|
135
|
+
* @param detail - Optional human-readable explanation (e.g. resource id).
|
|
136
|
+
* @since 0.1.0
|
|
137
|
+
*/
|
|
65
138
|
export class NotFoundError extends HttpError {
|
|
66
139
|
constructor(detail) {
|
|
67
140
|
super(404, {
|
|
@@ -72,6 +145,14 @@ export class NotFoundError extends HttpError {
|
|
|
72
145
|
this.name = "NotFoundError";
|
|
73
146
|
}
|
|
74
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* `401 Unauthorized` — authentication is required and missing or invalid.
|
|
150
|
+
* Pair with a `WWW-Authenticate` header on the response when issuing a
|
|
151
|
+
* challenge (the built-in `bearerAuth` middleware does this for you).
|
|
152
|
+
*
|
|
153
|
+
* @param detail - Optional human-readable explanation surfaced to the client.
|
|
154
|
+
* @since 0.1.0
|
|
155
|
+
*/
|
|
75
156
|
export class UnauthorizedError extends HttpError {
|
|
76
157
|
constructor(detail) {
|
|
77
158
|
super(401, {
|
|
@@ -82,6 +163,14 @@ export class UnauthorizedError extends HttpError {
|
|
|
82
163
|
this.name = "UnauthorizedError";
|
|
83
164
|
}
|
|
84
165
|
}
|
|
166
|
+
/**
|
|
167
|
+
* `403 Forbidden` — the request is authenticated but not permitted.
|
|
168
|
+
* Use for authorization failures ("who you are" is fine, "what you're asking
|
|
169
|
+
* for" is not). Used by built-in CSRF and bearer-token middleware.
|
|
170
|
+
*
|
|
171
|
+
* @param detail - Optional human-readable explanation surfaced to the client.
|
|
172
|
+
* @since 0.1.0
|
|
173
|
+
*/
|
|
85
174
|
export class ForbiddenError extends HttpError {
|
|
86
175
|
constructor(detail) {
|
|
87
176
|
super(403, {
|
|
@@ -92,6 +181,15 @@ export class ForbiddenError extends HttpError {
|
|
|
92
181
|
this.name = "ForbiddenError";
|
|
93
182
|
}
|
|
94
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* `405 Method Not Allowed` — thrown automatically by the router when a path
|
|
186
|
+
* matches but no route is registered for the request method. The response
|
|
187
|
+
* carries an `Allow` header enumerating the supported methods, as required by
|
|
188
|
+
* the HTTP spec.
|
|
189
|
+
*
|
|
190
|
+
* @param allow - List of HTTP method names supported on the matched path.
|
|
191
|
+
* @since 0.1.0
|
|
192
|
+
*/
|
|
95
193
|
export class MethodNotAllowedError extends HttpError {
|
|
96
194
|
constructor(allow) {
|
|
97
195
|
super(405, {
|
|
@@ -101,6 +199,15 @@ export class MethodNotAllowedError extends HttpError {
|
|
|
101
199
|
this.name = "MethodNotAllowedError";
|
|
102
200
|
}
|
|
103
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* `413 Payload Too Large` — thrown by `readBodyLimited` when an incoming
|
|
204
|
+
* request body exceeds {@link AppOptions.bodyLimitBytes} (or a tighter
|
|
205
|
+
* per-route limit). Trips both on the declared `Content-Length` (fail-fast)
|
|
206
|
+
* and on the actual streamed byte count (defence in depth).
|
|
207
|
+
*
|
|
208
|
+
* @param limit - The configured byte cap that was exceeded.
|
|
209
|
+
* @since 0.1.0
|
|
210
|
+
*/
|
|
104
211
|
export class PayloadTooLargeError extends HttpError {
|
|
105
212
|
constructor(limit) {
|
|
106
213
|
super(413, {
|
|
@@ -111,6 +218,15 @@ export class PayloadTooLargeError extends HttpError {
|
|
|
111
218
|
this.name = "PayloadTooLargeError";
|
|
112
219
|
}
|
|
113
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* `415 Unsupported Media Type` — thrown when the request `Content-Type` is
|
|
223
|
+
* not in {@link AppOptions.allowedContentTypes} for a route that declares a
|
|
224
|
+
* body schema.
|
|
225
|
+
*
|
|
226
|
+
* @param got - The `Content-Type` value the client sent.
|
|
227
|
+
* @param expected - Allowed media types for the route.
|
|
228
|
+
* @since 0.1.0
|
|
229
|
+
*/
|
|
114
230
|
export class UnsupportedMediaTypeError extends HttpError {
|
|
115
231
|
constructor(got, expected) {
|
|
116
232
|
super(415, {
|
|
@@ -121,6 +237,15 @@ export class UnsupportedMediaTypeError extends HttpError {
|
|
|
121
237
|
this.name = "UnsupportedMediaTypeError";
|
|
122
238
|
}
|
|
123
239
|
}
|
|
240
|
+
/**
|
|
241
|
+
* `429 Too Many Requests` — thrown by the built-in `rateLimit` middleware
|
|
242
|
+
* when the caller exceeds its quota. When `retryAfterSeconds` is provided the
|
|
243
|
+
* response carries a `Retry-After` header; clients should back off for that
|
|
244
|
+
* many seconds before retrying.
|
|
245
|
+
*
|
|
246
|
+
* @param retryAfterSeconds - Seconds the client should wait before retrying.
|
|
247
|
+
* @since 0.1.0
|
|
248
|
+
*/
|
|
124
249
|
export class TooManyRequestsError extends HttpError {
|
|
125
250
|
constructor(retryAfterSeconds) {
|
|
126
251
|
super(429, {
|
|
@@ -132,6 +257,15 @@ export class TooManyRequestsError extends HttpError {
|
|
|
132
257
|
this.name = "TooManyRequestsError";
|
|
133
258
|
}
|
|
134
259
|
}
|
|
260
|
+
/**
|
|
261
|
+
* `408 Request Timeout` — thrown when a handler exceeds
|
|
262
|
+
* {@link AppOptions.requestTimeoutMs}. The framework aborts the in-flight
|
|
263
|
+
* handler when this fires (handlers should respect the `AbortSignal` on
|
|
264
|
+
* `ctx.request.signal` to clean up).
|
|
265
|
+
*
|
|
266
|
+
* @param ms - The configured timeout that was exceeded.
|
|
267
|
+
* @since 0.1.0
|
|
268
|
+
*/
|
|
135
269
|
export class RequestTimeoutError extends HttpError {
|
|
136
270
|
constructor(ms) {
|
|
137
271
|
super(408, {
|
|
@@ -142,6 +276,15 @@ export class RequestTimeoutError extends HttpError {
|
|
|
142
276
|
this.name = "RequestTimeoutError";
|
|
143
277
|
}
|
|
144
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* `500 Internal Server Error` — generic fallback used by the framework when
|
|
281
|
+
* a handler throws something that isn't an `HttpError`. The original error is
|
|
282
|
+
* logged at `error` level with the request id for correlation. The `detail`
|
|
283
|
+
* is automatically scrubbed in production mode.
|
|
284
|
+
*
|
|
285
|
+
* @param detail - Optional explanation; suppressed in production output.
|
|
286
|
+
* @since 0.1.0
|
|
287
|
+
*/
|
|
145
288
|
export class InternalError extends HttpError {
|
|
146
289
|
constructor(detail) {
|
|
147
290
|
super(500, {
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AA2CH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,OAAO,SAAU,SAAQ,KAAK;IACzB,MAAM,CAAS;IACf,OAAO,CAAiB;IACxB,OAAO,CAA0B;IAE1C,YACE,MAAc,EACd,OAAoD,EACpD,OAAgC;QAEhC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,WAAW,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,OAAO,GAAG;YACb,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,2BAA2B,MAAM,EAAE;YACzD,GAAG,OAAO;YACV,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,MAAM;SACP,CAAC;QACF,IAAI,OAAO;YAAE,IAAI,CAAC,OA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AACH,MAAM,OAAO,mBAAoB,SAAQ,SAAS;IAChD,YAAY,EAAU;QACpB,KAAK,CAAC,GAAG,EAAE;YACT,IAAI,EAAE,4CAA4C;YAClD,KAAK,EAAE,iBAAiB;YACxB,MAAM,EAAE,oBAAoB,EAAE,IAAI;SACnC,CAAC,CAAC;QACH,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAED;;;;;;;;GAQG;AACH,MAAM,OAAO,aAAc,SAAQ,SAAS;IAC1C,YAAY,MAAe;QACzB,KAAK,CAAC,GAAG,EAAE;YACT,IAAI,EAAE,qCAAqC;YAC3C,KAAK,EAAE,uBAAuB;YAC9B,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SAC9B,CAAC,CAAC;QACH,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF"}
|
package/dist/logger.d.ts
CHANGED
|
@@ -21,6 +21,32 @@ export interface ConsoleLoggerOptions {
|
|
|
21
21
|
/** Where to write. Defaults to process.stdout.write or console.log. */
|
|
22
22
|
write?: (line: string) => void;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Build a structured JSON logger writing one record per line to stdout (or
|
|
26
|
+
* any sink you supply). Records always include `level`, `time`, and the
|
|
27
|
+
* caller's bindings; objects are merged shallowly and the optional `msg` is
|
|
28
|
+
* placed under the `msg` key for compatibility with downstream tools.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* import { createLogger, App } from "@daloyjs/core";
|
|
33
|
+
*
|
|
34
|
+
* const log = createLogger({ level: "info", bindings: { service: "books-api" } });
|
|
35
|
+
* const app = new App({ logger: log });
|
|
36
|
+
* log.info({ event: "boot" }, "server starting");
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* @param opts - Level, bindings merged into every record, and custom sink.
|
|
40
|
+
* @returns A {@link Logger} instance.
|
|
41
|
+
* @since 0.1.0
|
|
42
|
+
*/
|
|
24
43
|
export declare function createLogger(opts?: ConsoleLoggerOptions): Logger;
|
|
44
|
+
/**
|
|
45
|
+
* A {@link Logger} that discards every record. Used internally when the App
|
|
46
|
+
* is constructed with `{ logger: false }` and exported so tests can silence
|
|
47
|
+
* specific subsystems without monkey-patching console.
|
|
48
|
+
*
|
|
49
|
+
* @since 0.1.0
|
|
50
|
+
*/
|
|
25
51
|
export declare const noopLogger: Logger;
|
|
26
52
|
//# sourceMappingURL=logger.d.ts.map
|
package/dist/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,MAAM,MAAM,QAAQ,GAAG,OAAO,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAE/E,MAAM,WAAW,MAAM;IACrB,KAAK,EAAE,QAAQ,CAAC;IAChB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChD,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChD,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/C,IAAI,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/C,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChD,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAChD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G,IAAI,CAAC;IAChD,KAAK,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC;CAClD;AAWD,MAAM,WAAW,oBAAoB;IACnC,KAAK,CAAC,EAAE,QAAQ,CAAC;IACjB,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACnC,uEAAuE;IACvE,KAAK,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAC;CAChC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,YAAY,CAAC,IAAI,GAAE,oBAAyB,GAAG,MAAM,CA6CpE;AAED;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,EAAE,MAWxB,CAAC"}
|
package/dist/logger.js
CHANGED
|
@@ -12,6 +12,25 @@ const LEVELS = {
|
|
|
12
12
|
error: 50,
|
|
13
13
|
fatal: 60,
|
|
14
14
|
};
|
|
15
|
+
/**
|
|
16
|
+
* Build a structured JSON logger writing one record per line to stdout (or
|
|
17
|
+
* any sink you supply). Records always include `level`, `time`, and the
|
|
18
|
+
* caller's bindings; objects are merged shallowly and the optional `msg` is
|
|
19
|
+
* placed under the `msg` key for compatibility with downstream tools.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* import { createLogger, App } from "@daloyjs/core";
|
|
24
|
+
*
|
|
25
|
+
* const log = createLogger({ level: "info", bindings: { service: "books-api" } });
|
|
26
|
+
* const app = new App({ logger: log });
|
|
27
|
+
* log.info({ event: "boot" }, "server starting");
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @param opts - Level, bindings merged into every record, and custom sink.
|
|
31
|
+
* @returns A {@link Logger} instance.
|
|
32
|
+
* @since 0.1.0
|
|
33
|
+
*/
|
|
15
34
|
export function createLogger(opts = {}) {
|
|
16
35
|
const level = opts.level ?? "info";
|
|
17
36
|
const threshold = LEVELS[level];
|
|
@@ -59,6 +78,13 @@ export function createLogger(opts = {}) {
|
|
|
59
78
|
};
|
|
60
79
|
return logger;
|
|
61
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* A {@link Logger} that discards every record. Used internally when the App
|
|
83
|
+
* is constructed with `{ logger: false }` and exported so tests can silence
|
|
84
|
+
* specific subsystems without monkey-patching console.
|
|
85
|
+
*
|
|
86
|
+
* @since 0.1.0
|
|
87
|
+
*/
|
|
62
88
|
export const noopLogger = {
|
|
63
89
|
level: "fatal",
|
|
64
90
|
trace() { },
|
package/dist/logger.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAeH,MAAM,MAAM,GAA6B;IACvC,KAAK,EAAE,EAAE;IACT,KAAK,EAAE,EAAE;IACT,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,EAAE;IACR,KAAK,EAAE,EAAE;IACT,KAAK,EAAE,EAAE;CACV,CAAC;AASF,MAAM,UAAU,YAAY,CAAC,OAA6B,EAAE;IAC1D,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,MAAM,CAAC;IACnC,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAChC,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAC;IACrC,MAAM,KAAK,GACT,IAAI,CAAC,KAAK;QACV,CAAC,OAAO,OAAO,KAAK,WAAW,IAAI,OAAO,CAAC,MAAM,EAAE,KAAK;YACtD,CAAC,CAAC,CAAC,IAAY,EAAE,EAAE;gBACf,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,CAAC;YACpC,CAAC;YACH,CAAC,CAAC,CAAC,IAAY,EAAE,EAAE,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAE3C,SAAS,IAAI,CAAC,GAAa,EAAE,GA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