packages/docs/src/routes/api/qwik-city-middleware-request-handler/index.mdx
export declare class AbortMessage
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.
append(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
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> </td><td>(Optional)
</td></tr> </tbody></table>Returns:
void
<h2 id="cachecontrol">CacheControl</h2>export type CacheControl =
| CacheControlOptions
| number
| "day"
| "week"
| "month"
| "year"
| "no-cache"
| "immutable"
| "private";
export interface ClientConn
Property
</th><th>Modifiers
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td> </td><td> </td><td>string
</td><td>(Optional)
</td></tr> <tr><td> </td><td> </td><td>string
</td><td>(Optional)
</td></tr> </tbody></table> <h2 id="cookie">Cookie</h2>export interface Cookie
Method
</th><th>Description
</th></tr></thead> <tbody><tr><td> </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.
Deletes cookie value by name using the Response cookie header.
Gets a Request cookie header value by name.
Gets all Request cookie headers.
Checks if the Request cookie header name exists.
Returns an array of all the set Response Set-Cookie header values.
Sets a Response cookie header using the Set-Cookie header.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie
export interface CookieOptions
Property
</th><th>Modifiers
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td> </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> </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.
boolean
</td><td>(Optional) Forbids JavaScript from accessing the cookie, for example, through the document.cookie property.
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.
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> </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> </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)
export interface CookieValue
Property
</th><th>Modifiers
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td> </td><td> </td><td><T = unknown>() => T
</td><td> </td></tr> <tr><td> </td><td> </td><td>() => number
</td><td> </td></tr> <tr><td> </td><td> </td><td>string
</td><td> </td></tr> </tbody></table> <h2 id="servererror-data">data</h2>data: T;
export type DeferReturn<T> = () => Promise<T>;
Deletes cookie value by name using the Response cookie header.
delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain' | 'sameSite'>): void;
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>export interface EnvGetter
Method
</th><th>Description
</th></tr></thead> <tbody><tr><td> </td><td> </td></tr> </tbody></table> <h2 id="cookie-get">get</h2>Gets a Request cookie header value by name.
get(name: string): CookieValue | null;
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.
getAll(): Record<string, CookieValue>;
Returns:
Record<string, CookieValue>
<h2 id="geterrorhtml">getErrorHtml</h2>export declare function getErrorHtml(status: number, e: any): string;
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
<h2 id="cookie-has">has</h2>Checks if the Request cookie header name exists.
has(name: string): boolean;
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.
headers(): string[];
Returns:
string[]
<h2 id="mergeheaderscookies">mergeHeadersCookies</h2>mergeHeadersCookies: (headers: Headers, cookies: CookieInterface) => Headers;
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> </td><td> </td></tr> </tbody></table>Returns:
Headers
<h2 id="rewritemessage-pathname">pathname</h2>readonly pathname: string;
export declare class RedirectMessage extends AbortMessage
Extends: AbortMessage
<h2 id="requestevent">RequestEvent</h2>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> </td><td>readonly
boolean
</td><td>True if the middleware chain has finished executing.
</td></tr> <tr><td> </td><td>readonly
() => 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.
readonly
boolean
</td><td>True if headers have been sent, preventing any more headers from being set.
</td></tr> <tr><td> </td><td>readonly
() => Promise<void>
</td><td>Invoke the next middleware function in the chain.
NOTE: Ensure that the call to next() is awaited.
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> </td><td> </td><td><T extends Record<string, any>>(status: number, returnData: T) => FailReturn<T>
</td><td> </td></tr> </tbody></table> <h2 id="requesteventbase">RequestEventBase</h2>export interface RequestEventBase<PLATFORM = QwikCityPlatform>
Property
</th><th>Modifiers
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td> </td><td>readonly
string
</td><td>The base pathname of the request, which can be configured at build time. Defaults to /.
readonly
(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> </td><td>readonly
Provides information about the client connection, such as the IP address and the country the request originated from.
</td></tr> <tr><td> </td><td>readonly
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> </td><td>readonly
Platform provided environment variables.
</td></tr> <tr><td> </td><td>readonly
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> </td><td>readonly
string
</td><td>HTTP request method.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
</td></tr> <tr><td> </td><td>readonly
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> </td><td>readonly
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.
readonly
() => 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.
readonly
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> </td><td>readonly
PLATFORM
</td><td>Platform specific data and functions
</td></tr> <tr><td> </td><td>readonly
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> </td><td>readonly
Request
</td><td>HTTP request information.
</td></tr> <tr><td> </td><td>readonly
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> </td><td>readonly
AbortSignal
</td><td>Request's AbortSignal (same as request.signal). This signal indicates that the request has been aborted.
readonly
URL
</td><td>HTTP request URL.
</td></tr> </tbody></table> <h2 id="requesteventcommon">RequestEventCommon</h2>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> </td><td>readonly
<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.
readonly
() => AbortMessage
</td><td> </td></tr> <tr><td> </td><td>readonly
(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.
readonly
(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.
readonly
(local?: string) => string
</td><td>Which locale the content is in.
The locale value can be retrieved from selected methods using getLocale():
readonly
(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> </td><td>readonly
(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> </td><td>readonly
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.
readonly
(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> </td><td>readonly
(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.
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> </td><td> </td><td><T>(returnData: Promise<T> | (() => Promise<T>)) => DeferReturn<T>
</td><td> </td></tr> <tr><td> </td><td> </td><td> </td><td> </td></tr> </tbody></table> <h2 id="requesthandler-function">requestHandler</h2>export type RequestHandler<PLATFORM = QwikCityPlatform> = (
ev: RequestEvent<PLATFORM>,
) => Promise<void> | void;
References: RequestEvent
<h2 id="requesthandler-type-alias">RequestHandler</h2>export type RequestHandler<PLATFORM = QwikCityPlatform> = (
ev: RequestEvent<PLATFORM>,
) => Promise<void> | void;
References: RequestEvent
<h2 id="resolvesyncvalue">ResolveSyncValue</h2>export interface ResolveSyncValue
export interface ResolveValue
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> </td><td> </td><td>Constructs a new instance of the RewriteMessage class
Property
</th><th>Modifiers
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td> </td><td>readonly
string
</td><td> </td></tr> </tbody></table> <h2 id="servererror">ServerError</h2>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> </td><td> </td><td>Constructs a new instance of the ServerError class
Property
</th><th>Modifiers
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td> </td><td> </td><td>T
</td><td> </td></tr> <tr><td> </td><td> </td><td>number
</td><td> </td></tr> </tbody></table> <h2 id="serverrenderoptions">ServerRenderOptions</h2>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> </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.
QwikCityPlan
</td><td> </td></tr> <tr><td> </td><td> </td><td>Render
</td><td> </td></tr> </tbody></table> <h2 id="serverrequestevent">ServerRequestEvent</h2>Request event created by the server.
export interface ServerRequestEvent<T = unknown>
Property
</th><th>Modifiers
</th><th>Type
</th><th>Description
</th></tr></thead> <tbody><tr><td> </td><td> </td><td> </td><td> </td></tr> <tr><td> </td><td> </td><td>() => ClientConn
</td><td> </td></tr> <tr><td> </td><td> </td><td> </td><td> </td></tr> <tr><td> </td><td> </td><td>string | undefined
</td><td> </td></tr> <tr><td> </td><td> </td><td> </td><td> </td></tr> <tr><td> </td><td> </td><td>QwikCityPlatform
</td><td> </td></tr> <tr><td> </td><td> </td><td>Request
</td><td> </td></tr> <tr><td> </td><td> </td><td>URL
</td><td> </td></tr> </tbody></table> <h2 id="serverrequestmode">ServerRequestMode</h2>export type ServerRequestMode = "dev" | "static" | "server";
export type ServerResponseHandler<T = any> = (
status: number,
headers: Headers,
cookies: Cookie,
resolve: (response: T) => void,
requestEv: RequestEventInternal,
) => WritableStream<Uint8Array>;
References: Cookie
<h2 id="cookie-set">set</h2>Sets a Response cookie header using the Set-Cookie header.
set(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
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> </td><td>(Optional)
</td></tr> </tbody></table>Returns:
void
<h2 id="servererror-status">status</h2>status: number;