Skip to content

Commit 0d93ff4

Browse files
committed
fix: add support for empty responses
1 parent c69acf7 commit 0d93ff4

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

__tests__/example.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ describe('Example', () => {
185185
.addOperation(HttpMethods.HEAD, {
186186
operationId: 'checkUserIdAvailableCommand',
187187
security: noSecurityRequirement,
188+
responses: {
189+
204: new Response(api, 'HeadUserResponseFound'),
190+
404: new Response(api, 'HeadUserResponseNotFound'),
191+
},
188192
})
189193
.addOperation(HttpMethods.POST, {
190194
operationId: 'updateUserCommand',

lib/response.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,35 @@ import type { Header } from './header.js';
44
import { MediaType, type MediaTypeOptions } from './media-type.js';
55

66
interface ResponseOptions {
7-
content: MediaType | MediaTypeOptions;
7+
content?: MediaType | MediaTypeOptions;
88
description?: string;
99
headers?: Header[];
1010
}
1111

1212
export class Response extends Construct {
1313
private options: ResponseOptions;
1414

15-
private content: MediaType;
15+
private content?: MediaType | undefined;
1616

17-
constructor(scope: Construct, id: string, options: ResponseOptions) {
17+
constructor(scope: Construct, id: string, options: ResponseOptions = {}) {
1818
super(scope, id);
1919
this.options = options;
2020

21-
this.content =
22-
options.content instanceof MediaType
23-
? options.content
24-
: new MediaType(this, `${id}MediaType`, options.content);
21+
if (options.content) {
22+
this.content =
23+
options.content instanceof MediaType
24+
? options.content
25+
: new MediaType(this, `${id}MediaType`, options.content);
26+
}
2527
}
2628

2729
public synth(): OpenAPIV3_1.ResponseObject {
2830
return {
2931
description: this.options.description || '',
3032
content: {
31-
[this.options.content.contentType]: this.content.synth(),
33+
...(this.content && {
34+
[this.content.contentType]: this.content.synth(),
35+
}),
3236
},
3337
};
3438
}

0 commit comments

Comments
 (0)