Skip to content
master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

TypeScript/Node.js NATS template

All Contributors

This is a TypeScript/Node.js NATS template for the AsyncAPI generator.

This template is for generating a TypeScript/Node.js wrapper for the NATS client based on your AsyncAPI document. The template is based on the nats-ts library and can be used as both a TypeScript and Node.js library.

Have you found a bug or have an idea for improvement? Feel free to contribute! See the contribution guidelines how to do so.

How to use

Example generations can be found under examples which includes publish and subscribe example as well as request and reply.

Information about the generated files and a description can be found under the documentation folder.

Requirements

  • @asyncapi/generator < v2.0.0 >v1.1.1

Install the generator through npm or run it from docker official installer.

Example usage

The leading examples are both in TypeScript and in Node.js since this template can be used for both. The example code will be used later in the documentation to explain the different features.

Given any AsyncAPI file (AsyncAPI.yml) first generate the client with the AsyncAPI generator such as

ag --install --output ./nats-client ./AsyncAPI.yml @asyncapi/ts-nats-template --param "generateTestClient=true"  --param "promisifyReplyCallback=true"

Afterward, go into the generated folder nats-client and run the commands npm i and npm run build. The generated NATS client is now ready to be used in either TypeScript or Node.js.

TypeScript

import * as GeneratedClient from "./nats-client";
const natsClient = new GeneratedClient.NatsAsyncApiClient();
async function connect(){
  try{
    await natsClient.connect({url: "nats://demo.nats.io:4222", reconnectTimeWait: 1000, reconnect: true, maxReconnectAttempts: -1});
  }catch(e){
    console.log(e);
  }
}
connect();

Node.js

const GeneratedClient = require("./nats-client");
const natsClient = new GeneratedClient.NatsAsyncApiClient();
async function connect(){
  try{
    await natsClient.connect({url: "nats://demo.nats.io:4222", reconnectTimeWait: 1000, reconnect: true, maxReconnectAttempts: -1});
  }catch(e){
    console.log(e);
  }
}
connect();

Template Parameters

These are the available template parameters:

Parameter Type Description
generateTestClient Boolean Use this parameter to generate the test client. Add the following to the CLI when generating your code --param "generateTestClient=true"
promisifyReplyCallback Boolean Use this parameter to change from the default regular callback when using the request operation. Add the following to the CLI when generating your code --param "promisifyReplyCallback=true"

Features

  • Supports wildcard channels. AsyncAPI describes the channel path to be defined as RFC 6570 URI. So a channel containing a wildcard needs to be defined with parameters such as smartylighting/streetlights/{wildcard}.
  • Supports test/mirror client for testing or other useful scenarios.
  • This template can be used as a NodeJS library.
  • Generates payload models using the AsyncAPI model generation library.

Restrictions

  • Empty objects are not supported, use null types instead.

Contributing

Before contributing please read the CONTRIBUTING document.

Contributors

Thanks goes to these wonderful people (emoji key):


Jonas Lagoni

🐛 🚧 💻 📖 🤔 💬 ⚠️

Maciej Urbańczyk

👀

Lukasz Gornicki

🚇 👀

Fran Méndez

👀

Colin Sullivan

👀

Alberto Ricart

👀

Contributors

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!