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.
Files changed (50) hide show
  1. package/dist/esm/formatters/index.js +1 -248
  2. package/dist/esm/index.js +1 -585
  3. package/package.json +10 -26
  4. package/dist/cjs/formatters/index.js +0 -280
  5. package/dist/cjs/formatters/index.js.map +0 -16
  6. package/dist/cjs/formatters/rfc7807/index.js +0 -364
  7. package/dist/cjs/formatters/rfc7807/index.js.map +0 -16
  8. package/dist/cjs/index.js +0 -617
  9. package/dist/cjs/index.js.map +0 -23
  10. package/dist/esm/formatters/index.js.map +0 -16
  11. package/dist/esm/formatters/rfc7807/index.js +0 -333
  12. package/dist/esm/formatters/rfc7807/index.js.map +0 -16
  13. package/dist/esm/index.js.map +0 -23
  14. package/src/__tests__/core/convert-error.test.ts +0 -158
  15. package/src/__tests__/core/create-error.test.ts +0 -113
  16. package/src/__tests__/core/format-error.test.ts +0 -162
  17. package/src/__tests__/factories/factories.test.ts +0 -380
  18. package/src/__tests__/formatters/default-formatter.test.ts +0 -117
  19. package/src/__tests__/middleware/README.md +0 -49
  20. package/src/__tests__/middleware/ban-middleware.test.ts.disabled +0 -23
  21. package/src/__tests__/utils/sanitize.test.ts +0 -117
  22. package/src/__tests__/utils/validate.test.ts +0 -35
  23. package/src/constants/index.ts +0 -6
  24. package/src/constants/status-codes.ts +0 -52
  25. package/src/core/convert-error.ts +0 -74
  26. package/src/core/create-error.ts +0 -65
  27. package/src/core/format-error.ts +0 -76
  28. package/src/core/index.ts +0 -8
  29. package/src/factories/client-errors.ts +0 -492
  30. package/src/factories/index.ts +0 -7
  31. package/src/factories/server-errors.ts +0 -230
  32. package/src/formatters/default/index.ts +0 -72
  33. package/src/formatters/index.ts +0 -13
  34. package/src/formatters/interfaces.ts +0 -7
  35. package/src/formatters/rfc7807/formatter.ts +0 -105
  36. package/src/formatters/rfc7807/hooks.ts +0 -73
  37. package/src/formatters/rfc7807/index.ts +0 -20
  38. package/src/formatters/rfc7807/schemas.ts +0 -88
  39. package/src/index.ts +0 -23
  40. package/src/middleware/ban-middleware.ts +0 -77
  41. package/src/middleware/index.ts +0 -6
  42. package/src/types/common.ts +0 -14
  43. package/src/types/error.ts +0 -46
  44. package/src/types/formatter.ts +0 -61
  45. package/src/types/index.ts +0 -11
  46. package/src/types/options.ts +0 -71
  47. package/src/types/rfc7807.ts +0 -93
  48. package/src/utils/index.ts +0 -7
  49. package/src/utils/sanitize.ts +0 -34
  50. package/src/utils/validate.ts +0 -24
@@ -1,162 +0,0 @@
1
- import { describe, it, expect, mock } from "bun:test";
2
- import { formatError, createErrorResponse } from "../../core/format-error";
3
- import { createError } from "../../core/create-error";
4
- import type { BanError, ErrorFormatter } from "../../types";
5
-
6
- describe("formatError", () => {
7
- it("calls the formatter with the error", () => {
8
- const error = createError({ statusCode: 404, message: "Not found" });
9
-
10
- const mockFormatter: ErrorFormatter = {
11
- contentType: "application/json",
12
- format: mock((err) => ({ formatted: true, status: err.status })),
13
- };
14
-
15
- const result = formatError(error, mockFormatter);
16
-
17
- expect(mockFormatter.format).toHaveBeenCalledWith(
18
- error,
19
- undefined,
20
- undefined,
21
- undefined
22
- );
23
- expect(result).toEqual({ formatted: true, status: 404 });
24
- });
25
-
26
- it("passes options to the formatter", () => {
27
- const error = createError({ statusCode: 500 });
28
- const headers = { "X-Test": "value" };
29
- const sanitize = ["password", "token"];
30
-
31
- const mockFormatter: ErrorFormatter = {
32
- contentType: "application/json",
33
- format: mock(() => ({ formatted: true })),
34
- };
35
-
36
- formatError(error, mockFormatter, {
37
- headers,
38
- sanitize,
39
- includeStackTrace: true,
40
- });
41
-
42
- expect(mockFormatter.format).toHaveBeenCalledWith(
43
- error,
44
- headers,
45
- sanitize,
46
- true
47
- );
48
- });
49
- });
50
-
51
- describe("createErrorResponse", () => {
52
- it("creates a Response with the formatted error", () => {
53
- const error = createError({ statusCode: 400, message: "Bad request" });
54
- const formatted = { statusCode: 400, message: "Bad request" };
55
-
56
- const response = createErrorResponse(error, formatted);
57
-
58
- expect(response).toBeInstanceOf(Response);
59
- expect(response.status).toBe(400);
60
- expect(response.headers.get("Content-Type")).toBe("application/json");
61
- });
62
-
63
- it("includes custom headers from the error", () => {
64
- const error = createError({
65
- statusCode: 400,
66
- headers: { "X-Custom": "value" },
67
- });
68
-
69
- const response = createErrorResponse(error, {});
70
-
71
- expect(response.headers.get("X-Custom")).toBe("value");
72
- });
73
-
74
- it("adds Allow header for errors with allow property", () => {
75
- const error = createError({
76
- statusCode: 405,
77
- allow: ["GET", "POST"],
78
- });
79
-
80
- const response = createErrorResponse(error, {});
81
-
82
- expect(response.headers.get("Allow")).toBe("GET, POST");
83
- });
84
-
85
- it("sanitizes developer errors in production", async () => {
86
- // Mock process.env.NODE_ENV
87
- const originalNodeEnv = process.env.NODE_ENV;
88
- process.env.NODE_ENV = "production";
89
-
90
- try {
91
- const error = createError({
92
- statusCode: 500,
93
- message: "Internal server error with sensitive details",
94
- data: { isDeveloperError: true, sensitiveInfo: "secret" },
95
- });
96
-
97
- const formatted = {
98
- statusCode: 500,
99
- payload: {
100
- message: "Original error message",
101
- data: { sensitiveInfo: "secret" },
102
- },
103
- };
104
-
105
- const response = createErrorResponse(error, formatted);
106
-
107
- // Get the response body as text
108
- const responseText = await response.text();
109
- const body = JSON.parse(responseText) as {
110
- statusCode: number;
111
- payload: {
112
- statusCode: number;
113
- error: string;
114
- message: string;
115
- data?: unknown;
116
- };
117
- };
118
-
119
- // Should use sanitized message
120
- expect(body.payload.message).toBe("An internal server error occurred");
121
- // Should not include sensitive data
122
- expect(body.payload.data).toBeUndefined();
123
- } finally {
124
- // Restore original NODE_ENV
125
- process.env.NODE_ENV = originalNodeEnv;
126
- }
127
- });
128
-
129
- it("preserves the original formatted error in non-production", async () => {
130
- // Mock process.env.NODE_ENV
131
- const originalNodeEnv = process.env.NODE_ENV;
132
- process.env.NODE_ENV = "development";
133
-
134
- try {
135
- const error = createError({
136
- statusCode: 500,
137
- message: "Detailed error",
138
- data: { isDeveloperError: true, details: "stack trace" },
139
- });
140
-
141
- const formatted = {
142
- statusCode: 500,
143
- payload: {
144
- message: "Detailed error",
145
- data: { details: "stack trace" },
146
- },
147
- };
148
-
149
- const response = createErrorResponse(error, formatted);
150
-
151
- // Get the response body as text
152
- const responseText = await response.text();
153
- const responseJson = JSON.parse(responseText);
154
-
155
- // The response body should match the original formatted error
156
- expect(responseJson).toEqual(formatted);
157
- } finally {
158
- // Restore original NODE_ENV
159
- process.env.NODE_ENV = originalNodeEnv;
160
- }
161
- });
162
- });
@@ -1,380 +0,0 @@
1
- import { describe, it, expect } from "bun:test";
2
- import {
3
- badRequest,
4
- notFound,
5
- unauthorized,
6
- paymentRequired,
7
- forbidden,
8
- methodNotAllowed,
9
- notAcceptable,
10
- clientTimeout,
11
- conflict,
12
- resourceGone,
13
- badData,
14
- tooManyRequests,
15
- teapot,
16
- illegal,
17
- lengthRequired,
18
- preconditionFailed,
19
- entityTooLarge,
20
- uriTooLong,
21
- unsupportedMediaType,
22
- rangeNotSatisfiable,
23
- expectationFailed,
24
- } from "../../factories/client-errors";
25
- import {
26
- internal,
27
- badImplementation,
28
- serverUnavailable,
29
- notImplemented,
30
- badGateway,
31
- gatewayTimeout,
32
- httpVersionNotSupported,
33
- insufficientStorage,
34
- loopDetected,
35
- variantAlsoNegotiates,
36
- notExtended,
37
- networkAuthRequired,
38
- } from "../../factories/server-errors";
39
- import { STATUS_CODES } from "../../constants";
40
-
41
- describe("Error factories", () => {
42
- describe("Client error factories", () => {
43
- it("creates a 400 Bad Request error", () => {
44
- const error = badRequest();
45
- expect(error.status).toBe(400);
46
- expect(error.message).toBe(STATUS_CODES[400]);
47
- expect(error.isBan).toBe(true);
48
- });
49
-
50
- it("creates a 401 Unauthorized error", () => {
51
- const error = unauthorized();
52
- expect(error.status).toBe(401);
53
- expect(error.message).toBe(STATUS_CODES[401]);
54
- expect(error.isBan).toBe(true);
55
- });
56
-
57
- it("creates a 402 Payment Required error", () => {
58
- const error = paymentRequired();
59
- expect(error.status).toBe(402);
60
- expect(error.message).toBe(STATUS_CODES[402]);
61
- expect(error.isBan).toBe(true);
62
- });
63
-
64
- it("creates a 403 Forbidden error", () => {
65
- const error = forbidden();
66
- expect(error.status).toBe(403);
67
- expect(error.message).toBe(STATUS_CODES[403]);
68
- expect(error.isBan).toBe(true);
69
- });
70
-
71
- it("creates a 404 Not Found error", () => {
72
- const error = notFound();
73
- expect(error.status).toBe(404);
74
- expect(error.message).toBe(STATUS_CODES[404]);
75
- expect(error.isBan).toBe(true);
76
- });
77
-
78
- it("creates a 405 Method Not Allowed error", () => {
79
- const error = methodNotAllowed();
80
- expect(error.status).toBe(405);
81
- expect(error.message).toBe(STATUS_CODES[405]);
82
- expect(error.isBan).toBe(true);
83
- });
84
-
85
- it("creates a 406 Not Acceptable error", () => {
86
- const error = notAcceptable();
87
- expect(error.status).toBe(406);
88
- expect(error.message).toBe(STATUS_CODES[406]);
89
- expect(error.isBan).toBe(true);
90
- });
91
-
92
- it("creates a 408 Request Timeout error", () => {
93
- const error = clientTimeout();
94
- expect(error.status).toBe(408);
95
- expect(error.message).toBe(STATUS_CODES[408]);
96
- expect(error.isBan).toBe(true);
97
- });
98
-
99
- it("creates a 409 Conflict error", () => {
100
- const error = conflict();
101
- expect(error.status).toBe(409);
102
- expect(error.message).toBe(STATUS_CODES[409]);
103
- expect(error.isBan).toBe(true);
104
- });
105
-
106
- it("creates a 410 Gone error", () => {
107
- const error = resourceGone();
108
- expect(error.status).toBe(410);
109
- expect(error.message).toBe(STATUS_CODES[410]);
110
- expect(error.isBan).toBe(true);
111
- });
112
-
113
- it("creates a 411 Length Required error", () => {
114
- const error = lengthRequired();
115
- expect(error.status).toBe(411);
116
- expect(error.message).toBe(STATUS_CODES[411]);
117
- expect(error.isBan).toBe(true);
118
- });
119
-
120
- it("creates a 412 Precondition Failed error", () => {
121
- const error = preconditionFailed();
122
- expect(error.status).toBe(412);
123
- expect(error.message).toBe(STATUS_CODES[412]);
124
- expect(error.isBan).toBe(true);
125
- });
126
-
127
- it("creates a 413 Payload Too Large error", () => {
128
- const error = entityTooLarge();
129
- expect(error.status).toBe(413);
130
- expect(error.message).toBe(STATUS_CODES[413]);
131
- expect(error.isBan).toBe(true);
132
- });
133
-
134
- it("creates a 414 URI Too Long error", () => {
135
- const error = uriTooLong();
136
- expect(error.status).toBe(414);
137
- expect(error.message).toBe(STATUS_CODES[414]);
138
- expect(error.isBan).toBe(true);
139
- });
140
-
141
- it("creates a 415 Unsupported Media Type error", () => {
142
- const error = unsupportedMediaType();
143
- expect(error.status).toBe(415);
144
- expect(error.message).toBe(STATUS_CODES[415]);
145
- expect(error.isBan).toBe(true);
146
- });
147
-
148
- it("creates a 416 Range Not Satisfiable error", () => {
149
- const error = rangeNotSatisfiable();
150
- expect(error.status).toBe(416);
151
- expect(error.message).toBe(STATUS_CODES[416]);
152
- expect(error.isBan).toBe(true);
153
- });
154
-
155
- it("creates a 417 Expectation Failed error", () => {
156
- const error = expectationFailed();
157
- expect(error.status).toBe(417);
158
- expect(error.message).toBe(STATUS_CODES[417]);
159
- expect(error.isBan).toBe(true);
160
- });
161
-
162
- it("creates a 418 I'm a Teapot error", () => {
163
- const error = teapot();
164
- expect(error.status).toBe(418);
165
- expect(error.message).toBe(STATUS_CODES[418]);
166
- expect(error.isBan).toBe(true);
167
- });
168
-
169
- it("creates a 422 Unprocessable Entity error", () => {
170
- const error = badData();
171
- expect(error.status).toBe(422);
172
- expect(error.message).toBe(STATUS_CODES[422]);
173
- expect(error.isBan).toBe(true);
174
- });
175
-
176
- it("creates a 429 Too Many Requests error", () => {
177
- const error = tooManyRequests();
178
- expect(error.status).toBe(429);
179
- expect(error.message).toBe(STATUS_CODES[429]);
180
- expect(error.isBan).toBe(true);
181
- });
182
-
183
- it("creates a 451 Unavailable For Legal Reasons error", () => {
184
- const error = illegal();
185
- expect(error.status).toBe(451);
186
- expect(error.message).toBe(STATUS_CODES[451]);
187
- expect(error.isBan).toBe(true);
188
- });
189
-
190
- it("accepts a custom message", () => {
191
- const message = "Custom error message";
192
- const error = unauthorized(message);
193
- expect(error.status).toBe(401);
194
- expect(error.message).toBe(message);
195
- });
196
-
197
- it("accepts options object", () => {
198
- const options = {
199
- message: "Custom message",
200
- data: { field: "test" },
201
- headers: { "X-Custom": "value" },
202
- };
203
-
204
- const error = badRequest(options);
205
-
206
- expect(error.status).toBe(400);
207
- expect(error.message).toBe(options.message);
208
- expect(error.data).toEqual(options.data);
209
- expect(error.headers).toEqual(options.headers);
210
- });
211
-
212
- it("accepts message and options separately", () => {
213
- const message = "Custom message";
214
- const options = {
215
- data: { field: "test" },
216
- headers: { "X-Custom": "value" },
217
- };
218
-
219
- const error = notFound(message, options);
220
-
221
- expect(error.status).toBe(404);
222
- expect(error.message).toBe(message);
223
- expect(error.data).toEqual(options.data);
224
- expect(error.headers).toEqual(options.headers);
225
- });
226
- });
227
-
228
- describe("Server error factories", () => {
229
- it("creates a 500 Internal Server Error", () => {
230
- const error = internal();
231
- expect(error.status).toBe(500);
232
- expect(error.message).toBe(STATUS_CODES[500]);
233
- expect(error.isBan).toBe(true);
234
- });
235
-
236
- it("creates a 501 Not Implemented error", () => {
237
- const error = notImplemented();
238
- expect(error.status).toBe(501);
239
- expect(error.message).toBe(STATUS_CODES[501]);
240
- expect(error.isBan).toBe(true);
241
- });
242
-
243
- it("creates a 502 Bad Gateway error", () => {
244
- const error = badGateway();
245
- expect(error.status).toBe(502);
246
- expect(error.message).toBe(STATUS_CODES[502]);
247
- expect(error.isBan).toBe(true);
248
- });
249
-
250
- it("creates a 503 Service Unavailable error", () => {
251
- const error = serverUnavailable();
252
- expect(error.status).toBe(503);
253
- expect(error.message).toBe(STATUS_CODES[503]);
254
- expect(error.isBan).toBe(true);
255
- });
256
-
257
- it("creates a 504 Gateway Timeout error", () => {
258
- const error = gatewayTimeout();
259
- expect(error.status).toBe(504);
260
- expect(error.message).toBe(STATUS_CODES[504]);
261
- expect(error.isBan).toBe(true);
262
- });
263
-
264
- it("creates a 505 HTTP Version Not Supported error", () => {
265
- const error = httpVersionNotSupported();
266
- expect(error.status).toBe(505);
267
- expect(error.message).toBe(STATUS_CODES[505]);
268
- expect(error.isBan).toBe(true);
269
- });
270
-
271
- it("creates a 507 Insufficient Storage error", () => {
272
- const error = insufficientStorage();
273
- expect(error.status).toBe(507);
274
- expect(error.message).toBe(STATUS_CODES[507]);
275
- expect(error.isBan).toBe(true);
276
- });
277
-
278
- it("creates a 508 Loop Detected error", () => {
279
- const error = loopDetected();
280
- expect(error.status).toBe(508);
281
- expect(error.message).toBe(STATUS_CODES[508]);
282
- expect(error.isBan).toBe(true);
283
- });
284
-
285
- it("creates a 506 Variant Also Negotiates error", () => {
286
- const error = variantAlsoNegotiates();
287
- expect(error.status).toBe(506);
288
- expect(error.message).toBe(STATUS_CODES[506]);
289
- expect(error.isBan).toBe(true);
290
- });
291
-
292
- it("creates a 510 Not Extended error", () => {
293
- const error = notExtended();
294
- expect(error.status).toBe(510);
295
- expect(error.message).toBe(STATUS_CODES[510]);
296
- expect(error.isBan).toBe(true);
297
- });
298
-
299
- it("creates a 511 Network Authentication Required error", () => {
300
- const error = networkAuthRequired();
301
- expect(error.status).toBe(511);
302
- expect(error.message).toBe(STATUS_CODES[511]);
303
- expect(error.isBan).toBe(true);
304
- });
305
-
306
- it("accepts a custom message", () => {
307
- const message = "Custom error message";
308
- const error = internal(message);
309
- expect(error.status).toBe(500);
310
- expect(error.message).toBe(message);
311
- });
312
-
313
- it("accepts options object", () => {
314
- const options = {
315
- message: "Custom message",
316
- data: { field: "test" },
317
- headers: { "X-Custom": "value" },
318
- };
319
-
320
- const error = serverUnavailable(options);
321
-
322
- expect(error.status).toBe(503);
323
- expect(error.message).toBe(options.message);
324
- expect(error.data).toEqual(options.data);
325
- expect(error.headers).toEqual(options.headers);
326
- });
327
-
328
- it("accepts message and options separately", () => {
329
- const message = "Custom message";
330
- const options = {
331
- data: { field: "test" },
332
- headers: { "X-Custom": "value" },
333
- };
334
-
335
- const error = internal(message, options);
336
-
337
- expect(error.status).toBe(500);
338
- expect(error.message).toBe(message);
339
- expect(error.data).toEqual(options.data);
340
- expect(error.headers).toEqual(options.headers);
341
- });
342
- });
343
-
344
- describe("badImplementation", () => {
345
- it("creates a developer error", () => {
346
- const error = badImplementation();
347
-
348
- expect(error.status).toBe(500);
349
- expect(error.message).toBe(STATUS_CODES[500]);
350
- expect(error.data).toEqual({ isDeveloperError: true });
351
- });
352
-
353
- it("merges custom data with isDeveloperError flag", () => {
354
- const error = badImplementation({
355
- data: { field: "test" },
356
- });
357
-
358
- // Use type assertion to check the data structure
359
- const data = error.data as { isDeveloperError: boolean; field: string };
360
- expect(data.isDeveloperError).toBe(true);
361
- expect(data.field).toBe("test");
362
- });
363
-
364
- it("accepts message and options separately", () => {
365
- const message = "Developer error";
366
- const options = {
367
- data: { field: "test" },
368
- };
369
-
370
- const error = badImplementation(message, options);
371
-
372
- expect(error.status).toBe(500);
373
- expect(error.message).toBe(message);
374
- // Use type assertion to check the data structure
375
- const data = error.data as { isDeveloperError: boolean; field: string };
376
- expect(data.isDeveloperError).toBe(true);
377
- expect(data.field).toBe("test");
378
- });
379
- });
380
- });
@@ -1,117 +0,0 @@
1
- import { describe, it, expect } from "bun:test";
2
- import { defaultFormatter } from "../../formatters/default";
3
- import { createError } from "../../core/create-error";
4
- import { STATUS_CODES } from "../../constants";
5
-
6
- describe("defaultFormatter", () => {
7
- it("has the correct content type", () => {
8
- expect(defaultFormatter.contentType).toBe("application/json");
9
- });
10
-
11
- it("formats a basic error", () => {
12
- const error = createError({ statusCode: 404, message: "Not found" });
13
- const formatted = defaultFormatter.format(error);
14
-
15
- expect(formatted).toEqual({
16
- statusCode: 404,
17
- payload: {
18
- statusCode: 404,
19
- error: STATUS_CODES[404],
20
- message: "Not found",
21
- },
22
- });
23
- });
24
-
25
- it("passes custom headers to the formatter but doesn't include them in output", () => {
26
- const error = createError({ statusCode: 400 });
27
- const headers = { "X-Custom": "value" };
28
-
29
- const formatted = defaultFormatter.format(error, headers);
30
-
31
- // Headers should not be in the formatted output
32
- expect((formatted as any).headers).toBeUndefined();
33
- });
34
-
35
- it("doesn't add the Allow header to the response body", () => {
36
- const error = createError({
37
- statusCode: 405,
38
- allow: ["GET", "POST"],
39
- });
40
-
41
- const formatted = defaultFormatter.format(error);
42
-
43
- // The formatted output should not have headers
44
- expect((formatted as any).headers).toBeUndefined();
45
- });
46
-
47
- it("includes stack trace when includeStackTrace is true", () => {
48
- const error = createError({ statusCode: 500 });
49
- const formatted = defaultFormatter.format(error, {}, [], true);
50
-
51
- expect(formatted.payload.stack).toBeDefined();
52
- });
53
-
54
- it("includes stack trace for developer errors", () => {
55
- const error = createError({
56
- statusCode: 500,
57
- data: { isDeveloperError: true },
58
- });
59
-
60
- const formatted = defaultFormatter.format(error);
61
-
62
- expect(formatted.payload.stack).toBeDefined();
63
- });
64
-
65
- it("includes cause stack when available", () => {
66
- const cause = new Error("Original error");
67
- const error = createError({
68
- statusCode: 500,
69
- cause,
70
- });
71
-
72
- const formatted = defaultFormatter.format(error, {}, [], true);
73
-
74
- expect(formatted.payload.causeStack).toBeDefined();
75
- });
76
-
77
- it("includes error data when provided", () => {
78
- const data = { field: "email", reason: "invalid format" };
79
- const error = createError({
80
- statusCode: 400,
81
- data,
82
- });
83
-
84
- const formatted = defaultFormatter.format(error);
85
-
86
- expect(formatted.payload.data).toEqual(data);
87
- });
88
-
89
- it("sanitizes specified fields", () => {
90
- const data = {
91
- user: {
92
- id: 123,
93
- password: "secret",
94
- email: "user@example.com",
95
- profile: {
96
- token: "abc123",
97
- },
98
- },
99
- };
100
-
101
- const error = createError({
102
- statusCode: 400,
103
- data,
104
- });
105
-
106
- const formatted = defaultFormatter.format(error, {}, ["password", "token"]);
107
-
108
- // Data should be sanitized
109
- expect(formatted.payload.data).toEqual({
110
- user: {
111
- id: 123,
112
- email: "user@example.com",
113
- profile: {},
114
- },
115
- });
116
- });
117
- });
@@ -1,49 +0,0 @@
1
- # Middleware Tests
2
-
3
- ## Ban Middleware Tests
4
-
5
- The tests for the `ban-middleware.ts` file are currently disabled (renamed to `ban-middleware.test.ts.disabled`) due to a known issue with Bun's testing framework. There's a bug related to mocking in Bun that causes errors when testing middleware.
6
-
7
- ### Error Details
8
-
9
- When running the middleware tests, the following error occurs:
10
-
11
- ```
12
- # Unhandled error between tests
13
- -------------------------------
14
- 1 | (function (entry fetcher)
15
- ^
16
- SyntaxError: export 'ErrorFormatter' not found in '../types'
17
- -------------------------------
18
- ```
19
-
20
- ### Possible Workarounds
21
-
22
- For future implementation, consider these workarounds:
23
-
24
- - Using [bun-bagel](https://github.com/DRFR0ST/bun-bagel)
25
- - Using [bun-mock-fetch](https://github.com/aryzing/bun-mock-fetch)
26
- - Using `spyOn` for global.fetch:
27
-
28
- ```typescript
29
- const mockFetch = spyOn(globalThis, "fetch");
30
-
31
- beforeEach(() => {
32
- mockFetch.mockReset();
33
- });
34
-
35
- afterAll(() => {
36
- mockFetch.mockRestore();
37
- });
38
-
39
- test("fetches example.com", () => {
40
- mockFetch.mockImplementation(async (url, opts) => {
41
- // Test implementation
42
- return new Response('{"success":true}');
43
- });
44
-
45
- // Test code
46
- });
47
- ```
48
-
49
- These tests should be re-enabled when the Bun testing framework is more stable.