Core
Framework v2.x
2

KuzzleRequest #

Available since 2.11.0

Instantiates a new request.

Plugins and applications rarely need to build a request themselves: the EmbeddedSDK does it for them. The constructor is mostly used by protocols, and to rebuild a request from its serialized form.


Constructor #

new KuzzleRequest(data: JSONObject, options?: JSONObject);

ArgumentsTypeDescription
data
JSONObject
API call, following the request payload format
options
JSONObject
(optional) Request context and initial response (see below)

data #

The jwt, volatile, body, controller and action properties are stored at the root of the request input. Every other property is stored in input.args.

These properties are also read:

NameTypeDescription
requestId
string
Request id. A UUID is generated if not set
timestamp
number
Request timestamp, in milliseconds since Epoch. Defaults to Date.now()

options #

NameTypeDescription
connection
JSONObject
Connection information: id, protocol and ips. Any other property is stored in connection.misc (see RequestContext)
token
KuzzleToken
Authentication token
user
KuzzleUser
Associated user
result
any
Sets the request result, and the status to 200
error
Error
Sets the request error. A plain object is converted to a KuzzleError (status 500 unless it has a status property)
status
integer
Request status. Applied last, so it overrides the status set by result or error

Errors #

The constructor throws an InternalError if data is not an object, or if options is set to anything other than an object.

Usage #

import { KuzzleRequest } from 'kuzzle';

const request = new KuzzleRequest(
  {
    controller: 'document',
    action: 'create',
    index: 'nyc-open-data',
    collection: 'yellow-taxi',
    body: { name: 'Aschen' },
  },
  {
    connection: { id: 'connection-id', protocol: 'websocket' },
  });

request.getIndex(); // "nyc-open-data"
request.getBody(); // { name: "Aschen" }