-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
256 lines (255 loc) · 8.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import * as TestClient from './testclient/';
import {
ErrorCode,
NatsTypescriptTemplateError
} from './NatsTypescriptTemplateError';
import * as Nats from 'nats';
import * as streetlightStreetlightIdCommandTurnonChannel from "./channels/StreetlightStreetlightIdCommandTurnon";
import TurnOn from "./models/TurnOn";
export {
streetlightStreetlightIdCommandTurnonChannel
};
export {
TurnOn
};
export {
ErrorCode,
NatsTypescriptTemplateError
}
export {
TestClient
};
/**
* @class NatsAsyncApiClient
*
* The generated client based on your AsyncAPI document.
*/
export class NatsAsyncApiClient {
private nc ? : Nats.NatsConnection;
private js ? : Nats.JetStreamClient;
private codec ? : Nats.Codec < any > ;
private options ? : Nats.ConnectionOptions;
/**
* 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(NatsTypescriptTemplateError.errorForCode(ErrorCode.INTERNAL_NATS_TS_ERROR, e));
}
})
}
/**
* 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);
}
/**
* Connects the client to the AsyncAPI server called local.
* Local server used during development and testing
*/
async connectToLocal(codec ? : Nats.Codec < any > ) {
await this.connect({
servers: ["localhost:4222"]
}, codec);
}
/**
* Subscribe to the `streetlight/{streetlight_id}/command/turnon`
*
* Channel for the turn on command which should turn on the streetlight
*
* @param onDataCallback to call when messages are received
* @param streetlight_id parameter to use in topic
* @param flush ensure client is force flushed after subscribing
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified
*/
public subscribeToStreetlightStreetlightIdCommandTurnon(
onDataCallback: (
err ? : NatsTypescriptTemplateError,
msg ? : TurnOn, streetlight_id ? : string) => void, streetlight_id: string,
flush ? : boolean,
options ? : Nats.SubscriptionOptions
): Promise < Nats.Subscription > {
return new Promise(async (resolve, reject) => {
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined) {
try {
const sub = await streetlightStreetlightIdCommandTurnonChannel.subscribe(
onDataCallback,
this.nc,
this.codec, streetlight_id,
options
);
if (flush) {
await this.nc.flush();
}
resolve(sub);
} catch (e: any) {
reject(e);
}
} else {
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
});
}
/**
* JetStream pull function.
*
* Pull message from `streetlight/{streetlight_id}/command/turnon`
*
* Channel for the turn on command which should turn on the streetlight
*
* @param onDataCallback to call when messages are received
* @param streetlight_id parameter to use in topic
* @param options to pull message with, bindings from the AsyncAPI document overwrite these if specified
*/
public jetStreamPullStreetlightStreetlightIdCommandTurnon(
onDataCallback: (
err ? : NatsTypescriptTemplateError,
msg ? : TurnOn, streetlight_id ? : string,
jetstreamMsg ? : Nats.JsMsg) => void, streetlight_id: string
): void {
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined && this.js !== undefined) {
streetlightStreetlightIdCommandTurnonChannel.jetStreamPull(
onDataCallback,
this.js,
this.codec, streetlight_id,
);
} else {
throw NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED);
}
}
/**
* Push subscription to the `streetlight/{streetlight_id}/command/turnon`
*
* Channel for the turn on command which should turn on the streetlight
*
* @param onDataCallback to call when messages are received
* @param streetlight_id parameter to use in topic
* @param flush ensure client is force flushed after subscribing
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified
*/
public jetStreamPushSubscribeToStreetlightStreetlightIdCommandTurnon(
onDataCallback: (
err ? : NatsTypescriptTemplateError,
msg ? : TurnOn, streetlight_id ? : string,
jetstreamMsg ? : Nats.JsMsg) => void, streetlight_id: string,
options: Nats.ConsumerOptsBuilder | Partial < Nats.ConsumerOpts >
): Promise < Nats.JetStreamSubscription > {
return new Promise(async (resolve, reject) => {
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined && this.js !== undefined) {
try {
const sub = await streetlightStreetlightIdCommandTurnonChannel.jetStreamPushSubscribe(
onDataCallback,
this.js,
this.codec,
streetlight_id,
options
);
resolve(sub);
} catch (e: any) {
reject(e);
}
} else {
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
});
}
/**
* Push subscription to the `streetlight/{streetlight_id}/command/turnon`
*
* Channel for the turn on command which should turn on the streetlight
*
* @param onDataCallback to call when messages are received
* @param streetlight_id parameter to use in topic
* @param flush ensure client is force flushed after subscribing
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified
*/
public jetStreamPullSubscribeToStreetlightStreetlightIdCommandTurnon(
onDataCallback: (
err ? : NatsTypescriptTemplateError,
msg ? : TurnOn, streetlight_id ? : string,
jetstreamMsg ? : Nats.JsMsg) => void, streetlight_id: string,
options: Nats.ConsumerOptsBuilder | Partial < Nats.ConsumerOpts >
): Promise < Nats.JetStreamPullSubscription > {
return new Promise(async (resolve, reject) => {
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined && this.js !== undefined) {
try {
const sub = streetlightStreetlightIdCommandTurnonChannel.jetStreamPullSubscribe(
onDataCallback,
this.js,
this.codec, streetlight_id,
options
);
resolve(sub);
} catch (e: any) {
reject(e);
}
} else {
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
});
}
}