Skip to main content

gRPC script API reference

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

import { expect } from 'chai';

let names = [];

kreyaGrpc.onResponse(msg => {
kreya.test('Size should be ok', () => expect(msg.size).to.be.gt(10));
kreya.test('Greeting should start with Hello', () => expect(msg.content.greeting.startsWith('Hello')).to.be.true);

// Store all received names to use them when the gRPC call completes
names.push(msg.content.greeting.substr('Hello '.length));
});

kreyaGrpc.onCallCompleted(call => {
kreya.test('Status should be ok', () => expect(call.status.code).to.equal(0));

kreya.trace(`Got ${call.responseCount} names: ${names.join(', ')}`);
});
info

The msg.content object from the kreyaGrpc.onResponse callback will automatically be parsed to the defined response type. Kreya even provides auto completion for members of msg.content.

Functions

onCallCompleted()

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

Hook which is called when a gRPC call completes.

Parameters

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

Returns

void


onResponse()

function onResponse(callback: (arg: GrpcScriptResponseMessageContext) => void | Promise<void>): void;

Hook which is called when a gRPC response message arrives.

Parameters

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

Returns

void

Type Aliases

GrpcScriptCallCompletedContext

type GrpcScriptCallCompletedContext = {
durationMillis: number;
headers: Record<string, string>;
responseCount: number;
status: GrpcScriptStatus;
trailers: Record<string, string>;
};

Callback context of gRPC completed call callback.

Properties

durationMillis
readonly durationMillis: number;

Duration of the call in milliseconds.

headers
readonly headers: Record<string, string>;

The received gRPC headers.

responseCount
readonly responseCount: number;

The number of responses.

status
readonly status: GrpcScriptStatus;

The status of the call.

trailers
readonly trailers: Record<string, string>;

The received gRPC trailers.


GrpcScriptResponseMessageContext

type GrpcScriptResponseMessageContext = {
content: any;
index: number;
size: number;
};

Callback context of the callback when a gRPC response message has arrived.

Properties

content
readonly content: any;

The content of the message.

index
readonly index: number;

The index of the response.

size
readonly size: number;

The size of the message in bytes.


GrpcScriptStatus

type GrpcScriptStatus = {
code: number;
detail: string;
};

Status of a gRPC call.

Properties

code
readonly code: number;

The status code.

detail?
readonly optional detail: string;

The status detail.