Class: JsonResponse
Class: JsonResponse
Class representing a JsonResponse.
Author
Mr. Stone evensstone@gmail.com
Extends
Extended by
Constructors
Constructor
new JsonResponse(options): JsonResponse;
Constructor for OutgoingHttpResponse. Initializes headers and cookies based on the provided options.
Parameters
options
Options for the outgoing HTTP response.
Returns
JsonResponse
Inherited from
OutgoingHttpResponse
.constructor
Properties
_blueprintResolver()?
protected optional _blueprintResolver: () => undefined | IBlueprint;
Returns
undefined
| IBlueprint
Inherited from
OutgoingHttpResponse
._blueprintResolver
_charset?
protected optional _charset: Encoding;
Inherited from
_cookieCollection
protected readonly _cookieCollection: CookieCollection;
Inherited from
OutgoingHttpResponse
._cookieCollection
_formats?
protected optional _formats: Record<string, () => unknown>;
Inherited from
_headers
protected readonly _headers: Headers;
Inherited from
_incomingEventResolver()?
protected optional _incomingEventResolver: () => IncomingHttpEvent;
Returns
Inherited from
OutgoingHttpResponse
._incomingEventResolver
OUTGOING_HTTP_RESPONSE
static OUTGOING_HTTP_RESPONSE: string = 'stonejs@outgoing_http_json_response';
Overrides
OutgoingHttpResponse
.OUTGOING_HTTP_RESPONSE
Accessors
blueprint
Get Signature
get blueprint(): undefined | IBlueprint;
Get the blueprint associated with the response.
Returns
undefined
| IBlueprint
The blueprint or undefined if not set.
Inherited from
OutgoingHttpResponse
.blueprint
charset
Get Signature
get charset(): Encoding;
Get the character set encoding. Defaults to 'utf-8' if not explicitly set.
Returns
Encoding
The character set encoding.
Inherited from
charsetRegExp
Get Signature
get protected charsetRegExp(): RegExp;
Get the regular expression for matching charset in content type.
Returns
RegExp
The regular expression for matching charset in content type.
Inherited from
OutgoingHttpResponse
.charsetRegExp
etag
Get Signature
get etag(): undefined | string;
Get the ETag of the response.
Returns
undefined
| string
The value of the ETag header, if present.
Inherited from
headers
Get Signature
get headers(): Headers;
Get the headers of the response.
Returns
Headers
The headers of the response as a Headers object.
Inherited from
incomingEvent
Get Signature
get incomingEvent(): IncomingHttpEvent;
Get the associated IncomingHttpEvent.
Throws
InternalServerError if the IncomingHttpEvent resolver is not set.
Returns
The associated IncomingHttpEvent.
Inherited from
OutgoingHttpResponse
.incomingEvent
lastModified
Get Signature
get lastModified(): undefined | string;
Get the Last-Modified date of the response.
Returns
undefined
| string
The value of the Last-Modified header, if present.
Inherited from
OutgoingHttpResponse
.lastModified
status
Get Signature
get status(): undefined | number;
Get the HTTP status code.
Returns
undefined
| number
The HTTP status code.
Inherited from
vary
Get Signature
get vary(): undefined | string[];
Get the Vary header as an array of values.
Returns
undefined
| string
[]
The Vary header values split by comma, or undefined if not present.
Inherited from
Methods
addVary()
addVary(field): this;
Add a field to the Vary header.
Parameters
field
The field to add to the Vary header.
string
| string
[]
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
appendHeader()
appendHeader(key, value): this;
Append a value to an existing header or create a new header.
Parameters
key
string
The header name.
value
string
The value to append.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.appendHeader
calculateContentLength()
protected calculateContentLength(generateETag): number;
Calculate the content length.
Parameters
generateETag
boolean
Whether to generate an ETag for the content.
Returns
number
The content length.
Inherited from
OutgoingHttpResponse
.calculateContentLength
clearCookie()
clearCookie(
name,
options,
force): this;
Clear a specific cookie from the response.
Parameters
name
string
The name of the cookie to be cleared.
options
CookieOptions
= {}
Optional settings for the cookie.
force
boolean
= false
Whether to force the removal of the cookie, even if it doesn't exist.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.clearCookie
clearCookies()
clearCookies(force): this;
Clear all cookies from the response.
Parameters
force
boolean
= false
Whether to force the removal of all cookies.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.clearCookies
defaultEtagFn()
protected defaultEtagFn(content, encoding): string;
Generate a default ETag for the given content.
Parameters
content
string
The content to generate an ETag for.
encoding
Encoding
The encoding to use.
Returns
string
The generated ETag as a base64 string.
Inherited from
OutgoingHttpResponse
.defaultEtagFn
ensureCharset()
protected ensureCharset(value): this;
Ensure that the "Content-Type" header has a charset specified.
Parameters
value
string
The "Content-Type" header value.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.ensureCharset
format()
format(formats): this;
Handles content negotiation based on the Accept
header of the incoming request.
Parameters
formats
Record
<string
, () => unknown
>
An object where keys are MIME types and values are functions that return the content for that MIME type.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
getHashedContent()
protected getHashedContent(content, encoding): string;
Get the hashed content using the specified encoding.
Parameters
content
string
The content to hash.
encoding
Encoding
The encoding to use for hashing.
Returns
string
The hashed content as a hexadecimal string.
Inherited from
OutgoingHttpResponse
.getHashedContent
getHeader()
Get a header value.
Param
The header name.
Param
A fallback value if the header is not found.
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.
Inherited from
OutgoingHttpResponse
.getHeader
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.
Inherited from
OutgoingHttpResponse
.getHeader
getHeaderNames()
getHeaderNames(): string[];
Get all header names.
Returns
string
[]
An array of all header names.
Inherited from
OutgoingHttpResponse
.getHeaderNames
handleCacheHeaders()
protected handleCacheHeaders(): this;
Handle cache headers like ETag and Last-Modified.
Returns
this
The current instance of the response for chaining.
Inherited from
OutgoingHttpResponse
.handleCacheHeaders
handleContentNegotiation()
protected handleContentNegotiation(): this;
Handles content negotiation based on the Accept
header of the incoming request.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.handleContentNegotiation
hasHeader()
hasHeader(key): boolean;
Check if a specific header exists.
Parameters
key
string
The header name to check.
Returns
boolean
True if the header exists, false otherwise.
Inherited from
OutgoingHttpResponse
.hasHeader
is1xx()
is1xx(): boolean;
Check if the status code represents an informational response (1xx).
Returns
boolean
True if the status code is informational, otherwise false.
Inherited from
is2xx()
is2xx(): boolean;
Check if the status code represents a successful response (2xx).
Returns
boolean
True if the status code is successful, otherwise false.
Inherited from
is3xx()
is3xx(): boolean;
Check if the status code represents a redirection response (3xx).
Returns
boolean
True if the status code is a redirection, otherwise false.
Inherited from
is4xx()
is4xx(): boolean;
Check if the status code represents a client error response (4xx).
Returns
boolean
True if the status code is a client error, otherwise false.
Inherited from
is5xx()
is5xx(): boolean;
Check if the status code represents a server error response (5xx).
Returns
boolean
True if the status code is a server error, otherwise false.
Inherited from
isEmpty()
isEmpty(): boolean;
Check if the response is empty.
Returns
boolean
True if the status code indicates an empty response, otherwise false.
Inherited from
isError()
isError(): boolean;
Check if the status code is an error (i.e., 4xx or 5xx).
Returns
boolean
True if the status code is an error, otherwise false.
Inherited from
isForbidden()
isForbidden(): boolean;
Check if the status code is 403 (Forbidden).
Returns
boolean
True if the status code is 403, otherwise false.
Inherited from
OutgoingHttpResponse
.isForbidden
isInStatusRange()
isInStatusRange(start, end): boolean;
Check if the status code falls within the specified range.
Parameters
start
number
The starting value of the range (inclusive).
end
number
The ending value of the range (exclusive).
Returns
boolean
True if the status code is within the specified range, otherwise false.
Inherited from
OutgoingHttpResponse
.isInStatusRange
isInvalid()
isInvalid(): boolean;
Check if the status code is invalid.
Returns
boolean
True if the status code is invalid, otherwise false.
Inherited from
OutgoingHttpResponse
.isInvalid
isMovedPermanently()
isMovedPermanently(): boolean;
Check if the status code is 301 (Moved Permanently).
Returns
boolean
True if the status code is 301, otherwise false.
Inherited from
OutgoingHttpResponse
.isMovedPermanently
isNotError()
isNotError(): boolean;
Check if the status code is not an error (i.e., not 4xx or 5xx).
Returns
boolean
True if the status code is not an error, otherwise false.
Inherited from
OutgoingHttpResponse
.isNotError
isNotFound()
isNotFound(): boolean;
Check if the status code is 404 (Not Found).
Returns
boolean
True if the status code is 404, otherwise false.
Inherited from
OutgoingHttpResponse
.isNotFound
isOk()
isOk(): boolean;
Check if the status code is 200 (OK).
Returns
boolean
True if the status code is 200, otherwise false.
Inherited from
isRedirect()
isRedirect(location?): boolean;
Check if the response is a redirect.
Parameters
location?
string
The optional location to check for redirection.
Returns
boolean
True if the status code indicates a redirect, otherwise false.
Inherited from
OutgoingHttpResponse
.isRedirect
isResetContent()
isResetContent(): boolean;
Check if the status code is 205 (Reset Content).
Returns
boolean
True if the status code is 205, otherwise false.
Inherited from
OutgoingHttpResponse
.isResetContent
isUnauthorized()
isUnauthorized(): boolean;
Check if the status code is 401 (Unauthorized).
Returns
boolean
True if the status code is 401, otherwise false.
Inherited from
OutgoingHttpResponse
.isUnauthorized
isValidateable()
isValidateable(): boolean;
Check if the response is validateable.
Returns
boolean
True if the response has Last-Modified or ETag headers, otherwise false.
Inherited from
OutgoingHttpResponse
.isValidateable
makeJson()
protected makeJson(): this;
Make JSON response.
Returns
this
The current instance for method chaining.
morphToJson()
protected morphToJson(content, options): string;
Convert the given content to a JSON string.
Parameters
content
unknown
The content to convert.
options
Partial
<HttpJsonConfig
> = {}
Options to customize the serialization process.
Returns
string
A JSON string representation of the content.
Throws
InternalServerError if the content cannot be converted to JSON.
Inherited from
OutgoingHttpResponse
.morphToJson
prepare()
prepare(event, container?): JsonResponse | Promise<JsonResponse>;
Prepare the response before sending.
Parameters
event
The incoming HTTP event.
container?
Container
The service container.
Returns
JsonResponse
| Promise
<JsonResponse
>
The current instance of the response for chaining.
Overrides
prepareContentHeaders()
protected prepareContentHeaders(): this;
Prepare content-related headers such as Content-Length and ETag.
Returns
this
The current instance of the response for chaining.
Inherited from
OutgoingHttpResponse
.prepareContentHeaders
prepareCookies()
protected prepareCookies(): this;
Prepare cookies by setting the appropriate headers.
Returns
this
The current instance of the response for chaining.
Inherited from
OutgoingHttpResponse
.prepareCookies
removeHeader()
removeHeader(key): this;
Remove headers from the response.
Parameters
key
The header or headers to be removed.
string
| string
[]
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.removeHeader
secureCookies()
secureCookies(value): this;
Secure all cookies by setting the "Secure" attribute.
Parameters
value
boolean
= true
Whether to set or unset the "Secure" attribute for cookies.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.secureCookies
setBlueprintResolver()
setBlueprintResolver(resolver): this;
Set the resolver for the blueprint.
Parameters
resolver
() => undefined
| IBlueprint
A function that returns the blueprint.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.setBlueprintResolver
setCharset()
setCharset(value): this;
Set the character set for the response.
Parameters
value
string
The character encoding to use.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.setCharset
setContent()
setContent(value, options): this;
Set the response content. If the content should be JSON, it will be converted appropriately.
Parameters
value
unknown
The content to set.
options
Partial
<HttpJsonConfig
> = {}
The JSON options.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.setContent
setContentHeaders()
protected setContentHeaders(): this;
Set content headers such as Content-Length and ETag.
Returns
this
The current instance of the response for chaining.
Inherited from
OutgoingHttpResponse
.setContentHeaders
setContentType()
setContentType(value): this;
Set the content type of the response.
Parameters
value
string
The MIME type for the response.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Throws
InternalServerError if the provided MIME type is invalid.
Inherited from
OutgoingHttpResponse
.setContentType
setContentTypeIfNeeded()
protected setContentTypeIfNeeded(): this;
Set the content type if it's not already set.
Returns
this
The current instance of the response for chaining.
Inherited from
OutgoingHttpResponse
.setContentTypeIfNeeded
setCookie()
setCookie(
name,
value,
options): this;
Set a cookie for the response.
Parameters
name
string
The name of the cookie.
value
unknown
The value of the cookie.
options
CookieOptions
= {}
Optional settings for the cookie.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.setCookie
setEtag()
setEtag(etag?, weak?): this;
Set the ETag for the response.
Parameters
etag?
string
The ETag value to set.
weak?
boolean
= false
Whether the ETag should be marked as weak.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
setHeader()
setHeader(key, value): this;
Set a single header for the response. If the header is "Content-Type," ensures charset is set appropriately.
Parameters
key
string
The header name.
value
The value of the header.
string
| string
[]
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.setHeader
setHeaders()
setHeaders(values): this;
Set multiple headers for the response.
Parameters
values
A key-value pair of headers to be set.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.setHeaders
setIncomingEventResolver()
setIncomingEventResolver(resolver): this;
Set the resolver for the incoming HTTP event.
Parameters
resolver
() => IncomingHttpEvent
A function that returns the incoming HTTP event.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.setIncomingEventResolver
setLastModified()
setLastModified(date?): this;
Set the Last-Modified header for the response.
Parameters
date?
Date
The date to set as the Last-Modified header.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
OutgoingHttpResponse
.setLastModified
setLinks()
setLinks(links): this;
Set link headers for the response.
Parameters
links
Record
<string
, string
>
An object representing links to set.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
setStatus()
setStatus(code, text?): this;
Set the HTTP status code of the response. Also sets a default status message if none is provided.
Parameters
code
number
The HTTP status code.
text?
string
Optional status message.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Throws
InternalServerError if the status code is invalid.
Inherited from
OutgoingHttpResponse
.setStatus
setType()
setType(value): this;
Set the content type by file extension.
Parameters
value
string
The file extension.
Returns
this
The current instance of OutgoingHttpResponse for chaining.
Inherited from
shouldBeJson()
protected shouldBeJson(content): boolean;
Determine if the content should be serialized as JSON.
Parameters
content
unknown
The content to check.
Returns
boolean
True if the content should be serialized as JSON, otherwise false.
Inherited from
OutgoingHttpResponse
.shouldBeJson
stringify()
protected stringify(
value,
replacer?,
spaces?,
escape?): string;
Convert the given value to a JSON string with optional escaping.
Parameters
value
unknown
The value to convert.
replacer?
(this
, key
, value
) => unknown
A function or array that alters the behavior of the stringification process.
spaces?
string
The number of spaces to use for pretty-printing the JSON string.
escape?
boolean
Whether to escape special characters.
Returns
string
The JSON string representation of the value.
Inherited from
OutgoingHttpResponse
.stringify
create()
static create<T>(options): T;
Create an instance of OutgoingHttpResponse.
Type Parameters
T
T
extends OutgoingHttpResponse
= OutgoingHttpResponse
Parameters
options
Options for the outgoing HTTP response.
Returns
T
A new instance of OutgoingHttpResponse.