Skip to content

Commit 2e6e68c

Browse files
committed
feat: add order to force ordering for paths
1 parent 90dc5ac commit 2e6e68c

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

lib/operation.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Response } from './response.js';
77
import type { SecurityRequirement } from './security-requirement.js';
88
import type { Tag } from './tag.js';
99
import type { ExtractRouteParams } from './types.js';
10+
import { stripUndefined } from './utils.js';
1011

1112
export interface OperationOptions<TPath extends string = '/'> {
1213
operationId: string;
@@ -20,13 +21,16 @@ export interface OperationOptions<TPath extends string = '/'> {
2021
[statusCode: string | number]: Response;
2122
};
2223
requestBody?: RequestBody | RequestBodyOptions;
24+
order?: number;
2325
}
2426

2527
export class Operation<TPath extends string = '/'> extends Construct {
2628
private readonly options: OperationOptions<TPath>;
2729

2830
public readonly method: HttpMethods;
2931

32+
public readonly order: number;
33+
3034
private requestBody?: RequestBody;
3135

3236
public hasOperationId(operationId: OperationOptions['operationId']): boolean {
@@ -42,6 +46,8 @@ export class Operation<TPath extends string = '/'> extends Construct {
4246
this.method = method;
4347
this.options = options;
4448

49+
this.order = options.order || 0;
50+
4551
if (options.requestBody) {
4652
this.requestBody =
4753
options.requestBody instanceof RequestBody
@@ -68,10 +74,13 @@ export class Operation<TPath extends string = '/'> extends Construct {
6874
}
6975

7076
public synth() {
71-
return {
72-
...(this.options.operationId && {
73-
operationId: this.options.operationId,
74-
}),
77+
return stripUndefined({
78+
operationId: this.options.operationId,
79+
description: this.options.description || undefined,
80+
summary: this.options.summary || undefined,
81+
tags:
82+
this.options.tags && [...this.options.tags].map((child) => child.name),
83+
deprecated: this.options.deprecated,
7584
...(this.options.parameters && {
7685
parameters: this.options.parameters.map((child) => child.synth()),
7786
}),
@@ -92,6 +101,6 @@ export class Operation<TPath extends string = '/'> extends Construct {
92101
]),
93102
),
94103
}),
95-
} satisfies oas31.OperationObject;
104+
}) satisfies oas31.OperationObject;
96105
}
97106
}

lib/path.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { strict } from 'node:assert';
21
import { Construct } from 'constructs';
32
import type { oas31 } from 'openapi3-ts';
43
import type { Api } from './api.js';
@@ -30,7 +29,7 @@ export class Path<TPath extends string = '/'> extends Construct {
3029
options: OperationOptions<TPath>,
3130
): this {
3231
// make sure we are not duplicating tags
33-
options.tags?.forEach((tag) => strict(!this.options.tags?.has(tag)));
32+
// options.tags?.forEach((tag) => strict(!this.options.tags?.has(tag)));
3433

3534
// eslint-disable-next-line no-new
3635
new Operation(this, method, {
@@ -52,6 +51,7 @@ export class Path<TPath extends string = '/'> extends Construct {
5251
.filter(
5352
(child): child is Operation<TPath> => child instanceof Operation,
5453
)
54+
.sort((a, b) => a.order - b.order)
5555
.map((child) => [child.method, child.synth()]),
5656
),
5757
...(this.options.servers && {

0 commit comments

Comments
 (0)