Restructure first: split the two oversized files by cohesion (http.ts 449
→ http/{router,request,stream,ws,server,types}; app.ts 556 → app/{types,
introspect,index}), rename cryptic locals, JSDoc the public API, and
extract helpers so no function exceeds the complexity gate (createApp
187 → <15, listen 113 → 6). Public import paths are unchanged (barrels).
On that structure, five features that close the biggest gaps vs
Express/Fastify/Nest for a comparable beta:
- routing: `:name*` catch-all, specificity precedence (static ▸ param ▸
catch-all, not registration order), and 405 + Allow when a path exists
under a different method (was 404)
- visible degradation: optional providers that fail boot are summarized on
listen() and queryable via app.degraded(), instead of a silent warn
- mesh is alpha — gated behind `experimental: true`; createApp throws if
`mesh` is configured without it
- multipart moves to the optional `busboy` peer dependency (clear 501 when
absent), keeping the core at one runtime dependency
- `@header('name')` as a singular alias of `@headers`
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import type { StandardIssue } from './standard-schema';
|
|
|
|
/** Error carrying an HTTP status code and optional response body. Base of all signals. */
|
|
export class HttpError extends Error {
|
|
constructor(
|
|
readonly status: number,
|
|
message?: string,
|
|
readonly body?: unknown,
|
|
) {
|
|
super(message ?? `HTTP ${status}`);
|
|
this.name = new.target.name;
|
|
}
|
|
}
|
|
|
|
/** 401 Unauthorized. */
|
|
export class Unauthorized extends HttpError {
|
|
constructor(message?: string) {
|
|
super(401, message);
|
|
}
|
|
}
|
|
|
|
/** 404 Not Found. */
|
|
export class NotFound extends HttpError {
|
|
constructor(message?: string) {
|
|
super(404, message);
|
|
}
|
|
}
|
|
|
|
/** 304 Not Modified. */
|
|
export class NotModified extends HttpError {
|
|
constructor() {
|
|
super(304);
|
|
}
|
|
}
|
|
|
|
/** 302 redirect to `location`. */
|
|
export class Redirect extends HttpError {
|
|
constructor(readonly location: string) {
|
|
super(302);
|
|
}
|
|
}
|
|
|
|
/** 422 raised when request input fails Standard Schema validation. */
|
|
export class ValidationError extends HttpError {
|
|
constructor(
|
|
public issues: StandardIssue[],
|
|
public source: string,
|
|
) {
|
|
super(422, 'Validation failed');
|
|
}
|
|
}
|
|
|
|
/** Type guard: true if `error` is an {@link HttpError}. */
|
|
export function isHttpError(error: unknown): error is HttpError {
|
|
return error instanceof HttpError;
|
|
}
|