Skip to main content

Client

export default {
...,
generators: [
{
preset: 'client',
outputPath: './src/__gen__/',
language: 'typescript',
protocols: ['nats']
}
]
};

client preset with asyncapi input generates for each protocol a class ({Protocol}Client) that makes it easier to interact with the protocol.

It will generate;

  • Support function for connecting to the protocol
  • Simpler functions then those generated by channels to interact with the given protocols
  • Exporting all generated parameters
  • Exporting all generated payloads

This generator uses channels generators, in case you dont have any defined, it will automatically include them with default values and dependencies.

This is supported through the following inputs: asyncapi

It supports the following languages; typescript

It supports the following protocols; nats

TypeScript

Nats

Dependencies;

For Nats the NatsClient is generated that setups the correct Nats.js clients, marshalling codex, and provide simplier functions to improve DX.

Example;

//Import and export payload models
import {ComAdeoCasestudyCostingrequestCostingRequestPayload} from './payload/ComAdeoCasestudyCostingrequestCostingRequestPayload';
import {ComAdeoCasestudyCostingresponseCostingResponsePayload} from './payload/ComAdeoCasestudyCostingresponseCostingResponsePayload';
export {ComAdeoCasestudyCostingrequestCostingRequestPayload};
export {ComAdeoCasestudyCostingresponseCostingResponsePayload};

//Import and export parameter models
import {CostingRequestChannelParameters} from './parameters/CostingRequestChannelParameters';
export {CostingRequestChannelParameters};

//Import channel functions
import { Protocols } from './channels/index';
const { nats } = Protocols;

import * as Nats from 'nats';

/**
* @class NatsClient
*/
export class NatsClient {
public nc?: Nats.NatsConnection;
public js?: Nats.JetStreamClient;
public codec?: Nats.Codec<any>;
public options?: Nats.ConnectionOptions;

/**
* Disconnect all clients from the server
*/
async disconnect() {
if (!this.isClosed() && this.nc !== undefined) {
await this.nc.drain();
}
}
/**
* Returns whether or not any of the clients are closed
*/
isClosed() {
if (!this.nc || this.nc!.isClosed()) {
return true;
}
return false;
}
/**
* Try to connect to the NATS server with user credentials
*
* @param userCreds to use
* @param options to connect with
*/
async connectWithUserCreds(userCreds: string, options ? : Nats.ConnectionOptions, codec ? : Nats.Codec < any > ) {
await this.connect({
user: userCreds,
...options
}, codec);
}
/**
* Try to connect to the NATS server with user and password
*
* @param user username to use
* @param pass password to use
* @param options to connect with
*/
async connectWithUserPass(user: string, pass: string, options ? : Nats.ConnectionOptions, codec ? : Nats.Codec < any > ) {
await this.connect({
user: user,
pass: pass,
...options
}, codec);
}
/**
* Try to connect to the NATS server which has no authentication

* @param host to connect to
* @param options to connect with
*/
async connectToHost(host: string, options ? : Nats.ConnectionOptions, codec ? : Nats.Codec < any > ) {
await this.connect({
servers: [host],
...options
}, codec);
}

/**
* Try to connect to the NATS server with the different payloads.
* @param options to use, payload is omitted if sat in the AsyncAPI document.
*/
connect(options: Nats.ConnectionOptions, codec?: Nats.Codec<any>): Promise<void> {
return new Promise(async (resolve: () => void, reject: (error: any) => void) => {
if (!this.isClosed()) {
return reject('Client is still connected, please close it first.');
}
this.options = options;
if (codec) {
this.codec = codec;
} else {
this.codec = Nats.JSONCodec();
}
try {
this.nc = await Nats.connect(this.options);
this.js = this.nc.jetstream();
resolve();
} catch (e: any) {
reject('Could not connect to NATS server');
}
})
}

/**
*
*
* @param message to publish
* @param parameters for topic substitution
* @param options to use while publishing the message
*/
public async jetStreamPublishToCostingRequestChannel(
message: ComAdeoCasestudyCostingrequestCostingRequestPayload, parameters: CostingRequestChannelParameters, options: Partial<Nats.JetStreamPublishOptions> = {}
): Promise<void> {
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined && this.js !== undefined) {
return nats.jetStreamPublishToCostingRequestChannel(message, parameters, this.js, this.codec, options);
} else {
Promise.reject('Nats client not available yet, please connect or set the client');
}
}

..., jetStreamPullSubscribeTo..., jetStreamPushSubscriptionFrom..., publishTo..., subscribeTo
}