Skip to content

Commit c16944b

Browse files
authored
feat: add headers validation (#223)
- Fixed header property names not being accurate - Added filtering on the website blog post to only show those who are live i.e. date is past. - Added AsyncAPI headers example - Added AsyncAPI headers blog post - Added AsyncAPI payloads example - Added AsyncAPI payloads blog post - Improved headers docs - Added headers validation functions
1 parent 01aec5f commit c16944b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+13613
-169
lines changed

docs/configurations.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ For example, with the [`custom`](./generators/custom.md) generator, you provide
1212

1313
Reason those two exist, is because adding a `.js` configuration file to a Java project, might confuse developers, and if you dont need to take advantage of the customization features that require callback, it will probably be better to use one of the other two.
1414

15+
## Creating Configurations with the CLI
16+
17+
The easiest way to create a configuration file is by using the `codegen init` command. This interactive command will guide you through setting up a configuration file for your project, allowing you to specify:
18+
19+
- Input file (AsyncAPI or OpenAPI document)
20+
- Configuration file type (`esm`, `json`, `yaml`, `ts`)
21+
- Output directory
22+
- Language and generation options
23+
24+
```sh
25+
codegen init --input-file ./ecommerce.yml --input-type asyncapi --config-type ts --languages typescript
26+
```
27+
28+
For detailed usage instructions and all available options, see the [CLI usage documentation](./usage.md#codegen-init).
29+
30+
## Configuration File Lookup
31+
1532
If no explicit configuration file is sat, it will be looked for in the following order:
1633
- package.json
1734
- .codegenrc

docs/generators/headers.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,33 @@ The return type is a map of channels and the model that represent the headers.
3535
The `headers` preset with `openapi` input generates all the headers for each path in the OpenAPI document.
3636

3737
The return type is a map of paths and the model that represent the headers.
38+
39+
## Typescript
40+
Dependencies:
41+
- If validation enabled, [ajv](https://ajv.js.org/guide/getting-started.html): ^8.17.1
42+
43+
### Validation
44+
Each generated class includes built-in JSON Schema validation capabilities through two static methods:
45+
46+
- `validate`: Validates headers against the schema. Use this method when you want to validate data.
47+
48+
```typescript
49+
// Example
50+
const result = UserSignedUpHeaders.validate({ data: headers });
51+
if (!result.valid) {
52+
console.error('Validation errors:', result.errors);
53+
}
54+
```
55+
56+
- `createValidator`: Creates a reusable validator function. Use this when you need to validate multiple instances of the same type and want to avoid recreating the validator each time.
57+
58+
```typescript
59+
// Example
60+
const validator = UserSignedUpHeaders.createValidator();
61+
const result = UserSignedUpHeaders.validate({ data: headers, ajvValidatorFunction: validator });
62+
if (!result.valid) {
63+
console.error('Validation errors:', result.errors);
64+
}
65+
```
66+
67+
Both methods support custom Ajv instances and options for advanced validation scenarios.

docs/migrations/v0.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,3 @@ const subscriber = await jetStreamPullSubscribeToReceiveUserSignedup({
5757
config
5858
});
5959
```
60-
61-
62-
63-
64-
65-
66-

examples/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,18 @@ A complete example showing how to generate a TypeScript library from OpenAPI spe
1010
### [TypeScript Next.js](./typescript-nextjs/)
1111
An example demonstrating integration with Next.js applications.
1212

13-
### [E-commerce Payload Models](./ecommerce-payloads/)
13+
### [E-commerce AsyncAPI Payload](./ecommerce-asyncapi-payload/)
1414
A comprehensive example showing how to generate TypeScript payload models from AsyncAPI specifications for an e-commerce order processing system.
15+
16+
### [E-commerce AsyncAPI Headers](./ecommerce-asyncapi-headers/)
17+
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.
18+
19+
## Getting Started
20+
21+
1. Choose an example that matches your use case
22+
2. Copy the relevant files to your project
23+
3. Modify the configuration to match your specifications
24+
4. Run the generator
25+
5. Integrate the generated code into your application
26+
27+
For more detailed information, see the [documentation](https://the-codegen-project.org/docs/).
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# E-commerce Header Models
2+
A comprehensive example showing how to generate TypeScript header models from AsyncAPI specifications for an e-commerce messaging system.
3+
4+
**Files:**
5+
- `ecommerce-messaging-system.yaml` - AsyncAPI specification with comprehensive header patterns
6+
- `codegen.config.js` - Configuration for generating TypeScript header models
7+
- `src/index.ts` - Demo script showing header usage patterns
8+
- `src/generated` - All the generated files
9+
10+
**Features demonstrated:**
11+
- Authentication headers (JWT tokens, API keys)
12+
- Distributed tracing headers (correlation IDs, request IDs)
13+
- Multi-tenant routing headers
14+
- Event metadata headers (actor info, reason codes, priorities)
15+
- Payment processing headers (provider info, risk scores, idempotency)
16+
- Type-safe TypeScript classes with serialization/deserialization
17+
18+
**Usage:**
19+
```bash
20+
# Install dependencies
21+
npm install
22+
23+
# Generate header models
24+
npm run generate
25+
26+
# Run the demo script
27+
npm run demo
28+
```
29+
30+
The generated header models can be used with any messaging infrastructure (NATS, Kafka, RabbitMQ, HTTP APIs, etc.) while providing type safety and automatic serialization.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Configuration file for generating header models from the e-commerce AsyncAPI specification
3+
*
4+
* This configuration demonstrates how to set up The Codegen Project to generate
5+
* TypeScript header models from an AsyncAPI document.
6+
*
7+
* Usage:
8+
* 1. Place this file in your project root
9+
* 2. Run: npx @the-codegen-project/cli generate
10+
*
11+
* The generated models will include:
12+
* - Type-safe TypeScript classes for message headers
13+
* - Serialization/deserialization methods
14+
* - Support for complex header patterns like authentication, tracing, and metadata
15+
*/
16+
17+
/** @type {import("@the-codegen-project/cli").TheCodegenConfiguration} **/
18+
export default {
19+
// Input specification type and path
20+
inputType: 'asyncapi',
21+
inputPath: './ecommerce-messaging-system.yaml',
22+
23+
// Global language setting
24+
language: 'typescript',
25+
26+
// Generator configuration
27+
generators: [
28+
{
29+
// Use the headers preset for generating header models
30+
preset: 'headers',
31+
32+
// Output directory for generated header models
33+
outputPath: './src/generated/headers',
34+
35+
// Serialization type
36+
serializationType: 'json'
37+
}
38+
]
39+
};

0 commit comments

Comments
 (0)