Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,31 @@ codegen init
## Generate
With your configuration file in hand, time to generate the code and use it! This can be done manually or integrate into your build process. Checkout [all the examples](./examples/) for inspiration on how to do it.

### Basic Generation
```sh
# Generate code once
codegen generate

# Generate with specific config file
codegen generate ./my-config.js
```

### Watch Mode (Development)
For development workflows, use watch mode to automatically regenerate code when your input files change:

```sh
# Watch for changes in the input file specified in your config
codegen generate --watch

# Watch for changes in a specific file or directory
codegen generate --watch --watchPath ./asyncapi

# Short form
codegen generate -w -p ./schemas/
```

Watch mode is perfect for development environments where you want your generated code to stay in sync with your API specifications as you make changes.

# 👀 Goals
Besides the [milestones](https://github.com/the-codegen-project/cli/milestones), we have certain goals that we want to reach for various reasons;
- [ ] ⭐ Reach 50 stars - So we can publish the CLI on Brew and Chocolatey
Expand Down
24 changes: 23 additions & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,29 @@ Customize it to your heart's desire! [Each generator has unique set of options](
## Integrate
With your configuration file in hand, time to integrate it into your project and generate some code! Checkout [all the integrations](../examples/) for inspiration how to do it.

Or manually generate some code;
### Generate Code

#### One-time Generation
```sh
# Generate code once
codegen generate

# Generate with specific config file
codegen generate ./my-config.js
```

#### Development with Watch Mode
For active development, use watch mode to automatically regenerate code when your input files change:

```sh
# Watch for changes in the input file specified in your config
codegen generate --watch

# Watch for changes in a specific file or directory
codegen generate --watch --watchPath ./my-asyncapi.yaml

# Short form
codegen generate -w -p ./schemas/
```

**Pro tip:** Use watch mode during development to keep your generated code in sync with your API specifications. Press `Ctrl+C` to stop watching.
9 changes: 6 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ $ npm install -g @the-codegen-project/cli
$ codegen COMMAND
running command...
$ codegen (--version)
@the-codegen-project/cli/0.52.0 linux-x64 node-v18.20.8
@the-codegen-project/cli/0.52.0 darwin-arm64 node-v22.19.0
$ codegen --help [COMMAND]
USAGE
$ codegen COMMAND
Expand Down Expand Up @@ -69,13 +69,16 @@ Generate code based on your configuration, use `init` to get started.

```
USAGE
$ codegen generate [FILE] [--help]
$ codegen generate [FILE] [--help] [-w] [-p <value>]

ARGUMENTS
FILE Path or URL to the configuration file, defaults to root of where the command is run

FLAGS
--help Show CLI help.
-p, --watchPath=<value> Optional path to watch for changes when --watch flag is used. If not provided, watches the
input file from configuration
-w, --watch Watch for file changes and regenerate code automatically
--help Show CLI help.

DESCRIPTION
Generate code based on your configuration, use `init` to get started.
Expand Down
27 changes: 26 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ An example demonstrating integration with Next.js applications.
### [E-commerce AsyncAPI Payload](./ecommerce-asyncapi-payload/)
A comprehensive example showing how to generate TypeScript payload models from AsyncAPI specifications for an e-commerce order processing system.

### [E-commerce AsyncAPI Parameters](./ecommerce-asyncapi-parameters/)
A comprehensive example showing how to generate TypeScript parameter models from AsyncAPI specifications for dynamic channel routing in an e-commerce messaging system.

### [E-commerce AsyncAPI Headers](./ecommerce-asyncapi-headers/)
A comprehensive example showing how to generate TypeScript header models from AsyncAPI specifications for an e-commerce messaging system, covering authentication, tracing, routing, and metadata management.

### [E-commerce AsyncAPI types](./ecommerce-asyncapi-types/)
### [E-commerce AsyncAPI Channels](./ecommerce-asyncapi-channels/)
A focused example showing how to generate protocol-specific messaging functions from AsyncAPI specifications for order lifecycle management in an e-commerce system.

### [E-commerce AsyncAPI Client](./ecommerce-asyncapi-client/)
An example demonstrating how to generate complete, type-safe client SDKs for NATS messaging with order lifecycle management, including connection management and JetStream integration.

### [E-commerce AsyncAPI Types](./ecommerce-asyncapi-types/)
A comprehensive example showing how to generate TypeScript types from AsyncAPI specifications for an e-commerce messaging system.

## Getting Started
Expand All @@ -28,4 +36,21 @@ A comprehensive example showing how to generate TypeScript types from AsyncAPI s
4. Run the generator
5. Integrate the generated code into your application

## Development Workflow

For active development, most examples include watch mode scripts for automatic code regeneration:

```bash
# For TypeScript Library example
npm run dev

# For Next.js example
npm run generate:watch

# Or use the CLI directly
codegen generate --watch
```

Watch mode automatically regenerates your code when input files change, keeping your generated code in sync during development.

For more detailed information, see the [documentation](https://the-codegen-project.org/docs/).
3 changes: 2 additions & 1 deletion examples/typescript-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"scripts": {
"build": "npm run generate",
"start": "ts-node src/index.ts",
"generate": "codegen generate"
"generate": "codegen generate",
"dev": "codegen generate --watch"
},
"license": "Apache-2.0",
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion examples/typescript-nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"build": "npm run generate && next build",
"start": "next start",
"lint": "next lint",
"generate": "codegen generate"
"generate": "codegen generate",
"generate:watch": "codegen generate --watch"
},
"dependencies": {
"@t3-oss/env-nextjs": "^0.10.1",
Expand Down
29 changes: 29 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@oclif/plugin-help": "^6.0.21",
"@oclif/plugin-version": "^2.0.17",
"@readme/openapi-parser": "^5.0.1",
"chokidar": "^4.0.3",
"cosmiconfig": "^9.0.0",
"graphology": "^0.26.0",
"inquirer": "^8.2.6",
Expand Down
28 changes: 3 additions & 25 deletions src/codegen/generators/typescript/channels/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
TypeScriptChannelRenderedFunctionType,
SupportedProtocols,
TypeScriptChannelsContext,
TypeScriptChannelsGeneratorContext
TypeScriptChannelsGeneratorContext,
sendingFunctionTypes,
receivingFunctionTypes
} from './types';
import {findNameFromChannel} from '../../../utils';
import {ConstrainedObjectModel, OutputModel} from '@asyncapi/modelina';
Expand All @@ -24,30 +26,6 @@ import {generatehttpChannels} from './protocols/http';
import {generateWebSocketChannels} from './protocols/websocket';

type Action = 'send' | 'receive' | 'subscribe' | 'publish';
const sendingFunctionTypes = [
ChannelFunctionTypes.NATS_JETSTREAM_PUBLISH,
ChannelFunctionTypes.NATS_PUBLISH,
ChannelFunctionTypes.NATS_REQUEST,
ChannelFunctionTypes.MQTT_PUBLISH,
ChannelFunctionTypes.KAFKA_PUBLISH,
ChannelFunctionTypes.AMQP_EXCHANGE_PUBLISH,
ChannelFunctionTypes.AMQP_QUEUE_PUBLISH,
ChannelFunctionTypes.EVENT_SOURCE_EXPRESS,
ChannelFunctionTypes.HTTP_CLIENT,
ChannelFunctionTypes.WEBSOCKET_PUBLISH,
ChannelFunctionTypes.WEBSOCKET_REGISTER
];
const receivingFunctionTypes = [
ChannelFunctionTypes.NATS_JETSTREAM_PULL_SUBSCRIBE,
ChannelFunctionTypes.NATS_JETSTREAM_PUSH_SUBSCRIBE,
ChannelFunctionTypes.NATS_REPLY,
ChannelFunctionTypes.NATS_SUBSCRIBE,
ChannelFunctionTypes.MQTT_SUBSCRIBE,
ChannelFunctionTypes.KAFKA_SUBSCRIBE,
ChannelFunctionTypes.EVENT_SOURCE_FETCH,
ChannelFunctionTypes.AMQP_QUEUE_SUBSCRIBE,
ChannelFunctionTypes.WEBSOCKET_SUBSCRIBE
];

// eslint-disable-next-line sonarjs/cognitive-complexity
export function shouldRenderFunctionType(
Expand Down
26 changes: 26 additions & 0 deletions src/codegen/generators/typescript/channels/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,32 @@ export enum ChannelFunctionTypes {
WEBSOCKET_REGISTER = 'websocket_register'
}

export const sendingFunctionTypes = [
ChannelFunctionTypes.NATS_JETSTREAM_PUBLISH,
ChannelFunctionTypes.NATS_PUBLISH,
ChannelFunctionTypes.NATS_REQUEST,
ChannelFunctionTypes.MQTT_PUBLISH,
ChannelFunctionTypes.KAFKA_PUBLISH,
ChannelFunctionTypes.AMQP_EXCHANGE_PUBLISH,
ChannelFunctionTypes.AMQP_QUEUE_PUBLISH,
ChannelFunctionTypes.EVENT_SOURCE_EXPRESS,
ChannelFunctionTypes.HTTP_CLIENT,
ChannelFunctionTypes.WEBSOCKET_PUBLISH,
ChannelFunctionTypes.WEBSOCKET_REGISTER
];

export const receivingFunctionTypes = [
ChannelFunctionTypes.NATS_JETSTREAM_PULL_SUBSCRIBE,
ChannelFunctionTypes.NATS_JETSTREAM_PUSH_SUBSCRIBE,
ChannelFunctionTypes.NATS_REPLY,
ChannelFunctionTypes.NATS_SUBSCRIBE,
ChannelFunctionTypes.MQTT_SUBSCRIBE,
ChannelFunctionTypes.KAFKA_SUBSCRIBE,
ChannelFunctionTypes.EVENT_SOURCE_FETCH,
ChannelFunctionTypes.AMQP_QUEUE_SUBSCRIBE,
ChannelFunctionTypes.WEBSOCKET_SUBSCRIBE
];

export const zodTypescriptChannelsGenerator = z.object({
id: z.string().optional().default('channels-typescript'),
dependencies: z
Expand Down
Loading
Loading