Client
export default {
...,
generators: [
{
preset: 'client',
outputPath: './src/__gen__/',
language: 'typescript',
protocols: ['nats']
}
]
};
client preset generates a class that makes it easier to interact with a protocol. For message brokers (nats) this is a {Protocol}Client that manages the connection; for http it is a single API client class whose name is derived from the input document title (see HTTP).
It will generate;
- Support function for connecting to the protocol (message brokers) or holding shared request configuration (HTTP)
- 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. When a configured protocol has no matching channel functions in the input document, no client is emitted for it.
This is supported through the following inputs: asyncapi, openapi (HTTP only)
It supports the following languages; typescript
It supports the following protocols; nats, http
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
}
HTTP
For http a single API client class is generated that wraps the standalone http_client channel functions. You construct it once with the shared request configuration (baseUrl, auth, hooks, retry, ...) and every operation becomes a method that reuses that configuration; any field can still be overridden per call.
export default {
inputType: 'openapi',
inputPath: './my-api.json',
generators: [
{
preset: 'client',
outputPath: './src/__gen__/client',
language: 'typescript',
protocols: ['http']
}
]
};
The class name is derived from the input document title (a "Safepay Nordic API" title yields SafepayNordicClient), falling back to HttpClient. Override it explicitly with the clientName option.
Example generated usage:
import {SafepayNordicClient} from './__gen__/client/SafepayNordicClient';
const safepay = new SafepayNordicClient({
baseUrl: 'https://api.example.com',
auth: {type: 'bearer', token: process.env.API_TOKEN ?? ''}
});
// Shared server/auth are reused; the call only supplies what is operation-specific.
const response = await safepay.getV2Documents();
console.log(response.status, response.data);
Each method returns the same HttpClientResponse<T> as the underlying channel function, so response metadata (status, headers) is preserved.