Class: IncomingHttpEvent
Class: IncomingHttpEvent
Class representing an IncomingHttpEvent.
Author
Mr. Stone evensstone@gmail.com
Extends
IncomingEvent
Constructors
Constructor
protected new IncomingHttpEvent(options): IncomingHttpEvent;
Constructor for IncomingHttpEvent.
Parameters
options
The options to create an IncomingHttpEvent instance.
Returns
IncomingHttpEvent
Throws
If the URL option is not a valid instance of URL.
Overrides
IncomingEvent.constructor
Properties
_headers
protected readonly _headers: Headers;
The headers of the request.
accepts
readonly accepts: Accepts;
The content negotiation handler for the request.
body
readonly body: Record<string, unknown>;
The body of the request.
cookies
readonly cookies: CookieCollection;
The cookies included in the request.
files
readonly files: Record<string, UploadedFile[]>;
The files included in the request.
ip
readonly ip: string;
The IP address of the client making the request.
ips
readonly ips: string[];
The list of IP addresses, typically for proxies.
method
readonly method: HttpMethods;
The HTTP method of the request.
protocol
readonly protocol: string;
The protocol used for the request (e.g., http or https).
query
readonly query: URLSearchParams;
The query parameters of the request.
queryString?
readonly optional queryString: string;
The query string of the request.
routeResolver()?
protected optional routeResolver: () => IRoute;
Returns
url
readonly url: URL;
The URL of the request.
userResolver()?
protected optional userResolver: () => unknown;
Returns
unknown
INCOMING_HTTP_EVENT
readonly static INCOMING_HTTP_EVENT: "stonejs@incoming_http_event" = 'stonejs@incoming_http_event';
Accessors
charset
Get Signature
get charset(): undefined | string;
Returns
undefined
| string
The charset specified in the content-type header.
charsets
Get Signature
get charsets(): string[];
Returns
string
[]
An array of acceptable character sets for the request.
contentType
Get Signature
get contentType(): string;
Returns
string
The content type specified in the headers.
decodedPathname
Get Signature
get decodedPathname(): undefined | string;
Returns
undefined
| string
The decoded pathname of the URL.
encodings
Get Signature
get encodings(): string[];
Returns
string
[]
An array of acceptable encodings for the request.
etag
Get Signature
get etag(): undefined | string;
Returns
undefined
| string
The ETag of the request, if present.
hash
Get Signature
get hash(): string;
Returns
string
The hash part of the URL.
headers
Get Signature
get headers(): Record<string, string>;
Returns
Record
<string
, string
>
The headers of the request.
host
Get Signature
get host(): string;
Returns
string
The host of the URL (hostname:port).
hostname
Get Signature
get hostname(): string;
Returns
string
The hostname of the URL.
isAjax
Get Signature
get isAjax(): boolean;
Returns
boolean
Whether the request is an AJAX request.
isPrefetch
Get Signature
get isPrefetch(): boolean;
Returns
boolean
Whether the request was prefetch.
isSecure
Get Signature
get isSecure(): boolean;
Returns
boolean
Whether the request was made over a secure connection.
isXhr
Get Signature
get isXhr(): boolean;
Returns
boolean
Whether the request is an XMLHttpRequest.
languages
Get Signature
get languages(): string[];
Returns
string
[]
An array of acceptable languages for the request.
params
Get Signature
get params(): undefined | Record<string, unknown>;
Returns
undefined
| Record
<string
, unknown
>
The route parameters.
path
Get Signature
get path(): string;
Returns
string
The full path including pathname and search query.
pathname
Get Signature
get pathname(): string;
Returns
string
The pathname of the URL.
scheme
Get Signature
get scheme(): string;
Returns
string
The protocol of the URL (e.g., "http" or "https").
segments
Get Signature
get segments(): string[];
Returns
string
[]
The URL segments split by '/'.
types
Get Signature
get types(): string[];
Returns
string
[]
An array of acceptable content types for the request.
uri
Get Signature
get uri(): string;
Returns
string
The full URL as a string.
userAgent
Get Signature
get userAgent(): undefined | string;
Returns
undefined
| string
The user agent of the request.
Methods
acceptsCharsets()
acceptsCharsets(...values): string | false | string[];
Return the first accepted charset.
Parameters
values
...string
[]
The charsets to check.
Returns
string
| false
| string
[]
The first accepted charset, or false if none are accepted.
acceptsEncodings()
acceptsEncodings(...values): string | false | string[];
Return the first accepted encoding.
Parameters
values
...string
[]
The encodings to check.
Returns
string
| false
| string
[]
The first accepted encoding, or false if none are accepted.
acceptsLanguages()
acceptsLanguages(...values): string | false | string[];
Return the first accepted language.
Parameters
values
...string
[]
The languages to check.
Returns
string
| false
| string
[]
The first accepted language, or false if none are accepted.
acceptsTypes()
acceptsTypes(...values): string | false | string[];
Return the first accepted content type.
Parameters
values
...string
[]
The content types to check.
Returns
string
| false
| string
[]
The first accepted type, or false if none are accepted.
filterFiles()
filterFiles(files): Record<string, UploadedFile[]>;
Filter and return files based on their names.
Parameters
files
string
[]
The array of file names to filter.
Returns
Record
<string
, UploadedFile
[]>
An object containing the filtered files.
fingerprint()
fingerprint(full?): string;
Generate a unique fingerprint for the event.
Parameters
full?
boolean
Whether to include the user agent and IP address in the fingerprint.
Returns
string
The generated fingerprint as a base64 string.
get()
Get data from the request.
Priority:
- Route params
- Body
- Query params
- Headers
- Cookies
- Metadata
- Fallback value
Param
The key to look for.
Param
A fallback value if the key is not found.
Call Signature
get<TReturn>(key): undefined | TReturn;
Get data from the request.
Priority:
- Route params
- Body
- Query params
- Headers
- Cookies
- Metadata
- Fallback value
Type Parameters
TReturn
TReturn
= unknown
Parameters
key
string
The key to look for.
Returns
undefined
| TReturn
The value of the key or the fallback.
Param
The key to look for.
Param
A fallback value if the key is not found.
Overrides
IncomingEvent.get
Call Signature
get<TReturn>(key, fallback): TReturn;
Get data from the request.
Priority:
- Route params
- Body
- Query params
- Headers
- Cookies
- Metadata
- Fallback value
Type Parameters
TReturn
TReturn
= unknown
Parameters
key
string
The key to look for.
fallback
TReturn
A fallback value if the key is not found.
Returns
TReturn
The value of the key or the fallback.
Param
The key to look for.
Param
A fallback value if the key is not found.
Overrides
IncomingEvent.get
getBody()
Get the body of the request.
Param
The fallback value if the body is not found.
Call Signature
getBody<TReturn>(): undefined | TReturn;
Get the body of the request.
Type Parameters
TReturn
TReturn
= unknown
Returns
undefined
| TReturn
The body of the request or the fallback.
Param
The fallback value if the body is not found.
Call Signature
getBody<TReturn>(fallback): TReturn;
Get the body of the request.
Type Parameters
TReturn
TReturn
= unknown
Parameters
fallback
TReturn
The fallback value if the body is not found.
Returns
TReturn
The body of the request or the fallback.
Param
The fallback value if the body is not found.
getCookie()
Get a cookie value.
Param
The cookie name.
Param
A fallback value if the cookie is not found.
Call Signature
getCookie<TReturn>(name): undefined | TReturn;
Get a cookie value.
Type Parameters
TReturn
TReturn
extends Cookie
= Cookie
Parameters
name
string
The cookie name.
Returns
undefined
| TReturn
The cookie value or the fallback.
Param
The cookie name.
Param
A fallback value if the cookie is not found.
Call Signature
getCookie<TReturn>(name, fallback): TReturn;
Get a cookie value.
Type Parameters
TReturn
TReturn
extends Cookie
= Cookie
Parameters
name
string
The cookie name.
fallback
TReturn
A fallback value if the cookie is not found.
Returns
TReturn
The cookie value or the fallback.
Param
The cookie name.
Param
A fallback value if the cookie is not found.
getFile()
getFile(name): undefined | UploadedFile[];
Get a file by its name.
Parameters
name
string
The name of the file.
Returns
undefined
| UploadedFile
[]
The file if it exists, otherwise undefined.
getFormat()
getFormat(mimeType): undefined | string;
Get file extension for a given MIME type.
Parameters
mimeType
string
The MIME type.
Returns
undefined
| string
The corresponding file extension, or undefined if not found.
getHeader()
Get a header value.
Param
The header name.
Param
A fallback value if the header is not found.
Throws
If the header name is not a valid string.
Call Signature
getHeader<TReturn>(name): undefined | TReturn;
Get a header value.
Type Parameters
TReturn
TReturn
= string
Parameters
name
string
The header name.
Returns
undefined
| TReturn
The header value or the fallback value.
Param
The header name.
Param
A fallback value if the header is not found.
Throws
If the header name is not a valid string.
Throws
If the header name is not a valid string.
Call Signature
getHeader<TReturn>(name, fallback): TReturn;
Get a header value.
Type Parameters
TReturn
TReturn
= string
Parameters
name
string
The header name.
fallback
TReturn
A fallback value if the header is not found.
Returns
TReturn
The header value or the fallback value.
Param
The header name.
Param
A fallback value if the header is not found.
Throws
If the header name is not a valid string.
Throws
If the header name is not a valid string.
getMimeType()
getMimeType(format): undefined | string;
Get MIME type for a given file path or extension.
Parameters
format
string
The file path or extension.
Returns
undefined
| string
The corresponding MIME type, or undefined if not found.
getParam()
Retrieve a parameter from the route if it exists.
Param
The name of the parameter to retrieve.
Param
The fallback value if the parameter does not exist.
Call Signature
getParam<TReturn>(name): undefined | TReturn;
Retrieve a parameter from the route if it exists.
Type Parameters
TReturn
TReturn
= unknown
Parameters
name
string
The name of the parameter to retrieve.
Returns
undefined
| TReturn
The value of the parameter if it exists, otherwise undefined.
Param
The name of the parameter to retrieve.
Param
The fallback value if the parameter does not exist.
Call Signature
getParam<TReturn>(name, fallback): TReturn;
Retrieve a parameter from the route if it exists.
Type Parameters
TReturn
TReturn
= unknown
Parameters
name
string
The name of the parameter to retrieve.
fallback
TReturn
The fallback value if the parameter does not exist.
Returns
TReturn
The value of the parameter if it exists, otherwise undefined.
Param
The name of the parameter to retrieve.
Param
The fallback value if the parameter does not exist.
getRoute()
getRoute<RouteType>(): undefined | RouteType;
Return the current route or a route parameter.
Type Parameters
RouteType
RouteType
extends IRoute
= IRoute
Returns
undefined
| RouteType
The route parameter or the route object.
getRouteResolver()
getRouteResolver(): () => undefined | IRoute;
Get the route resolver function.
Returns
The route resolver function.
(): undefined | IRoute;
Returns
undefined
| IRoute
getUri()
getUri(withDomain): undefined | string;
Get the URI with or without the domain.
Parameters
withDomain
boolean
= false
Whether to include the domain in the URI.
Returns
undefined
| string
The URI with or without the domain.
getUser()
getUser<T>(): undefined | T;
Get the user instance.
Type Parameters
T
T
Returns
undefined
| T
The user object, resolved through a user resolver function if available.
getUserResolver()
getUserResolver(): () => unknown;
Get the user resolver function.
Returns
The user resolver function.
(): unknown;
Returns
unknown
hasCookie()
hasCookie(name): boolean;
Check if a cookie exists.
Parameters
name
string
The cookie name to check.
Returns
boolean
True if the cookie exists, otherwise false.
hasFile()
hasFile(name): boolean;
Check if a file exists by its name.
Parameters
name
string
The name of the file.
Returns
boolean
True if the file exists, otherwise false.
hasHeader()
hasHeader(name): boolean;
Check if a header exists.
Parameters
name
string
The header name to check.
Returns
boolean
True if the header exists, otherwise false.
hasJson()
hasJson(key): boolean;
Check if a key exists in the JSON body.
Parameters
key
string
The key to check for.
Returns
boolean
True if the key exists, otherwise false.
isFresh()
isFresh(response): boolean;
Determine if the response cache is fresh.
Parameters
response
The outgoing HTTP response to check freshness against.
Returns
boolean
True if the cache is fresh, otherwise false.
isMethod()
isMethod(method): boolean;
Check if the current event method matches the given method.
Parameters
method
string
The method to check.
Returns
boolean
True if the event method matches, otherwise false.
isMethodCacheable()
isMethodCacheable(): boolean;
Check if the current event method is cacheable.
Returns
boolean
True if the method is cacheable, otherwise false.
isMethodSafe()
isMethodSafe(): boolean;
Check if the current event method is considered safe.
Returns
boolean
True if the method is safe, otherwise false.
isStale()
isStale(response): boolean;
Determine if the response cache is stale.
Parameters
response
The outgoing HTTP response to check staleness against.
Returns
boolean
True if the cache is stale, otherwise false.
isType()
isType(...types): string | false;
Check if the request matches one of the given content types.
Parameters
types
...string
[]
The content types to check.
Returns
string
| false
The best match, or false if no match is found.
json()
Get a value from the JSON body.
Param
The key to look for in the JSON body.
Param
A fallback value if the key is not found.
Call Signature
json<TReturn>(key): undefined | TReturn;
Get a value from the JSON body.
Type Parameters
TReturn
TReturn
= unknown
Parameters
key
string
The key to look for in the JSON body.
Returns
undefined
| TReturn
The value of the key or the fallback.
Param
The key to look for in the JSON body.
Param
A fallback value if the key is not found.
Call Signature
json<TReturn>(key, fallback): TReturn;
Get a value from the JSON body.
Type Parameters
TReturn
TReturn
= unknown
Parameters
key
string
The key to look for in the JSON body.
fallback
TReturn
A fallback value if the key is not found.
Returns
TReturn
The value of the key or the fallback.
Param
The key to look for in the JSON body.
Param
A fallback value if the key is not found.
preferredType()
preferredType(types, defaultType): string;
Determines the preferred response type based on content negotiation. Uses Accept, Content-Type, User-Agent, and AJAX detection.
Parameters
types
string
[] = ...
Allowed response types, in priority order.
defaultType
string
= 'json'
Default type if none match.
Returns
string
The best response type as a string.
range()
range(size, combine): undefined | Result | Ranges;
Get request range.
Parameters
size
number
The maximum size of the resource.
combine
boolean
= false
Specifies if overlapping & adjacent ranges should be combined.
Returns
undefined
| Result
| Ranges
The parsed range, or undefined if not applicable.
setRouteResolver()
setRouteResolver<RouteType>(resolver): this;
Set the route resolver function.
Type Parameters
RouteType
RouteType
extends IRoute
= IRoute
Parameters
resolver
() => RouteType
The route resolver function.
Returns
this
The current instance for method chaining.
setUserResolver()
setUserResolver(resolver): this;
Set the user resolver function.
Parameters
resolver
() => unknown
The user resolver function.
Returns
this
The current instance for method chaining.
uriForPath()
uriForPath(path): string;
Generate a full URL for the given path.
Parameters
path
string
The path to append to the base URL.
Returns
string
The full URL for the given path.
create()
static create(options): IncomingHttpEvent;
Create an IncomingHttpEvent.
Parameters
options
The IncomingHttpEvent options.
Returns
IncomingHttpEvent
A new instance of IncomingHttpEvent.
Overrides
IncomingEvent.create