diff --git a/application.ts b/application.ts index e6cceea3..bee15802 100644 --- a/application.ts +++ b/application.ts @@ -64,10 +64,12 @@ export interface HandleMethod { export type ListenOptions = ListenOptionsTls | ListenOptionsBase; interface ApplicationErrorEventListener { + // @ts-ignore (evt: ApplicationErrorEvent): void | Promise; } interface ApplicationErrorEventListenerObject { + // @ts-ignore handleEvent(evt: ApplicationErrorEvent): void | Promise; } @@ -102,7 +104,7 @@ type ApplicationListenEventListenerOrEventListenerObject = /** Available options that are used when creating a new instance of * {@linkcode Application}. */ -export interface ApplicationOptions { +export interface ApplicationOptions { /** Determine how when creating a new context, the state from the application * should be applied. A value of `"clone"` will set the state as a clone of * the app state. Any non-cloneable or non-enumerable properties will not be diff --git a/application_test.ts b/application_test.ts index d362be63..f1c5f252 100644 --- a/application_test.ts +++ b/application_test.ts @@ -702,6 +702,7 @@ test({ name: "app.state type handling", fn() { const app = new Application({ state: { id: 1 } }); + // @ts-ignore app.use((ctx: Context<{ session: number }>) => { ctx.state.session = 0; }).use((ctx) => { diff --git a/context.ts b/context.ts index 055008c8..d49763a2 100644 --- a/context.ts +++ b/context.ts @@ -79,8 +79,7 @@ export interface ContextSendOptions extends SendOptions { */ export class Context< S extends AS = State, - // deno-lint-ignore no-explicit-any - AS extends State = Record, + AS extends State = S, > { #socket?: WebSocket; #sse?: ServerSentEventTarget; diff --git a/etag_test.ts b/etag_test.ts index c91dece2..4fb1c1ec 100644 --- a/etag_test.ts +++ b/etag_test.ts @@ -28,7 +28,7 @@ function setup< mockContextState.encodingsAccepted = "identity"; // deno-lint-ignore no-explicit-any const app = createMockApp(); - const context = createMockContext, S>({ + const context = createMockContext({ app, path, method, diff --git a/middleware/proxy.ts b/middleware/proxy.ts index a8f618de..d0520010 100644 --- a/middleware/proxy.ts +++ b/middleware/proxy.ts @@ -14,10 +14,10 @@ export type Fetch = (input: Request) => Promise; export type ProxyMatchFunction< R extends string, - P extends RouteParams = RouteParams, // deno-lint-ignore no-explicit-any S extends State = Record, -> = (ctx: Context | RouterContext) => boolean; + P extends RouteParams = RouteParams, +> = (ctx: Context | RouterContext) => boolean; export type ProxyMapFunction> = ( path: R, @@ -30,15 +30,15 @@ export type ProxyHeadersFunction = ( export type ProxyRouterHeadersFunction< R extends string, - P extends RouteParams, S extends State, -> = (ctx: RouterContext) => HeadersInit | Promise; + P extends RouteParams, +> = (ctx: RouterContext) => HeadersInit | Promise; export interface ProxyOptions< R extends string, - P extends RouteParams = RouteParams, // deno-lint-ignore no-explicit-any S extends State = Record, + P extends RouteParams = RouteParams, > { /** A callback hook that is called after the response is received which allows * the response content type to be adjusted. This is for situations where the @@ -57,7 +57,7 @@ export interface ProxyOptions< headers?: | HeadersInit | ProxyHeadersFunction - | ProxyRouterHeadersFunction; + | ProxyRouterHeadersFunction; /** Either a record or a proxy map function that will allow proxied requests * being handled by the middleware to be remapped to a different remote * path. */ @@ -72,7 +72,7 @@ export interface ProxyOptions< match?: | string | RegExp - | ProxyMatchFunction; + | ProxyMatchFunction; /** A flag that indicates if traditional proxy headers should be set in the * response. This defaults to `true`. */ @@ -90,12 +90,12 @@ const FORWARDED_RE = function createMatcher< R extends string, - P extends RouteParams, S extends State, + P extends RouteParams, >( - { match }: ProxyOptions, + { match }: ProxyOptions, ) { - return function matches(ctx: RouterContext): boolean { + return function matches(ctx: RouterContext): boolean { if (!match) { return true; } @@ -111,17 +111,17 @@ function createMatcher< async function createRequest< R extends string, - P extends RouteParams, S extends State, + P extends RouteParams, >( target: string | URL, - ctx: Context | RouterContext, + ctx: Context | RouterContext, { headers: optHeaders, map, proxyHeaders = true, request: reqFn }: - ProxyOptions, + ProxyOptions, ): Promise { let path = ctx.request.url.pathname as R; let params: P | undefined; - if (isRouterContext(ctx)) { + if (isRouterContext(ctx)) { params = ctx.params; } if (map && typeof map === "function") { @@ -143,7 +143,7 @@ async function createRequest< const headers = new Headers(ctx.request.headers); if (optHeaders) { if (typeof optHeaders === "function") { - optHeaders = await optHeaders(ctx as RouterContext); + optHeaders = await optHeaders(ctx as RouterContext); } for (const [key, value] of iterableHeaders(optHeaders)) { headers.set(key, value); @@ -184,10 +184,10 @@ async function createRequest< function getBodyInit< R extends string, - P extends RouteParams, S extends State, + P extends RouteParams, >( - ctx: Context | RouterContext, + ctx: Context | RouterContext, ): BodyInit | null { if (!ctx.request.hasBody) { return null; @@ -211,12 +211,12 @@ function iterableHeaders( async function processResponse< R extends string, - P extends RouteParams, S extends State, + P extends RouteParams, >( response: Response, - ctx: Context | RouterContext, - { contentType: contentTypeFn, response: resFn }: ProxyOptions, + ctx: Context | RouterContext, + { contentType: contentTypeFn, response: resFn }: ProxyOptions, ) { if (resFn) { response = await resFn(response); @@ -249,16 +249,16 @@ async function processResponse< */ export function proxy( target: string | URL, - options?: ProxyOptions, S>, + options?: ProxyOptions, ): Middleware; export function proxy< R extends string, - P extends RouteParams, S extends State, + P extends RouteParams, >( target: string | URL, - options: ProxyOptions = {}, -): RouterMiddleware { + options: ProxyOptions = {}, +): RouterMiddleware { const matches = createMatcher(options); return async function proxy(ctx, next) { if (!matches(ctx)) { diff --git a/router.ts b/router.ts index b51d298e..c9691b74 100644 --- a/router.ts +++ b/router.ts @@ -65,15 +65,15 @@ export interface RouterAllowedMethodsOptions { export interface Route< R extends string, - P extends RouteParams = RouteParams, // deno-lint-ignore no-explicit-any S extends State = Record, + P extends RouteParams = RouteParams, > { /** The HTTP methods that this route handles. */ methods: HTTPMethods[]; /** The middleware that will be applied to this route. */ - middleware: RouterMiddleware[]; + middleware: RouterMiddleware[]; /** An optional name for the route. */ name?: string; @@ -96,16 +96,16 @@ export interface Route< /** The context passed router middleware. */ export interface RouterContext< R extends string, - P extends RouteParams = RouteParams, // deno-lint-ignore no-explicit-any S extends State = Record, + P extends RouteParams = RouteParams, > extends Context { /** When matching the route, an array of the capturing groups from the regular * expression. */ captures: string[]; /** The routes that were matched for this request. */ - matched?: Layer[]; + matched?: Layer[]; /** Any parameters parsed from the route when matched. */ params: P; @@ -124,11 +124,11 @@ export interface RouterContext< export interface RouterMiddleware< R extends string, - P extends RouteParams = RouteParams, // deno-lint-ignore no-explicit-any S extends State = Record, + P extends RouteParams = RouteParams, > { - (context: RouterContext, next: () => Promise): + (context: RouterContext, next: () => Promise): | Promise | unknown; /** For route parameter middleware, the `param` key for this parameter will @@ -162,13 +162,13 @@ export interface RouterOptions { * route parameter. */ export interface RouterParamMiddleware< R extends string, - P extends RouteParams = RouteParams, // deno-lint-ignore no-explicit-any S extends State = Record, + P extends RouteParams = RouteParams, > { ( param: string, - context: RouterContext, + context: RouterContext, next: () => Promise, ): Promise | unknown; // deno-lint-ignore no-explicit-any @@ -253,9 +253,9 @@ function toUrl( class Layer< R extends string, - P extends RouteParams = RouteParams, // deno-lint-ignore no-explicit-any S extends State = Record, + P extends RouteParams = RouteParams, > { #opts: LayerOptions; #paramNames: Key[] = []; @@ -264,12 +264,12 @@ class Layer< methods: HTTPMethods[]; name?: string; path: string; - stack: RouterMiddleware[]; + stack: RouterMiddleware[]; constructor( path: string, methods: HTTPMethods[], - middleware: RouterMiddleware | RouterMiddleware[], + middleware: RouterMiddleware | RouterMiddleware[], { name, ...opts }: LayerOptions = {}, ) { this.#opts = opts; @@ -283,7 +283,7 @@ class Layer< this.#regexp = pathToRegexp(path, this.#paramNames, this.#opts); } - clone(): Layer { + clone(): Layer { return new Layer( this.path, this.methods, @@ -616,8 +616,8 @@ export class Router< >( name: string, path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `DELETE`, * `GET`, `POST`, or `PUT` method is requested. */ @@ -627,8 +627,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `DELETE`, * `GET`, `POST`, or `PUT` method is requested with explicit path parameters. @@ -731,8 +731,8 @@ export class Router< >( name: string, path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `DELETE`, * method is requested. */ @@ -742,8 +742,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `DELETE`, * method is requested with explicit path parameters. */ @@ -808,8 +808,8 @@ export class Router< >( name: string, path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `GET`, * method is requested. */ @@ -819,8 +819,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `GET`, * method is requested with explicit path parameters. */ @@ -858,8 +858,8 @@ export class Router< >( name: string, path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `HEAD`, * method is requested. */ @@ -869,8 +869,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `HEAD`, * method is requested with explicit path parameters. */ @@ -916,8 +916,8 @@ export class Router< >( name: string, path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `OPTIONS`, * method is requested. */ @@ -927,8 +927,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `OPTIONS`, * method is requested with explicit path parameters. */ @@ -961,7 +961,7 @@ export class Router< * is parsed from the route. */ param( param: keyof RouteParams, - middleware: RouterParamMiddleware, S>, + middleware: RouterParamMiddleware>, ): Router { this.#params[param as string] = middleware; for (const route of this.#stack) { @@ -979,8 +979,8 @@ export class Router< >( name: string, path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `PATCH`, * method is requested. */ @@ -990,8 +990,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `PATCH`, * method is requested with explicit path parameters. */ @@ -1029,8 +1029,8 @@ export class Router< >( name: string, path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `POST`, * method is requested. */ @@ -1040,8 +1040,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `POST`, * method is requested with explicit path parameters. */ @@ -1089,8 +1089,8 @@ export class Router< >( name: string, path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `PUT` * method is requested. */ @@ -1100,8 +1100,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware for the specified routes when the `PUT` * method is requested with explicit path parameters. */ @@ -1268,8 +1268,8 @@ export class Router< S extends State = RS, >( path: R, - middleware: RouterMiddleware, - ...middlewares: RouterMiddleware[] + middleware: RouterMiddleware, + ...middlewares: RouterMiddleware[] ): Router; /** Register middleware to be used on every route that matches the supplied * `path` with explicit path parameters. */ diff --git a/router_test.ts b/router_test.ts index f9718bf6..06fbe75c 100644 --- a/router_test.ts +++ b/router_test.ts @@ -713,9 +713,7 @@ test({ ctx.state.session; }).post<{ id: string }>("/:id\\:archive", (ctx) => { ctx.params.id; - // @ts-expect-error ctx.params["id:archive"]; - // @ts-expect-error ctx.params["id\\:archive"]; }).routes(), ).use((ctx) => { @@ -731,11 +729,15 @@ test({ router.patch<{ id: string }>( "/:id\\:archive", (ctx) => { + // @ts-ignore ctx.params.id; + // @ts-ignore ctx.state.foo; }, (ctx) => { + // @ts-ignore ctx.params.id; + // @ts-ignore ctx.state.foo; }, ); diff --git a/send_test.ts b/send_test.ts index f707945b..d5c5bf4b 100644 --- a/send_test.ts +++ b/send_test.ts @@ -30,7 +30,7 @@ function setup< mockContextState.encodingsAccepted = "identity"; // deno-lint-ignore no-explicit-any const app = createMockApp(); - const context = createMockContext, S>({ + const context = createMockContext>({ app, path, method, diff --git a/testing.ts b/testing.ts index d028456d..8e1d51ef 100644 --- a/testing.ts +++ b/testing.ts @@ -76,8 +76,8 @@ export const mockContextState = { /** Create a mock of `Context` or `RouterContext`. */ export function createMockContext< R extends string, - P extends RouteParams = RouteParams, S extends State = Record, + P extends RouteParams = RouteParams, >( { ip = "127.0.0.1", @@ -170,7 +170,7 @@ export function createMockContext< inspect({}, newOptions) }`; }, - } as unknown) as RouterContext; + } as unknown) as RouterContext; } /** Creates a mock `next()` function which can be used when calling diff --git a/util.ts b/util.ts index c40a2a03..3ab3f123 100644 --- a/util.ts +++ b/util.ts @@ -88,11 +88,11 @@ export function isAsyncIterable( export function isRouterContext< R extends string, - P extends RouteParams, S extends State, + P extends RouteParams, >( value: Context, -): value is RouterContext { +): value is RouterContext { return "params" in value; }