diff --git a/docs/README.md b/docs/README.md index efc9d7d..2c6164d 100644 --- a/docs/README.md +++ b/docs/README.md @@ -46,9 +46,3 @@ Each input has its own limitations, corner cases, and features; thus, each has s ### Protocols Each protocol has its own limitations, corner cases, and features; thus, each has separate documentation. - [NATS](./protocols/nats.md) - - - - - - diff --git a/docs/contributing.md b/docs/contributing.md index d2d38f8..9302916 100644 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -137,7 +137,3 @@ Prefix that follows specification is not enough though. Remember that the title ## License When you submit changes, your submissions are understood to be under the same [Apache 2.0 License](../LICENSE) that covers the project. - - - - diff --git a/package.json b/package.json index fd1e0b7..0107238 100644 --- a/package.json +++ b/package.json @@ -107,7 +107,7 @@ "pack:tarballs": "oclif pack tarballs -t linux-x64 && npm run pack:rename", "pack:windows": "oclif pack win && npm run pack:rename", "pack:rename": "node scripts/releasePackagesRename.js", - "prepublishOnly": "npm run build", + "prepublishOnly": "npm run build && npm run generate:assets && cd website && npm i && npm run build:resources", "test": "jest --coverage --testPathIgnorePatterns ./test/blackbox", "coverage": "nyc npm run test", "bump:version": "npm --no-git-tag-version --allow-same-version version $VERSION", diff --git a/website/docs/README.md b/website/docs/README.md index e4f9bf2..694254e 100644 --- a/website/docs/README.md +++ b/website/docs/README.md @@ -1,7 +1,6 @@ --- sidebar_position: 1 --- - # Documentation @@ -9,6 +8,8 @@ sidebar_position: 1 - [Configurations](#configurations) +- [Getting Started](#getting-started) +- [Contributing](#contributing) - [Usage](#usage) - [Generators](#generators) - [Architectural Decisions](#architectural-decisions) @@ -25,6 +26,9 @@ Contains all the information about the how the configuration file works and how ### [Getting Started](./getting-started.md) Get started in 5 minutes :fire: +### [Contributing](./contributing.md) +Get an overview of how to contribute to the project + ### [Usage](./usage.md) Contains all the information about what the CLI can do and how. @@ -41,4 +45,11 @@ Each input has its own limitations, corner cases, and features; thus, each has s ### Protocols Each protocol has its own limitations, corner cases, and features; thus, each has separate documentation. -- [NATS](./protocols/nats.md) \ No newline at end of file +- [NATS](./protocols/nats.md) + + + + + + + diff --git a/website/docs/contributing.md b/website/docs/contributing.md new file mode 100644 index 0000000..565e568 --- /dev/null +++ b/website/docs/contributing.md @@ -0,0 +1,144 @@ +--- +sidebar_position: 5 +--- +# Contributing to The Codegen Project + + + + + +- [Acceptance criteria and process](#acceptance-criteria-and-process) + * [Fixing bugs](#fixing-bugs) + * [New features](#new-features) +- [Repository Architecture](#repository-architecture) +- [Getting started](#getting-started) +- [Contribution recogniton](#contribution-recogniton) +- [Summary of the contribution flow](#summary-of-the-contribution-flow) +- [Code of Conduct](#code-of-conduct) +- [Our Development Process](#our-development-process) +- [Pull Requests](#pull-requests) +- [Conventional commits](#conventional-commits) +- [License](#license) + + + +First of all, thank you πŸ™‡πŸΎβ€β™€οΈ for considering contributing to The Codegen Project + +If you have any questions, are unsure how your use-case fits in, or want something clarified, don't hesitate to reach out, we are always happy to help out! + +## Acceptance criteria and process + +Even though we love contributions, we need to maintain a certain standard of what can be merged into the codebase. + +The below sections provide information about our acceptance criteria, based on the type of contribution you make. + +### Fixing bugs + +The Acceptance Criteria for _fixing any bug_ means that you should be able to reproduce the error using tests that will fail, unless a fix is implemented. + +### New features + +The Acceptance Criteria for _adding new features_ requires a few things in order to be accepted. This ensures all features are well described and implemented before being released. + +1. **Not all feature requests from the community (or maintainers!) are accepted:** Even though you are welcome to create a new feature without an issue, it might be rejected and turn out to be a waste of your time. We don't want that to happen, so make sure to create an issue first and wait to see if it's accepted after community discussion of the proposal. +1. **When creating tests for your new feature, aim for as high coverage numbers as possible:** When you run the tests (`npm run test`), you should see a `./coverage/lcov-report/index.html` file being generated. Use this to see in depth where your tests are not covering your implementation. +1. **No documentation, no feature:** If a user cannot understand a new feature, that feature basically doesn't exist! Remember to make sure that any and all relevant [documentation](./) is consistently updated. + - New features such as new generators or inputs, etc, need associated use case documentation along side [examples](../examples). + +## Repository Architecture + +The repository is setup with multiple functions to keep it simple. +- `src`; includes the CLI and library code both exposed through the same package. +- `website`; is... Well the website. +- `scripts`; includes helpfull scripts for the release flow such as for building JSON Schema files from the code for configuration validation and CLI release configurations +- `test`; include all the testing done for the repository + - `blackbox`; is a quick and dirt syntax testing of different configurations against different inputs to quickly detect problems + - `runtime`; is sematic testing of the generated code in the corresponding languages they generate for, ensuring correct behaviour at runtime + - The rest normal unit and integration testing of the actual CLI and library +- `examples`; is the showcase of actual projects using the CLI to simplify the implementation phase of software development. +- `schemas`; is the autogenerated JSON schemas for validating configurations. + +## Getting started + +Here is a quick litle get started quick tutorial; + +1. Fork the repository +2. Create a branch from the upstream repository +3. Install the dependencies `npm install` +4. Make sure test pass `npm run test` +5. Adapt the source code as well as test and documentation +6. Push the changes +7. Create a PR with the proposed change +8. Get the change reviewed and merged :tada: + +Happy contributing :heart: + +## Contribution recogniton + +We use [All Contributors](https://allcontributors.org/docs/en/specification) specification to handle recognitions. + +## Summary of the contribution flow + +The following is a summary of the ideal contribution flow. Please, note that Pull Requests can also be rejected by the maintainers when appropriate. + +``` + β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” + β”‚ β”‚ + β”‚ Open an issue β”‚ + β”‚ (a bug report or a β”‚ + β”‚ feature request) β”‚ + β”‚ β”‚ + β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ + ⇩ + β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” + β”‚ β”‚ + β”‚ Open a Pull Request β”‚ + β”‚ (only after issue β”‚ + β”‚ is approved) β”‚ + β”‚ β”‚ + β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ + ⇩ + β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” + β”‚ β”‚ + β”‚ Your changes will β”‚ + β”‚ be merged and β”‚ + β”‚ published on the next β”‚ + β”‚ release β”‚ + β”‚ β”‚ + β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ +``` + +## Code of Conduct +AsyncAPI has adopted a Code of Conduct that we expect project participants to adhere to. Please [read the full text](../CODE_OF_CONDUCT.md) so that you can understand what sort of behaviour is expected. + +## Our Development Process +We use Github to host code, to track issues and feature requests, as well as accept pull requests. + +## Pull Requests + +**Please, make sure you open an issue before starting with a Pull Request, unless it's a typo or a really obvious error.** Pull requests are the best way to propose changes to the specification. + +## Conventional commits + +Our repositories follow [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/#summary) specification. Releasing to GitHub and NPM is done with the support of [semantic-release](https://semantic-release.gitbook.io/semantic-release/). + +Pull requests should have a title that follows the specification, otherwise, merging is blocked. If you are not familiar with the specification simply ask maintainers to modify. You can also use this cheatsheet if you want: + +- `fix: ` prefix in the title indicates that PR is a bug fix and PATCH release must be triggered. +- `feat: ` prefix in the title indicates that PR is a feature and MINOR release must be triggered. +- `docs: ` prefix in the title indicates that PR is only related to the documentation and there is no need to trigger release. +- `chore: ` prefix in the title indicates that PR is only related to cleanup in the project and there is no need to trigger release. +- `test: ` prefix in the title indicates that PR is only related to tests and there is no need to trigger release. +- `refactor: ` prefix in the title indicates that PR is only related to refactoring and there is no need to trigger release. + +What about MAJOR release? just add `!` to the prefix, like `fix!: ` or `refactor!: ` + +Prefix that follows specification is not enough though. Remember that the title must be clear and descriptive with usage of [imperative mood](https://chris.beams.io/posts/git-commit/#imperative). + +## License +When you submit changes, your submissions are understood to be under the same [Apache 2.0 License](../LICENSE) that covers the project. + + + + + diff --git a/website/docs/generators/README.md b/website/docs/generators/README.md index ac52f62..ea4a4e4 100644 --- a/website/docs/generators/README.md +++ b/website/docs/generators/README.md @@ -8,11 +8,12 @@ All available generators, across languages and inputs: - [`parameters`](./parameters.md) - [`custom`](./custom.md) - [`channels`](./channels.md) +- [`client`](./client.md) -| **Inputs** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`channels`](./channels.md) | [`custom`](./custom.md)Β | +| **Inputs** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`custom`](./custom.md)Β | |---|---|---|---|---| -| AsyncAPI | X | X | X | X | +| AsyncAPI | X | X | X | X | X | -| **Languages** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`channels`](./channels.md) | [`custom`](./custom.md)Β | +| **Languages** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`custom`](./custom.md)Β | |---|---|---|---|---| -| TypeScript | X | X | X | X | +| TypeScript | X | X | X | X | X | diff --git a/website/docs/generators/channels.md b/website/docs/generators/channels.md index 49ff264..dbfc1f5 100644 --- a/website/docs/generators/channels.md +++ b/website/docs/generators/channels.md @@ -8,12 +8,12 @@ sidebar_position: 99 export default { ..., generators: [ - { - preset: 'channels', - outputPath: './src/__gen__/', - language: 'typescript', - protocols: ['nats'] - } + { + preset: 'channels', + outputPath: './src/__gen__/', + language: 'typescript', + protocols: ['nats'] + } ] }; ``` diff --git a/website/docs/generators/client.md b/website/docs/generators/client.md new file mode 100644 index 0000000..8748157 --- /dev/null +++ b/website/docs/generators/client.md @@ -0,0 +1,173 @@ +--- +sidebar_position: 99 +--- + +# Client + +```js +export default { + ..., + generators: [ + { + preset: 'client', + outputPath: './src/__gen__/', + language: 'typescript', + protocols: ['nats'] + } + ] +}; +``` + +`client` preset with `asyncapi` input generates for each protocol a class (`{Protocol}Client`) that makes it easier to interact with the protocol. + +It will generate; +- Support function for connecting to the protocol +- Simpler functions then those generated by [`channels`](./channels.md) to interact with the given protocols +- Exporting all generated [`parameters`](./parameters.md) +- Exporting all generated [`payloads`](./payloads.md) + +This generator uses `channels` generators, in case you dont have any defined, it will automatically include them with default values and dependencies. + +This is supported through the following inputs: [`asyncapi`](#inputs) + +It supports the following languages; [`typescript`](#typescript) + +It supports the following protocols; [`nats`](../protocols/nats.md) + +## TypeScript + +### Nats + +Dependencies; +- `NATS`: https://github.com/nats-io/nats.js v2 + +For Nats the `NatsClient` is generated that setups the correct [Nats.js](https://github.com/nats-io/nats.js) clients, marshalling codex, and provide simplier functions to improve DX. + +Example; +```ts +//Import and export payload models +import {ComAdeoCasestudyCostingrequestCostingRequestPayload} from './payload/ComAdeoCasestudyCostingrequestCostingRequestPayload'; +import {ComAdeoCasestudyCostingresponseCostingResponsePayload} from './payload/ComAdeoCasestudyCostingresponseCostingResponsePayload'; +export {ComAdeoCasestudyCostingrequestCostingRequestPayload}; +export {ComAdeoCasestudyCostingresponseCostingResponsePayload}; + +//Import and export parameter models +import {CostingRequestChannelParameters} from './parameters/CostingRequestChannelParameters'; +export {CostingRequestChannelParameters}; + +//Import channel functions +import { Protocols } from './channels/index'; +const { nats } = Protocols; + +import * as Nats from 'nats'; + +/** + * @class NatsClient + */ +export class NatsClient { + public nc?: Nats.NatsConnection; + public js?: Nats.JetStreamClient; + public codec?: Nats.Codec; + public options?: Nats.ConnectionOptions; + + /** + * 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); + } + + /** + * 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): Promise { + 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('Could not connect to NATS server'); + } + }) + } + + /** + * + * + * @param message to publish + * @param parameters for topic substitution + * @param options to use while publishing the message + */ + public async jetStreamPublishToCostingRequestChannel( + message: ComAdeoCasestudyCostingrequestCostingRequestPayload, parameters: CostingRequestChannelParameters, options: Partial = {} + ): Promise { + if (!this.isClosed() && this.nc !== undefined && this.codec !== undefined && this.js !== undefined) { + return nats.jetStreamPublishToCostingRequestChannel(message, parameters, this.js, this.codec, options); + } else { + Promise.reject('Nats client not available yet, please connect or set the client'); + } + } + + ..., jetStreamPullSubscribeTo..., jetStreamPushSubscriptionFrom..., publishTo..., subscribeTo +} +``` \ No newline at end of file diff --git a/website/docs/protocols/nats.md b/website/docs/protocols/nats.md index c506467..60056ac 100644 --- a/website/docs/protocols/nats.md +++ b/website/docs/protocols/nats.md @@ -6,7 +6,7 @@ sidebar_position: 99 [NATS is an open-source, high-performance messaging system designed for cloud-native, distributed systems, and Internet of Things (IoT) applications](https://nats.io/). Developed by Synadia, NATS stands out for its simplicity, lightweight architecture, and low latency, making it ideal for real-time messaging. It supports a variety of messaging patterns including publish-subscribe, request-reply, and queueing. -It is one of the first protocols for The Codegen Project to support, here is what is currently available through the generators ([channels](../generators/channels.md)): +It is one of the first protocols for The Codegen Project to support, here is what is currently available through the generators ([channels](../generators/channels.md) and [client](../generators/client.md)): | **Languages** | Core publishΒ | Core subscribe | JetStream publishΒ | JetStream pull subscribeΒ | JetStream push subscription |---|---|---|---|---|---| diff --git a/website/docs/usage.md b/website/docs/usage.md index ba30233..d00cce9 100644 --- a/website/docs/usage.md +++ b/website/docs/usage.md @@ -1,7 +1,6 @@ --- sidebar_position: 3 --- - # CLI Usage @@ -10,7 +9,7 @@ $ npm install -g @the-codegen-project/cli $ codegen COMMAND running command... $ codegen (--version) -@the-codegen-project/cli/0.19.0 linux-x64 node-v18.20.4 +@the-codegen-project/cli/0.20.0 linux-x64 node-v18.20.2 $ codegen --help [COMMAND] USAGE $ codegen COMMAND @@ -21,12 +20,9 @@ USAGE ## Table of contents -* [Table of contents](#table-of-contents) -* [Usage](#usage) -* [Commands](#commands) +* [CLI Usage](#cli-usage) - ## Commands @@ -85,7 +81,7 @@ DESCRIPTION Generate code based on your configuration, use `init` to get started. ``` -_See code: [src/commands/generate.ts](https://github.com/the-codegen-project/cli/blob/v0.19.0/src/commands/generate.ts)_ +_See code: [src/commands/generate.ts](https://github.com/the-codegen-project/cli/blob/v0.20.0/src/commands/generate.ts)_ ## `codegen help [COMMAND]` @@ -141,7 +137,7 @@ DESCRIPTION Initialize The Codegen Project in your project ``` -_See code: [src/commands/init.ts](https://github.com/the-codegen-project/cli/blob/v0.19.0/src/commands/init.ts)_ +_See code: [src/commands/init.ts](https://github.com/the-codegen-project/cli/blob/v0.20.0/src/commands/init.ts)_ ## `codegen version`