mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-08 08:44:37 +08:00
A published bundle's JSDoc can name a type that only its .d.ts declares:
hono's bearerAuth carries `@template E` and `@returns {MiddlewareHandler<E>}`
without importing MiddlewareHandler. The checker types that result as its
error type, and a call of such a generic JavaScript function was refused
with SC2001 ("values of type 'MiddlewareHandler<E>' cannot be compiled").
The unresolved annotation is documentation, not a contract, so the call's
result is now checked-dynamic, as an `any` result of a TypeScript generic
already is.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
44 lines
947 B
JavaScript
44 lines
947 B
JavaScript
/**
|
|
* Guard middleware.
|
|
*
|
|
* @template E - The environment type.
|
|
* @param {GuardOptions} options - The options.
|
|
* @returns {Middleware<E>} The middleware.
|
|
*/
|
|
export const guard = (options) => {
|
|
return async function guard(ctx, next) {
|
|
if (ctx.token !== options.token) {
|
|
ctx.log.push(`denied ${ctx.path}`);
|
|
return;
|
|
}
|
|
ctx.log.push(`allowed ${ctx.path}`);
|
|
await next();
|
|
};
|
|
};
|
|
|
|
export class Router {
|
|
constructor() {
|
|
this.routes = [];
|
|
}
|
|
|
|
on(path, ...handlers) {
|
|
this.routes.push({ path, handlers });
|
|
return this;
|
|
}
|
|
|
|
async dispatch(path, token) {
|
|
const log = [];
|
|
for (const route of this.routes) {
|
|
if (route.path !== path) continue;
|
|
const ctx = { path, token, log };
|
|
let index = 0;
|
|
const next = async () => {
|
|
const handler = route.handlers[index++];
|
|
if (handler) await handler(ctx, next);
|
|
};
|
|
await next();
|
|
}
|
|
return log;
|
|
}
|
|
}
|