Back to Qwik

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

packages/docs/src/routes/api/qwik-city-middleware-request-handler/index.mdx

1.7.127.0 KB
Original Source

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

<h2 id="abortmessage">AbortMessage</h2>
typescript
export declare class AbortMessage

Edit this section

<h2 id="cookie-append">append</h2>

Appends a Response cookie header using the Set-Cookie header.

The difference between set() and append() is that if the specified header already exists, set() will overwrite the existing value with the new one, whereas append() will append the new value onto the end of the set of values.

typescript
append(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
<table><thead><tr><th>

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

name

</td><td>

string

</td><td> </td></tr> <tr><td>

value

</td><td>

string | number | Record<string, any>

</td><td> </td></tr> <tr><td>

options

</td><td>

CookieOptions

</td><td>

(Optional)

</td></tr> </tbody></table>

Returns:

void

<h2 id="cachecontrol">CacheControl</h2>
typescript
export type CacheControl =
  | CacheControlOptions
  | number
  | "day"
  | "week"
  | "month"
  | "year"
  | "no-cache"
  | "immutable"
  | "private";

Edit this section

<h2 id="clientconn">ClientConn</h2>
typescript
export interface ClientConn
<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

country?

</td><td> </td><td>

string

</td><td>

(Optional)

</td></tr> <tr><td>

ip?

</td><td> </td><td>

string

</td><td>

(Optional)

</td></tr> </tbody></table>

Edit this section

<h2 id="cookie">Cookie</h2>
typescript
export interface Cookie
<table><thead><tr><th>

Method

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

append(name, value, options)

</td><td>

Appends a Response cookie header using the Set-Cookie header.

The difference between set() and append() is that if the specified header already exists, set() will overwrite the existing value with the new one, whereas append() will append the new value onto the end of the set of values.

</td></tr> <tr><td>

delete(name, options)

</td><td>

Deletes cookie value by name using the Response cookie header.

</td></tr> <tr><td>

get(name)

</td><td>

Gets a Request cookie header value by name.

</td></tr> <tr><td>

getAll()

</td><td>

Gets all Request cookie headers.

</td></tr> <tr><td>

has(name)

</td><td>

Checks if the Request cookie header name exists.

</td></tr> <tr><td>

headers()

</td><td>

Returns an array of all the set Response Set-Cookie header values.

</td></tr> <tr><td>

set(name, value, options)

</td><td>

Sets a Response cookie header using the Set-Cookie header.

</td></tr> </tbody></table>

Edit this section

<h2 id="cookieoptions">CookieOptions</h2>

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie

typescript
export interface CookieOptions
<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

domain?

</td><td> </td><td>

string

</td><td>

(Optional) Defines the host to which the cookie will be sent. If omitted, this attribute defaults to the host of the current document URL, not including subdomains.

</td></tr> <tr><td>

expires?

</td><td> </td><td>

Date | string

</td><td>

(Optional) Indicates the maximum lifetime of the cookie as an HTTP-date timestamp. If both expires and maxAge are set, maxAge has precedence.

</td></tr> <tr><td>

httpOnly?

</td><td> </td><td>

boolean

</td><td>

(Optional) Forbids JavaScript from accessing the cookie, for example, through the document.cookie property.

</td></tr> <tr><td>

maxAge?

</td><td> </td><td>

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

</td><td>

(Optional) Indicates the number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. If both expires and maxAge are set, maxAge has precedence. You can also use the array syntax to set the max-age using minutes, hours, days or weeks. For example, { maxAge: [3, "days"] } would set the cookie to expire in 3 days.

</td></tr> <tr><td>

path?

</td><td> </td><td>

string

</td><td>

(Optional) Indicates the path that must exist in the requested URL for the browser to send the Cookie header.

</td></tr> <tr><td>

sameSite?

</td><td> </td><td>

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

</td><td>

(Optional) Controls whether or not a cookie is sent with cross-site requests, providing some protection against cross-site request forgery attacks (CSRF).

</td></tr> <tr><td>

secure?

</td><td> </td><td>

boolean

</td><td>

(Optional) Indicates that the cookie is sent to the server only when a request is made with the https: scheme (except on localhost)

</td></tr> </tbody></table>

Edit this section

<h2 id="cookievalue">CookieValue</h2>
typescript
export interface CookieValue
<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

json

</td><td> </td><td>

<T = unknown>() => T

</td><td> </td></tr> <tr><td>

number

</td><td> </td><td>

() => number

</td><td> </td></tr> <tr><td>

value

</td><td> </td><td>

string

</td><td> </td></tr> </tbody></table>

Edit this section

<h2 id="servererror-data">data</h2>
typescript
data: T;
<h2 id="deferreturn">DeferReturn</h2>
typescript
export type DeferReturn<T> = () => Promise<T>;

Edit this section

<h2 id="cookie-delete">delete</h2>

Deletes cookie value by name using the Response cookie header.

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

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

name

</td><td>

string

</td><td> </td></tr> <tr><td>

options

</td><td>

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

</td><td>

(Optional)

</td></tr> </tbody></table>

Returns:

void

<h2 id="envgetter">EnvGetter</h2>
typescript
export interface EnvGetter
<table><thead><tr><th>

Method

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

get(key)

</td><td> </td></tr> </tbody></table>

Edit this section

<h2 id="cookie-get">get</h2>

Gets a Request cookie header value by name.

typescript
get(name: string): CookieValue | null;
<table><thead><tr><th>

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

name

</td><td>

string

</td><td> </td></tr> </tbody></table>

Returns:

CookieValue | null

<h2 id="cookie-getall">getAll</h2>

Gets all Request cookie headers.

typescript
getAll(): Record<string, CookieValue>;

Returns:

Record<string, CookieValue>

<h2 id="geterrorhtml">getErrorHtml</h2>
typescript
export declare function getErrorHtml(status: number, e: any): string;
<table><thead><tr><th>

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

status

</td><td>

number

</td><td> </td></tr> <tr><td>

e

</td><td>

any

</td><td> </td></tr> </tbody></table>

Returns:

string

Edit this section

<h2 id="cookie-has">has</h2>

Checks if the Request cookie header name exists.

typescript
has(name: string): boolean;
<table><thead><tr><th>

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

name

</td><td>

string

</td><td> </td></tr> </tbody></table>

Returns:

boolean

<h2 id="cookie-headers">headers</h2>

Returns an array of all the set Response Set-Cookie header values.

typescript
headers(): string[];

Returns:

string[]

<h2 id="mergeheaderscookies">mergeHeadersCookies</h2>
typescript
mergeHeadersCookies: (headers: Headers, cookies: CookieInterface) => Headers;
<table><thead><tr><th>

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

headers

</td><td>

Headers

</td><td> </td></tr> <tr><td>

cookies

</td><td>

CookieInterface

</td><td> </td></tr> </tbody></table>

Returns:

Headers

Edit this section

<h2 id="rewritemessage-pathname">pathname</h2>
typescript
readonly pathname: string;
<h2 id="redirectmessage">RedirectMessage</h2>
typescript
export declare class RedirectMessage extends AbortMessage

Extends: AbortMessage

Edit this section

<h2 id="requestevent">RequestEvent</h2>
typescript
export interface RequestEvent<PLATFORM = QwikCityPlatform> extends RequestEventCommon<PLATFORM>

Extends: RequestEventCommon<PLATFORM>

<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

exited

</td><td>

readonly

</td><td>

boolean

</td><td>

True if the middleware chain has finished executing.

</td></tr> <tr><td>

getWritableStream

</td><td>

readonly

</td><td>

() => WritableStream<Uint8Array>

</td><td>

Low-level access to write to the HTTP response stream. Once getWritableStream() is called, the status and headers can no longer be modified and will be sent over the network.

</td></tr> <tr><td>

headersSent

</td><td>

readonly

</td><td>

boolean

</td><td>

True if headers have been sent, preventing any more headers from being set.

</td></tr> <tr><td>

next

</td><td>

readonly

</td><td>

() => Promise<void>

</td><td>

Invoke the next middleware function in the chain.

NOTE: Ensure that the call to next() is awaited.

</td></tr> </tbody></table>

Edit this section

<h2 id="requesteventaction">RequestEventAction</h2>
typescript
export interface RequestEventAction<PLATFORM = QwikCityPlatform> extends RequestEventCommon<PLATFORM>

Extends: RequestEventCommon<PLATFORM>

<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

fail

</td><td> </td><td>

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

</td><td> </td></tr> </tbody></table>

Edit this section

<h2 id="requesteventbase">RequestEventBase</h2>
typescript
export interface RequestEventBase<PLATFORM = QwikCityPlatform>
<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

basePathname

</td><td>

readonly

</td><td>

string

</td><td>

The base pathname of the request, which can be configured at build time. Defaults to /.

</td></tr> <tr><td>

cacheControl

</td><td>

readonly

</td><td>

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

</td><td>

Convenience method to set the Cache-Control header. Depending on your CDN, you may want to add another cacheControl with the second argument set to CDN-Cache-Control or any other value (we provide the most common values for auto-complete, but you can use any string you want).

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control and https://qwik.dev/docs/caching/\#CDN-Cache-Controls for more information.

</td></tr> <tr><td>

clientConn

</td><td>

readonly

</td><td>

ClientConn

</td><td>

Provides information about the client connection, such as the IP address and the country the request originated from.

</td></tr> <tr><td>

cookie

</td><td>

readonly

</td><td>

Cookie

</td><td>

HTTP request and response cookie. Use the get() method to retrieve a request cookie value. Use the set() method to set a response cookie value.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies

</td></tr> <tr><td>

env

</td><td>

readonly

</td><td>

EnvGetter

</td><td>

Platform provided environment variables.

</td></tr> <tr><td>

headers

</td><td>

readonly

</td><td>

Headers

</td><td>

HTTP response headers. Notice it will be empty until you first add a header. If you want to read the request headers, use request.headers instead.

https://developer.mozilla.org/en-US/docs/Glossary/Response\_header

</td></tr> <tr><td>

method

</td><td>

readonly

</td><td>

string

</td><td>

HTTP request method.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

</td></tr> <tr><td>

originalUrl

</td><td>

readonly

</td><td>

URL

</td><td>

The original HTTP request URL.

This property was introduced to support the rewrite feature.

If rewrite is called, the url property will be changed to the rewritten url. while originalUrl will stay the same(e.g the url inserted to the address bar).

If rewrite is never called as part of the request, the url property and the originalUrl are equal.

</td></tr> <tr><td>

params

</td><td>

readonly

</td><td>

Readonly<Record<string, string>>

</td><td>

URL path params which have been parsed from the current url pathname segments. Use query to instead retrieve the query string search params.

</td></tr> <tr><td>

parseBody

</td><td>

readonly

</td><td>

() => Promise<unknown>

</td><td>

This method will check the request headers for a Content-Type header and parse the body accordingly. It supports application/json, application/x-www-form-urlencoded, and multipart/form-data content types.

If the Content-Type header is not set, it will return null.

</td></tr> <tr><td>

pathname

</td><td>

readonly

</td><td>

string

</td><td>

URL pathname. Does not include the protocol, domain, query string (search params) or hash.

https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname

</td></tr> <tr><td>

platform

</td><td>

readonly

</td><td>

PLATFORM

</td><td>

Platform specific data and functions

</td></tr> <tr><td>

query

</td><td>

readonly

</td><td>

URLSearchParams

</td><td>

URL Query Strings (URL Search Params). Use params to instead retrieve the route params found in the url pathname.

https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

</td></tr> <tr><td>

request

</td><td>

readonly

</td><td>

Request

</td><td>

HTTP request information.

</td></tr> <tr><td>

sharedMap

</td><td>

readonly

</td><td>

Map<string, any>

</td><td>

Shared Map across all the request handlers. Every HTTP request will get a new instance of the shared map. The shared map is useful for sharing data between request handlers.

</td></tr> <tr><td>

signal

</td><td>

readonly

</td><td>

AbortSignal

</td><td>

Request's AbortSignal (same as request.signal). This signal indicates that the request has been aborted.

</td></tr> <tr><td>

url

</td><td>

readonly

</td><td>

URL

</td><td>

HTTP request URL.

</td></tr> </tbody></table>

Edit this section

<h2 id="requesteventcommon">RequestEventCommon</h2>
typescript
export interface RequestEventCommon<PLATFORM = QwikCityPlatform> extends RequestEventBase<PLATFORM>

Extends: RequestEventBase<PLATFORM>

<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

error

</td><td>

readonly

</td><td>

<T = any>(statusCode: ErrorCodes, message: T) => ServerError<T>

</td><td>

When called, the response will immediately end with the given status code. This could be useful to end a response with 404, and use the 404 handler in the routes directory. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status for which status code should be used.

</td></tr> <tr><td>

exit

</td><td>

readonly

</td><td>

() => AbortMessage

</td><td> </td></tr> <tr><td>

html

</td><td>

readonly

</td><td>

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

</td><td>

Convenience method to send an HTML body response. The response will be automatically set the Content-Type header totext/html; charset=utf-8. An html() response can only be called once.

</td></tr> <tr><td>

json

</td><td>

readonly

</td><td>

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

</td><td>

Convenience method to JSON stringify the data and send it in the response. The response will be automatically set the Content-Type header to application/json; charset=utf-8. A json() response can only be called once.

</td></tr> <tr><td>

locale

</td><td>

readonly

</td><td>

(local?: string) => string

</td><td>

Which locale the content is in.

The locale value can be retrieved from selected methods using getLocale():

</td></tr> <tr><td>

redirect

</td><td>

readonly

</td><td>

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

</td><td>

URL to redirect to. When called, the response will immediately end with the correct redirect status and headers.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections

</td></tr> <tr><td>

rewrite

</td><td>

readonly

</td><td>

(pathname: string) => RewriteMessage

</td><td>

When called, qwik-city will execute the path's matching route flow.

The url in the browser will remain unchanged.

</td></tr> <tr><td>

send

</td><td>

readonly

</td><td>

SendMethod

</td><td>

Send a body response. The Content-Type response header is not automatically set when using send() and must be set manually. A send() response can only be called once.

</td></tr> <tr><td>

status

</td><td>

readonly

</td><td>

(statusCode?: StatusCodes) => number

</td><td>

HTTP response status code. Sets the status code when called with an argument. Always returns the status code, so calling status() without an argument will can be used to return the current status code.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

</td></tr> <tr><td>

text

</td><td>

readonly

</td><td>

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

</td><td>

Convenience method to send an text body response. The response will be automatically set the Content-Type header totext/plain; charset=utf-8. An text() response can only be called once.

</td></tr> </tbody></table>

Edit this section

<h2 id="requesteventloader">RequestEventLoader</h2>
typescript
export interface RequestEventLoader<PLATFORM = QwikCityPlatform> extends RequestEventAction<PLATFORM>

Extends: RequestEventAction<PLATFORM>

<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

defer

</td><td> </td><td>

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

</td><td> </td></tr> <tr><td>

resolveValue

</td><td> </td><td>

ResolveValue

</td><td> </td></tr> </tbody></table>

Edit this section

<h2 id="requesthandler-function">requestHandler</h2>
typescript
export type RequestHandler<PLATFORM = QwikCityPlatform> = (
  ev: RequestEvent<PLATFORM>,
) => Promise<void> | void;

References: RequestEvent

Edit this section

<h2 id="requesthandler-type-alias">RequestHandler</h2>
typescript
export type RequestHandler<PLATFORM = QwikCityPlatform> = (
  ev: RequestEvent<PLATFORM>,
) => Promise<void> | void;

References: RequestEvent

Edit this section

<h2 id="resolvesyncvalue">ResolveSyncValue</h2>
typescript
export interface ResolveSyncValue

Edit this section

<h2 id="resolvevalue">ResolveValue</h2>
typescript
export interface ResolveValue

Edit this section

<h2 id="rewritemessage">RewriteMessage</h2>
typescript
export declare class RewriteMessage extends AbortMessage

Extends: AbortMessage

<table><thead><tr><th>

Constructor

</th><th>

Modifiers

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

(constructor)(pathname)

</td><td> </td><td>

Constructs a new instance of the RewriteMessage class

</td></tr> </tbody></table> <table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

pathname

</td><td>

readonly

</td><td>

string

</td><td> </td></tr> </tbody></table>

Edit this section

<h2 id="servererror">ServerError</h2>
typescript
export declare class ServerError<T = any> extends Error

Extends: Error

<table><thead><tr><th>

Constructor

</th><th>

Modifiers

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

(constructor)(status, data)

</td><td> </td><td>

Constructs a new instance of the ServerError class

</td></tr> </tbody></table> <table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

data

</td><td> </td><td>

T

</td><td> </td></tr> <tr><td>

status

</td><td> </td><td>

number

</td><td> </td></tr> </tbody></table>

Edit this section

<h2 id="serverrenderoptions">ServerRenderOptions</h2>
typescript
export interface ServerRenderOptions extends RenderOptions

Extends: RenderOptions

<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

checkOrigin?

</td><td> </td><td>

boolean | 'lax-proto'

</td><td>

(Optional) Protection against cross-site request forgery (CSRF) attacks.

When true, for every incoming POST, PUT, PATCH, or DELETE form submissions, the request origin is checked to match the server's origin. lax-proto is for SSL-terminating proxies

Be careful when disabling this option as it may lead to CSRF attacks.

Defaults to true.

</td></tr> <tr><td>

qwikCityPlan

</td><td> </td><td>

QwikCityPlan

</td><td> </td></tr> <tr><td>

render

</td><td> </td><td>

Render

</td><td> </td></tr> </tbody></table>

Edit this section

<h2 id="serverrequestevent">ServerRequestEvent</h2>

Request event created by the server.

typescript
export interface ServerRequestEvent<T = unknown>
<table><thead><tr><th>

Property

</th><th>

Modifiers

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

env

</td><td> </td><td>

EnvGetter

</td><td> </td></tr> <tr><td>

getClientConn

</td><td> </td><td>

() => ClientConn

</td><td> </td></tr> <tr><td>

getWritableStream

</td><td> </td><td>

ServerResponseHandler<T>

</td><td> </td></tr> <tr><td>

locale

</td><td> </td><td>

string | undefined

</td><td> </td></tr> <tr><td>

mode

</td><td> </td><td>

ServerRequestMode

</td><td> </td></tr> <tr><td>

platform

</td><td> </td><td>

QwikCityPlatform

</td><td> </td></tr> <tr><td>

request

</td><td> </td><td>

Request

</td><td> </td></tr> <tr><td>

url

</td><td> </td><td>

URL

</td><td> </td></tr> </tbody></table>

Edit this section

<h2 id="serverrequestmode">ServerRequestMode</h2>
typescript
export type ServerRequestMode = "dev" | "static" | "server";

Edit this section

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

References: Cookie

Edit this section

<h2 id="cookie-set">set</h2>

Sets a Response cookie header using the Set-Cookie header.

typescript
set(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
<table><thead><tr><th>

Parameter

</th><th>

Type

</th><th>

Description

</th></tr></thead> <tbody><tr><td>

name

</td><td>

string

</td><td> </td></tr> <tr><td>

value

</td><td>

string | number | Record<string, any>

</td><td> </td></tr> <tr><td>

options

</td><td>

CookieOptions

</td><td>

(Optional)

</td></tr> </tbody></table>

Returns:

void

<h2 id="servererror-status">status</h2>
typescript
status: number;