hono-ban 0.2.5 → 0.2.7
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/dist/esm/formatters/index.js +1 -248
- package/dist/esm/index.js +1 -585
- package/package.json +10 -26
- package/dist/cjs/formatters/index.js +0 -280
- package/dist/cjs/formatters/index.js.map +0 -16
- package/dist/cjs/formatters/rfc7807/index.js +0 -364
- package/dist/cjs/formatters/rfc7807/index.js.map +0 -16
- package/dist/cjs/index.js +0 -617
- package/dist/cjs/index.js.map +0 -23
- package/dist/esm/formatters/index.js.map +0 -16
- package/dist/esm/formatters/rfc7807/index.js +0 -333
- package/dist/esm/formatters/rfc7807/index.js.map +0 -16
- package/dist/esm/index.js.map +0 -23
- package/src/__tests__/core/convert-error.test.ts +0 -158
- package/src/__tests__/core/create-error.test.ts +0 -113
- package/src/__tests__/core/format-error.test.ts +0 -162
- package/src/__tests__/factories/factories.test.ts +0 -380
- package/src/__tests__/formatters/default-formatter.test.ts +0 -117
- package/src/__tests__/middleware/README.md +0 -49
- package/src/__tests__/middleware/ban-middleware.test.ts.disabled +0 -23
- package/src/__tests__/utils/sanitize.test.ts +0 -117
- package/src/__tests__/utils/validate.test.ts +0 -35
- package/src/constants/index.ts +0 -6
- package/src/constants/status-codes.ts +0 -52
- package/src/core/convert-error.ts +0 -74
- package/src/core/create-error.ts +0 -65
- package/src/core/format-error.ts +0 -76
- package/src/core/index.ts +0 -8
- package/src/factories/client-errors.ts +0 -492
- package/src/factories/index.ts +0 -7
- package/src/factories/server-errors.ts +0 -230
- package/src/formatters/default/index.ts +0 -72
- package/src/formatters/index.ts +0 -13
- package/src/formatters/interfaces.ts +0 -7
- package/src/formatters/rfc7807/formatter.ts +0 -105
- package/src/formatters/rfc7807/hooks.ts +0 -73
- package/src/formatters/rfc7807/index.ts +0 -20
- package/src/formatters/rfc7807/schemas.ts +0 -88
- package/src/index.ts +0 -23
- package/src/middleware/ban-middleware.ts +0 -77
- package/src/middleware/index.ts +0 -6
- package/src/types/common.ts +0 -14
- package/src/types/error.ts +0 -46
- package/src/types/formatter.ts +0 -61
- package/src/types/index.ts +0 -11
- package/src/types/options.ts +0 -71
- package/src/types/rfc7807.ts +0 -93
- package/src/utils/index.ts +0 -7
- package/src/utils/sanitize.ts +0 -34
- package/src/utils/validate.ts +0 -24
package/dist/cjs/index.js
DELETED
|
@@ -1,617 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
6
|
-
var __toCommonJS = (from) => {
|
|
7
|
-
var entry = __moduleCache.get(from), desc;
|
|
8
|
-
if (entry)
|
|
9
|
-
return entry;
|
|
10
|
-
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
-
__getOwnPropNames(from).map((key) => !__hasOwnProp.call(entry, key) && __defProp(entry, key, {
|
|
13
|
-
get: () => from[key],
|
|
14
|
-
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
-
}));
|
|
16
|
-
__moduleCache.set(from, entry);
|
|
17
|
-
return entry;
|
|
18
|
-
};
|
|
19
|
-
var __export = (target, all) => {
|
|
20
|
-
for (var name in all)
|
|
21
|
-
__defProp(target, name, {
|
|
22
|
-
get: all[name],
|
|
23
|
-
enumerable: true,
|
|
24
|
-
configurable: true,
|
|
25
|
-
set: (newValue) => all[name] = () => newValue
|
|
26
|
-
});
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
// src/index.ts
|
|
30
|
-
var exports_src = {};
|
|
31
|
-
__export(exports_src, {
|
|
32
|
-
variantAlsoNegotiates: () => variantAlsoNegotiates,
|
|
33
|
-
uriTooLong: () => uriTooLong,
|
|
34
|
-
upgradeRequired: () => upgradeRequired,
|
|
35
|
-
unsupportedMediaType: () => unsupportedMediaType,
|
|
36
|
-
unauthorized: () => unauthorized,
|
|
37
|
-
tooManyRequests: () => tooManyRequests,
|
|
38
|
-
tooEarly: () => tooEarly,
|
|
39
|
-
teapot: () => teapot,
|
|
40
|
-
serverUnavailable: () => serverUnavailable,
|
|
41
|
-
rfc7807Hook: () => rfc7807Hook,
|
|
42
|
-
resourceGone: () => resourceGone,
|
|
43
|
-
rangeNotSatisfiable: () => rangeNotSatisfiable,
|
|
44
|
-
proxyAuthRequired: () => proxyAuthRequired,
|
|
45
|
-
preconditionRequired: () => preconditionRequired,
|
|
46
|
-
preconditionFailed: () => preconditionFailed,
|
|
47
|
-
paymentRequired: () => paymentRequired,
|
|
48
|
-
notImplemented: () => notImplemented,
|
|
49
|
-
notFound: () => notFound,
|
|
50
|
-
notExtended: () => notExtended,
|
|
51
|
-
notAcceptable: () => notAcceptable,
|
|
52
|
-
networkAuthRequired: () => networkAuthRequired,
|
|
53
|
-
misdirectedRequest: () => misdirectedRequest,
|
|
54
|
-
methodNotAllowed: () => methodNotAllowed,
|
|
55
|
-
loopDetected: () => loopDetected,
|
|
56
|
-
locked: () => locked,
|
|
57
|
-
lengthRequired: () => lengthRequired,
|
|
58
|
-
isBanError: () => isBanError,
|
|
59
|
-
internal: () => internal,
|
|
60
|
-
insufficientStorage: () => insufficientStorage,
|
|
61
|
-
illegal: () => illegal,
|
|
62
|
-
httpVersionNotSupported: () => httpVersionNotSupported,
|
|
63
|
-
headerFieldsTooLarge: () => headerFieldsTooLarge,
|
|
64
|
-
gatewayTimeout: () => gatewayTimeout,
|
|
65
|
-
formatError: () => formatError,
|
|
66
|
-
forbidden: () => forbidden,
|
|
67
|
-
failedDependency: () => failedDependency,
|
|
68
|
-
expectationFailed: () => expectationFailed,
|
|
69
|
-
entityTooLarge: () => entityTooLarge,
|
|
70
|
-
defaultFormatter: () => defaultFormatter,
|
|
71
|
-
default: () => src_default,
|
|
72
|
-
createRFC7807ZodValidationError: () => createRFC7807ZodValidationError,
|
|
73
|
-
createRFC7807ValidationError: () => createRFC7807ValidationError,
|
|
74
|
-
createRFC7807Hook: () => createRFC7807Hook,
|
|
75
|
-
createRFC7807Formatter: () => createRFC7807Formatter,
|
|
76
|
-
createRFC7807ConstraintViolation: () => createRFC7807ConstraintViolation,
|
|
77
|
-
createErrorResponse: () => createErrorResponse,
|
|
78
|
-
createError: () => createError,
|
|
79
|
-
convertToBanError: () => convertToBanError,
|
|
80
|
-
conflict: () => conflict,
|
|
81
|
-
clientTimeout: () => clientTimeout,
|
|
82
|
-
badRequest: () => badRequest,
|
|
83
|
-
badImplementation: () => badImplementation,
|
|
84
|
-
badGateway: () => badGateway,
|
|
85
|
-
badData: () => badData,
|
|
86
|
-
STATUS_CODES: () => STATUS_CODES,
|
|
87
|
-
RFC7807ValidationParamSchema: () => RFC7807ValidationParamSchema,
|
|
88
|
-
RFC7807ErrorDataSchema: () => RFC7807ErrorDataSchema,
|
|
89
|
-
RFC7807DetailsSchema: () => RFC7807DetailsSchema,
|
|
90
|
-
RFC7807ConstraintViolationSchema: () => RFC7807ConstraintViolationSchema
|
|
91
|
-
});
|
|
92
|
-
module.exports = __toCommonJS(exports_src);
|
|
93
|
-
|
|
94
|
-
// src/constants/status-codes.ts
|
|
95
|
-
var STATUS_CODES = {
|
|
96
|
-
400: "Bad Request",
|
|
97
|
-
401: "Unauthorized",
|
|
98
|
-
402: "Payment Required",
|
|
99
|
-
403: "Forbidden",
|
|
100
|
-
404: "Not Found",
|
|
101
|
-
405: "Method Not Allowed",
|
|
102
|
-
406: "Not Acceptable",
|
|
103
|
-
407: "Proxy Authentication Required",
|
|
104
|
-
408: "Request Timeout",
|
|
105
|
-
409: "Conflict",
|
|
106
|
-
410: "Gone",
|
|
107
|
-
411: "Length Required",
|
|
108
|
-
412: "Precondition Failed",
|
|
109
|
-
413: "Payload Too Large",
|
|
110
|
-
414: "URI Too Long",
|
|
111
|
-
415: "Unsupported Media Type",
|
|
112
|
-
416: "Range Not Satisfiable",
|
|
113
|
-
417: "Expectation Failed",
|
|
114
|
-
418: "I'm a teapot",
|
|
115
|
-
421: "Misdirected Request",
|
|
116
|
-
422: "Unprocessable Entity",
|
|
117
|
-
423: "Locked",
|
|
118
|
-
424: "Failed Dependency",
|
|
119
|
-
425: "Too Early",
|
|
120
|
-
426: "Upgrade Required",
|
|
121
|
-
428: "Precondition Required",
|
|
122
|
-
429: "Too Many Requests",
|
|
123
|
-
431: "Request Header Fields Too Large",
|
|
124
|
-
451: "Unavailable For Legal Reasons",
|
|
125
|
-
500: "Internal Server Error",
|
|
126
|
-
501: "Not Implemented",
|
|
127
|
-
502: "Bad Gateway",
|
|
128
|
-
503: "Service Unavailable",
|
|
129
|
-
504: "Gateway Timeout",
|
|
130
|
-
505: "HTTP Version Not Supported",
|
|
131
|
-
506: "Variant Also Negotiates",
|
|
132
|
-
507: "Insufficient Storage",
|
|
133
|
-
508: "Loop Detected",
|
|
134
|
-
510: "Not Extended",
|
|
135
|
-
511: "Network Authentication Required"
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
// src/utils/validate.ts
|
|
139
|
-
function validateStatusCode(code) {
|
|
140
|
-
if (typeof code !== "number" || isNaN(code)) {
|
|
141
|
-
return 500;
|
|
142
|
-
}
|
|
143
|
-
if (code < 400 || code >= 600) {
|
|
144
|
-
return 500;
|
|
145
|
-
}
|
|
146
|
-
return code;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// src/core/create-error.ts
|
|
150
|
-
function createError(options) {
|
|
151
|
-
const status = validateStatusCode(options.statusCode ?? 500);
|
|
152
|
-
const error = {
|
|
153
|
-
status,
|
|
154
|
-
message: options.message ?? STATUS_CODES[status],
|
|
155
|
-
isBan: true
|
|
156
|
-
};
|
|
157
|
-
if (options.data !== undefined) {
|
|
158
|
-
error.data = options.data;
|
|
159
|
-
}
|
|
160
|
-
if (options.headers) {
|
|
161
|
-
error.headers = { ...options.headers };
|
|
162
|
-
}
|
|
163
|
-
if (options.allow) {
|
|
164
|
-
error.allow = Array.isArray(options.allow) ? [...options.allow] : [options.allow];
|
|
165
|
-
}
|
|
166
|
-
if (options.cause) {
|
|
167
|
-
error.cause = options.cause;
|
|
168
|
-
if (options.cause instanceof Error) {
|
|
169
|
-
error.causeStack = options.cause.stack;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
Error.captureStackTrace(error, createError);
|
|
173
|
-
return error;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// src/core/convert-error.ts
|
|
177
|
-
function isBanError(err, statusCode) {
|
|
178
|
-
return typeof err === "object" && err !== null && "isBan" in err && err.isBan === true && (!statusCode || err.status === statusCode);
|
|
179
|
-
}
|
|
180
|
-
function convertToBanError(err, options = {}) {
|
|
181
|
-
if (isBanError(err)) {
|
|
182
|
-
const merged = {
|
|
183
|
-
status: options.statusCode ?? err.status,
|
|
184
|
-
message: options.message ?? err.message,
|
|
185
|
-
isBan: true,
|
|
186
|
-
data: options.data !== undefined ? options.data : err.data,
|
|
187
|
-
headers: {
|
|
188
|
-
...err.headers,
|
|
189
|
-
...options.headers
|
|
190
|
-
},
|
|
191
|
-
allow: options.allow ? Array.isArray(options.allow) ? [...options.allow] : [options.allow] : err.allow,
|
|
192
|
-
cause: options.cause ?? err.cause,
|
|
193
|
-
stack: err.stack
|
|
194
|
-
};
|
|
195
|
-
return merged;
|
|
196
|
-
}
|
|
197
|
-
if (err instanceof Error) {
|
|
198
|
-
return createError({
|
|
199
|
-
...options,
|
|
200
|
-
message: options.message ?? err.message,
|
|
201
|
-
cause: err
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
return createError({
|
|
205
|
-
...options,
|
|
206
|
-
message: options.message ?? (typeof err === "string" ? err : "Unknown error"),
|
|
207
|
-
cause: err
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
// src/core/format-error.ts
|
|
212
|
-
function formatError(error, formatter, options = {}) {
|
|
213
|
-
return formatter.format(error, options.headers, options.sanitize, options.includeStackTrace);
|
|
214
|
-
}
|
|
215
|
-
function createErrorResponse(error, formatted) {
|
|
216
|
-
const headers = new Headers({
|
|
217
|
-
"Content-Type": "application/json",
|
|
218
|
-
...error.headers
|
|
219
|
-
});
|
|
220
|
-
if (error.allow?.length) {
|
|
221
|
-
headers.set("Allow", error.allow.join(", "));
|
|
222
|
-
}
|
|
223
|
-
const isDeveloperError = error.data && typeof error.data === "object" && "isDeveloperError" in error.data && error.data.isDeveloperError === true;
|
|
224
|
-
if (isDeveloperError && false) {
|
|
225
|
-
}
|
|
226
|
-
return new Response(JSON.stringify(formatted), {
|
|
227
|
-
status: error.status,
|
|
228
|
-
headers
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
// src/utils/sanitize.ts
|
|
232
|
-
function sanitizeObject(obj, keysToRemove) {
|
|
233
|
-
if (!keysToRemove.length) {
|
|
234
|
-
return obj;
|
|
235
|
-
}
|
|
236
|
-
if (Array.isArray(obj)) {
|
|
237
|
-
return obj.map((item) => sanitizeObject(item, keysToRemove));
|
|
238
|
-
}
|
|
239
|
-
if (obj !== null && typeof obj === "object") {
|
|
240
|
-
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
241
|
-
if (keysToRemove.includes(key)) {
|
|
242
|
-
return acc;
|
|
243
|
-
}
|
|
244
|
-
acc[key] = sanitizeObject(value, keysToRemove);
|
|
245
|
-
return acc;
|
|
246
|
-
}, {});
|
|
247
|
-
}
|
|
248
|
-
return obj;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
// src/formatters/default/index.ts
|
|
252
|
-
var defaultFormatter = {
|
|
253
|
-
contentType: "application/json",
|
|
254
|
-
format(error, headers = {}, sanitize = [], includeStackTrace = false) {
|
|
255
|
-
const { status, message, data } = error;
|
|
256
|
-
const payload = {
|
|
257
|
-
statusCode: status,
|
|
258
|
-
error: STATUS_CODES[status] || "Unknown Error"
|
|
259
|
-
};
|
|
260
|
-
if (typeof message === "string") {
|
|
261
|
-
payload.message = message;
|
|
262
|
-
}
|
|
263
|
-
const isDeveloperError = data && typeof data === "object" && "isDeveloperError" in data && data.isDeveloperError === true;
|
|
264
|
-
if (includeStackTrace || isDeveloperError) {
|
|
265
|
-
if (error.stack) {
|
|
266
|
-
payload.stack = error.stack;
|
|
267
|
-
}
|
|
268
|
-
if (error.causeStack) {
|
|
269
|
-
payload.causeStack = error.causeStack;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
if (data !== undefined) {
|
|
273
|
-
payload.data = data;
|
|
274
|
-
}
|
|
275
|
-
const output = {
|
|
276
|
-
statusCode: status,
|
|
277
|
-
payload: sanitizeObject(payload, sanitize)
|
|
278
|
-
};
|
|
279
|
-
return output;
|
|
280
|
-
}
|
|
281
|
-
};
|
|
282
|
-
|
|
283
|
-
// src/formatters/rfc7807/schemas.ts
|
|
284
|
-
var import_zod_openapi = require("@hono/zod-openapi");
|
|
285
|
-
var RFC7807ValidationParamSchema = import_zod_openapi.z.object({
|
|
286
|
-
name: import_zod_openapi.z.string().openapi({
|
|
287
|
-
example: "username",
|
|
288
|
-
description: "The field name that failed validation"
|
|
289
|
-
}),
|
|
290
|
-
reason: import_zod_openapi.z.string().openapi({
|
|
291
|
-
example: "String must contain at least 3 character(s)",
|
|
292
|
-
description: "The reason for validation failure"
|
|
293
|
-
})
|
|
294
|
-
});
|
|
295
|
-
var RFC7807ConstraintViolationSchema = import_zod_openapi.z.object({
|
|
296
|
-
name: import_zod_openapi.z.string().openapi({
|
|
297
|
-
example: "email",
|
|
298
|
-
description: "The field name that violated a constraint"
|
|
299
|
-
}),
|
|
300
|
-
reason: import_zod_openapi.z.string().openapi({
|
|
301
|
-
example: "Email already exists",
|
|
302
|
-
description: "The reason for the constraint violation"
|
|
303
|
-
}),
|
|
304
|
-
resource: import_zod_openapi.z.string().openapi({
|
|
305
|
-
example: "user",
|
|
306
|
-
description: "The resource type that contains the constraint"
|
|
307
|
-
}),
|
|
308
|
-
constraint: import_zod_openapi.z.string().openapi({
|
|
309
|
-
example: "unique",
|
|
310
|
-
description: "The type of constraint that was violated"
|
|
311
|
-
})
|
|
312
|
-
});
|
|
313
|
-
var RFC7807ErrorDataSchema = import_zod_openapi.z.object({
|
|
314
|
-
"invalid-params": import_zod_openapi.z.array(RFC7807ValidationParamSchema).optional(),
|
|
315
|
-
violations: import_zod_openapi.z.array(RFC7807ConstraintViolationSchema).optional()
|
|
316
|
-
});
|
|
317
|
-
var RFC7807DetailsSchema = import_zod_openapi.z.object({
|
|
318
|
-
type: import_zod_openapi.z.string().url().openapi({
|
|
319
|
-
example: "https://api.example.com/problems/validation-error",
|
|
320
|
-
description: "A URI reference that identifies the problem type"
|
|
321
|
-
}),
|
|
322
|
-
title: import_zod_openapi.z.string().openapi({
|
|
323
|
-
example: "Validation Failed",
|
|
324
|
-
description: "A short, human-readable summary of the problem type"
|
|
325
|
-
}),
|
|
326
|
-
status: import_zod_openapi.z.number().int().min(400).max(599).openapi({
|
|
327
|
-
example: 400,
|
|
328
|
-
description: "The HTTP status code"
|
|
329
|
-
}),
|
|
330
|
-
detail: import_zod_openapi.z.string().optional().openapi({
|
|
331
|
-
example: "The request contains invalid fields",
|
|
332
|
-
description: "A human-readable explanation specific to this occurrence of the problem"
|
|
333
|
-
}),
|
|
334
|
-
instance: import_zod_openapi.z.string().url().optional().openapi({
|
|
335
|
-
example: "urn:uuid:6b56944d-5e89-4b4d-9ca7-c1be3d1f0e3f",
|
|
336
|
-
description: "A URI reference that identifies the specific occurrence of the problem"
|
|
337
|
-
}),
|
|
338
|
-
timestamp: import_zod_openapi.z.string().datetime().optional().openapi({
|
|
339
|
-
example: "2025-02-26T12:34:56.789Z",
|
|
340
|
-
description: "When the error occurred"
|
|
341
|
-
})
|
|
342
|
-
}).merge(RFC7807ErrorDataSchema);
|
|
343
|
-
|
|
344
|
-
// src/formatters/rfc7807/formatter.ts
|
|
345
|
-
function createRFC7807Formatter(options = {}) {
|
|
346
|
-
const baseUrl = options.baseUrl || "https://api.example.com/problems";
|
|
347
|
-
return {
|
|
348
|
-
contentType: "application/problem+json",
|
|
349
|
-
format(error) {
|
|
350
|
-
const base = {
|
|
351
|
-
type: `${baseUrl}/${error.status}`,
|
|
352
|
-
title: STATUS_CODES[error.status] || "Unknown Error",
|
|
353
|
-
status: error.status,
|
|
354
|
-
detail: error.message,
|
|
355
|
-
instance: `urn:uuid:${crypto.randomUUID()}`,
|
|
356
|
-
timestamp: new Date().toISOString()
|
|
357
|
-
};
|
|
358
|
-
if (error.data?.["invalid-params"]) {
|
|
359
|
-
return RFC7807DetailsSchema.parse({
|
|
360
|
-
...base,
|
|
361
|
-
"invalid-params": error.data["invalid-params"]
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
if (error.data?.violations) {
|
|
365
|
-
return RFC7807DetailsSchema.parse({
|
|
366
|
-
...base,
|
|
367
|
-
violations: error.data.violations
|
|
368
|
-
});
|
|
369
|
-
}
|
|
370
|
-
return RFC7807DetailsSchema.parse(base);
|
|
371
|
-
}
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
function createRFC7807ValidationError(params) {
|
|
375
|
-
return {
|
|
376
|
-
"invalid-params": RFC7807ValidationParamSchema.array().parse(params)
|
|
377
|
-
};
|
|
378
|
-
}
|
|
379
|
-
function createRFC7807ZodValidationError(error) {
|
|
380
|
-
return {
|
|
381
|
-
"invalid-params": RFC7807ValidationParamSchema.array().parse(error.errors.map((e) => ({
|
|
382
|
-
name: e.path.join("."),
|
|
383
|
-
reason: e.message
|
|
384
|
-
})))
|
|
385
|
-
};
|
|
386
|
-
}
|
|
387
|
-
function createRFC7807ConstraintViolation(name, reason, resource, constraint = "unique") {
|
|
388
|
-
return {
|
|
389
|
-
violations: RFC7807ConstraintViolationSchema.array().parse([
|
|
390
|
-
{
|
|
391
|
-
name,
|
|
392
|
-
reason,
|
|
393
|
-
resource,
|
|
394
|
-
constraint
|
|
395
|
-
}
|
|
396
|
-
])
|
|
397
|
-
};
|
|
398
|
-
}
|
|
399
|
-
// src/formatters/rfc7807/hooks.ts
|
|
400
|
-
var import_zod = require("zod");
|
|
401
|
-
|
|
402
|
-
// src/factories/client-errors.ts
|
|
403
|
-
function createErrorWithStatus(statusCode, messageOrOptions, options) {
|
|
404
|
-
if (typeof messageOrOptions === "string") {
|
|
405
|
-
return createError({
|
|
406
|
-
...options,
|
|
407
|
-
statusCode,
|
|
408
|
-
message: messageOrOptions
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
return createError({ ...messageOrOptions, ...options, statusCode });
|
|
412
|
-
}
|
|
413
|
-
function badRequest(messageOrOptions, options) {
|
|
414
|
-
return createErrorWithStatus(400, messageOrOptions, options);
|
|
415
|
-
}
|
|
416
|
-
function unauthorized(messageOrOptions, options) {
|
|
417
|
-
return createErrorWithStatus(401, messageOrOptions, options);
|
|
418
|
-
}
|
|
419
|
-
function paymentRequired(messageOrOptions, options) {
|
|
420
|
-
return createErrorWithStatus(402, messageOrOptions, options);
|
|
421
|
-
}
|
|
422
|
-
function forbidden(messageOrOptions, options) {
|
|
423
|
-
return createErrorWithStatus(403, messageOrOptions, options);
|
|
424
|
-
}
|
|
425
|
-
function notFound(messageOrOptions, options) {
|
|
426
|
-
return createErrorWithStatus(404, messageOrOptions, options);
|
|
427
|
-
}
|
|
428
|
-
function methodNotAllowed(messageOrOptions, options) {
|
|
429
|
-
return createErrorWithStatus(405, messageOrOptions, options);
|
|
430
|
-
}
|
|
431
|
-
function notAcceptable(messageOrOptions, options) {
|
|
432
|
-
return createErrorWithStatus(406, messageOrOptions, options);
|
|
433
|
-
}
|
|
434
|
-
function proxyAuthRequired(messageOrOptions, options) {
|
|
435
|
-
return createErrorWithStatus(407, messageOrOptions, options);
|
|
436
|
-
}
|
|
437
|
-
function clientTimeout(messageOrOptions, options) {
|
|
438
|
-
return createErrorWithStatus(408, messageOrOptions, options);
|
|
439
|
-
}
|
|
440
|
-
function conflict(messageOrOptions, options) {
|
|
441
|
-
return createErrorWithStatus(409, messageOrOptions, options);
|
|
442
|
-
}
|
|
443
|
-
function resourceGone(messageOrOptions, options) {
|
|
444
|
-
return createErrorWithStatus(410, messageOrOptions, options);
|
|
445
|
-
}
|
|
446
|
-
function lengthRequired(messageOrOptions, options) {
|
|
447
|
-
return createErrorWithStatus(411, messageOrOptions, options);
|
|
448
|
-
}
|
|
449
|
-
function preconditionFailed(messageOrOptions, options) {
|
|
450
|
-
return createErrorWithStatus(412, messageOrOptions, options);
|
|
451
|
-
}
|
|
452
|
-
function entityTooLarge(messageOrOptions, options) {
|
|
453
|
-
return createErrorWithStatus(413, messageOrOptions, options);
|
|
454
|
-
}
|
|
455
|
-
function uriTooLong(messageOrOptions, options) {
|
|
456
|
-
return createErrorWithStatus(414, messageOrOptions, options);
|
|
457
|
-
}
|
|
458
|
-
function unsupportedMediaType(messageOrOptions, options) {
|
|
459
|
-
return createErrorWithStatus(415, messageOrOptions, options);
|
|
460
|
-
}
|
|
461
|
-
function rangeNotSatisfiable(messageOrOptions, options) {
|
|
462
|
-
return createErrorWithStatus(416, messageOrOptions, options);
|
|
463
|
-
}
|
|
464
|
-
function expectationFailed(messageOrOptions, options) {
|
|
465
|
-
return createErrorWithStatus(417, messageOrOptions, options);
|
|
466
|
-
}
|
|
467
|
-
function teapot(messageOrOptions, options) {
|
|
468
|
-
return createErrorWithStatus(418, messageOrOptions, options);
|
|
469
|
-
}
|
|
470
|
-
function misdirectedRequest(messageOrOptions, options) {
|
|
471
|
-
return createErrorWithStatus(421, messageOrOptions, options);
|
|
472
|
-
}
|
|
473
|
-
function badData(messageOrOptions, options) {
|
|
474
|
-
return createErrorWithStatus(422, messageOrOptions, options);
|
|
475
|
-
}
|
|
476
|
-
function locked(messageOrOptions, options) {
|
|
477
|
-
return createErrorWithStatus(423, messageOrOptions, options);
|
|
478
|
-
}
|
|
479
|
-
function failedDependency(messageOrOptions, options) {
|
|
480
|
-
return createErrorWithStatus(424, messageOrOptions, options);
|
|
481
|
-
}
|
|
482
|
-
function tooEarly(messageOrOptions, options) {
|
|
483
|
-
return createErrorWithStatus(425, messageOrOptions, options);
|
|
484
|
-
}
|
|
485
|
-
function upgradeRequired(messageOrOptions, options) {
|
|
486
|
-
return createErrorWithStatus(426, messageOrOptions, options);
|
|
487
|
-
}
|
|
488
|
-
function preconditionRequired(messageOrOptions, options) {
|
|
489
|
-
return createErrorWithStatus(428, messageOrOptions, options);
|
|
490
|
-
}
|
|
491
|
-
function tooManyRequests(messageOrOptions, options) {
|
|
492
|
-
return createErrorWithStatus(429, messageOrOptions, options);
|
|
493
|
-
}
|
|
494
|
-
function headerFieldsTooLarge(messageOrOptions, options) {
|
|
495
|
-
return createErrorWithStatus(431, messageOrOptions, options);
|
|
496
|
-
}
|
|
497
|
-
function illegal(messageOrOptions, options) {
|
|
498
|
-
return createErrorWithStatus(451, messageOrOptions, options);
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
// src/factories/server-errors.ts
|
|
502
|
-
function createErrorWithStatus2(statusCode, messageOrOptions, options) {
|
|
503
|
-
if (typeof messageOrOptions === "string") {
|
|
504
|
-
return createError({
|
|
505
|
-
...options,
|
|
506
|
-
statusCode,
|
|
507
|
-
message: messageOrOptions
|
|
508
|
-
});
|
|
509
|
-
}
|
|
510
|
-
return createError({ ...messageOrOptions, ...options, statusCode });
|
|
511
|
-
}
|
|
512
|
-
function internal(messageOrOptions, options) {
|
|
513
|
-
return createErrorWithStatus2(500, messageOrOptions, options);
|
|
514
|
-
}
|
|
515
|
-
function notImplemented(messageOrOptions, options) {
|
|
516
|
-
return createErrorWithStatus2(501, messageOrOptions, options);
|
|
517
|
-
}
|
|
518
|
-
function badGateway(messageOrOptions, options) {
|
|
519
|
-
return createErrorWithStatus2(502, messageOrOptions, options);
|
|
520
|
-
}
|
|
521
|
-
function serverUnavailable(messageOrOptions, options) {
|
|
522
|
-
return createErrorWithStatus2(503, messageOrOptions, options);
|
|
523
|
-
}
|
|
524
|
-
function gatewayTimeout(messageOrOptions, options) {
|
|
525
|
-
return createErrorWithStatus2(504, messageOrOptions, options);
|
|
526
|
-
}
|
|
527
|
-
function httpVersionNotSupported(messageOrOptions, options) {
|
|
528
|
-
return createErrorWithStatus2(505, messageOrOptions, options);
|
|
529
|
-
}
|
|
530
|
-
function variantAlsoNegotiates(messageOrOptions, options) {
|
|
531
|
-
return createErrorWithStatus2(506, messageOrOptions, options);
|
|
532
|
-
}
|
|
533
|
-
function insufficientStorage(messageOrOptions, options) {
|
|
534
|
-
return createErrorWithStatus2(507, messageOrOptions, options);
|
|
535
|
-
}
|
|
536
|
-
function loopDetected(messageOrOptions, options) {
|
|
537
|
-
return createErrorWithStatus2(508, messageOrOptions, options);
|
|
538
|
-
}
|
|
539
|
-
function notExtended(messageOrOptions, options) {
|
|
540
|
-
return createErrorWithStatus2(510, messageOrOptions, options);
|
|
541
|
-
}
|
|
542
|
-
function networkAuthRequired(messageOrOptions, options) {
|
|
543
|
-
return createErrorWithStatus2(511, messageOrOptions, options);
|
|
544
|
-
}
|
|
545
|
-
function badImplementation(messageOrOptions, options) {
|
|
546
|
-
const mergedOptions = typeof messageOrOptions === "string" ? { ...options, message: messageOrOptions } : { ...messageOrOptions, ...options };
|
|
547
|
-
const mergedData = {
|
|
548
|
-
isDeveloperError: true,
|
|
549
|
-
...mergedOptions.data || {}
|
|
550
|
-
};
|
|
551
|
-
return createError({
|
|
552
|
-
...mergedOptions,
|
|
553
|
-
statusCode: 500,
|
|
554
|
-
data: mergedData
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
|
|
558
|
-
// src/formatters/rfc7807/hooks.ts
|
|
559
|
-
function createRFC7807Hook(options) {
|
|
560
|
-
return (result, c) => {
|
|
561
|
-
if (!result.success && "error" in result && result.error instanceof import_zod.ZodError) {
|
|
562
|
-
const validationData = createRFC7807ZodValidationError(result.error);
|
|
563
|
-
throw badRequest({
|
|
564
|
-
message: options?.message || "Validation Error",
|
|
565
|
-
data: validationData,
|
|
566
|
-
formatter: options?.formatter,
|
|
567
|
-
headers: options?.headers,
|
|
568
|
-
sanitize: options?.sanitize,
|
|
569
|
-
includeStackTrace: options?.includeStackTrace
|
|
570
|
-
});
|
|
571
|
-
}
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
var rfc7807Hook = createRFC7807Hook();
|
|
575
|
-
// src/middleware/ban-middleware.ts
|
|
576
|
-
var DEFAULT_OPTIONS = {
|
|
577
|
-
formatter: defaultFormatter,
|
|
578
|
-
sanitize: [],
|
|
579
|
-
includeStackTrace: false,
|
|
580
|
-
headers: {}
|
|
581
|
-
};
|
|
582
|
-
function ban(options = {}) {
|
|
583
|
-
const resolvedOptions = {
|
|
584
|
-
...DEFAULT_OPTIONS,
|
|
585
|
-
...options,
|
|
586
|
-
formatter: options.formatter ? options.formatter : DEFAULT_OPTIONS.formatter,
|
|
587
|
-
headers: {
|
|
588
|
-
...DEFAULT_OPTIONS.headers,
|
|
589
|
-
...options.headers
|
|
590
|
-
},
|
|
591
|
-
sanitize: [...DEFAULT_OPTIONS.sanitize, ...options.sanitize || []]
|
|
592
|
-
};
|
|
593
|
-
return async (_, next) => {
|
|
594
|
-
try {
|
|
595
|
-
await next();
|
|
596
|
-
} catch (err) {
|
|
597
|
-
const formatter = resolvedOptions.formatter;
|
|
598
|
-
const error = convertToBanError(err, {
|
|
599
|
-
formatter,
|
|
600
|
-
headers: resolvedOptions.headers,
|
|
601
|
-
sanitize: resolvedOptions.sanitize,
|
|
602
|
-
includeStackTrace: resolvedOptions.includeStackTrace
|
|
603
|
-
});
|
|
604
|
-
const formatted = formatError(error, formatter, {
|
|
605
|
-
headers: resolvedOptions.headers,
|
|
606
|
-
sanitize: resolvedOptions.sanitize,
|
|
607
|
-
includeStackTrace: resolvedOptions.includeStackTrace
|
|
608
|
-
});
|
|
609
|
-
return createErrorResponse(error, formatted);
|
|
610
|
-
}
|
|
611
|
-
};
|
|
612
|
-
}
|
|
613
|
-
// src/index.ts
|
|
614
|
-
var src_default = ban;
|
|
615
|
-
|
|
616
|
-
//# debugId=03A50C231029D24464756E2164756E21
|
|
617
|
-
//# sourceMappingURL=index.js.map
|