From caeecf99a871d00b21cd4d5708ef277f3df709e0 Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Wed, 30 Oct 2024 17:58:08 +0900 Subject: [PATCH] fix(types): remove `any` and fix types of adapter/deno (#3291) * fix(types): remove any and fix types of adapter/deno * fix some errors * refactor jsdoc --- src/adapter/deno/deno.d.ts | 57 ++++++++++++++++++++++++++++++-- src/adapter/deno/serve-static.ts | 6 ++-- src/adapter/deno/websocket.ts | 22 +----------- 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/src/adapter/deno/deno.d.ts b/src/adapter/deno/deno.d.ts index bebe8e96..38eaf320 100644 --- a/src/adapter/deno/deno.d.ts +++ b/src/adapter/deno/deno.d.ts @@ -1,4 +1,28 @@ declare namespace Deno { + interface FileHandleLike { + readonly readable: ReadableStream + } + + /** + * Open the file using the specified path. + * + * @param path The path to open the file. + * @returns FileHandle object. + */ + export function open(path: string): Promise + + interface StatsLike { + isDirectory: boolean + } + + /** + * Get stats with the specified path. + * + * @param path The path to get stats. + * @returns Stats object. + */ + export function lstatSync(path: string): StatsLike + /** * Creates a new directory with the specified path. * @@ -12,17 +36,44 @@ declare namespace Deno { * Write a new file, with the specified path and data. * * @param path The path to the file to write. - * @param data The data to write to the file. + * @param data The data to write into the file. * @returns A promise that resolves when the file is written. */ export function writeFile(path: string, data: Uint8Array): Promise + /** + * Errors of Deno + */ + export const errors: Record + export function upgradeWebSocket( req: Request, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - options: any + options: UpgradeWebSocketOptions ): { response: Response socket: WebSocket } + + /** + * Options of `upgradeWebSocket` + */ + export interface UpgradeWebSocketOptions { + /** + * Sets the `.protocol` property on the client-side web socket to the + * value provided here, which should be one of the strings specified in the + * `protocols` parameter when requesting the web socket. This is intended + * for clients and servers to specify sub-protocols to use to communicate to + * each other. + */ + protocol?: string + /** + * If the client does not respond to this frame with a + * `pong` within the timeout specified, the connection is deemed + * unhealthy and is closed. The `close` and `error` events will be emitted. + * + * The unit is seconds, with a default of 30. + * Set to `0` to disable timeouts. + */ + idleTimeout?: number + } } diff --git a/src/adapter/deno/serve-static.ts b/src/adapter/deno/serve-static.ts index 0a575dfe..0e28424d 100644 --- a/src/adapter/deno/serve-static.ts +++ b/src/adapter/deno/serve-static.ts @@ -2,8 +2,6 @@ import type { ServeStaticOptions } from '../../middleware/serve-static' import { serveStatic as baseServeStatic } from '../../middleware/serve-static' import type { Env, MiddlewareHandler } from '../../types' -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore const { open, lstatSync, errors } = Deno export const serveStatic = ( @@ -13,12 +11,12 @@ export const serveStatic = ( const getContent = async (path: string) => { try { const file = await open(path) - // eslint-disable-next-line @typescript-eslint/no-explicit-any - return file ? (file.readable as any) : null + return file.readable } catch (e) { if (!(e instanceof errors.NotFound)) { console.warn(`${e}`) } + return null } } const pathResolve = (path: string) => { diff --git a/src/adapter/deno/websocket.ts b/src/adapter/deno/websocket.ts index a99bf8dd..d50ad7f1 100644 --- a/src/adapter/deno/websocket.ts +++ b/src/adapter/deno/websocket.ts @@ -1,27 +1,7 @@ import type { UpgradeWebSocket, WSReadyState } from '../../helper/websocket' import { WSContext, defineWebSocketHelper } from '../../helper/websocket' -export interface UpgradeWebSocketOptions { - /** - * Sets the `.protocol` property on the client side web socket to the - * value provided here, which should be one of the strings specified in the - * `protocols` parameter when requesting the web socket. This is intended - * for clients and servers to specify sub-protocols to use to communicate to - * each other. - */ - protocol?: string - /** - * If the client does not respond to this frame with a - * `pong` within the timeout specified, the connection is deemed - * unhealthy and is closed. The `close` and `error` event will be emitted. - * - * The unit is seconds, with a default of 30. - * Set to `0` to disable timeouts. - */ - idleTimeout?: number -} - -export const upgradeWebSocket: UpgradeWebSocket = +export const upgradeWebSocket: UpgradeWebSocket = defineWebSocketHelper(async (c, events, options) => { if (c.req.header('upgrade') !== 'websocket') { return