forked from block65/openapi-constructs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter.ts
More file actions
77 lines (68 loc) · 2.04 KB
/
parameter.ts
File metadata and controls
77 lines (68 loc) · 2.04 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
68
69
70
71
72
73
74
75
76
77
import { Construct } from 'constructs';
import type { oas31 } from 'openapi3-ts';
import type { Api } from './api.ts';
import type { Schema } from './schema.ts';
interface ParameterOptionsBase<
TName extends string | number | symbol,
TIn extends 'query' | 'header' | 'path' | 'cookie',
> {
name: TName;
in: TIn;
required: boolean;
description?: string;
deprecated?: boolean;
allowEmptyValue?: boolean;
allowReserved?: boolean;
style?: 'simple';
}
interface ParameterOptions<
TName extends string | number | symbol,
TIn extends 'query' | 'header' | 'path' | 'cookie',
> extends ParameterOptionsBase<TName, TIn> {
schema: Schema;
}
// interface ParameterOptions<TName extends string | number | symbol>
// extends ParameterOptionsBase<TName> {
// content: unknown;
// }
export class Parameter<
TName extends string | number | symbol = '',
TIn extends 'query' | 'header' | 'path' | 'cookie' = 'query',
> extends Construct {
private options: ParameterOptions<TName, TIn>;
constructor(scope: Api, id: string, options: ParameterOptions<TName, TIn>) {
super(scope, id);
this.options = options;
}
public referenceObject(): oas31.ReferenceObject {
return {
$ref: this.jsonPointer(),
};
}
public get schemaKey() {
return this.node.id;
}
public jsonPointer(): string {
return `#/components/parameters/${this.schemaKey}`;
}
public synth() {
return {
name: this.options.name.toString(),
in: this.options.in,
...(this.options.description && {
description: this.options.description,
}),
...(this.options.allowReserved && {
allowReserved: this.options.allowReserved,
}),
required: this.options.required,
...(this.options.allowEmptyValue && {
allowEmptyValue: this.options.allowEmptyValue,
}),
...(this.options.style && { style: this.options.style }),
...(this.options.schema && {
schema: this.options.schema.referenceObject(),
}),
} satisfies oas31.ParameterObject | oas31.ReferenceObject;
}
}