0
0
mirror of https://github.com/honojs/hono.git synced 2024-12-01 10:51:01 +00:00

refactor: define Next type (#160)

This commit is contained in:
Yusuke Wada 2022-04-20 18:12:51 +09:00 committed by GitHub
parent a101f8067d
commit 798425a17d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 29 additions and 17 deletions

View File

@ -18,11 +18,12 @@ declare global {
export type Handler<RequestParamKeyType = string> = (
c: Context<RequestParamKeyType>,
next?: Function
next?: Next
) => Response | Promise<Response>
export type MiddlewareHandler = (c: Context, next: Function) => Promise<void>
export type MiddlewareHandler = (c: Context, next: Next) => Promise<void>
export type NotFoundHandler = (c: Context) => Response | Promise<Response>
export type ErrorHandler = (err: Error, c: Context) => Response
export type Next = () => Promise<void>
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type ParamKeyName<NameWithPattern> = 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')

View File

@ -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'

View File

@ -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) {

View File

@ -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()
}

View File

@ -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)

View File

@ -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) {

View File

@ -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()

View File

@ -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.

View File

@ -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)

View File

@ -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')

View File

@ -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')
}

View File

@ -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()

View File

@ -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)