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));
});
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
| Parameter | Type | Description |
|---|---|---|
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
| Parameter | Type | Description |
|---|---|---|
callback | (arg: RestScriptStreamEventReceivedContext) => void | Promise<void> |
Returns
void
Type Aliases
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;
};
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;
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).