SDK
SDK Javascript v7.x
2

Strong Typing #

The SDK exposes numerous types to help Typescript developer to maintain a safer codebase and prevents obvious errors.

Kuzzle Document (KDocument) #

Available since 7.9.0

The Document controller methods can be used with an explicit type that represents the content of the manipulated document.

Document content must be defined by extending the KDocumentContent interface.

Copied to clipboard!
interface DeviceContent extends KDocumentContent {
  model: string;
  reference: string;
  battery: number;
}

const device = await sdk.document.get<DeviceContent>('iot', 'devices', 'abeeway-H72K2');

The returned type is KDocument<DeviceContent> and it contains a _source property of type DeviceContent.

By default, a generic document content with only a strongly defined _kuzzle_info property is returned.

Kuzzle.query method #

Available since 7.10.1

The Kuzzle.query method can accept 2 optional types.

Those types are used to strongly type both the request payload and the response result for each API action used.

Copied to clipboard!
query<TRequest extends BaseRequest, TResult> (
  req: TRequest,
  opts: JSONObject = {},
): Promise<ResponsePayload<TResult>>;

You can define the TRequest type by extending the BaseRequest type. It corresponds to the payload sent to Kuzzle API.

Copied to clipboard!
interface NotificationSmsRequest extends BaseRequest {
  body: {
    phone: string;
    message: string;
  }
}

The TResult is just a definition of the expected result for the API action.

Copied to clipboard!
interface NotificationSmsResult {
  smsCount: number;
}

The complete usage with strong typing will be:

Copied to clipboard!
const { result } = await sdk.query<NotificationSmsRequest, NotificationSmsResult>({
  controller: 'notification',
  action: 'sms',
  body: {
    phone: '+33629951621',
    message: 'Hello, world',
  }
});

result.smsCount; // number