API β€Ί @builder.io/qwik-city/middleware/request-handler

AbortMessage

export declare class AbortMessage

Edit this section

CacheControl

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

Edit this section

ClientConn

export interface ClientConn
PropertyModifiersTypeDescription
country?string(Optional)
ip?string(Optional)

Edit this section

export interface Cookie
MethodDescription
delete(name, options)Deletes cookie value by name using the Response cookie header.
get(name)Gets a Request cookie header value by name.
getAll()Gets all Request cookie headers.
has(name)Checks if the Request cookie header name exists.
headers()Returns an array of all the set Response Set-Cookie header values.
set(name, value, options)Sets a Response cookie header using the Set-Cookie header.

Edit this section

CookieOptions

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

export interface CookieOptions
PropertyModifiersTypeDescription
domain?string(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.
expires?Date | string(Optional) Indicates the maximum lifetime of the cookie as an HTTP-date timestamp. If both expires and maxAge are set, maxAge has precedence.
httpOnly?boolean(Optional) Forbids JavaScript from accessing the cookie, for example, through the document.cookie property.
maxAge?number | [number, 'seconds' | 'minutes' | 'hours' | 'days' | 'weeks'](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.
path?string(Optional) Indicates the path that must exist in the requested URL for the browser to send the Cookie header.
sameSite?'strict' | 'lax' | 'none' | 'Strict' | 'Lax' | 'None' | boolean(Optional) Controls whether or not a cookie is sent with cross-site requests, providing some protection against cross-site request forgery attacks (CSRF).
secure?boolean(Optional) Indicates that the cookie is sent to the server only when a request is made with the https: scheme (except on localhost)

Edit this section

CookieValue

export interface CookieValue
PropertyModifiersTypeDescription
json<T = unknown>() => T
number() => number
valuestring

Edit this section

DeferReturn

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

Edit this section

delete

Deletes cookie value by name using the Response cookie header.

delete(name: string, options?: Pick<CookieOptions, 'path' | 'domain'>): void;
ParameterTypeDescription
namestring
optionsPick<CookieOptions, 'path' | 'domain'>(Optional)

Returns:

void

EnvGetter

export interface EnvGetter
MethodDescription
get(key)

Edit this section

get

Gets a Request cookie header value by name.

get(name: string): CookieValue | null;
ParameterTypeDescription
namestring

Returns:

CookieValue | null

getAll

Gets all Request cookie headers.

getAll(): Record<string, CookieValue>;

Returns:

Record<string, CookieValue>

getErrorHtml

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

Returns:

string

Edit this section

has

Checks if the Request cookie header name exists.

has(name: string): boolean;
ParameterTypeDescription
namestring

Returns:

boolean

headers

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

headers(): string[];

Returns:

string[]

mergeHeadersCookies

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

Edit this section

RedirectMessage

export declare class RedirectMessage extends AbortMessage

Extends: AbortMessage

Edit this section

RequestEvent

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

Extends: RequestEventCommon<PLATFORM>

PropertyModifiersTypeDescription
exitedreadonlybooleanTrue if the middleware chain has finished executing.
getWritableStreamreadonly() => WritableStream<Uint8Array>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.
headersSentreadonlybooleanTrue if headers have been sent, preventing any more headers from being set.
nextreadonly() => Promise<void>Invoke the next middleware function in the chain.NOTE: Ensure that the call to next() is awaited.

Edit this section

RequestEventAction

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

Extends: RequestEventCommon<PLATFORM>

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

Edit this section

RequestEventBase

export interface RequestEventBase<PLATFORM = QwikCityPlatform>
PropertyModifiersTypeDescription
basePathnamereadonlystringThe base pathname of the request, which can be configured at build time. Defaults to /.
cacheControlreadonly(cacheControl: CacheControl, target?: CacheControlTarget) => voidConvenience 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.builder.io/docs/caching/\#CDN-Cache-Controls for more information.
clientConnreadonlyClientConnProvides information about the client connection, such as the IP address and the country the request originated from.
cookiereadonlyCookieHTTP 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
envreadonlyEnvGetterPlatform provided environment variables.
headersreadonlyHeadersHTTP 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
methodreadonlystringHTTP request method.https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
paramsreadonlyReadonly<Record<string, string>>URL path params which have been parsed from the current url pathname segments. Use query to instead retrieve the query string search params.
parseBodyreadonly() => Promise<unknown>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.
pathnamereadonlystringURL pathname. Does not include the protocol, domain, query string (search params) or hash.https://developer.mozilla.org/en-US/docs/Web/API/URL/pathname
platformreadonlyPLATFORMPlatform specific data and functions
queryreadonlyURLSearchParamsURL 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
requestreadonlyRequestHTTP request information.
sharedMapreadonlyMap<string, any>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.
signalreadonlyAbortSignalRequest's AbortSignal (same as request.signal). This signal indicates that the request has been aborted.
urlreadonlyURLHTTP request URL.

Edit this section

RequestEventCommon

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

Extends: RequestEventBase<PLATFORM>

PropertyModifiersTypeDescription
errorreadonly(statusCode: ErrorCodes, message: string) => ErrorResponseWhen 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.
exitreadonly() => AbortMessage
htmlreadonly(statusCode: StatusCodes, html: string) => AbortMessageConvenience 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.
jsonreadonly(statusCode: StatusCodes, data: any) => AbortMessageConvenience 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.
localereadonly(local?: string) => stringWhich locale the content is in.The locale value can be retrieved from selected methods using getLocale():
redirectreadonly(statusCode: RedirectCode, url: string) => RedirectMessageURL 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
sendreadonlySendMethodSend 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.
statusreadonly(statusCode?: StatusCodes) => numberHTTP 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
textreadonly(statusCode: StatusCodes, text: string) => AbortMessageConvenience 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.

Edit this section

RequestEventLoader

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

Extends: RequestEventAction<PLATFORM>

PropertyModifiersTypeDescription
defer<T>(returnData: Promise<T> | (() => Promise<T>)) => DeferReturn<T>
resolveValueResolveValue

Edit this section

requestHandler

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

References: RequestEvent

Edit this section

RequestHandler

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

References: RequestEvent

Edit this section

ResolveSyncValue

export interface ResolveSyncValue

Edit this section

ResolveValue

export interface ResolveValue

Edit this section

ServerRenderOptions

export interface ServerRenderOptions extends RenderOptions

Extends: RenderOptions

PropertyModifiersTypeDescription
checkOrigin?boolean(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.Be careful when disabling this option as it may lead to CSRF attacks.Defaults to true.
qwikCityPlanQwikCityPlan
renderRender

Edit this section

ServerRequestEvent

Request event created by the server.

export interface ServerRequestEvent<T = unknown>
PropertyModifiersTypeDescription
envEnvGetter
getClientConn() => ClientConn
getWritableStreamServerResponseHandler<T>
localestring | undefined
modeServerRequestMode
platformQwikCityPlatform
requestRequest
urlURL

Edit this section

ServerRequestMode

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

Edit this section

ServerResponseHandler

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

References: Cookie

Edit this section

set

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

set(name: string, value: string | number | Record<string, any>, options?: CookieOptions): void;
ParameterTypeDescription
namestring
valuestring | number | Record<string, any>
optionsCookieOptions(Optional)

Returns:

void