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
4 changes: 4 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ A comprehensive example showing how to generate TypeScript payload models from A
### [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/)

A comprehensive example showing how to generate TypeScript types from AsyncAPI specifications for an e-commerce messaging system.

## Getting Started

1. Choose an example that matches your use case
Expand Down
31 changes: 31 additions & 0 deletions examples/ecommerce-asyncapi-types/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# E-commerce Channel Types - Comprehensive Example

A comprehensive example showing how to generate type-safe channel routing from AsyncAPI specifications for an e-commerce event system. This example demonstrates all the patterns from the accompanying [blog post](https://the-codegen-project.org/blog/asyncapi-types-generator).

**Files:**
- `ecommerce-channels.yaml` - AsyncAPI specification defining event channels and routing
- `codegen.config.js` - Configuration for generating TypeScript channel types
- `src/index.ts` - Comprehensive demo showcasing all blog post examples

**Features demonstrated:**
- **Type-Safe Event Publisher** - Publish events with compile-time channel validation
- **Type-Safe Event Router** - Route events to handlers with type safety
- **Message Broker Integration** - Examples for NATS and Kafka
- **Environment-Specific Channel Mapping** - Add environment prefixes
- **Channel Health Monitoring** - Track publishing metrics and error rates
- **Parameter Substitution** - Dynamic channel addresses with entity IDs
- **Error Handling** - Runtime validation with proper error handling

**Usage:**
```bash
# Install dependencies
npm install

# Generate channel types from AsyncAPI spec
npm run generate

# Run the comprehensive demo showcasing all patterns
npm run demo
```

The generated types can be used with any messaging infrastructure (NATS, Kafka, RabbitMQ, etc.) while providing compile-time safety for channel routing.
11 changes: 11 additions & 0 deletions examples/ecommerce-asyncapi-types/codegen.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default {
inputType: 'asyncapi',
inputPath: './ecommerce-channels.yaml',
generators: [
{
preset: 'types',
outputPath: './src/generated/types',
language: 'typescript',
}
]
};
155 changes: 155 additions & 0 deletions examples/ecommerce-asyncapi-types/ecommerce-channels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
asyncapi: 3.0.0
info:
title: E-commerce Event Channels
version: 1.0.0
description: Channel definitions for e-commerce event routing

channels:
order-events:
address: 'ecommerce.orders.{orderId}'
description: Order lifecycle events
parameters:
orderId:
description: The order identifier
messages:
OrderCreated:
name: OrderCreated
title: Order Created
summary: Fired when a new order is created
payload:
type: object
properties:
orderId:
type: string
format: uuid
customerId:
type: string
format: uuid
totalAmount:
type: number
OrderUpdated:
name: OrderUpdated
title: Order Updated
summary: Fired when order details are modified
payload:
type: object
properties:
orderId:
type: string
format: uuid
changes:
type: object

payment-events:
address: 'ecommerce.payments.{paymentId}'
description: Payment processing events
parameters:
paymentId:
description: The payment identifier
messages:
PaymentProcessed:
name: PaymentProcessed
title: Payment Processed
summary: Fired when payment is successfully processed
payload:
type: object
properties:
paymentId:
type: string
format: uuid
orderId:
type: string
format: uuid
amount:
type: number
PaymentFailed:
name: PaymentFailed
title: Payment Failed
summary: Fired when payment processing fails
payload:
type: object
properties:
paymentId:
type: string
format: uuid
orderId:
type: string
format: uuid
reason:
type: string

inventory-events:
address: 'ecommerce.inventory.{productId}'
description: Product inventory changes
parameters:
productId:
description: The product identifier
messages:
StockUpdated:
name: StockUpdated
title: Stock Updated
summary: Fired when product stock levels change
payload:
type: object
properties:
productId:
type: string
oldQuantity:
type: integer
newQuantity:
type: integer

customer-notifications:
address: 'ecommerce.notifications.{customerId}'
description: Customer notification events
parameters:
customerId:
description: The customer identifier
messages:
NotificationSent:
name: NotificationSent
title: Notification Sent
summary: Fired when a notification is sent to customer
payload:
type: object
properties:
customerId:
type: string
format: uuid
type:
type: string
enum: [email, sms, push]
message:
type: string

analytics-events:
address: 'ecommerce.analytics.events'
description: Business analytics and metrics
messages:
UserAction:
name: UserAction
title: User Action
summary: Tracks user interactions and behaviors
payload:
type: object
properties:
userId:
type: string
action:
type: string
timestamp:
type: string
format: date-time
ConversionEvent:
name: ConversionEvent
title: Conversion Event
summary: Tracks conversion funnel events
payload:
type: object
properties:
sessionId:
type: string
event:
type: string
value:
type: number
Loading