Skip to main content

REST script API reference

In operation scripts for REST operations, despite the operation script API, the definitions below are available through the kreyaRest namespace. For example:

import { expect } from 'chai';

kreyaRest.onCallCompleted(call => {
kreya.trace('The REST call completed.');

kreya.test('Status code', () => expect(call.status.code).to.equal(200));
kreya.test('status is success', () => expect(call.status.isSuccess).to.be.true);

kreya.test('Content type', () => expect(call.response.contentType).to.eq('application/json'));

const curlyBraceCharCode = '{'.charCodeAt(0);
kreya.test('Byte content first entry should be a curly brace', () => expect(call.response.rawContentBytes[0]).to.eq(curlyBraceCharCode));
kreya.test('Text content first char should be a curly brace', () => expect(call.response.rawContentText[0]).to.eq('{'));

kreya.test('Deserialized number should equal', () => expect(call.response.content.myInt).to.eq(26));
});
info

Accessing call.response.content, call.response.rawContentText or rcall.response.awContentBytes will throw an exception if the response size is greater than 10 MiB.

Accessing call.response.content will throw if the response content is not valid JSON or YAML.

Functions

onCallCompleted()

function onCallCompleted(callback: (arg: RestScriptCallCompletedContext) => void | Promise<void>): void;

Hook which is called when a REST call completes.

Parameters

ParameterTypeDescription
callback(arg: RestScriptCallCompletedContext) => void | Promise<void>The callback to be invoked.

Returns

void


onStreamEventReceived()

function onStreamEventReceived(callback: (arg: RestScriptStreamEventReceivedContext) => void | Promise<void>): void;

Hook which is called when a streamed event is received.

Parameters

ParameterTypeDescription
callback(arg: RestScriptStreamEventReceivedContext) => void | Promise<void>

Returns

void

Type Aliases

RestScriptCallCompletedContext

type RestScriptCallCompletedContext = {
durationMillis: number;
headers: Record<string, string>;
response: RestScriptResponse;
status: RestScriptStatus;
trailers: Record<string, string>;
};

Callback context of REST completed call callback.

Properties

durationMillis
readonly durationMillis: number;

Duration of the call in milliseconds.

headers
readonly headers: Record<string, string>;

The received REST headers.

response
readonly response: RestScriptResponse;

The response.

status
readonly status: RestScriptStatus;

The status of the call.

trailers
readonly trailers: Record<string, string>;

The received REST trailers.


RestScriptResponse

type RestScriptResponse = {
content: any;
contentType: string;
rawContentBytes: Uint8Array;
rawContentText: string;
size: number;
};

Response of a REST call.

Properties

content
readonly content: any;

The parsed and deserialized content, if available. Only available for JSON and YAML content types.

contentType
readonly contentType: string;

The content type.

rawContentBytes
readonly rawContentBytes: Uint8Array;

The byte representation of the content.

rawContentText
readonly rawContentText: string;

The string representation of the content.

size
readonly size: number;

The size in bytes.


RestScriptStatus

type RestScriptStatus = {
code: number;
isSuccess: boolean;
reasonPhrase: string;
};

Status of a REST call.

Properties

code
readonly code: number;

The status code.

isSuccess
readonly isSuccess: boolean;

True if the status code was in the range 200-299.

reasonPhrase?
readonly optional reasonPhrase: string;

The optional reason phrase.


RestScriptStreamEventReceivedContext

type RestScriptStreamEventReceivedContext = {
data: string;
event: string;
id: string;
};

Callback context of server sent event received context.

Properties

data?
readonly optional data: string;

The data of the event.

event?
readonly optional event: string;

The event of the event (if a server sent event and the event field is set).

id?
readonly optional id: string;

The id of the event (if a server sent event and the id field is set).