Skip to content

Commit

Permalink
Merge branch 'main' into fix_weird_naming_problem
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Oct 31, 2024
2 parents 7193a15 + 6b87102 commit 54db1da
Show file tree
Hide file tree
Showing 29 changed files with 507 additions and 36 deletions.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,5 @@ Each protocol has its own limitations, corner cases, and features; thus, each ha





2 changes: 2 additions & 0 deletions docs/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@ When you submit changes, your submissions are understood to be under the same [A





13 changes: 7 additions & 6 deletions docs/generators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ Each language and inputs have specific generators;
All available generators, across languages and inputs:
- [`payloads`](./payloads.md)
- [`parameters`](./parameters.md)
- [`headers`](./headers.md)
- [`channels`](./channels.md)
- [`client`](./client.md)
- [`custom`](./custom.md)

| **Inputs** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`custom`](./custom.md) |
|---|---|---|---|---|---|
| AsyncAPI | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| **Inputs** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`headers`](./headers.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`custom`](./custom.md) |
|---|---|---|---|---|---|---|
| AsyncAPI | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |

| **Languages** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`custom`](./custom.md) |
|---|---|---|---|---|---|
| TypeScript | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| **Languages** | [`payloads`](./payloads.md) | [`parameters`](./parameters.md) | [`headers`](./headers.md) | [`channels`](./channels.md) | [`client`](./client.md) | [`custom`](./custom.md) |
|---|---|---|---|---|---|---|
| TypeScript | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
32 changes: 32 additions & 0 deletions docs/generators/headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
sidebar_position: 99
---

# Headers

```js
export default {
...,
generators: [
{
preset: 'headers',
outputPath: './src/headers',
serializationType: 'json',
language: 'typescript',
}
]
};
```

`headers` preset is for generating models that represent typed models representing headers.

This is supported through the following inputs: [`asyncapi`](#inputs)

It supports the following languages; [`typescript`](#typescript)

## Inputs

### `asyncapi`
The `headers` preset with `asyncapi` input generates all the message headers for each channel in the AsyncAPI document.

The return type is a map of channels and the model that represent the headers.
9 changes: 5 additions & 4 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.22.1 linux-x64 node-v18.20.4
@the-codegen-project/cli/0.24.0 linux-x64 node-v18.20.4
$ codegen --help [COMMAND]
USAGE
$ codegen COMMAND
Expand Down Expand Up @@ -81,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.22.1/src/commands/generate.ts)_
_See code: [src/commands/generate.ts](https://github.com/the-codegen-project/cli/blob/v0.24.0/src/commands/generate.ts)_

## `codegen help [COMMAND]`

Expand Down Expand Up @@ -111,7 +111,7 @@ Initialize The Codegen Project in your project
USAGE
$ codegen init [--help] [--input-file <value>] [--config-name <value>] [--input-type asyncapi]
[--output-directory <value>] [--config-type esm|json|yaml|ts] [--languages typescript] [--no-tty]
[--include-payloads] [--include-client] [--include-parameters] [--include-channels]
[--include-payloads] [--include-headers] [--include-client] [--include-parameters] [--include-channels]
FLAGS
--config-name=<value> [default: codegen] The name to use for the configuration file (dont include file
Expand All @@ -123,6 +123,7 @@ FLAGS
--help Show CLI help.
--include-channels Include channels generation, available for TypeScript
--include-client Include client generation, available for TypeScript
--include-headers Include headers generation, available for TypeScript
--include-parameters Include parameters generation, available for TypeScript
--include-payloads Include payloads generation, available for TypeScript
--input-file=<value> File path for the code generation input such as AsyncAPI document
Expand All @@ -138,7 +139,7 @@ DESCRIPTION
Initialize The Codegen Project in your project
```

_See code: [src/commands/init.ts](https://github.com/the-codegen-project/cli/blob/v0.22.1/src/commands/init.ts)_
_See code: [src/commands/init.ts](https://github.com/the-codegen-project/cli/blob/v0.24.0/src/commands/init.ts)_

## `codegen version`

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@the-codegen-project/cli",
"description": "CLI to work with code generation in any environment",
"version": "0.22.1",
"version": "0.24.0",
"bin": {
"codegen": "./bin/run.mjs"
},
Expand Down
36 changes: 36 additions & 0 deletions schemas/configuration-schema-0.json
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,42 @@
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
"id": {
"type": "string",
"default": "headers-typescript"
},
"dependencies": {
"type": "array",
"items": {
"type": "string"
},
"default": []
},
"preset": {
"type": "string",
"const": "headers",
"default": "headers"
},
"outputPath": {
"type": "string",
"default": "src/__gen__/headers"
},
"serializationType": {
"type": "string",
"const": "json",
"default": "json"
},
"language": {
"type": "string",
"const": "typescript",
"default": "typescript"
}
},
"additionalProperties": false
},
{
"type": "object",
"properties": {
Expand Down
23 changes: 20 additions & 3 deletions src/codegen/configuration-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ const explorer = cosmiconfig(moduleName, {
mergeSearchPlaces: true
});

/**
* Load the configuration from file.
*/
export async function loadConfigFile(filePath?: string): Promise<{
config: TheCodegenConfigurationInternal;
config: TheCodegenConfiguration;
filePath: string;
}> {
let cosmiConfig: any;
Expand All @@ -46,13 +49,27 @@ export async function loadConfigFile(filePath?: string): Promise<{
} else {
codegenConfig = cosmiConfig.config;
}
const realizedConfiguration = realizeConfiguration(codegenConfig);
return {
config: realizedConfiguration,
config: codegenConfig,
filePath: cosmiConfig.filepath
};
}

/**
* Load the configuration file and realize it with default options if necessary.
*/
export async function loadAndRealizeConfigFile(filePath?: string): Promise<{
config: TheCodegenConfigurationInternal;
filePath: string;
}> {
const codegenConfig = await loadConfigFile(filePath);
const realizedConfiguration = realizeConfiguration(codegenConfig.config);
return {
config: realizedConfiguration,
filePath: codegenConfig.filePath
};
}

/**
* Ensure that each generator has the default options along side custom properties
*/
Expand Down
47 changes: 42 additions & 5 deletions src/codegen/generators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,21 @@ import {
TypeScriptChannelsContext,
TypeScriptClientContext,
TypeScriptPayloadContext,
TypescriptParametersContext
TypescriptParametersContext,
TypescriptHeadersContext,
TypescriptHeadersGenerator,
defaultTypeScriptHeadersOptions,
generateTypescriptHeaders
} from './typescript';
import {defaultCustomGenerator, CustomGenerator} from './generic/custom';
import {TypeScriptPayloadGeneratorInternal} from './typescript/payloads';
import {TypescriptParametersGeneratorInternal} from './typescript/parameters';
import {TypeScriptChannelsGeneratorInternal} from './typescript/channels';
import {loadConfigFile} from '../configuration-manager';
import {loadAndRealizeConfigFile} from '../configuration-manager';
import {loadAsyncapi} from '../inputs/asyncapi';
import {runGenerators} from '..';
import {TypeScriptClientGeneratorInternal} from './typescript/client';
import {TypescriptHeadersGeneratorInternal} from './typescript/headers';

export {
TypeScriptChannelsGenerator,
Expand All @@ -53,7 +58,11 @@ export {
TypeScriptChannelsContext,
TypeScriptClientContext,
TypeScriptPayloadContext,
TypescriptParametersContext
TypescriptParametersContext,
TypescriptHeadersContext,
TypescriptHeadersGenerator,
defaultTypeScriptHeadersOptions,
generateTypescriptHeaders
};

export async function renderGenerator(
Expand Down Expand Up @@ -94,6 +103,27 @@ export async function renderGenerator(
}
}
}
case 'headers': {
switch (language) {
case 'typescript': {
return generateTypescriptHeaders({
asyncapiDocument,
generator: {
...(generator as TypescriptHeadersGeneratorInternal),
outputPath
},
inputType: configuration.inputType,
dependencyOutputs: renderedContext
});
}

default: {
throw new Error(
'Unable to determine language generator for headers preset'
);
}
}
}

case 'parameters': {
switch (language) {
Expand Down Expand Up @@ -155,7 +185,7 @@ export async function renderGenerator(

default: {
throw new Error(
'Unable to determine language generator for channels preset'
'Unable to determine language generator for client preset'
);
}
}
Expand Down Expand Up @@ -188,6 +218,13 @@ export function getDefaultConfiguration(
default:
return undefined;
}
case 'headers':
switch (language) {
case 'typescript':
return defaultTypeScriptHeadersOptions;
default:
return undefined;
}
case 'channels':
switch (language) {
case 'typescript':
Expand Down Expand Up @@ -224,7 +261,7 @@ export function getDefaultConfiguration(
export async function realizedConfiguration(
configFile: string | undefined
): Promise<RunGeneratorContext> {
const {config, filePath} = await loadConfigFile(configFile);
const {config, filePath} = await loadAndRealizeConfigFile(configFile);
Logger.info(`Found configuration was ${JSON.stringify(config)}`);
const documentPath = path.resolve(path.dirname(filePath), config.inputPath);
Logger.info(`Found document at '${documentPath}'`);
Expand Down
14 changes: 12 additions & 2 deletions src/codegen/generators/typescript/channels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import {renderJetstreamPullSubscribe} from './protocols/nats/jetstreamPullSubscr
import {
defaultTypeScriptParametersOptions,
TypeScriptparameterRenderType,
TypescriptParametersGenerator,
TypescriptParametersGeneratorInternal
} from '../parameters';
import {ConstrainedObjectModel, OutputModel} from '@asyncapi/modelina';
import {
TypeScriptPayloadGenerator,
TypeScriptPayloadGeneratorInternal,
TypeScriptPayloadRenderType,
defaultTypeScriptPayloadGenerator
Expand Down Expand Up @@ -280,10 +282,18 @@ export function includeTypeScriptChannelDependencies(
(generatorSearch) => generatorSearch.id === payloadGeneratorId
) !== undefined;
if (!hasParameterGenerator) {
newGenerators.push(defaultTypeScriptParametersOptions);
const defaultChannelParameterGenerator: TypescriptParametersGenerator = {
...defaultTypeScriptParametersOptions,
outputPath: path.resolve(generator.outputPath ?? '', './parameter')
};
newGenerators.push(defaultChannelParameterGenerator);
}
if (!hasPayloadGenerator) {
newGenerators.push(defaultTypeScriptPayloadGenerator);
const defaultChannelPayloadGenerator: TypeScriptPayloadGenerator = {
...defaultTypeScriptPayloadGenerator,
outputPath: path.resolve(generator.outputPath ?? '', './payload')
};
newGenerators.push(defaultChannelPayloadGenerator);
}
return newGenerators;
}
11 changes: 9 additions & 2 deletions src/codegen/generators/typescript/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import {AsyncAPIDocumentInterface} from '@asyncapi/parser';
import {mkdir, writeFile} from 'node:fs/promises';
import path from 'node:path';
import {z} from 'zod';
import {defaultTypeScriptChannelsGenerator} from '../channels';
import {
defaultTypeScriptChannelsGenerator,
TypeScriptChannelsGenerator
} from '../channels';
import {generateNatsClient} from './protocols/nats';
export type SupportedProtocols = 'nats';

Expand Down Expand Up @@ -80,7 +83,11 @@ export function includeTypeScriptClientDependencies(
(generatorSearch) => generatorSearch.id === channelsGeneratorId
) !== undefined;
if (!hasChannelsGenerator) {
newGenerators.push(defaultTypeScriptChannelsGenerator);
const defaultClientPayloadGenerator: TypeScriptChannelsGenerator = {
...defaultTypeScriptChannelsGenerator,
outputPath: path.resolve(generator.outputPath ?? '', './channels')
};
newGenerators.push(defaultClientPayloadGenerator);
}
return newGenerators;
}
Loading

0 comments on commit 54db1da

Please sign in to comment.