Skip to content

Commit

Permalink
feat: add typescript payload options (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Jun 21, 2024
1 parent eab2a11 commit fd4b573
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 52 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
testRegex: '((\\.|/)(test|spec))\\.[jt]sx?$',
// Module file extensions for importing
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testTimeout: 10_000,
testTimeout: 100000,
collectCoverageFrom: ['src/**'],
moduleNameMapper: {
'^nimma/legacy$': '<rootDir>/node_modules/nimma/dist/legacy/cjs/index.js',
Expand Down
18 changes: 6 additions & 12 deletions src/codegen/generators/java/payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import {z} from 'zod';
export const zodJavaPayloadGenerator = z.object({
id: z.string().optional().default('payloads-java'),
dependencies: z.array(z.string()).optional().default([]),
preset: z.literal('payloads'),
preset: z.literal('payloads').default('payloads'),
outputPath: z
.string()
.default('./target/generated-sources/the/codegen/project'),
packageName: z.string().default('the.codegen.project'),
serializationType: z.literal('json').optional(),
language: z.literal('java').optional()
serializationType: z.literal('json').optional().default('json'),
language: z.literal('java').optional().default('java')
});

export type JavaPayloadGenerator = z.infer<typeof zodJavaPayloadGenerator>;
Expand All @@ -23,15 +23,9 @@ export interface JavaPayloadContext extends GenericCodegenContext {
generator: JavaPayloadGenerator;
}

export const defaultJavaPayloadGenerator: JavaPayloadGenerator = {
preset: 'payloads',
language: 'java',
outputPath: './target/generated-sources/the/codegen/project',
packageName: 'the.codegen.project',
id: 'payloads-java',
serializationType: 'json',
dependencies: []
};
export const defaultJavaPayloadGenerator: JavaPayloadGenerator =
zodJavaPayloadGenerator.parse({});

export async function generateJavaPayload(context: JavaPayloadContext) {
const {documentPath, generator} = context;
const modelinaGenerator = new JavaFileGenerator({
Expand Down
27 changes: 10 additions & 17 deletions src/codegen/generators/typescript/channels/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,32 @@ export const zodTypescriptChannelsGenerator = z.object({
.array(z.string())
.optional()
.default(['parameters-typescript', 'payloads-typescript']),
preset: z.literal('channels'),
outputPath: z.string(),
protocols: z.array(z.enum(['nats'])),
preset: z.literal('channels').default('channels'),
outputPath: z.string().default('src/__gen__/channels'),
protocols: z.array(z.enum(['nats'])).default(['nats']),
parameterGeneratorId: z
.string()
.optional()
.describe(
'In case you have multiple TypeScript parameter generators, you can specify which one to use as the dependency for this channels generator.'
),
)
.default('parameters-typescript'),
payloadGeneratorId: z
.string()
.optional()
.describe(
'In case you have multiple TypeScript payload generators, you can specify which one to use as the dependency for this channels generator.'
),
language: z.literal('typescript').optional()
)
.default('payloads-typescript'),
language: z.literal('typescript').optional().default('typescript')
});

export type TypeScriptChannelsGenerator = z.infer<
typeof zodTypescriptChannelsGenerator
>;

export const defaultTypeScriptChannelsGenerator: TypeScriptChannelsGenerator = {
preset: 'channels',
language: 'typescript',
outputPath: 'src/__gen__/channels',
// eslint-disable-next-line sonarjs/no-duplicate-string
dependencies: ['parameters-typescript', 'payloads-typescript'],
protocols: ['nats'],
id: 'channels-typescript',
parameterGeneratorId: 'parameters-typescript',
payloadGeneratorId: 'payloads-typescript'
};
export const defaultTypeScriptChannelsGenerator: TypeScriptChannelsGenerator =
zodTypescriptChannelsGenerator.parse({});

export interface TypeScriptChannelsContext extends GenericCodegenContext {
inputType: 'asyncapi';
Expand Down
13 changes: 3 additions & 10 deletions src/codegen/generators/typescript/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,18 @@ import {z} from 'zod';
export const zodTypescriptParametersGenerator = z.object({
id: z.string().optional().default('parameters-typescript'),
dependencies: z.array(z.string()).optional().default([]),
preset: z.literal('parameters'),
preset: z.literal('parameters').default('parameters'),
outputPath: z.string().default('src/__gen__/parameters'),
serializationType: z.literal('json').optional().default('json'),
language: z.literal('typescript').optional()
language: z.literal('typescript').optional().default('typescript')
});

export type TypescriptParametersGenerator = z.infer<
typeof zodTypescriptParametersGenerator
>;

export const defaultTypeScriptParametersOptions: TypescriptParametersGenerator =
{
preset: 'parameters',
language: 'typescript',
outputPath: 'src/__gen__/parameters',
serializationType: 'json',
id: 'parameters-typescript',
dependencies: []
};
zodTypescriptParametersGenerator.parse({});

export interface TypescriptParametersContext extends GenericCodegenContext {
inputType: 'asyncapi';
Expand Down
58 changes: 46 additions & 12 deletions src/codegen/generators/typescript/payloads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,51 @@ import {z} from 'zod';
export const zodTypeScriptPayloadGenerator = z.object({
id: z.string().optional().default('payloads-typescript'),
dependencies: z.array(z.string()).optional().default([]),
preset: z.literal('payloads'),
outputPath: z.string(),
preset: z.literal('payloads').default('payloads'),
outputPath: z.string().default('src/__gen__/payloads'),
serializationType: z.literal('json').optional().default('json'),
language: z.literal('typescript').optional()
language: z.literal('typescript').optional().default('typescript'),
model: z
.enum(['class', 'interface'])
.optional()
.default('class')
.describe(
'By default all payloads are generated as class types, but in some cases interfaces might be more prudent.'
),
enum: z
.enum(['enum', 'union'])
.optional()
.default('enum')
.describe(
'By default all payloads enum types are generated as separate enum types, but in some cases a simple union type might be more prudent.'
),
map: z
.enum(['indexedObject', 'map', 'record'])
.optional()
.default('record')
.describe(''),
moduleSystem: z.enum(['esm', 'cjs']).optional().default('esm').describe(''),
useForJavaScript: z
.boolean()
.optional()
.default(true)
.describe(
'By default we assume that the models might be transpiled to JS, therefore JS restrictions will be applied by default.'
),
rawPropertyNames: z
.boolean()
.optional()
.default(false)
.describe(
'Use raw property names instead of constrained ones, where you most likely need to access them with obj["propertyName"] instead of obj.propertyName'
)
});
export type TypeScriptPayloadGenerator = z.infer<
typeof zodTypeScriptPayloadGenerator
>;

export const defaultTypeScriptPayloadGenerator: TypeScriptPayloadGenerator = {
preset: 'payloads',
language: 'typescript',
outputPath: 'src/__gen__/payloads',
serializationType: 'json',
id: 'payloads-typescript',
dependencies: []
};
export const defaultTypeScriptPayloadGenerator: TypeScriptPayloadGenerator =
zodTypeScriptPayloadGenerator.parse({});

export interface TypeScriptPayloadContext extends GenericCodegenContext {
inputType: 'asyncapi';
Expand All @@ -47,7 +75,13 @@ export async function generateTypescriptPayload(
marshalling: true
}
}
]
],
enumType: generator.enum,
mapType: generator.map,
modelType: generator.model,
moduleSystem: generator.moduleSystem.toUpperCase() as any,
rawPropertyNames: generator.rawPropertyNames,
useJavascriptReservedKeywords: generator.useForJavaScript
});
return generateAsyncAPIPayloads(
asyncapiDocument!,
Expand Down

0 comments on commit fd4b573

Please sign in to comment.