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 kreya.rest namespace. For example:

import { expect } from 'chai';

kreya.rest.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​

HttpScriptResponseSize​

type HttpScriptResponseSize = {
bodySize: number;
compressedBodySize?: number;
compressedSize?: number;
compressionRatio?: number;
encoding?: string;
headerSize: number;
size: number;
};

Properties​

bodySize​
readonly bodySize: number;
compressedBodySize?​
readonly optional compressedBodySize?: number;
compressedSize?​
readonly optional compressedSize?: number;
compressionRatio?​
readonly optional compressionRatio?: number;
encoding?​
readonly optional encoding?: string;
headerSize​
readonly headerSize: number;
size​
readonly size: number;

HttpScriptStatus​

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

Properties​

code​
readonly code: number;
isSuccess​
readonly isSuccess: boolean;
reasonPhrase?​
readonly optional reasonPhrase?: string;

RestScriptCallCompletedContext​

type RestScriptCallCompletedContext = {
durationMillis: number;
headers: Record<string, string>;
response: RestScriptResponse;
status: HttpScriptStatus;
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: HttpScriptStatus;

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;
sizeInfo: HttpScriptResponseSize;
};

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;
rawContentText​
readonly rawContentText: string;
size​
readonly size: number;
sizeInfo​
readonly sizeInfo: HttpScriptResponseSize;

The size information for the response.


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).