-
-
Notifications
You must be signed in to change notification settings - Fork 257
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
feat: added the template for README.md #1368
Open
ItshMoh
wants to merge
4
commits into
asyncapi:master
Choose a base branch
from
ItshMoh:readme_client
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
137 changes: 137 additions & 0 deletions
137
packages/templates/clients/js/websocket/template/README.md.js
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,137 @@ | ||
import { File, Text } from '@asyncapi/generator-react-sdk'; | ||
import { getClientName } from '@asyncapi/generator-helpers'; | ||
|
||
export default function({ asyncapi, params }) { | ||
const server = asyncapi.servers().get(params.server); | ||
const info = asyncapi.info(); | ||
const clientName = getClientName(info); | ||
|
||
|
||
const operations = asyncapi.operations().all(); | ||
|
||
return ( | ||
<File name="README.md"> | ||
<Text> | ||
{`# ${info.title()} | ||
|
||
## Overview | ||
|
||
${info.description() || `A WebSocket client for ${info.title()}.`} | ||
|
||
- **Version:** ${info.version()} | ||
- **URL:** ${server.url()} | ||
|
||
|
||
## Client API Reference | ||
|
||
\`\`\`javascript | ||
const ${clientName} = require('./${params.clientFileName.replace('.js', '')}'); | ||
const wsClient = new ${clientName}(); | ||
\`\`\` | ||
|
||
Here the wsClient is an instance of the \`${clientName}\` class. | ||
### Core Methods | ||
|
||
#### \`connect()\` | ||
Establishes a WebSocket connection to the server. | ||
|
||
#### \`registerMessageHandler(handlerFunction)\` | ||
Registers a callback to handle incoming messages. | ||
- **Parameter:** \`handlerFunction\` - This Function takes a parameter \`message\` which is a string. | ||
|
||
#### \`registerErrorHandler(handlerFunction)\` | ||
Registers a callback to handle WebSocket errors. | ||
- **Parameter:** \`handlerFunction\` - This Function takes a parameter \`error\` which is an object | ||
|
||
#### \`close()\` | ||
Closes the WebSocket connection. | ||
|
||
### Available Operations | ||
|
||
${operations.length > 0 ? | ||
operations.map(operation => { | ||
const operationId = operation.id(); | ||
|
||
const channels = operation.channels().all(); | ||
const channelAddress = channels.length > 0 ? channels[0].address() : 'default'; | ||
|
||
let messageExamples = ''; | ||
if (channels.length > 0) { | ||
const channelMessages = channels[0].messages().all(); | ||
if (channelMessages && channelMessages.length > 0) { | ||
const firstMessage = channelMessages[0]; | ||
if (firstMessage.examples && firstMessage.examples().length > 0) { | ||
const example = firstMessage.examples()[0]; | ||
messageExamples = `\n\n**Example:**\n\`\`\`javascript\nclient.${operationId}(${JSON.stringify(example.payload(), null, 2)});\n\`\`\``; | ||
} | ||
} | ||
} | ||
|
||
return `#### \`${operationId}(payload)\` | ||
${operation.summary() || `Sends a message to the '${channelAddress}' channel.`} | ||
${operation.description() ? `\n${operation.description()}` : ''}${messageExamples}`; | ||
}).join('\n\n') | ||
: | ||
`#### \`sendEchoMessage(payload)\` | ||
Sends a message to the server that will be echoed back. | ||
|
||
**Example:** | ||
\`\`\`javascript | ||
client.sendEchoMessage({ message: "Hello World" }); | ||
\`\`\` | ||
`} | ||
|
||
## Testing the client | ||
|
||
\`\`\`javascript | ||
const ${clientName} = require('./${params.clientFileName.replace('.js', '')}'); | ||
const wsClient = new ${clientName}(); | ||
|
||
|
||
// Example of how custom message handler that operates on incoming messages can look like | ||
|
||
function myHandler(message) { | ||
console.log('===================='); | ||
console.log('Just proving I got the message in myHandler:', message); | ||
console.log('===================='); | ||
} | ||
|
||
// Example of custom error handler | ||
|
||
function myErrorHandler(error) { | ||
console.error('Errors from Websocket:', error.message); | ||
} | ||
|
||
async function main() { | ||
wsClient.registerMessageHandler(myHandler); | ||
wsClient.registerErrorHandler(myErrorHandler); | ||
|
||
try { | ||
await wsClient.connect(); | ||
|
||
// Loop to send messages every 5 seconds | ||
const interval = 5000; // 5 seconds | ||
const message = 'Hello, Echo!'; | ||
|
||
while (true) { | ||
try { | ||
await wsClient.sendEchoMessage(message); | ||
} catch (error) { | ||
console.error('Error while sending message:', error); | ||
} | ||
// Wait for the interval before sending the next message | ||
await new Promise(resolve => setTimeout(resolve, interval)); | ||
} | ||
} catch (error) { | ||
console.error('Failed to connect to WebSocket:', error.message); | ||
} | ||
} | ||
|
||
main(); | ||
\`\`\` | ||
|
||
`} | ||
</Text> | ||
</File> | ||
); | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Improve the testing example with proper cleanup and documentation.
The testing example could be enhanced with proper cleanup and better documentation:
📝 Committable suggestion