-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add typescript headers generation (#162)
- Loading branch information
1 parent
c0b0a7b
commit cc9f0a2
Showing
14 changed files
with
333 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { | ||
OutputModel, | ||
TS_COMMON_PRESET, | ||
TS_DESCRIPTION_PRESET, | ||
TypeScriptFileGenerator | ||
} from '@asyncapi/modelina'; | ||
import {AsyncAPIDocumentInterface} from '@asyncapi/parser'; | ||
import {GenericCodegenContext, HeadersRenderType} from '../../types'; | ||
import {z} from 'zod'; | ||
import {defaultCodegenTypescriptModelinaOptions, pascalCase} from './utils'; | ||
|
||
export const zodTypescriptHeadersGenerator = z.object({ | ||
id: z.string().optional().default('headers-typescript'), | ||
dependencies: z.array(z.string()).optional().default([]), | ||
preset: z.literal('headers').default('headers'), | ||
outputPath: z.string().default('src/__gen__/headers'), | ||
serializationType: z.literal('json').optional().default('json'), | ||
language: z.literal('typescript').optional().default('typescript') | ||
}); | ||
|
||
export type TypescriptHeadersGenerator = z.input< | ||
typeof zodTypescriptHeadersGenerator | ||
>; | ||
export type TypescriptHeadersGeneratorInternal = z.infer< | ||
typeof zodTypescriptHeadersGenerator | ||
>; | ||
|
||
export const defaultTypeScriptHeadersOptions: TypescriptHeadersGenerator = | ||
zodTypescriptHeadersGenerator.parse({}); | ||
|
||
export interface TypescriptHeadersContext extends GenericCodegenContext { | ||
inputType: 'asyncapi'; | ||
asyncapiDocument?: AsyncAPIDocumentInterface; | ||
generator: TypescriptHeadersGeneratorInternal; | ||
} | ||
|
||
export type TypeScriptHeadersRenderType = HeadersRenderType; | ||
|
||
export async function generateTypescriptHeaders( | ||
context: TypescriptHeadersContext | ||
): Promise<TypeScriptHeadersRenderType> { | ||
const {asyncapiDocument, inputType, generator} = context; | ||
if (inputType === 'asyncapi' && asyncapiDocument === undefined) { | ||
throw new Error('Expected AsyncAPI input, was not given'); | ||
} | ||
const modelinaGenerator = new TypeScriptFileGenerator({ | ||
...defaultCodegenTypescriptModelinaOptions, | ||
enumType: 'union', | ||
useJavascriptReservedKeywords: false, | ||
presets: [ | ||
TS_DESCRIPTION_PRESET, | ||
{ | ||
preset: TS_COMMON_PRESET, | ||
options: { | ||
marshalling: true | ||
} | ||
}, | ||
] | ||
}); | ||
const returnType: Record<string, OutputModel | undefined> = {}; | ||
for (const channel of asyncapiDocument!.allChannels().all()) { | ||
const messages = channel.messages().all(); | ||
for (const message of messages) { | ||
if (message.hasHeaders()) { | ||
const schemaObj: any = { | ||
additionalProperties: false, | ||
...message.headers()?.json(), | ||
type: 'object', | ||
$id: pascalCase(`${message.id()}_headers`), | ||
$schema: 'http://json-schema.org/draft-07/schema' | ||
}; | ||
const models = await modelinaGenerator.generateToFiles( | ||
schemaObj, | ||
generator.outputPath, | ||
{exportType: 'named'}, | ||
true | ||
); | ||
returnType[channel.id()] = models[0]; | ||
} else { | ||
returnType[channel.id()] = undefined; | ||
} | ||
} | ||
} | ||
|
||
return { | ||
channelModels: returnType, | ||
generator | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** @type {import("@the-codegen-project/cli").TheCodegenConfiguration} TheCodegenConfiguration **/ | ||
export default { | ||
inputType: 'asyncapi', | ||
inputPath: 'asyncapi.json', | ||
language: 'typescript', | ||
generators: [ | ||
{ | ||
preset: 'headers', | ||
outputPath: './', | ||
serializationType: 'json', | ||
} | ||
] | ||
}; |
Oops, something went wrong.