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 channelsto 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;
- NATS: https://github.com/nats-io/nats.js v2
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 {Payload} from './payload/Payload';
export {Payload};
//Import and export parameter models
import {Parameters} from './parameters/Parameters';
//Import channel functions
import { Protocols } from './channels/index';
const { nats } = Protocols;
import * as Nats from 'nats';
/**
 * @class NatsClient
 */
export class NatsClient {
  /**
   * Disconnect all clients from the server
   */
  async disconnect() {
    ...
  }
  /**
   * Returns whether or not any of the clients are closed
   */
  isClosed() {
    ...
  }
  /**
   * Try to connect to the NATS server with user credentials
   */
  async connectWithUserCreds(userCreds: string, options ? : Nats.ConnectionOptions, codec ? : Nats.Codec < any > ) {
    ...
  }
  /**
   * Try to connect to the NATS server with user and password
   */
  async connectWithUserPass(user: string, pass: string, options ? : Nats.ConnectionOptions, codec ? : Nats.Codec < any > ) {
    ...
  }
  /**
   * Try to connect to the NATS server which has no authentication
   */
  async connectToHost(host: string, options ? : Nats.ConnectionOptions, codec ? : Nats.Codec < any > ) {
    ...
  }
  /**
   * Try to connect to the NATS server with the different payloads.
   */
  connect(options: Nats.ConnectionOptions, codec?: Nats.Codec<any><any>): Promise<void> {
    ...
  }
  
  public async jetStreamPublishToChannel(
    message: Payload, 
    parameters: Parameters, 
    options: Partial<Nats.JetStreamPublishOptions> = {}
  ): Promise<void> {
    ...
  }
  jetStreamPullSubscribeToChannel
  jetStreamPushSubscriptionFromChannel
  publishToChannel
  subscribeToChannel
}