forked from block65/openapi-constructs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.ts
More file actions
67 lines (60 loc) · 1.9 KB
/
path.ts
File metadata and controls
67 lines (60 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Construct } from 'constructs';
import type { oas31 } from 'openapi3-ts';
import type { Api } from './api.ts';
import type { HttpMethod } from './http-method.ts';
import { Operation, type OperationOptions } from './operation.ts';
import type { Server } from './server.ts';
import type { Tag } from './tag.ts';
import type { ValidParameter } from './types.ts';
interface PathOptions<TPath extends string> {
path: TPath;
summary?: string;
servers?: Server[];
parameters?: ValidParameter<TPath>[];
tags?: Set<Tag>;
}
export class Path<TPath extends string = '/'> extends Construct {
public options: PathOptions<TPath>;
constructor(scope: Api, options: PathOptions<TPath>) {
super(scope, options.path);
this.options = options;
}
public addOperation(
method: HttpMethod,
options: OperationOptions<TPath>,
): this {
// make sure we are not duplicating tags
// options.tags?.forEach((tag) => strict(!this.options.tags?.has(tag)));
// eslint-disable-next-line no-new
new Operation(this, method, {
...options,
tags: new Set([...(this.options.tags || []), ...(options.tags || [])]),
});
return this;
}
public get schemaKey() {
return this.options.path;
}
public synth() {
return {
...Object.fromEntries(
this.node
.findAll()
.filter(
(child): child is Operation<TPath> => child instanceof Operation,
)
.sort((a, b) => a.order - b.order)
.map((child) => [child.method, child.synth()]),
),
...(this.options.servers && {
servers: this.options.servers.map((child) => child.synth()),
}),
...(this.options.parameters && {
parameters: this.options.parameters.map((child) =>
child.referenceObject(),
),
}),
...(this.options.summary && { summary: this.options.summary }),
} satisfies oas31.PathItemObject;
}
}