diff --git a/deno_dist/context.ts b/deno_dist/context.ts index 99fe18c98..ce647e4cf 100644 --- a/deno_dist/context.ts +++ b/deno_dist/context.ts @@ -1,6 +1,6 @@ import type { HonoRequest } from './request.ts' -import type { Env, FetchEventLike, NotFoundHandler, Input, TypedResponse } from './types.ts' -import { resolveCallback, HtmlEscapedCallbackPhase } from './utils/html.ts' +import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types.ts' +import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html.ts' import type { RedirectStatusCode, StatusCode } from './utils/http-status.ts' import type { JSONValue, JSONParsed, IsAny, Simplify } from './utils/types.ts' @@ -224,7 +224,14 @@ export class Context< */ render: Renderer = (...args) => this.renderer(...args) - setLayout = (layout: Layout) => (this.layout = layout) + setLayout = ( + layout: Layout + ): Layout< + PropsForRenderer & { + Layout: Layout + } + > => (this.layout = layout) + getLayout = () => this.layout /** diff --git a/deno_dist/helper/accepts/accepts.ts b/deno_dist/helper/accepts/accepts.ts index 9800874c0..bdf64fd93 100644 --- a/deno_dist/helper/accepts/accepts.ts +++ b/deno_dist/helper/accepts/accepts.ts @@ -25,7 +25,7 @@ export interface acceptsOptions extends acceptsConfig { match?: (accepts: Accept[], config: acceptsConfig) => string } -export const parseAccept = (acceptHeader: string) => { +export const parseAccept = (acceptHeader: string): Accept[] => { // Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 const accepts = acceptHeader.split(',') // ['text/html', 'application/xhtml+xml', 'application/xml;q=0.9', 'image/webp', '*/*;q=0.8'] return accepts.map((accept) => { @@ -42,7 +42,7 @@ export const parseAccept = (acceptHeader: string) => { }) } -export const defaultMatch = (accepts: Accept[], config: acceptsConfig) => { +export const defaultMatch = (accepts: Accept[], config: acceptsConfig): string => { const { supports, default: defaultSupport } = config const accept = accepts.sort((a, b) => b.q - a.q).find((accept) => supports.includes(accept.type)) return accept ? accept.type : defaultSupport @@ -61,7 +61,7 @@ export const defaultMatch = (accepts: Accept[], config: acceptsConfig) => { * }) * ``` */ -export const accepts = (c: Context, options: acceptsOptions) => { +export const accepts = (c: Context, options: acceptsOptions): string => { const acceptHeader = c.req.header(options.header) if (!acceptHeader) { return options.default diff --git a/deno_dist/helper/adapter/index.ts b/deno_dist/helper/adapter/index.ts index abb5ece13..c9e46833a 100644 --- a/deno_dist/helper/adapter/index.ts +++ b/deno_dist/helper/adapter/index.ts @@ -30,7 +30,7 @@ export const env = , C extends Context = Conte return runtimeEnvHandlers[runtime]() } -export const getRuntimeKey = () => { +export const getRuntimeKey = (): Runtime => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const global = globalThis as any diff --git a/deno_dist/helper/cookie/index.ts b/deno_dist/helper/cookie/index.ts index ae2a84abb..7c3840fac 100644 --- a/deno_dist/helper/cookie/index.ts +++ b/deno_dist/helper/cookie/index.ts @@ -118,7 +118,7 @@ export const setSignedCookie = async ( c.header('set-cookie', cookie, { append: true }) } -export const deleteCookie = (c: Context, name: string, opt?: CookieOptions) => { +export const deleteCookie = (c: Context, name: string, opt?: CookieOptions): string | undefined => { const deletedCookie = getCookie(c, name) setCookie(c, name, '', { ...opt, maxAge: 0 }) return deletedCookie diff --git a/deno_dist/helper/css/common.ts b/deno_dist/helper/css/common.ts index 46a02e9d8..aff38a280 100644 --- a/deno_dist/helper/css/common.ts +++ b/deno_dist/helper/css/common.ts @@ -3,12 +3,12 @@ export const PSEUDO_GLOBAL_SELECTOR = ':-hono-global' export const isPseudoGlobalSelectorRe = new RegExp(`^${PSEUDO_GLOBAL_SELECTOR}{(.*)}$`) export const DEFAULT_STYLE_ID = 'hono-css' -export const SELECTOR = Symbol() -export const CLASS_NAME = Symbol() -export const STYLE_STRING = Symbol() -export const SELECTORS = Symbol() -export const EXTERNAL_CLASS_NAMES = Symbol() -const CSS_ESCAPED = Symbol() +export const SELECTOR: unique symbol = Symbol() +export const CLASS_NAME: unique symbol = Symbol() +export const STYLE_STRING: unique symbol = Symbol() +export const SELECTORS: unique symbol = Symbol() +export const EXTERNAL_CLASS_NAMES: unique symbol = Symbol() +const CSS_ESCAPED: unique symbol = Symbol() export interface CssClassName { [SELECTOR]: string @@ -49,12 +49,12 @@ const toHash = (str: string): string => { return 'css-' + out } -const cssStringReStr = [ +const cssStringReStr: string = [ '"(?:(?:\\\\[\\s\\S]|[^"\\\\])*)"', // double quoted string // eslint-disable-next-line quotes "'(?:(?:\\\\[\\s\\S]|[^'\\\\])*)'", // single quoted string ].join('|') -const minifyCssRe = new RegExp( +const minifyCssRe: RegExp = new RegExp( [ '(' + cssStringReStr + ')', // $1: quoted string diff --git a/deno_dist/helper/css/index.ts b/deno_dist/helper/css/index.ts index 9b737e77f..33d53e619 100644 --- a/deno_dist/helper/css/index.ts +++ b/deno_dist/helper/css/index.ts @@ -24,12 +24,36 @@ type usedClassNameData = [ Record // class name already added ] +interface CssType { + (strings: TemplateStringsArray, ...values: CssVariableType[]): Promise +} + +interface CxType { + ( + ...args: (CssClassName | Promise | string | boolean | null | undefined)[] + ): Promise +} + +interface KeyframesType { + (strings: TemplateStringsArray, ...values: CssVariableType[]): CssClassNameCommon +} + +interface ViewTransitionType { + (strings: TemplateStringsArray, ...values: CssVariableType[]): Promise + (content: Promise): Promise + (): Promise +} + +interface StyleType { + (args?: { children?: Promise }): HtmlEscapedString +} + /** * @experimental * `createCssContext` is an experimental feature. * The API might be changed. */ -export const createCssContext = ({ id }: { id: Readonly }) => { +export const createCssContext = ({ id }: { id: Readonly }): DefaultContextType => { const [cssJsxDomObject, StyleRenderToDom] = createCssJsxDomObjects({ id }) const contextMap: WeakMap = new WeakMap() @@ -106,13 +130,11 @@ export const createCssContext = ({ id }: { id: Readonly }) => { return promise } - const css = (strings: TemplateStringsArray, ...values: CssVariableType[]): Promise => { + const css: CssType = (strings, ...values) => { return newCssClassNameObject(cssCommon(strings, values)) } - const cx = ( - ...args: (CssClassName | Promise | string | boolean | null | undefined)[] - ): Promise => { + const cx: CxType = (...args) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any args = cxCommon(args as any) as any // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -121,20 +143,15 @@ export const createCssContext = ({ id }: { id: Readonly }) => { const keyframes = keyframesCommon - type ViewTransitionType = { - (strings: TemplateStringsArray, ...values: CssVariableType[]): Promise - (content: Promise): Promise - (): Promise - } const viewTransition: ViewTransitionType = (( strings: TemplateStringsArray | Promise | undefined, ...values: CssVariableType[] - ): Promise => { + ) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any return newCssClassNameObject(viewTransitionCommon(strings as any, values)) }) as ViewTransitionType - const Style = ({ children }: { children?: Promise } = {}) => + const Style: StyleType = ({ children } = {}) => children ? raw(``) : raw(``) @@ -150,7 +167,17 @@ export const createCssContext = ({ id }: { id: Readonly }) => { } } -const defaultContext = createCssContext({ id: DEFAULT_STYLE_ID }) +interface DefaultContextType { + css: CssType + cx: CxType + keyframes: KeyframesType + viewTransition: ViewTransitionType + Style: StyleType +} + +const defaultContext: DefaultContextType = createCssContext({ + id: DEFAULT_STYLE_ID, +}) /** * @experimental diff --git a/deno_dist/helper/dev/index.ts b/deno_dist/helper/dev/index.ts index 1898c16b8..7ba8613ee 100644 --- a/deno_dist/helper/dev/index.ts +++ b/deno_dist/helper/dev/index.ts @@ -15,7 +15,7 @@ interface RouteData { isMiddleware: boolean } -const handlerName = (handler: Function) => { +const handlerName = (handler: Function): string => { return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]') } @@ -68,7 +68,7 @@ export const showRoutes = (hono: Hono, opts?: ShowRoutesOption }) } -export const getRouterName = (app: Hono) => { +export const getRouterName = (app: Hono): string => { app.router.match('GET', '/') return app.router.name } diff --git a/deno_dist/helper/factory/index.ts b/deno_dist/helper/factory/index.ts index b8fcf8b45..2478031a7 100644 --- a/deno_dist/helper/factory/index.ts +++ b/deno_dist/helper/factory/index.ts @@ -216,7 +216,7 @@ export class Factory { * @experimental * `createApp` is an experimental feature. */ - createApp = () => { + createApp = (): Hono => { const app = new Hono() if (this.initApp) { this.initApp(app) @@ -234,8 +234,8 @@ export class Factory { export const createFactory = (init?: { initApp?: InitApp -}) => new Factory(init) +}): Factory => new Factory(init) export const createMiddleware = ( middleware: MiddlewareHandler -) => createFactory().createMiddleware(middleware) +): MiddlewareHandler => createFactory().createMiddleware(middleware) diff --git a/deno_dist/helper/streaming/sse.ts b/deno_dist/helper/streaming/sse.ts index a459ec87c..4fa30be01 100644 --- a/deno_dist/helper/streaming/sse.ts +++ b/deno_dist/helper/streaming/sse.ts @@ -62,7 +62,7 @@ export const streamSSE = ( c: Context, cb: (stream: SSEStreamingApi) => Promise, onError?: (e: Error, stream: SSEStreamingApi) => Promise -) => { +): Response => { const { readable, writable } = new TransformStream() const stream = new SSEStreamingApi(writable, readable) diff --git a/deno_dist/helper/testing/index.ts b/deno_dist/helper/testing/index.ts index 99ad7ddb9..b542ef5dd 100644 --- a/deno_dist/helper/testing/index.ts +++ b/deno_dist/helper/testing/index.ts @@ -1,6 +1,8 @@ import { hc } from '../../client/index.ts' +import type { Client } from '../../client/types.ts' import type { ExecutionContext } from '../../context.ts' import type { Hono } from '../../hono.ts' +import type { UnionToIntersection } from '../../utils/types.ts' // eslint-disable-next-line @typescript-eslint/no-explicit-any type ExtractEnv = T extends Hono ? E : never @@ -10,7 +12,7 @@ export const testClient = >( app: T, Env?: ExtractEnv['Bindings'] | {}, executionCtx?: ExecutionContext -) => { +): UnionToIntersection> => { const customFetch = (input: RequestInfo | URL, init?: RequestInit) => { return app.request(input, init, Env, executionCtx) } diff --git a/deno_dist/hono-base.ts b/deno_dist/hono-base.ts index b49a6fc33..5a69e8055 100644 --- a/deno_dist/hono-base.ts +++ b/deno_dist/hono-base.ts @@ -233,7 +233,7 @@ class Hono< * }) * ``` */ - onError = (handler: ErrorHandler) => { + onError = (handler: ErrorHandler): Hono => { this.errorHandler = handler return this } @@ -247,7 +247,7 @@ class Hono< * ``` * @see https://hono.dev/api/hono#not-found */ - notFound = (handler: NotFoundHandler) => { + notFound = (handler: NotFoundHandler): Hono => { this.notFoundHandler = handler return this } @@ -396,7 +396,7 @@ class Hono< requestInit?: RequestInit, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext - ) => { + ): Response | Promise => { if (input instanceof Request) { if (requestInit !== undefined) { input = new Request(input, requestInit) diff --git a/deno_dist/jsx/dom/context.ts b/deno_dist/jsx/dom/context.ts index 52fe061ba..0089d052b 100644 --- a/deno_dist/jsx/dom/context.ts +++ b/deno_dist/jsx/dom/context.ts @@ -5,7 +5,7 @@ import { globalContexts } from '../context.ts' import { Fragment } from './jsx-runtime.ts' import { setInternalTagFlag } from './utils.ts' -export const createContextProviderFunction = (values: T[]) => +export const createContextProviderFunction = (values: T[]): Function => setInternalTagFlag(({ value, children }: { value: T; children: Child[] }) => { if (!children) { return undefined diff --git a/deno_dist/jsx/dom/css.ts b/deno_dist/jsx/dom/css.ts index 29917d727..338c078d2 100644 --- a/deno_dist/jsx/dom/css.ts +++ b/deno_dist/jsx/dom/css.ts @@ -56,7 +56,16 @@ const splitRule = (rule: string): string[] => { return result } -export const createCssJsxDomObjects = ({ id }: { id: Readonly }) => { +interface CreateCssJsxDomObjectsType { + (args: { id: Readonly }): readonly [ + { + toString(this: CssClassName): string + }, + FC> + ] +} + +export const createCssJsxDomObjects: CreateCssJsxDomObjectsType = ({ id }) => { let styleSheet: CSSStyleSheet | null | undefined = undefined const findStyleSheet = (): [CSSStyleSheet, Set] | [] => { if (!styleSheet) { diff --git a/deno_dist/jsx/dom/jsx-dev-runtime.ts b/deno_dist/jsx/dom/jsx-dev-runtime.ts index 216aca4fc..fc1837012 100644 --- a/deno_dist/jsx/dom/jsx-dev-runtime.ts +++ b/deno_dist/jsx/dom/jsx-dev-runtime.ts @@ -28,4 +28,4 @@ export const jsxDEV = (tag: string | Function, props: Props, key?: string): JSXN ) as JSXNode } -export const Fragment = (props: Record) => jsxDEV('', props, undefined) +export const Fragment = (props: Record): JSXNode => jsxDEV('', props, undefined) diff --git a/deno_dist/jsx/dom/utils.ts b/deno_dist/jsx/dom/utils.ts index 5e2340d0e..803652155 100644 --- a/deno_dist/jsx/dom/utils.ts +++ b/deno_dist/jsx/dom/utils.ts @@ -1,6 +1,6 @@ import { DOM_INTERNAL_TAG } from '../constants.ts' -export const setInternalTagFlag = (fn: Function) => { +export const setInternalTagFlag = (fn: Function): Function => { // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(fn as any)[DOM_INTERNAL_TAG] = true return fn diff --git a/deno_dist/jsx/hooks/index.ts b/deno_dist/jsx/hooks/index.ts index 27566990e..93e63b22e 100644 --- a/deno_dist/jsx/hooks/index.ts +++ b/deno_dist/jsx/hooks/index.ts @@ -18,7 +18,10 @@ export type EffectData = [ (() => void) | undefined // effect ] -const resolvedPromiseValueMap = new WeakMap, unknown>() +const resolvedPromiseValueMap: WeakMap, unknown> = new WeakMap< + Promise, + unknown +>() const isDepsChanged = ( prevDeps: readonly unknown[] | undefined, diff --git a/deno_dist/jsx/jsx-runtime.ts b/deno_dist/jsx/jsx-runtime.ts index 61d7096ba..cf1c0c0a9 100644 --- a/deno_dist/jsx/jsx-runtime.ts +++ b/deno_dist/jsx/jsx-runtime.ts @@ -2,7 +2,11 @@ export { jsxDEV as jsx, Fragment } from './jsx-dev-runtime.ts' export { jsxDEV as jsxs } from './jsx-dev-runtime.ts' import { raw, html } from '../helper/html/index.ts' +import type { HtmlEscapedString } from '../utils/html.ts' export { html as jsxTemplate } -export const jsxAttr = (name: string, value: string | Promise) => +export const jsxAttr = ( + name: string, + value: string | Promise +): HtmlEscapedString | Promise => typeof value === 'string' ? raw(name + '="' + html`${value}` + '"') : html`${name}="${value}"` export const jsxEscape = (value: string) => value diff --git a/deno_dist/middleware/jsx-renderer/index.ts b/deno_dist/middleware/jsx-renderer/index.ts index 4f7dd75c4..0d76102e3 100644 --- a/deno_dist/middleware/jsx-renderer/index.ts +++ b/deno_dist/middleware/jsx-renderer/index.ts @@ -3,11 +3,13 @@ import type { Context, PropsForRenderer } from '../../context.ts' import { html, raw } from '../../helper/html/index.ts' import { jsx, createContext, useContext, Fragment } from '../../jsx/index.ts' import type { FC, PropsWithChildren, JSXNode } from '../../jsx/index.ts' +import type { Context as JSXContext } from '../../jsx/index.ts' import { renderToReadableStream } from '../../jsx/streaming.ts' import type { Env, Input, MiddlewareHandler } from '../../types.ts' import type { HtmlEscapedString } from '../../utils/html.ts' -export const RequestContext = createContext(null) +export const RequestContext: JSXContext | null> = + createContext(null) type RendererOptions = { docType?: boolean | string diff --git a/deno_dist/middleware/timing/index.ts b/deno_dist/middleware/timing/index.ts index 429929c32..3881f8ce4 100644 --- a/deno_dist/middleware/timing/index.ts +++ b/deno_dist/middleware/timing/index.ts @@ -24,7 +24,7 @@ interface TimingOptions { crossOrigin: boolean | string | ((c: Context) => boolean | string) } -const getTime = () => { +const getTime = (): number => { try { return performance.now() } catch {} diff --git a/deno_dist/request.ts b/deno_dist/request.ts index ea1825379..6fd80d42b 100644 --- a/deno_dist/request.ts +++ b/deno_dist/request.ts @@ -289,7 +289,7 @@ export class HonoRequest

{ * ``` * @see https://hono.dev/api/request#url */ - get url() { + get url(): string { return this.raw.url } @@ -303,7 +303,7 @@ export class HonoRequest

{ * ``` * @see https://hono.dev/api/request#method */ - get method() { + get method(): string { return this.raw.method } diff --git a/deno_dist/router/smart-router/router.ts b/deno_dist/router/smart-router/router.ts index 9c35f2b70..f0ade4671 100644 --- a/deno_dist/router/smart-router/router.ts +++ b/deno_dist/router/smart-router/router.ts @@ -59,7 +59,7 @@ export class SmartRouter implements Router { return res as Result } - get activeRouter() { + get activeRouter(): Router { if (this.routes || this.routers.length !== 1) { throw new Error('No active router has been determined yet.') } diff --git a/deno_dist/utils/buffer.ts b/deno_dist/utils/buffer.ts index 075c3d5ec..5ba55a4e0 100644 --- a/deno_dist/utils/buffer.ts +++ b/deno_dist/utils/buffer.ts @@ -48,7 +48,10 @@ export const bufferToString = (buffer: ArrayBuffer): string => { return buffer } -export const bufferToFormData = (arrayBuffer: ArrayBuffer, contentType: string) => { +export const bufferToFormData = ( + arrayBuffer: ArrayBuffer, + contentType: string +): Promise => { const response = new Response(arrayBuffer, { headers: { 'Content-Type': contentType, diff --git a/deno_dist/utils/color.ts b/deno_dist/utils/color.ts index fd2f62b85..7f354e32d 100644 --- a/deno_dist/utils/color.ts +++ b/deno_dist/utils/color.ts @@ -1,4 +1,4 @@ -export function getColorEnabled() { +export function getColorEnabled(): boolean { // eslint-disable-next-line @typescript-eslint/no-explicit-any const { process, Deno } = globalThis as any diff --git a/deno_dist/utils/jwt/utf8.ts b/deno_dist/utils/jwt/utf8.ts index 107407aee..01d1972a1 100644 --- a/deno_dist/utils/jwt/utf8.ts +++ b/deno_dist/utils/jwt/utf8.ts @@ -1,2 +1,2 @@ -export const utf8Encoder = new TextEncoder() -export const utf8Decoder = new TextDecoder() +export const utf8Encoder: TextEncoder = new TextEncoder() +export const utf8Decoder: TextDecoder = new TextDecoder() diff --git a/deno_dist/utils/stream.ts b/deno_dist/utils/stream.ts index 4269a3598..b129caf91 100644 --- a/deno_dist/utils/stream.ts +++ b/deno_dist/utils/stream.ts @@ -30,7 +30,7 @@ export class StreamingApi { }) } - async write(input: Uint8Array | string) { + async write(input: Uint8Array | string): Promise { try { if (typeof input === 'string') { input = this.encoder.encode(input) @@ -42,12 +42,12 @@ export class StreamingApi { return this } - async writeln(input: string) { + async writeln(input: string): Promise { await this.write(input + '\n') return this } - sleep(ms: number) { + sleep(ms: number): Promise { return new Promise((res) => setTimeout(res, ms)) } diff --git a/src/context.ts b/src/context.ts index 8de2f9e30..405c841b2 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,6 +1,6 @@ import type { HonoRequest } from './request' -import type { Env, FetchEventLike, NotFoundHandler, Input, TypedResponse } from './types' -import { resolveCallback, HtmlEscapedCallbackPhase } from './utils/html' +import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types' +import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html' import type { RedirectStatusCode, StatusCode } from './utils/http-status' import type { JSONValue, JSONParsed, IsAny, Simplify } from './utils/types' @@ -224,7 +224,14 @@ export class Context< */ render: Renderer = (...args) => this.renderer(...args) - setLayout = (layout: Layout) => (this.layout = layout) + setLayout = ( + layout: Layout + ): Layout< + PropsForRenderer & { + Layout: Layout + } + > => (this.layout = layout) + getLayout = () => this.layout /** diff --git a/src/helper/accepts/accepts.ts b/src/helper/accepts/accepts.ts index 4a8338e66..b984e6f36 100644 --- a/src/helper/accepts/accepts.ts +++ b/src/helper/accepts/accepts.ts @@ -25,7 +25,7 @@ export interface acceptsOptions extends acceptsConfig { match?: (accepts: Accept[], config: acceptsConfig) => string } -export const parseAccept = (acceptHeader: string) => { +export const parseAccept = (acceptHeader: string): Accept[] => { // Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 const accepts = acceptHeader.split(',') // ['text/html', 'application/xhtml+xml', 'application/xml;q=0.9', 'image/webp', '*/*;q=0.8'] return accepts.map((accept) => { @@ -42,7 +42,7 @@ export const parseAccept = (acceptHeader: string) => { }) } -export const defaultMatch = (accepts: Accept[], config: acceptsConfig) => { +export const defaultMatch = (accepts: Accept[], config: acceptsConfig): string => { const { supports, default: defaultSupport } = config const accept = accepts.sort((a, b) => b.q - a.q).find((accept) => supports.includes(accept.type)) return accept ? accept.type : defaultSupport @@ -61,7 +61,7 @@ export const defaultMatch = (accepts: Accept[], config: acceptsConfig) => { * }) * ``` */ -export const accepts = (c: Context, options: acceptsOptions) => { +export const accepts = (c: Context, options: acceptsOptions): string => { const acceptHeader = c.req.header(options.header) if (!acceptHeader) { return options.default diff --git a/src/helper/adapter/index.ts b/src/helper/adapter/index.ts index 0df451fd1..b8d544409 100644 --- a/src/helper/adapter/index.ts +++ b/src/helper/adapter/index.ts @@ -30,7 +30,7 @@ export const env = , C extends Context = Conte return runtimeEnvHandlers[runtime]() } -export const getRuntimeKey = () => { +export const getRuntimeKey = (): Runtime => { // eslint-disable-next-line @typescript-eslint/no-explicit-any const global = globalThis as any diff --git a/src/helper/cookie/index.ts b/src/helper/cookie/index.ts index eeaba9d86..fd90704bb 100644 --- a/src/helper/cookie/index.ts +++ b/src/helper/cookie/index.ts @@ -118,7 +118,7 @@ export const setSignedCookie = async ( c.header('set-cookie', cookie, { append: true }) } -export const deleteCookie = (c: Context, name: string, opt?: CookieOptions) => { +export const deleteCookie = (c: Context, name: string, opt?: CookieOptions): string | undefined => { const deletedCookie = getCookie(c, name) setCookie(c, name, '', { ...opt, maxAge: 0 }) return deletedCookie diff --git a/src/helper/css/common.ts b/src/helper/css/common.ts index 46a02e9d8..aff38a280 100644 --- a/src/helper/css/common.ts +++ b/src/helper/css/common.ts @@ -3,12 +3,12 @@ export const PSEUDO_GLOBAL_SELECTOR = ':-hono-global' export const isPseudoGlobalSelectorRe = new RegExp(`^${PSEUDO_GLOBAL_SELECTOR}{(.*)}$`) export const DEFAULT_STYLE_ID = 'hono-css' -export const SELECTOR = Symbol() -export const CLASS_NAME = Symbol() -export const STYLE_STRING = Symbol() -export const SELECTORS = Symbol() -export const EXTERNAL_CLASS_NAMES = Symbol() -const CSS_ESCAPED = Symbol() +export const SELECTOR: unique symbol = Symbol() +export const CLASS_NAME: unique symbol = Symbol() +export const STYLE_STRING: unique symbol = Symbol() +export const SELECTORS: unique symbol = Symbol() +export const EXTERNAL_CLASS_NAMES: unique symbol = Symbol() +const CSS_ESCAPED: unique symbol = Symbol() export interface CssClassName { [SELECTOR]: string @@ -49,12 +49,12 @@ const toHash = (str: string): string => { return 'css-' + out } -const cssStringReStr = [ +const cssStringReStr: string = [ '"(?:(?:\\\\[\\s\\S]|[^"\\\\])*)"', // double quoted string // eslint-disable-next-line quotes "'(?:(?:\\\\[\\s\\S]|[^'\\\\])*)'", // single quoted string ].join('|') -const minifyCssRe = new RegExp( +const minifyCssRe: RegExp = new RegExp( [ '(' + cssStringReStr + ')', // $1: quoted string diff --git a/src/helper/css/index.ts b/src/helper/css/index.ts index a77346836..0393e9e29 100644 --- a/src/helper/css/index.ts +++ b/src/helper/css/index.ts @@ -24,12 +24,36 @@ type usedClassNameData = [ Record // class name already added ] +interface CssType { + (strings: TemplateStringsArray, ...values: CssVariableType[]): Promise +} + +interface CxType { + ( + ...args: (CssClassName | Promise | string | boolean | null | undefined)[] + ): Promise +} + +interface KeyframesType { + (strings: TemplateStringsArray, ...values: CssVariableType[]): CssClassNameCommon +} + +interface ViewTransitionType { + (strings: TemplateStringsArray, ...values: CssVariableType[]): Promise + (content: Promise): Promise + (): Promise +} + +interface StyleType { + (args?: { children?: Promise }): HtmlEscapedString +} + /** * @experimental * `createCssContext` is an experimental feature. * The API might be changed. */ -export const createCssContext = ({ id }: { id: Readonly }) => { +export const createCssContext = ({ id }: { id: Readonly }): DefaultContextType => { const [cssJsxDomObject, StyleRenderToDom] = createCssJsxDomObjects({ id }) const contextMap: WeakMap = new WeakMap() @@ -106,13 +130,11 @@ export const createCssContext = ({ id }: { id: Readonly }) => { return promise } - const css = (strings: TemplateStringsArray, ...values: CssVariableType[]): Promise => { + const css: CssType = (strings, ...values) => { return newCssClassNameObject(cssCommon(strings, values)) } - const cx = ( - ...args: (CssClassName | Promise | string | boolean | null | undefined)[] - ): Promise => { + const cx: CxType = (...args) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any args = cxCommon(args as any) as any // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -121,20 +143,15 @@ export const createCssContext = ({ id }: { id: Readonly }) => { const keyframes = keyframesCommon - type ViewTransitionType = { - (strings: TemplateStringsArray, ...values: CssVariableType[]): Promise - (content: Promise): Promise - (): Promise - } const viewTransition: ViewTransitionType = (( strings: TemplateStringsArray | Promise | undefined, ...values: CssVariableType[] - ): Promise => { + ) => { // eslint-disable-next-line @typescript-eslint/no-explicit-any return newCssClassNameObject(viewTransitionCommon(strings as any, values)) }) as ViewTransitionType - const Style = ({ children }: { children?: Promise } = {}) => + const Style: StyleType = ({ children } = {}) => children ? raw(``) : raw(``) @@ -150,7 +167,17 @@ export const createCssContext = ({ id }: { id: Readonly }) => { } } -const defaultContext = createCssContext({ id: DEFAULT_STYLE_ID }) +interface DefaultContextType { + css: CssType + cx: CxType + keyframes: KeyframesType + viewTransition: ViewTransitionType + Style: StyleType +} + +const defaultContext: DefaultContextType = createCssContext({ + id: DEFAULT_STYLE_ID, +}) /** * @experimental diff --git a/src/helper/dev/index.ts b/src/helper/dev/index.ts index 3393aeacd..f27b1f3be 100644 --- a/src/helper/dev/index.ts +++ b/src/helper/dev/index.ts @@ -15,7 +15,7 @@ interface RouteData { isMiddleware: boolean } -const handlerName = (handler: Function) => { +const handlerName = (handler: Function): string => { return handler.name || (isMiddleware(handler) ? '[middleware]' : '[handler]') } @@ -68,7 +68,7 @@ export const showRoutes = (hono: Hono, opts?: ShowRoutesOption }) } -export const getRouterName = (app: Hono) => { +export const getRouterName = (app: Hono): string => { app.router.match('GET', '/') return app.router.name } diff --git a/src/helper/factory/index.ts b/src/helper/factory/index.ts index ecd1e6f64..faeec0812 100644 --- a/src/helper/factory/index.ts +++ b/src/helper/factory/index.ts @@ -216,7 +216,7 @@ export class Factory { * @experimental * `createApp` is an experimental feature. */ - createApp = () => { + createApp = (): Hono => { const app = new Hono() if (this.initApp) { this.initApp(app) @@ -234,8 +234,8 @@ export class Factory { export const createFactory = (init?: { initApp?: InitApp -}) => new Factory(init) +}): Factory => new Factory(init) export const createMiddleware = ( middleware: MiddlewareHandler -) => createFactory().createMiddleware(middleware) +): MiddlewareHandler => createFactory().createMiddleware(middleware) diff --git a/src/helper/streaming/sse.ts b/src/helper/streaming/sse.ts index bcadf1f43..e909fc274 100644 --- a/src/helper/streaming/sse.ts +++ b/src/helper/streaming/sse.ts @@ -62,7 +62,7 @@ export const streamSSE = ( c: Context, cb: (stream: SSEStreamingApi) => Promise, onError?: (e: Error, stream: SSEStreamingApi) => Promise -) => { +): Response => { const { readable, writable } = new TransformStream() const stream = new SSEStreamingApi(writable, readable) diff --git a/src/helper/testing/index.ts b/src/helper/testing/index.ts index 23e879f4f..4920a5cbc 100644 --- a/src/helper/testing/index.ts +++ b/src/helper/testing/index.ts @@ -1,6 +1,8 @@ import { hc } from '../../client' +import type { Client } from '../../client/types' import type { ExecutionContext } from '../../context' import type { Hono } from '../../hono' +import type { UnionToIntersection } from '../../utils/types' // eslint-disable-next-line @typescript-eslint/no-explicit-any type ExtractEnv = T extends Hono ? E : never @@ -10,7 +12,7 @@ export const testClient = >( app: T, Env?: ExtractEnv['Bindings'] | {}, executionCtx?: ExecutionContext -) => { +): UnionToIntersection> => { const customFetch = (input: RequestInfo | URL, init?: RequestInit) => { return app.request(input, init, Env, executionCtx) } diff --git a/src/hono-base.ts b/src/hono-base.ts index cb4092e38..293d3874f 100644 --- a/src/hono-base.ts +++ b/src/hono-base.ts @@ -233,7 +233,7 @@ class Hono< * }) * ``` */ - onError = (handler: ErrorHandler) => { + onError = (handler: ErrorHandler): Hono => { this.errorHandler = handler return this } @@ -247,7 +247,7 @@ class Hono< * ``` * @see https://hono.dev/api/hono#not-found */ - notFound = (handler: NotFoundHandler) => { + notFound = (handler: NotFoundHandler): Hono => { this.notFoundHandler = handler return this } @@ -396,7 +396,7 @@ class Hono< requestInit?: RequestInit, Env?: E['Bindings'] | {}, executionCtx?: ExecutionContext - ) => { + ): Response | Promise => { if (input instanceof Request) { if (requestInit !== undefined) { input = new Request(input, requestInit) diff --git a/src/jsx/dom/context.ts b/src/jsx/dom/context.ts index c1a3d8b33..d39f52f8c 100644 --- a/src/jsx/dom/context.ts +++ b/src/jsx/dom/context.ts @@ -5,7 +5,7 @@ import { globalContexts } from '../context' import { Fragment } from './jsx-runtime' import { setInternalTagFlag } from './utils' -export const createContextProviderFunction = (values: T[]) => +export const createContextProviderFunction = (values: T[]): Function => setInternalTagFlag(({ value, children }: { value: T; children: Child[] }) => { if (!children) { return undefined diff --git a/src/jsx/dom/css.ts b/src/jsx/dom/css.ts index 4629ef9c5..5a145ad7f 100644 --- a/src/jsx/dom/css.ts +++ b/src/jsx/dom/css.ts @@ -56,7 +56,16 @@ const splitRule = (rule: string): string[] => { return result } -export const createCssJsxDomObjects = ({ id }: { id: Readonly }) => { +interface CreateCssJsxDomObjectsType { + (args: { id: Readonly }): readonly [ + { + toString(this: CssClassName): string + }, + FC> + ] +} + +export const createCssJsxDomObjects: CreateCssJsxDomObjectsType = ({ id }) => { let styleSheet: CSSStyleSheet | null | undefined = undefined const findStyleSheet = (): [CSSStyleSheet, Set] | [] => { if (!styleSheet) { diff --git a/src/jsx/dom/jsx-dev-runtime.ts b/src/jsx/dom/jsx-dev-runtime.ts index 28b9acdc9..a4c439b16 100644 --- a/src/jsx/dom/jsx-dev-runtime.ts +++ b/src/jsx/dom/jsx-dev-runtime.ts @@ -28,4 +28,4 @@ export const jsxDEV = (tag: string | Function, props: Props, key?: string): JSXN ) as JSXNode } -export const Fragment = (props: Record) => jsxDEV('', props, undefined) +export const Fragment = (props: Record): JSXNode => jsxDEV('', props, undefined) diff --git a/src/jsx/dom/utils.ts b/src/jsx/dom/utils.ts index cf4c58072..fef0c3c05 100644 --- a/src/jsx/dom/utils.ts +++ b/src/jsx/dom/utils.ts @@ -1,6 +1,6 @@ import { DOM_INTERNAL_TAG } from '../constants' -export const setInternalTagFlag = (fn: Function) => { +export const setInternalTagFlag = (fn: Function): Function => { // eslint-disable-next-line @typescript-eslint/no-explicit-any ;(fn as any)[DOM_INTERNAL_TAG] = true return fn diff --git a/src/jsx/hooks/index.ts b/src/jsx/hooks/index.ts index 60e2d240a..d142d44af 100644 --- a/src/jsx/hooks/index.ts +++ b/src/jsx/hooks/index.ts @@ -18,7 +18,10 @@ export type EffectData = [ (() => void) | undefined // effect ] -const resolvedPromiseValueMap = new WeakMap, unknown>() +const resolvedPromiseValueMap: WeakMap, unknown> = new WeakMap< + Promise, + unknown +>() const isDepsChanged = ( prevDeps: readonly unknown[] | undefined, diff --git a/src/jsx/jsx-runtime.ts b/src/jsx/jsx-runtime.ts index a20521e29..d75da1697 100644 --- a/src/jsx/jsx-runtime.ts +++ b/src/jsx/jsx-runtime.ts @@ -2,7 +2,11 @@ export { jsxDEV as jsx, Fragment } from './jsx-dev-runtime' export { jsxDEV as jsxs } from './jsx-dev-runtime' import { raw, html } from '../helper/html' +import type { HtmlEscapedString } from '../utils/html' export { html as jsxTemplate } -export const jsxAttr = (name: string, value: string | Promise) => +export const jsxAttr = ( + name: string, + value: string | Promise +): HtmlEscapedString | Promise => typeof value === 'string' ? raw(name + '="' + html`${value}` + '"') : html`${name}="${value}"` export const jsxEscape = (value: string) => value diff --git a/src/middleware/jsx-renderer/index.ts b/src/middleware/jsx-renderer/index.ts index 1a3f261bf..e42b9868a 100644 --- a/src/middleware/jsx-renderer/index.ts +++ b/src/middleware/jsx-renderer/index.ts @@ -3,11 +3,13 @@ import type { Context, PropsForRenderer } from '../../context' import { html, raw } from '../../helper/html' import { jsx, createContext, useContext, Fragment } from '../../jsx' import type { FC, PropsWithChildren, JSXNode } from '../../jsx' +import type { Context as JSXContext } from '../../jsx' import { renderToReadableStream } from '../../jsx/streaming' import type { Env, Input, MiddlewareHandler } from '../../types' import type { HtmlEscapedString } from '../../utils/html' -export const RequestContext = createContext(null) +export const RequestContext: JSXContext | null> = + createContext(null) type RendererOptions = { docType?: boolean | string diff --git a/src/middleware/timing/index.ts b/src/middleware/timing/index.ts index 504331b15..2675b9e5b 100644 --- a/src/middleware/timing/index.ts +++ b/src/middleware/timing/index.ts @@ -24,7 +24,7 @@ interface TimingOptions { crossOrigin: boolean | string | ((c: Context) => boolean | string) } -const getTime = () => { +const getTime = (): number => { try { return performance.now() } catch {} diff --git a/src/request.ts b/src/request.ts index b5fd8546e..7c028aee5 100644 --- a/src/request.ts +++ b/src/request.ts @@ -289,7 +289,7 @@ export class HonoRequest

{ * ``` * @see https://hono.dev/api/request#url */ - get url() { + get url(): string { return this.raw.url } @@ -303,7 +303,7 @@ export class HonoRequest

{ * ``` * @see https://hono.dev/api/request#method */ - get method() { + get method(): string { return this.raw.method } diff --git a/src/router/smart-router/router.ts b/src/router/smart-router/router.ts index 7b0510d87..aa1201122 100644 --- a/src/router/smart-router/router.ts +++ b/src/router/smart-router/router.ts @@ -59,7 +59,7 @@ export class SmartRouter implements Router { return res as Result } - get activeRouter() { + get activeRouter(): Router { if (this.routes || this.routers.length !== 1) { throw new Error('No active router has been determined yet.') } diff --git a/src/utils/buffer.ts b/src/utils/buffer.ts index fecfd82eb..6d24f692f 100644 --- a/src/utils/buffer.ts +++ b/src/utils/buffer.ts @@ -48,7 +48,10 @@ export const bufferToString = (buffer: ArrayBuffer): string => { return buffer } -export const bufferToFormData = (arrayBuffer: ArrayBuffer, contentType: string) => { +export const bufferToFormData = ( + arrayBuffer: ArrayBuffer, + contentType: string +): Promise => { const response = new Response(arrayBuffer, { headers: { 'Content-Type': contentType, diff --git a/src/utils/color.ts b/src/utils/color.ts index fd2f62b85..7f354e32d 100644 --- a/src/utils/color.ts +++ b/src/utils/color.ts @@ -1,4 +1,4 @@ -export function getColorEnabled() { +export function getColorEnabled(): boolean { // eslint-disable-next-line @typescript-eslint/no-explicit-any const { process, Deno } = globalThis as any diff --git a/src/utils/jwt/utf8.ts b/src/utils/jwt/utf8.ts index 107407aee..01d1972a1 100644 --- a/src/utils/jwt/utf8.ts +++ b/src/utils/jwt/utf8.ts @@ -1,2 +1,2 @@ -export const utf8Encoder = new TextEncoder() -export const utf8Decoder = new TextDecoder() +export const utf8Encoder: TextEncoder = new TextEncoder() +export const utf8Decoder: TextDecoder = new TextDecoder() diff --git a/src/utils/stream.ts b/src/utils/stream.ts index 4269a3598..b129caf91 100644 --- a/src/utils/stream.ts +++ b/src/utils/stream.ts @@ -30,7 +30,7 @@ export class StreamingApi { }) } - async write(input: Uint8Array | string) { + async write(input: Uint8Array | string): Promise { try { if (typeof input === 'string') { input = this.encoder.encode(input) @@ -42,12 +42,12 @@ export class StreamingApi { return this } - async writeln(input: string) { + async writeln(input: string): Promise { await this.write(input + '\n') return this } - sleep(ms: number) { + sleep(ms: number): Promise { return new Promise((res) => setTimeout(res, ms)) }