API › @builder.io/qwik-city/middleware/request-handler

AbortMessage

export declare class AbortMessage

编辑此部分

append

使用 Set-Cookie 头部追加 Response cookie 头部。

set()append() 之间的区别在于,如果指定的头部已经存在,set() 将用新值覆盖现有值,而 append() 将将新值追加到值集的末尾。

append(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;

参数

类型

描述

name

string

value

string | number | Record<string, any>

options

CookieOptions

(可选)

void

CacheControl

export type CacheControl =
  | CacheControlOptions
  | number
  | "day"
  | "week"
  | "month"
  | "year"
  | "no-cache"
  | "immutable"
  | "private";

编辑此部分

ClientConn

export interface ClientConn

属性

修饰符

类型

描述

country?

string

(可选)

ip?

string

(可选)

编辑此部分

export interface Cookie

方法

描述

append(name, value, options)

使用 Set-Cookie 头部追加 Response cookie 头部。

set()append() 之间的区别在于,如果指定的头部已经存在,set() 将用新值覆盖现有值,而 append() 将将新值追加到值集的末尾。

delete(name, options)

使用 Response cookie 头部按名称删除 cookie 值。

get(name)

按名称获取 Request cookie 头部值。

getAll()

获取所有 Request cookie 头部。

has(name)

检查 Request cookie 头部名称是否存在。

headers()

返回所有设置的 Response Set-Cookie 头部值的数组。

set(name, value, options)

使用 Set-Cookie 头部设置 Response cookie 头部。

编辑此部分

CookieOptions

https://mdn.org.cn/en-US/docs/Web/HTTP/Headers/Set-Cookie

export interface CookieOptions

属性

修饰符

类型

描述

domain?

string

(可选) 定义将发送 cookie 的主机。如果省略,此属性默认为当前文档 URL 的主机,不包括子域。

expires?

Date | string

(可选) 以 HTTP 日期时间戳的形式指示 cookie 的最大生命周期。如果同时设置了 expiresmaxAge,则 maxAge 优先。

httpOnly?

boolean

(可选) 禁止 JavaScript 访问 cookie,例如,通过 document.cookie 属性。

maxAge?

number | [number, 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks']

(可选) 指示 cookie 过期前的秒数。零或负数将立即使 cookie 过期。如果同时设置了 expiresmaxAge,则 maxAge 优先。您还可以使用数组语法以分钟、小时、天或周为单位设置最大年龄。例如,{ maxAge: [3, "days"] } 将使 cookie 在 3 天后过期。

path?

string

(可选) 指示请求 URL 中必须存在的路径,以便浏览器发送 Cookie 头部。

sameSite?

'strict' | 'lax' | 'none' | 'Strict' | 'Lax' | 'None' | boolean

(可选) 控制 cookie 是否随跨站点请求发送,提供一定程度的防止跨站点请求伪造攻击 (CSRF) 的保护。

secure?

boolean

(可选) 指示只有在使用 https: 方案(除 localhost 外)进行请求时,才会将 cookie 发送到服务器。

编辑此部分

CookieValue

export interface CookieValue

属性

修饰符

类型

描述

json

<T = unknown>() => T

number

() => number

value

string

编辑此部分

data

data: T;

DeferReturn

export type DeferReturn<T> = () => Promise<T>;

编辑此部分

delete

使用 Response cookie 头部按名称删除 cookie 值。

delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain' | 'sameSite'>): void;

参数

类型

描述

name

string

options

Pick<CookieOptions, 'path' | 'domain' | 'sameSite'>

(可选)

void

EnvGetter

export interface EnvGetter

方法

描述

get(key)

编辑此部分

get

按名称获取 Request cookie 头部值。

get(name: string): CookieValue | null;

参数

类型

描述

name

string

CookieValue | null

getAll

获取所有 Request cookie 头部。

getAll(): Record<string, CookieValue>;

返回

Record<string, CookieValue>

getErrorHtml

export declare function getErrorHtml(status: number, e: any): string;

参数

类型

描述

status

number

e

any

string

编辑此部分

has

检查 Request cookie 头部名称是否存在。

has(name: string): boolean;

参数

类型

描述

name

string

boolean

headers

返回所有设置的 Response Set-Cookie 头部值的数组。

headers(): string[];

返回

string[]

mergeHeadersCookies

mergeHeadersCookies: (headers: Headers, cookies: CookieInterface) => Headers;

参数

类型

描述

headers

Headers

cookies

CookieInterface

Headers

编辑此部分

RedirectMessage

export declare class RedirectMessage extends AbortMessage

扩展自: AbortMessage

编辑此部分

RequestEvent

export interface RequestEvent<PLATFORM = QwikCityPlatform> extends RequestEventCommon<PLATFORM>

扩展自: RequestEventCommon<PLATFORM>

属性

修饰符

类型

描述

exited

readonly

boolean

如果中间件链已完成执行,则为 true。

getWritableStream

readonly

() => WritableStream<Uint8Array>

用于写入 HTTP 响应流的低级访问。一旦调用 getWritableStream(),状态和头部将不再可修改,并将通过网络发送。

headersSent

readonly

boolean

如果头部已发送,则为 true,阻止设置更多头部。

next

readonly

() => Promise<void>

调用链中的下一个中间件函数。

注意:确保对 next() 的调用被 await

编辑此部分

RequestEventAction

export interface RequestEventAction<PLATFORM = QwikCityPlatform> extends RequestEventCommon<PLATFORM>

扩展自: RequestEventCommon<PLATFORM>

属性

修饰符

类型

描述

fail

<T extends Record<string, any>>(status: number, returnData: T) => FailReturn<T>

编辑此部分

RequestEventBase

export interface RequestEventBase<PLATFORM = QwikCityPlatform>

属性

修饰符

类型

描述

basePathname

readonly

string

请求的基路径名,可以在构建时配置。默认为 /

cacheControl

readonly

(cacheControl: CacheControl, target?: CacheControlTarget) => void

用于设置 Cache-Control 头部的便捷方法。根据您的 CDN,您可能需要添加另一个 cacheControl,并将第二个参数设置为 CDN-Cache-Control 或任何其他值(我们提供了最常见的自动完成值,但您可以使用任何您想要的字符串)。

有关更多信息,请参阅 https://mdn.org.cn/en-US/docs/Web/HTTP/Headers/Cache-Controlhttps://qwik.node.org.cn/docs/caching/\#CDN-Cache-Controls

clientConn

readonly

ClientConn

提供有关客户端连接的信息,例如 IP 地址和请求来源的国家/地区。

cookie

readonly

Cookie

HTTP 请求和响应 cookie。使用 get() 方法检索请求 cookie 值。使用 set() 方法设置响应 cookie 值。

https://mdn.org.cn/en-US/docs/Web/HTTP/Cookies

env

readonly

EnvGetter

平台提供的环境变量。

headers

readonly

Headers

HTTP 响应头部。请注意,它在您第一次添加头部之前将为空。如果您想读取请求头部,请使用 request.headers 代替。

https://mdn.org.cn/en-US/docs/Glossary/Response\_header

method

readonly

string

HTTP 请求方法。

https://mdn.org.cn/en-US/docs/Web/HTTP/Methods

params

readonly

Readonly<Record<string, string>>

从当前 URL 路径名段解析的 URL 路径参数。使用 query 代替检索查询字符串搜索参数。

parseBody

readonly

() => Promise<unknown>

此方法将检查请求头部以获取 Content-Type 头部,并相应地解析主体。它支持 application/jsonapplication/x-www-form-urlencodedmultipart/form-data 内容类型。

如果未设置 Content-Type 头部,它将返回 null

pathname

readonly

string

URL 路径名。不包括协议、域、查询字符串(搜索参数)或哈希值。

https://mdn.org.cn/en-US/docs/Web/API/URL/pathname

平台

readonly

PLATFORM

平台特定数据和函数

查询

readonly

URLSearchParams

URL 查询字符串 (URL 搜索参数)。使用 params 代替检索 URL 路径名中的路由参数。

https://mdn.org.cn/en-US/docs/Web/API/URLSearchParams

请求

readonly

Request

HTTP 请求信息。

共享映射

readonly

Map<string, any>

所有请求处理程序共享的映射。每个 HTTP 请求都将获得一个新的共享映射实例。共享映射对于在请求处理程序之间共享数据很有用。

信号

readonly

AbortSignal

请求的 AbortSignal(与 request.signal 相同)。此信号表示请求已被中止。

网址

readonly

URL

HTTP 请求 URL。

编辑此部分

RequestEventCommon

export interface RequestEventCommon<PLATFORM = QwikCityPlatform> extends RequestEventBase<PLATFORM>

扩展: RequestEventBase<PLATFORM>

属性

修饰符

类型

描述

错误

readonly

(statusCode: ErrorCodes, message: string) => ErrorResponse

调用时,响应将立即以给定的状态码结束。这对于以 404 结束响应并使用路由目录中的 404 处理程序可能很有用。有关应使用哪个状态码,请参阅 https://mdn.org.cn/en-US/docs/Web/HTTP/Status

退出

readonly

() => AbortMessage

html

readonly

(statusCode: StatusCodes, html: string) => AbortMessage

用于发送 HTML 主体响应的便捷方法。响应将自动将 Content-Type 标头设置为 text/html; charset=utf-8html() 响应只能调用一次。

json

readonly

(statusCode: StatusCodes, data: any) => AbortMessage

用于将数据 JSON 字符串化并在响应中发送的便捷方法。响应将自动将 Content-Type 标头设置为 application/json; charset=utf-8json() 响应只能调用一次。

语言环境

readonly

(local?: string) => string

内容所在的语言环境。

语言环境值可以使用 getLocale() 从选定的方法中检索。

重定向

readonly

(statusCode: RedirectCode, url: string) => RedirectMessage

要重定向到的 URL。调用时,响应将立即以正确的重定向状态和标头结束。

https://mdn.org.cn/en-US/docs/Web/HTTP/Redirections

发送

readonly

SendMethod

发送主体响应。使用 send() 时,Content-Type 响应标头不会自动设置,必须手动设置。send() 响应只能调用一次。

status

readonly

(statusCode?: StatusCodes) => number

HTTP 响应状态码。在使用参数调用时设置状态码。始终返回状态码,因此在不使用参数的情况下调用 status() 可以用于返回当前状态码。

https://mdn.org.cn/en-US/docs/Web/HTTP/Status

文本

readonly

(statusCode: StatusCodes, text: string) => AbortMessage

用于发送文本主体响应的便捷方法。响应将自动将 Content-Type 标头设置为 text/plain; charset=utf-8text() 响应只能调用一次。

编辑此部分

RequestEventLoader

export interface RequestEventLoader<PLATFORM = QwikCityPlatform> extends RequestEventAction<PLATFORM>

扩展: RequestEventAction<PLATFORM>

属性

修饰符

类型

描述

延迟

<T>(returnData: Promise<T> | (() => Promise<T>)) => DeferReturn<T>

解析值

ResolveValue

编辑此部分

requestHandler

export type RequestHandler<PLATFORM = QwikCityPlatform> = (
  ev: RequestEvent<PLATFORM>,
) => Promise<void> | void;

引用: RequestEvent

编辑此部分

RequestHandler

export type RequestHandler<PLATFORM = QwikCityPlatform> = (
  ev: RequestEvent<PLATFORM>,
) => Promise<void> | void;

引用: RequestEvent

编辑此部分

ResolveSyncValue

export interface ResolveSyncValue

编辑此部分

ResolveValue

export interface ResolveValue

编辑此部分

ServerError

export declare class ServerError<T = Record<any, any>> extends Error

扩展: Error

构造函数

修饰符

描述

(constructor)(status, data)

构造 ServerError 类的新实例

属性

修饰符

类型

描述

数据

T

status

number

编辑此部分

ServerRenderOptions

export interface ServerRenderOptions extends RenderOptions

扩展: RenderOptions

属性

修饰符

类型

描述

checkOrigin?

boolean

(可选) 防范跨站点请求伪造 (CSRF) 攻击的保护措施。

true 时,对于每个传入的 POST、PUT、PATCH 或 DELETE 表单提交,都会检查请求来源是否与服务器来源匹配。

在禁用此选项时要小心,因为它可能会导致 CSRF 攻击。

默认为 true

qwikCityPlan

QwikCityPlan

渲染

Render

编辑此部分

ServerRequestEvent

由服务器创建的请求事件。

export interface ServerRequestEvent<T = unknown>

属性

修饰符

类型

描述

env

EnvGetter

getClientConn

() => ClientConn

getWritableStream

ServerResponseHandler<T>

语言环境

string | undefined

模式

ServerRequestMode

平台

QwikCityPlatform

请求

Request

网址

URL

编辑此部分

ServerRequestMode

export type ServerRequestMode = "dev" | "static" | "server";

编辑此部分

ServerResponseHandler

export type ServerResponseHandler<T = any> = (
  status: number,
  headers: Headers,
  cookies: Cookie,
  resolve: (response: T) => void,
  requestEv: RequestEventInternal,
) => WritableStream<Uint8Array>;

引用: Cookie

编辑此部分

set

使用 Set-Cookie 头部设置 Response cookie 头部。

set(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;

参数

类型

描述

name

string

value

string | number | Record<string, any>

options

CookieOptions

(可选)

void

status

status: number;