-
-
Notifications
You must be signed in to change notification settings - Fork 388
fix: support multiple WebSocket channels with query parameters #1973 #2106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SushanthMusham
wants to merge
7
commits into
asyncapi:master
Choose a base branch
from
SushanthMusham:fix/getQueryParams
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−44
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1dde58a
fix/getQueryParams #1973
SushanthMusham 17faff9
Addressed 3 coderabit changes
SushanthMusham 02453cf
added tests for getQueryParamsForAllChannels
SushanthMusham 0f154c6
added JSDoc for getFilteredAllChannelsParams
SushanthMusham 94e943e
fix:indentation
SushanthMusham d042c69
Merge branch 'master' into fix/getQueryParams
Adi-204 b713385
Update packages/helpers/test/bindings.test.js
SushanthMusham File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 |
|---|---|---|
| @@ -1,52 +1,130 @@ | ||
| /** | ||
| * Extracts default query parameters from a channel’s WebSocket binding. | ||
| * Extracts query parameters from all channels with WebSocket bindings. | ||
| * | ||
| * @param {Map<string,string>} channels - A Map representing all AsyncAPI channels. | ||
| * @returns {Map<string,string>} A Map whose keys are parameter names and whose values are their defaults (or `''`). | ||
| * @param {Object} channels - An AsyncAPI channels collection object with isEmpty() and all() methods. | ||
| * @returns {Map<string, Map<string,string>>} A Map where keys are channel names and values are Maps of parameter names to their defaults (or `''`). | ||
| * | ||
| * @example | ||
| * // Suppose channel.bindings().get("ws").values() returns: | ||
| * // { query: { properties: { foo: { default: 'bar' }, baz: {} } } } | ||
| * const params = getQueryParams(channel); | ||
| * console.log(params.get('foo')); // → 'bar' | ||
| * console.log(params.get('baz')); // → '' | ||
| * // Suppose you have multiple channels with WS bindings: | ||
| * // channels: | ||
| * // chat: | ||
| * // bindings: | ||
| * // ws: | ||
| * // query: | ||
| * // properties: | ||
| * // token: { default: 'auth123' } | ||
| * // roomId: { } | ||
| * // notifications: | ||
| * // bindings: | ||
| * // ws: | ||
| * // query: | ||
| * // properties: | ||
| * // userId: { default: 'user456' } | ||
| * // token: { } | ||
| * | ||
| * const allParams = getQueryParamsForAllChannels(channels); | ||
| * | ||
| * // Returns a Map like: | ||
| * // { | ||
| * // 'chat' => { 'token' => 'auth123', 'roomId' => '' }, | ||
| * // 'notifications' => { 'userId' => 'user456', 'token' => '' } | ||
| * // } | ||
| * | ||
| * const chatParams = allParams.get('chat'); | ||
| * console.log(chatParams.get('token')); // → 'auth123' | ||
| * console.log(chatParams.get('roomId')); // → '' | ||
| * | ||
| * const notifParams = allParams.get('notifications'); | ||
| * console.log(notifParams.get('userId')); // → 'user456' | ||
| * console.log(notifParams.get('token')); // → '' | ||
| */ | ||
| function getQueryParams(channels) { | ||
| // current implementation assumes there is always one channel | ||
| // at the moment only WebSocket binding support query params and the use case for WebSocket is that there is always one channel per AsyncAPI document | ||
| const channel = !channels.isEmpty() && channels.all().entries().next().value[1]; | ||
|
|
||
| const queryMap = new Map(); | ||
| function getQueryParamsForAllChannels(channels) { | ||
| const allChannelsParams = new Map(); | ||
|
|
||
| const bindings = channel?.bindings?.(); | ||
| const hasWsBinding = bindings?.has('ws'); | ||
|
|
||
| if (!hasWsBinding) { | ||
| return null; | ||
| if (channels.isEmpty()) { | ||
| return allChannelsParams; | ||
| } | ||
|
|
||
| const wsBinding = bindings.get('ws'); | ||
| const query = wsBinding.value()?.query; | ||
| //we do not throw error, as user do not have to use query params, we just exit with null as it doesn't make sense to continue with query building | ||
| if (!query) { | ||
| return null; | ||
| for (const channel of channels.all()) { | ||
| const channelName = channel.id(); | ||
| const bindings = channel?.bindings?.(); | ||
| if (!bindings?.has('ws')) { | ||
| continue; | ||
| } | ||
|
|
||
| const wsBinding = bindings.get('ws'); | ||
| const query = wsBinding.value()?.query; | ||
| if (!query) { | ||
| continue; | ||
| } | ||
|
|
||
| const properties = query.properties; | ||
| if (!properties || typeof properties !== 'object' || Object.keys(properties).length === 0) { | ||
| continue; | ||
| } | ||
|
|
||
| const channelParams = new Map(); | ||
| for (const [key, schema] of Object.entries(properties)) { | ||
| const value = schema.default ?? ''; | ||
| channelParams.set(key, String(value)); | ||
| } | ||
|
|
||
| allChannelsParams.set(channelName, channelParams); | ||
| } | ||
|
|
||
| return allChannelsParams; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts default query parameters from the first channel's WebSocket binding. | ||
| * (Maintained for backward compatibility) | ||
| * | ||
| * @param {Map<string,string>} channels - A Map representing all AsyncAPI channels. | ||
| * @returns {Map<string,string>|null} A Map of parameter names to defaults (or `''`), or null if no WS binding found. | ||
| * | ||
| * @example | ||
| * // Suppose you have multiple channels with WS bindings: | ||
| * // channels: | ||
| * // chat: | ||
| * // bindings: | ||
| * // ws: | ||
| * // query: | ||
| * // properties: | ||
| * // token: { default: 'auth123' } | ||
| * // roomId: { } | ||
| * // notifications: | ||
| * // bindings: | ||
| * // ws: | ||
| * // query: | ||
| * // properties: | ||
| * // userId: { default: 'user456' } | ||
| * // token: { } | ||
| * | ||
| * const params = getQueryParams(channels); | ||
| * | ||
| * // Returns only the first channel's params (e.g., 'chat'): | ||
| * // { 'token' => 'auth123', 'roomId' => '' } | ||
| * | ||
| * console.log(params.get('token')); // → 'auth123' | ||
| * console.log(params.get('roomId')); // → '' | ||
| * | ||
| * // Note: To access query parameters for all channels, use getQueryParamsForAllChannels() | ||
| * const allParams = getQueryParamsForAllChannels(channels); | ||
| * const notifParams = allParams.get('notifications'); | ||
| * console.log(notifParams.get('userId')); // → 'user456' | ||
| */ | ||
| function getQueryParams(channels) { | ||
| const allChannelsParams = getQueryParamsForAllChannels(channels); | ||
|
|
||
| // Drill into the JSON Schema properties | ||
| const properties = query.properties; | ||
| if (!properties || typeof properties !== 'object') { | ||
| if (allChannelsParams.size === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| // Populate the map, preserving defaults | ||
| for (const [key, schema] of Object.entries(properties)) { | ||
| const value = schema.default ?? ''; | ||
| queryMap.set(key, String(value)); | ||
| } | ||
|
|
||
| return queryMap; | ||
| // Return the first channel's params for backward compatibility | ||
| return allChannelsParams.values().next().value; | ||
| } | ||
|
|
||
| module.exports = { | ||
| getQueryParams | ||
| }; | ||
| getQueryParams, | ||
| getQueryParamsForAllChannels | ||
| }; | ||
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.