This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jetstream fetch wrappers (#479)
- Loading branch information
1 parent
d1df8d5
commit cbda2ff
Showing
12 changed files
with
311 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { camelCase, getMessageType, realizeParametersForChannelWrapper, renderJSDocParameters, messageHasNullPayload, realizeChannelName} from '../../utils/index'; | ||
// eslint-disable-next-line no-unused-vars | ||
import { Message, ChannelParameter } from '@asyncapi/parser'; | ||
import { unwrap } from './ChannelParameterUnwrap'; | ||
|
||
/** | ||
* Component which returns a subscribe to function for the client | ||
* | ||
* @param {string} defaultContentType | ||
* @param {string} channelName to publish to | ||
* @param {Message} message which is being received | ||
* @param {Object.<string, ChannelParameter>} channelParameters parameters to the channel | ||
*/ | ||
export function JetstreamFetch(channelName, message, channelParameters) { | ||
const messageType = getMessageType(message); | ||
let parameters = []; | ||
parameters = Object.entries(channelParameters).map(([parameterName]) => { | ||
return `${camelCase(parameterName)}Param`; | ||
}); | ||
const hasNullPayload = messageHasNullPayload(message.payload()); | ||
|
||
//Determine the callback process when receiving messages. | ||
//If the message payload is null no hooks are called to process the received data. | ||
let whenReceivingMessage = `onDataCallback(undefined, null ${parameters.length > 0 && `, ${parameters.join(',')}`});`; | ||
if (!hasNullPayload) { | ||
whenReceivingMessage = ` | ||
let receivedData: any = codec.decode(msg.data); | ||
onDataCallback(undefined, ${messageType}.unmarshal(receivedData) ${parameters.length > 0 && `, ${parameters.join(',')}`}, msg); | ||
`; | ||
} | ||
return ` | ||
/** | ||
* Internal functionality to setup jetstrema fetch on the \`${channelName}\` channel | ||
* | ||
* @param onDataCallback to call when messages are received | ||
* @param js client to fetch with | ||
* @param codec used to convert messages | ||
${renderJSDocParameters(channelParameters)} | ||
*/ | ||
export function jetsStreamFetch( | ||
onDataCallback: ( | ||
err ? : NatsTypescriptTemplateError, | ||
msg ? : ${messageType} | ||
${realizeParametersForChannelWrapper(channelParameters, false)}, | ||
jetstreamMsg?: Nats.JsMsg) => void, | ||
js: Nats.JetStreamClient, | ||
codec: Nats.Codec < any > | ||
${realizeParametersForChannelWrapper(channelParameters)}, | ||
durable: string, options?: Partial<Nats.PullOptions> | ||
) { | ||
const stream = ${realizeChannelName(channelParameters, channelName)}; | ||
(async () => { | ||
let msgs = await js.fetch(stream, durable, options); | ||
for await (const msg of msgs) { | ||
${unwrap(channelName, channelParameters)} | ||
${whenReceivingMessage} | ||
} | ||
})(); | ||
} | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { camelCase, getMessageType, realizeParametersForChannelWrapper, renderJSDocParameters, realizeParametersForChannelWithoutType, pascalCase} from '../../utils/index'; | ||
|
||
export function JetstreamFetch(channelName, message, messageDescription, channelParameters) { | ||
return ` | ||
/** | ||
* JetStream fetch function. | ||
* | ||
* Pull message from \`${channelName}\` | ||
* | ||
* ${messageDescription} | ||
* | ||
* @param onDataCallback to call when messages are received | ||
${renderJSDocParameters(channelParameters)} | ||
* @param options to pull message with, bindings from the AsyncAPI document overwrite these if specified | ||
*/ | ||
public jetStreamFetch${pascalCase(channelName)}( | ||
onDataCallback: ( | ||
err ? : NatsTypescriptTemplateError, | ||
msg?: ${getMessageType(message)} | ||
${realizeParametersForChannelWrapper(channelParameters, false)}, | ||
jetstreamMsg?: Nats.JsMsg) => void | ||
${realizeParametersForChannelWrapper(channelParameters)}, | ||
durable: string, options?: Partial<Nats.PullOptions> | ||
): void { | ||
if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined && this.js !== undefined) { | ||
${camelCase(channelName)}Channel.jetsStreamFetch( | ||
onDataCallback, | ||
this.js, | ||
this.codec | ||
${Object.keys(channelParameters).length ? ` ,${realizeParametersForChannelWithoutType(channelParameters)}` : ''}, | ||
durable, | ||
options | ||
); | ||
} else { | ||
throw NatsTypescriptTemplateError.errorForCode(ErrorCode.NOT_CONNECTED); | ||
} | ||
} | ||
`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.