burger-api 1.0.0-beta.1 → 1.0.0-beta.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.
@@ -124,17 +124,19 @@ function schemaToJsonSchema(schema, mapJsonSchema, io = 'output') {
124
124
  }
125
125
  /**
126
126
  * Builds a request body object for OpenAPI based on a SchemaInput.
127
+ *
128
+ * A schema with no JSON Schema available (e.g. a Standard Schema with no
129
+ * configured converter) still emits a body with `schema: {}`, matching how
130
+ * parameters fall back — never a silent omission.
127
131
  */
128
132
  function buildRequestBody(zodSchema, mapJsonSchema) {
129
133
  if (!(zodSchema instanceof ZodType) && !isStandardSchema(zodSchema))
130
134
  return undefined;
131
135
  const jsonSchema = schemaToJsonSchema(zodSchema, mapJsonSchema, 'input');
132
- if (!jsonSchema)
133
- return undefined;
134
136
  return {
135
137
  content: {
136
138
  'application/json': {
137
- schema: jsonSchema,
139
+ schema: jsonSchema ?? {},
138
140
  },
139
141
  },
140
142
  description: 'Request body',
@@ -1,4 +1,5 @@
1
1
  import { HTTPError } from './http-error.js';
2
+ import type { HTTPErrorOptions } from './http-error.js';
2
3
  /**
3
4
  * 403 Forbidden — thrown when the user is authenticated but lacks permission.
4
5
  *
@@ -10,5 +11,5 @@ import { HTTPError } from './http-error.js';
10
11
  export declare class ForbiddenError extends HTTPError {
11
12
  readonly name = "ForbiddenError";
12
13
  readonly status: 403;
13
- constructor(message?: string, options?: ErrorOptions);
14
+ constructor(message?: string, options?: HTTPErrorOptions);
14
15
  }
@@ -6,10 +6,17 @@
6
6
  * Details. Subclasses: ValidationError (422), NotFoundError (404),
7
7
  * UnauthorizedError (401), ForbiddenError (403), MethodNotAllowedError (405).
8
8
  */
9
+ /** Options accepted by `HTTPError` and its subclasses. */
10
+ export type HTTPErrorOptions = ErrorOptions & {
11
+ /** Response headers rendered with the error (e.g. `WWW-Authenticate`). */
12
+ headers?: HeadersInit;
13
+ };
9
14
  export declare class HTTPError extends Error {
10
15
  /** HTTP status code for this error. */
11
16
  readonly status: number;
12
- constructor(status: number, message: string, options?: ErrorOptions);
17
+ /** Response headers rendered with the error, when provided. */
18
+ readonly headers?: HeadersInit;
19
+ constructor(status: number, message: string, options?: HTTPErrorOptions);
13
20
  }
14
21
  /**
15
22
  * Logs a 5xx error that no user `onError` hook handled. Server-side only:
@@ -1,12 +1,18 @@
1
1
  import { resolveDebug } from '../utils/env.js';
2
2
  /**
3
- * Base HTTP error class for BurgerAPI.
4
- *
5
- * All framework errors extend this; each subclass sets a static `status` and a
6
- * default `name`. The `onError` pipeline renders them as RFC 9457 Problem
7
- * Details. Subclasses: ValidationError (422), NotFoundError (404),
8
- * UnauthorizedError (401), ForbiddenError (403), MethodNotAllowedError (405).
3
+ * Cross-copy brand. A second installed copy of this package has a different
4
+ * `HTTPError` class, so `instanceof` is false even for a genuine error —
5
+ * ecosystem plugins import `burger-api` themselves and can resolve to another
6
+ * copy of the framework. `Symbol.for` is registry-global, so the brand still
7
+ * identifies the error on both sides.
9
8
  */
9
+ const HTTP_ERROR_BRAND = Symbol.for('burger-api.http-error');
10
+ /** True for an `HTTPError` from any copy of this package. */
11
+ function isHTTPError(value) {
12
+ return (value !== null &&
13
+ typeof value === 'object' &&
14
+ value[HTTP_ERROR_BRAND] === true);
15
+ }
10
16
  export class HTTPError extends Error {
11
17
  /** HTTP status code for this error. */
12
18
  status;
@@ -14,6 +20,11 @@ export class HTTPError extends Error {
14
20
  super(message, options);
15
21
  this.status = status;
16
22
  this.name = 'HTTPError';
23
+ // Non-enumerable own brand (see HTTP_ERROR_BRAND).
24
+ Object.defineProperty(this, HTTP_ERROR_BRAND, { value: true });
25
+ if (options?.headers !== undefined) {
26
+ this.headers = options.headers;
27
+ }
17
28
  }
18
29
  }
19
30
  /**
@@ -103,7 +114,7 @@ export function renderUncaught(error, request, debug) {
103
114
  * `title`/`detail`.
104
115
  */
105
116
  export function renderHTTPError(error, isDev, extras) {
106
- const httpError = error instanceof HTTPError ||
117
+ const httpError = isHTTPError(error) ||
107
118
  (error !== null &&
108
119
  typeof error === 'object' &&
109
120
  'status' in error &&
@@ -122,7 +133,7 @@ export function renderHTTPError(error, isDev, extras) {
122
133
  // may hold secrets, so its 5xx renders the bare status title.
123
134
  const hide = !isDev &&
124
135
  status >= 500 &&
125
- (status === 500 || !(error instanceof HTTPError));
136
+ (status === 500 || !isHTTPError(error));
126
137
  const genericTitle = STATUS_TITLES[status] ?? 'Internal Server Error';
127
138
  const problem = {
128
139
  type: 'about:blank',
@@ -148,8 +159,11 @@ export function renderHTTPError(error, isDev, extras) {
148
159
  : { message: String(httpError.cause) };
149
160
  }
150
161
  }
151
- return new Response(JSON.stringify(problem), {
152
- status,
153
- headers: { 'Content-Type': 'application/problem+json' },
154
- });
162
+ // Caller-supplied headers come from real HTTPError instances only; a
163
+ // duck-typed thrown object must not inject response headers. The brand
164
+ // makes this work across copies of the package.
165
+ const headers = new Headers(isHTTPError(error) ? error.headers : undefined);
166
+ // RFC 9457 media type is set last so it always wins.
167
+ headers.set('Content-Type', 'application/problem+json');
168
+ return new Response(JSON.stringify(problem), { status, headers });
155
169
  }
@@ -1,4 +1,5 @@
1
1
  import { HTTPError } from './http-error.js';
2
+ import type { HTTPErrorOptions } from './http-error.js';
2
3
  /**
3
4
  * 405 Method Not Allowed — thrown when the HTTP method is not supported.
4
5
  *
@@ -9,5 +10,5 @@ import { HTTPError } from './http-error.js';
9
10
  export declare class MethodNotAllowedError extends HTTPError {
10
11
  readonly name = "MethodNotAllowedError";
11
12
  readonly status: 405;
12
- constructor(message?: string, options?: ErrorOptions);
13
+ constructor(message?: string, options?: HTTPErrorOptions);
13
14
  }
@@ -1,4 +1,5 @@
1
1
  import { HTTPError } from './http-error.js';
2
+ import type { HTTPErrorOptions } from './http-error.js';
2
3
  /**
3
4
  * 404 Not Found — thrown when a requested resource does not exist.
4
5
  *
@@ -10,5 +11,5 @@ import { HTTPError } from './http-error.js';
10
11
  export declare class NotFoundError extends HTTPError {
11
12
  readonly name = "NotFoundError";
12
13
  readonly status: 404;
13
- constructor(message?: string, options?: ErrorOptions);
14
+ constructor(message?: string, options?: HTTPErrorOptions);
14
15
  }
@@ -1,4 +1,5 @@
1
1
  import { HTTPError } from './http-error.js';
2
+ import type { HTTPErrorOptions } from './http-error.js';
2
3
  /**
3
4
  * 401 Unauthorized — thrown when authentication is missing or invalid.
4
5
  *
@@ -10,5 +11,5 @@ import { HTTPError } from './http-error.js';
10
11
  export declare class UnauthorizedError extends HTTPError {
11
12
  readonly name = "UnauthorizedError";
12
13
  readonly status: 401;
13
- constructor(message?: string, options?: ErrorOptions);
14
+ constructor(message?: string, options?: HTTPErrorOptions);
14
15
  }
@@ -9,6 +9,7 @@
9
9
  * schema internals.
10
10
  */
11
11
  import { HTTPError } from '../errors/http-error.js';
12
+ import type { HTTPErrorOptions } from '../errors/http-error.js';
12
13
  import type { ValidationIssue, ValidationResult, ValidatorConfig, ValidationSlot } from './types.js';
13
14
  /**
14
15
  * A structured validation error thrown when request validation fails.
@@ -26,7 +27,7 @@ export declare class ValidationError extends HTTPError {
26
27
  readonly issues: ValidationIssue[];
27
28
  /** Per-slot grouping of issues, when available. */
28
29
  readonly errorsBySlot?: Record<string, ValidationIssue[]>;
29
- constructor(slot: ValidationSlot | 'response', issues: ValidationIssue[], options?: ErrorOptions & {
30
+ constructor(slot: ValidationSlot | 'response', issues: ValidationIssue[], options?: HTTPErrorOptions & {
30
31
  errorsBySlot?: Record<string, ValidationIssue[]>;
31
32
  /** Custom status code; defaults to 422 (ValidatorConfig.status). */
32
33
  status?: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "burger-api",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.2",
4
4
  "description": "Fast, file-based API framework for Bun with hooks, Standard Schema validation, OpenAPI and WebSockets. Runs on Bun, Node, Cloudflare Workers, Deno and Vercel.",
5
5
  "author": "Isfhan Ahmed",
6
6
  "type": "module",
@@ -50,7 +50,7 @@
50
50
  "prepare": "bun run build",
51
51
  "test": "bun run test:examples && bun run test:ecosystem && bun run test:lifecycle && bun run test:context && bun run test:utils && bun run test:chain && bun run test:plugin && bun run test:router && bun run test:core && bun run test:errors && bun run test:validation && bun run test:compiler && bun run test:adapter && bun run test:provider && bun run test:smoke && bun run test:ws",
52
52
  "test:examples": "bun test --timeout 30000 --max-concurrency 1 ./examples",
53
- "test:ecosystem": "bun test --timeout 60000 --max-concurrency 1 test/ecosystem-smoke.test.ts test/ecosystem/u16-hardening.test.ts test/ecosystem/correctness.test.ts",
53
+ "test:ecosystem": "bun test --timeout 60000 --max-concurrency 1 test/ecosystem-smoke.test.ts test/ecosystem/u16-hardening.test.ts test/ecosystem/correctness.test.ts test/ecosystem/auth-before-validation.test.ts test/ecosystem/onrequest-response-hooks.test.ts",
54
54
  "test:lifecycle": "bun test --timeout 30000 ./test/lifecycle",
55
55
  "test:context": "bun test --timeout 30000 ./test/context",
56
56
  "test:utils": "bun test --timeout 30000 ./test/utils",