Skip to content

Commit 454beb0

Browse files
committed
refactor(asyncapi): enhance channel parameter handling with enum support
1 parent 92d97dd commit 454beb0

4 files changed

Lines changed: 51 additions & 2 deletions

File tree

packages/asyncapi-typescript-plugin/src/transform/channels-object.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
AsyncAPIChannelObject,
33
AsyncAPIContext,
4+
AsyncAPIParameterObject,
45
ReferenceObject,
56
} from '../types.js';
67
import {
@@ -9,6 +10,7 @@ import {
910
tsLiteral,
1011
tsModifiers,
1112
tsPropertyIndex,
13+
tsUnion,
1214
} from 'openapi-typescript/dist/lib/ts.js';
1315
import { getEntries } from 'openapi-typescript/dist/lib/utils.js';
1416
import ts from 'typescript';
@@ -83,13 +85,16 @@ export default function transformChannelsObject(
8385
if (channel.parameters && typeof channel.parameters === 'object') {
8486
const paramMembers: ts.TypeElement[] = [];
8587

86-
for (const [paramId] of Object.entries(channel.parameters)) {
88+
for (const [paramId, param] of Object.entries(channel.parameters)) {
8789
paramMembers.push(
8890
ts.factory.createPropertySignature(
8991
tsModifiers({ readonly: ctx.immutable }),
9092
tsPropertyIndex(paramId),
9193
undefined,
92-
ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword)
94+
transformChannelParameterType(
95+
param as AsyncAPIParameterObject | ReferenceObject,
96+
ctx
97+
)
9398
)
9499
);
95100
}
@@ -151,3 +156,24 @@ export default function transformChannelsObject(
151156

152157
return ts.factory.createTypeLiteralNode(members);
153158
}
159+
160+
function transformChannelParameterType(
161+
parameter: AsyncAPIParameterObject | ReferenceObject,
162+
ctx: AsyncAPIContext
163+
): ts.TypeNode {
164+
const parameterObject =
165+
'$ref' in parameter
166+
? ctx.resolve<AsyncAPIParameterObject>(parameter.$ref)
167+
: parameter;
168+
169+
const enumValues =
170+
parameterObject?.enum?.filter(
171+
(value): value is string => typeof value === 'string'
172+
) ?? [];
173+
174+
if (enumValues.length > 0) {
175+
return tsUnion(enumValues.map(tsLiteral));
176+
}
177+
178+
return ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword);
179+
}

packages/asyncapi-typescript-plugin/src/transform/components-object.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
addJSDocComment,
1515
NEVER,
1616
QUESTION_TOKEN,
17+
tsLiteral,
1718
tsModifiers,
1819
tsPropertyIndex,
1920
} from 'openapi-typescript/dist/lib/ts.js';
@@ -329,6 +330,17 @@ function transformParameters(
329330
);
330331
}
331332

333+
if (Array.isArray(param.enum) && param.enum.length > 0) {
334+
members.push(
335+
ts.factory.createPropertySignature(
336+
tsModifiers({ readonly: ctx.immutable }),
337+
tsPropertyIndex('enum'),
338+
undefined,
339+
ts.factory.createTupleTypeNode(param.enum.map(tsLiteral))
340+
)
341+
);
342+
}
343+
332344
if (param.schema) {
333345
let schemaType: ts.TypeNode;
334346
if ('$ref' in param.schema) {

packages/asyncapi-typescript-plugin/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ export interface AsyncAPIMessageObject {
162162

163163
export interface AsyncAPIParameterObject {
164164
description?: string;
165+
enum?: string[];
166+
default?: string;
167+
examples?: string[];
165168
schema?: SchemaObject | ReferenceObject;
166169
location?: string;
167170
[key: `x-${string}`]: unknown;

packages/test-fixtures/asyncapi.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
"parameters": {
7070
"streetlightId": {
7171
"description": "The ID of the streetlight.",
72+
"enum": [
73+
"streetlight-1",
74+
"streetlight-2"
75+
],
7276
"location": "$message.payload#/item/id"
7377
}
7478
},
@@ -820,6 +824,10 @@
820824
"parameters": {
821825
"streetlightId": {
822826
"description": "The ID of the streetlight.",
827+
"enum": [
828+
"streetlight-1",
829+
"streetlight-2"
830+
],
823831
"location": "$message.payload#/item/id"
824832
}
825833
},

0 commit comments

Comments
 (0)