-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
reply.js
70 lines (68 loc) · 2.97 KB
/
reply.js
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
import { pascalCase, camelCase, getMessageType, realizeParametersForChannelWithoutType, realizeParametersForChannelWrapper, renderJSDocParameters} from '../../utils/index';
// eslint-disable-next-line no-unused-vars
import { Message, ChannelParameter } from '@asyncapi/parser';
/**
* @typedef TemplateParameters
* @type {object}
* @property {boolean|string} generateTestClient - whether or not test client should be generated.
* @property {boolean|string} promisifyReplyCallback - whether or not reply callbacks should be promisify.
*/
/**
* Component which returns a reply to function for the client
*
* @param {string} channelName to setup reply to
* @param {Message} replyMessage used to reply to request
* @param {Message} receiveMessage which is received by the request
* @param {string} messageDescription
* @param {Object.<string, ChannelParameter>} channelParameters parameters to the channel
* @param {TemplateParameters} params passed template parameters
*/
export function Reply(channelName, replyMessage, receiveMessage, messageDescription, channelParameters, params) {
return `
/**
* Reply to the \`${channelName}\` channel
*
* ${messageDescription}
*
* @param onRequest called when request is received
* @param onReplyError called when it was not possible to send the reply
${renderJSDocParameters(channelParameters)}
* @param flush ensure client is force flushed after subscribing
* @param options to subscribe with, bindings from the AsyncAPI document overwrite these if specified
*/
public replyTo${pascalCase(channelName)}(
onRequest : (
err?: NatsTypescriptTemplateError,
msg?: ${getMessageType(receiveMessage)}
${realizeParametersForChannelWrapper(channelParameters, false)}
) => ${params.promisifyReplyCallback.length && 'Promise<'}${getMessageType(replyMessage)}${ params.promisifyReplyCallback.length && '>'},
onReplyError : (err: NatsTypescriptTemplateError) => void
${realizeParametersForChannelWrapper(channelParameters)},
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 ${ camelCase(channelName) }Channel.reply(
onRequest,
onReplyError,
this.nc,
this.codec
${Object.keys(channelParameters).length ? `,${realizeParametersForChannelWithoutType(channelParameters)}` : ''},
options
);
if(flush){
await this.nc!.flush();
}
resolve(sub);
} catch (e: any) {
reject(e);
}
} else {
reject(NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED));
}
});
}
`;
}