From 798425a17d5e06b6b7c0b0082c7ee3b5a4137271 Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Wed, 20 Apr 2022 18:12:51 +0900 Subject: [PATCH] refactor: define `Next` type (#160) --- src/hono.ts | 7 ++++--- src/index.ts | 2 +- src/middleware/basic-auth/index.ts | 3 ++- src/middleware/body-parse/index.ts | 3 ++- src/middleware/cookie/index.ts | 3 ++- src/middleware/cors/index.ts | 3 ++- src/middleware/etag/index.ts | 3 ++- src/middleware/graphql-server/index.ts | 5 +++-- src/middleware/logger/index.ts | 3 ++- src/middleware/mustache/index.ts | 5 +++-- src/middleware/powered-by/index.ts | 3 ++- src/middleware/pretty-json/index.ts | 3 ++- src/middleware/serve-static/index.ts | 3 ++- 13 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/hono.ts b/src/hono.ts index bf73fc1e..bc1007b5 100644 --- a/src/hono.ts +++ b/src/hono.ts @@ -18,11 +18,12 @@ declare global { export type Handler = ( c: Context, - next?: Function + next?: Next ) => Response | Promise -export type MiddlewareHandler = (c: Context, next: Function) => Promise +export type MiddlewareHandler = (c: Context, next: Next) => Promise export type NotFoundHandler = (c: Context) => Response | Promise export type ErrorHandler = (err: Error, c: Context) => Response +export type Next = () => Promise // eslint-disable-next-line @typescript-eslint/no-unused-vars type ParamKeyName = NameWithPattern extends `${infer Name}{${infer _Pattern}` @@ -184,7 +185,7 @@ export class Hono { } } - const wrappedHandler = async (context: Context, next: Function) => { + const wrappedHandler = async (context: Context, next: Next) => { const res = await handler(context) if (!(res instanceof Response)) { throw new TypeError('response must be a instace of Response') diff --git a/src/index.ts b/src/index.ts index bb1fdba3..7f0f7e72 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,4 @@ export { Hono } from '@/hono' -export type { Handler, MiddlewareHandler } from '@/hono' +export type { Handler, MiddlewareHandler, Next } from '@/hono' export { Context } from '@/context' export type { Env } from '@/context' diff --git a/src/middleware/basic-auth/index.ts b/src/middleware/basic-auth/index.ts index a7e4c1d1..a13d1623 100644 --- a/src/middleware/basic-auth/index.ts +++ b/src/middleware/basic-auth/index.ts @@ -1,4 +1,5 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' import { timingSafeEqual } from '@/utils/buffer' import { decodeBase64 } from '@/utils/crypto' @@ -45,7 +46,7 @@ export const basicAuth = ( } users.unshift({ username: options.username, password: options.password }) - return async (ctx: Context, next: Function) => { + return async (ctx: Context, next: Next) => { const requestUser = auth(ctx.req) if (requestUser) { diff --git a/src/middleware/body-parse/index.ts b/src/middleware/body-parse/index.ts index af4198c8..207cd1b0 100644 --- a/src/middleware/body-parse/index.ts +++ b/src/middleware/body-parse/index.ts @@ -1,8 +1,9 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' import { parseBody } from '@/utils/body' export const bodyParse = () => { - return async (ctx: Context, next: Function) => { + return async (ctx: Context, next: Next) => { ctx.req.parsedBody = await parseBody(ctx.req) await next() } diff --git a/src/middleware/cookie/index.ts b/src/middleware/cookie/index.ts index 13a0bbd7..e8207677 100644 --- a/src/middleware/cookie/index.ts +++ b/src/middleware/cookie/index.ts @@ -1,4 +1,5 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' declare global { interface Request { @@ -25,7 +26,7 @@ export type CookieOptions = { } export const cookie = () => { - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { c.req.cookie = (name: string): string => { const cookie = c.req.headers.get('Cookie') const obj = parse(cookie) diff --git a/src/middleware/cors/index.ts b/src/middleware/cors/index.ts index e38964b2..dbc80b60 100644 --- a/src/middleware/cors/index.ts +++ b/src/middleware/cors/index.ts @@ -1,4 +1,5 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' type CORSOptions = { origin: string @@ -21,7 +22,7 @@ export const cors = (options?: CORSOptions) => { ...options, } - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { await next() function set(key: string, value: string) { diff --git a/src/middleware/etag/index.ts b/src/middleware/etag/index.ts index ccb66e1a..fbce0459 100644 --- a/src/middleware/etag/index.ts +++ b/src/middleware/etag/index.ts @@ -1,4 +1,5 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' import { parseBody } from '@/utils/body' import { sha1 } from '@/utils/crypto' @@ -7,7 +8,7 @@ type ETagOptions = { } export const etag = (options: ETagOptions = { weak: false }) => { - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { const ifNoneMatch = c.req.header('If-None-Match') || c.req.header('if-none-match') await next() diff --git a/src/middleware/graphql-server/index.ts b/src/middleware/graphql-server/index.ts index d8528258..1bc5294b 100644 --- a/src/middleware/graphql-server/index.ts +++ b/src/middleware/graphql-server/index.ts @@ -1,7 +1,6 @@ // Based on the code in the `express-graphql` package. // https://github.com/graphql/express-graphql/blob/main/src/index.ts - import { Source, parse, @@ -20,7 +19,9 @@ import type { FormattedExecutionResult, GraphQLFormattedError, } from 'graphql' + import type { Context } from '@/context' +import type { Next } from '@/hono' import { parseBody } from '@/middleware/graphql-server/parse-body' type Options = { @@ -38,7 +39,7 @@ export const graphqlServer = (options: Options) => { const validationRules = options.validationRules ?? [] // const showGraphiQL = options.graphiql ?? false - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { await next() // GraphQL HTTP only supports GET and POST methods. diff --git a/src/middleware/logger/index.ts b/src/middleware/logger/index.ts index 0610bfd1..af60b9ed 100644 --- a/src/middleware/logger/index.ts +++ b/src/middleware/logger/index.ts @@ -1,4 +1,5 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' import { getPathFromURL } from '@/utils/url' const humanize = (n: string[], opts?: { delimiter?: string; separator?: string }) => { @@ -52,7 +53,7 @@ function log( } export const logger = (fn = console.log) => { - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { const { method } = c.req const path = getPathFromURL(c.req.url) diff --git a/src/middleware/mustache/index.ts b/src/middleware/mustache/index.ts index ee2fa739..9d4af50b 100644 --- a/src/middleware/mustache/index.ts +++ b/src/middleware/mustache/index.ts @@ -1,5 +1,6 @@ -import { getContentFromKVAsset, getKVFilePath } from '../../utils/cloudflare' import type { Context } from '@/context' +import type { Next } from '@/hono' +import { getContentFromKVAsset, getKVFilePath } from '@/utils/cloudflare' const EXTENSION = '.mustache' const DEFAULT_DOCUMENT = 'index.mustache' @@ -17,7 +18,7 @@ type Init = { export const mustache = (init: Init = { root: '' }) => { const { root } = init - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { let Mustache: Mustache try { Mustache = require('mustache') diff --git a/src/middleware/powered-by/index.ts b/src/middleware/powered-by/index.ts index e4ed8272..b682a181 100644 --- a/src/middleware/powered-by/index.ts +++ b/src/middleware/powered-by/index.ts @@ -1,7 +1,8 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' export const poweredBy = () => { - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { await next() c.res.headers.append('X-Powered-By', 'Hono') } diff --git a/src/middleware/pretty-json/index.ts b/src/middleware/pretty-json/index.ts index ece3535f..08a5df20 100644 --- a/src/middleware/pretty-json/index.ts +++ b/src/middleware/pretty-json/index.ts @@ -1,11 +1,12 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' type prettyOptions = { space: number } export const prettyJSON = (options: prettyOptions = { space: 2 }) => { - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false c.pretty(pretty, options.space) await next() diff --git a/src/middleware/serve-static/index.ts b/src/middleware/serve-static/index.ts index c1d1c88b..34b648e5 100644 --- a/src/middleware/serve-static/index.ts +++ b/src/middleware/serve-static/index.ts @@ -1,4 +1,5 @@ import type { Context } from '@/context' +import type { Next } from '@/hono' import { getContentFromKVAsset, getKVFilePath } from '@/utils/cloudflare' import { getMimeType } from '@/utils/mime' @@ -10,7 +11,7 @@ const DEFAULT_DOCUMENT = 'index.html' // This middleware is available only on Cloudflare Workers. export const serveStatic = (opt: Options = { root: '' }) => { - return async (c: Context, next: Function) => { + return async (c: Context, next: Next) => { await next() const url = new URL(c.req.url)