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:
parent
a101f8067d
commit
798425a17d
@ -18,11 +18,12 @@ declare global {
|
|||||||
|
|
||||||
export type Handler<RequestParamKeyType = string> = (
|
export type Handler<RequestParamKeyType = string> = (
|
||||||
c: Context<RequestParamKeyType>,
|
c: Context<RequestParamKeyType>,
|
||||||
next?: Function
|
next?: Next
|
||||||
) => Response | Promise<Response>
|
) => 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 NotFoundHandler = (c: Context) => Response | Promise<Response>
|
||||||
export type ErrorHandler = (err: Error, c: Context) => Response
|
export type ErrorHandler = (err: Error, c: Context) => Response
|
||||||
|
export type Next = () => Promise<void>
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||||
type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}`
|
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)
|
const res = await handler(context)
|
||||||
if (!(res instanceof Response)) {
|
if (!(res instanceof Response)) {
|
||||||
throw new TypeError('response must be a instace of Response')
|
throw new TypeError('response must be a instace of Response')
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
export { Hono } from '@/hono'
|
export { Hono } from '@/hono'
|
||||||
export type { Handler, MiddlewareHandler } from '@/hono'
|
export type { Handler, MiddlewareHandler, Next } from '@/hono'
|
||||||
export { Context } from '@/context'
|
export { Context } from '@/context'
|
||||||
export type { Env } from '@/context'
|
export type { Env } from '@/context'
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
import { timingSafeEqual } from '@/utils/buffer'
|
import { timingSafeEqual } from '@/utils/buffer'
|
||||||
import { decodeBase64 } from '@/utils/crypto'
|
import { decodeBase64 } from '@/utils/crypto'
|
||||||
|
|
||||||
@ -45,7 +46,7 @@ export const basicAuth = (
|
|||||||
}
|
}
|
||||||
users.unshift({ username: options.username, password: options.password })
|
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)
|
const requestUser = auth(ctx.req)
|
||||||
|
|
||||||
if (requestUser) {
|
if (requestUser) {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
import { parseBody } from '@/utils/body'
|
import { parseBody } from '@/utils/body'
|
||||||
|
|
||||||
export const bodyParse = () => {
|
export const bodyParse = () => {
|
||||||
return async (ctx: Context, next: Function) => {
|
return async (ctx: Context, next: Next) => {
|
||||||
ctx.req.parsedBody = await parseBody(ctx.req)
|
ctx.req.parsedBody = await parseBody(ctx.req)
|
||||||
await next()
|
await next()
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Request {
|
interface Request {
|
||||||
@ -25,7 +26,7 @@ export type CookieOptions = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const cookie = () => {
|
export const cookie = () => {
|
||||||
return async (c: Context, next: Function) => {
|
return async (c: Context, next: Next) => {
|
||||||
c.req.cookie = (name: string): string => {
|
c.req.cookie = (name: string): string => {
|
||||||
const cookie = c.req.headers.get('Cookie')
|
const cookie = c.req.headers.get('Cookie')
|
||||||
const obj = parse(cookie)
|
const obj = parse(cookie)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
|
|
||||||
type CORSOptions = {
|
type CORSOptions = {
|
||||||
origin: string
|
origin: string
|
||||||
@ -21,7 +22,7 @@ export const cors = (options?: CORSOptions) => {
|
|||||||
...options,
|
...options,
|
||||||
}
|
}
|
||||||
|
|
||||||
return async (c: Context, next: Function) => {
|
return async (c: Context, next: Next) => {
|
||||||
await next()
|
await next()
|
||||||
|
|
||||||
function set(key: string, value: string) {
|
function set(key: string, value: string) {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
import { parseBody } from '@/utils/body'
|
import { parseBody } from '@/utils/body'
|
||||||
import { sha1 } from '@/utils/crypto'
|
import { sha1 } from '@/utils/crypto'
|
||||||
|
|
||||||
@ -7,7 +8,7 @@ type ETagOptions = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const etag = (options: ETagOptions = { weak: false }) => {
|
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')
|
const ifNoneMatch = c.req.header('If-None-Match') || c.req.header('if-none-match')
|
||||||
|
|
||||||
await next()
|
await next()
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
// Based on the code in the `express-graphql` package.
|
// Based on the code in the `express-graphql` package.
|
||||||
// https://github.com/graphql/express-graphql/blob/main/src/index.ts
|
// https://github.com/graphql/express-graphql/blob/main/src/index.ts
|
||||||
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Source,
|
Source,
|
||||||
parse,
|
parse,
|
||||||
@ -20,7 +19,9 @@ import type {
|
|||||||
FormattedExecutionResult,
|
FormattedExecutionResult,
|
||||||
GraphQLFormattedError,
|
GraphQLFormattedError,
|
||||||
} from 'graphql'
|
} from 'graphql'
|
||||||
|
|
||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
import { parseBody } from '@/middleware/graphql-server/parse-body'
|
import { parseBody } from '@/middleware/graphql-server/parse-body'
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
@ -38,7 +39,7 @@ export const graphqlServer = (options: Options) => {
|
|||||||
const validationRules = options.validationRules ?? []
|
const validationRules = options.validationRules ?? []
|
||||||
// const showGraphiQL = options.graphiql ?? false
|
// const showGraphiQL = options.graphiql ?? false
|
||||||
|
|
||||||
return async (c: Context, next: Function) => {
|
return async (c: Context, next: Next) => {
|
||||||
await next()
|
await next()
|
||||||
|
|
||||||
// GraphQL HTTP only supports GET and POST methods.
|
// GraphQL HTTP only supports GET and POST methods.
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
import { getPathFromURL } from '@/utils/url'
|
import { getPathFromURL } from '@/utils/url'
|
||||||
|
|
||||||
const humanize = (n: string[], opts?: { delimiter?: string; separator?: string }) => {
|
const humanize = (n: string[], opts?: { delimiter?: string; separator?: string }) => {
|
||||||
@ -52,7 +53,7 @@ function log(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const logger = (fn = console.log) => {
|
export const logger = (fn = console.log) => {
|
||||||
return async (c: Context, next: Function) => {
|
return async (c: Context, next: Next) => {
|
||||||
const { method } = c.req
|
const { method } = c.req
|
||||||
const path = getPathFromURL(c.req.url)
|
const path = getPathFromURL(c.req.url)
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { getContentFromKVAsset, getKVFilePath } from '../../utils/cloudflare'
|
|
||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
|
import { getContentFromKVAsset, getKVFilePath } from '@/utils/cloudflare'
|
||||||
|
|
||||||
const EXTENSION = '.mustache'
|
const EXTENSION = '.mustache'
|
||||||
const DEFAULT_DOCUMENT = 'index.mustache'
|
const DEFAULT_DOCUMENT = 'index.mustache'
|
||||||
@ -17,7 +18,7 @@ type Init = {
|
|||||||
export const mustache = (init: Init = { root: '' }) => {
|
export const mustache = (init: Init = { root: '' }) => {
|
||||||
const { root } = init
|
const { root } = init
|
||||||
|
|
||||||
return async (c: Context, next: Function) => {
|
return async (c: Context, next: Next) => {
|
||||||
let Mustache: Mustache
|
let Mustache: Mustache
|
||||||
try {
|
try {
|
||||||
Mustache = require('mustache')
|
Mustache = require('mustache')
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
|
|
||||||
export const poweredBy = () => {
|
export const poweredBy = () => {
|
||||||
return async (c: Context, next: Function) => {
|
return async (c: Context, next: Next) => {
|
||||||
await next()
|
await next()
|
||||||
c.res.headers.append('X-Powered-By', 'Hono')
|
c.res.headers.append('X-Powered-By', 'Hono')
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
|
|
||||||
type prettyOptions = {
|
type prettyOptions = {
|
||||||
space: number
|
space: number
|
||||||
}
|
}
|
||||||
|
|
||||||
export const prettyJSON = (options: prettyOptions = { space: 2 }) => {
|
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
|
const pretty = c.req.query('pretty') || c.req.query('pretty') === '' ? true : false
|
||||||
c.pretty(pretty, options.space)
|
c.pretty(pretty, options.space)
|
||||||
await next()
|
await next()
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { Context } from '@/context'
|
import type { Context } from '@/context'
|
||||||
|
import type { Next } from '@/hono'
|
||||||
import { getContentFromKVAsset, getKVFilePath } from '@/utils/cloudflare'
|
import { getContentFromKVAsset, getKVFilePath } from '@/utils/cloudflare'
|
||||||
import { getMimeType } from '@/utils/mime'
|
import { getMimeType } from '@/utils/mime'
|
||||||
|
|
||||||
@ -10,7 +11,7 @@ const DEFAULT_DOCUMENT = 'index.html'
|
|||||||
|
|
||||||
// This middleware is available only on Cloudflare Workers.
|
// This middleware is available only on Cloudflare Workers.
|
||||||
export const serveStatic = (opt: Options = { root: '' }) => {
|
export const serveStatic = (opt: Options = { root: '' }) => {
|
||||||
return async (c: Context, next: Function) => {
|
return async (c: Context, next: Next) => {
|
||||||
await next()
|
await next()
|
||||||
const url = new URL(c.req.url)
|
const url = new URL(c.req.url)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user