0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-21 18:18:57 +01:00

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
This commit is contained in:
EdamAmex 2024-10-30 17:58:08 +09:00 committed by GitHub
parent 7a9e387af6
commit caeecf99a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 57 additions and 28 deletions

View File

@ -1,4 +1,28 @@
declare namespace Deno { declare namespace Deno {
interface FileHandleLike {
readonly readable: ReadableStream<Uint8Array>
}
/**
* Open the file using the specified path.
*
* @param path The path to open the file.
* @returns FileHandle object.
*/
export function open(path: string): Promise<FileHandleLike>
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. * 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. * Write a new file, with the specified path and data.
* *
* @param path The path to the file to write. * @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. * @returns A promise that resolves when the file is written.
*/ */
export function writeFile(path: string, data: Uint8Array): Promise<void> export function writeFile(path: string, data: Uint8Array): Promise<void>
/**
* Errors of Deno
*/
export const errors: Record<string, Function>
export function upgradeWebSocket( export function upgradeWebSocket(
req: Request, req: Request,
// eslint-disable-next-line @typescript-eslint/no-explicit-any options: UpgradeWebSocketOptions
options: any
): { ): {
response: Response response: Response
socket: WebSocket 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
}
} }

View File

@ -2,8 +2,6 @@ import type { ServeStaticOptions } from '../../middleware/serve-static'
import { serveStatic as baseServeStatic } from '../../middleware/serve-static' import { serveStatic as baseServeStatic } from '../../middleware/serve-static'
import type { Env, MiddlewareHandler } from '../../types' import type { Env, MiddlewareHandler } from '../../types'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const { open, lstatSync, errors } = Deno const { open, lstatSync, errors } = Deno
export const serveStatic = <E extends Env = Env>( export const serveStatic = <E extends Env = Env>(
@ -13,12 +11,12 @@ export const serveStatic = <E extends Env = Env>(
const getContent = async (path: string) => { const getContent = async (path: string) => {
try { try {
const file = await open(path) const file = await open(path)
// eslint-disable-next-line @typescript-eslint/no-explicit-any return file.readable
return file ? (file.readable as any) : null
} catch (e) { } catch (e) {
if (!(e instanceof errors.NotFound)) { if (!(e instanceof errors.NotFound)) {
console.warn(`${e}`) console.warn(`${e}`)
} }
return null
} }
} }
const pathResolve = (path: string) => { const pathResolve = (path: string) => {

View File

@ -1,27 +1,7 @@
import type { UpgradeWebSocket, WSReadyState } from '../../helper/websocket' import type { UpgradeWebSocket, WSReadyState } from '../../helper/websocket'
import { WSContext, defineWebSocketHelper } from '../../helper/websocket' import { WSContext, defineWebSocketHelper } from '../../helper/websocket'
export interface UpgradeWebSocketOptions { export const upgradeWebSocket: UpgradeWebSocket<WebSocket, Deno.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<WebSocket, UpgradeWebSocketOptions> =
defineWebSocketHelper(async (c, events, options) => { defineWebSocketHelper(async (c, events, options) => {
if (c.req.header('upgrade') !== 'websocket') { if (c.req.header('upgrade') !== 'websocket') {
return return