-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathOpenApi3Spec.ts
More file actions
94 lines (82 loc) · 2.99 KB
/
Copy pathOpenApi3Spec.ts
File metadata and controls
94 lines (82 loc) · 2.99 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import type { OpenAPIV3 } from 'openapi-types';
import type { ResponseObject } from './AbstractOpenApiSpec';
import {
defaultBasePath,
findOpenApiPathMatchingPossiblePathnames,
getPathnameWithoutBasePath,
} from '../utils/common.utils';
import {
serversPropertyNotProvidedOrIsEmptyArray,
getMatchingServerUrlsAndServerBasePaths,
} from '../utils/OpenApi3Spec.utils';
import AbstractOpenApiSpec from './AbstractOpenApiSpec';
import ValidationError, { ErrorCode } from './errors/ValidationError';
export default class OpenApi3Spec extends AbstractOpenApiSpec {
public didUserDefineServers: boolean;
constructor(protected spec: OpenAPIV3.Document) {
super(spec);
this.didUserDefineServers = !serversPropertyNotProvidedOrIsEmptyArray(spec);
this.ensureDefaultServer();
}
/**
* "If the servers property is not provided, or is an empty array, the default value would be a Server Object with a url value of '/'"
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields
*/
ensureDefaultServer(): void {
if (serversPropertyNotProvidedOrIsEmptyArray(this.spec)) {
this.spec.servers = [{ url: defaultBasePath }];
}
}
servers(): OpenAPIV3.ServerObject[] {
return this.spec.servers;
}
getServerUrls(): string[] {
return this.servers().map((server) => server.url);
}
getMatchingServerUrls(pathname: string): string[] {
return getMatchingServerUrlsAndServerBasePaths(
this.servers(),
pathname,
).map(({ concreteUrl }) => concreteUrl);
}
getMatchingServerBasePaths(pathname: string): string[] {
return getMatchingServerUrlsAndServerBasePaths(
this.servers(),
pathname,
).map(({ matchingBasePath }) => matchingBasePath);
}
findOpenApiPathMatchingPathname(pathname: string): string {
const matchingServerBasePaths = this.getMatchingServerBasePaths(pathname);
if (!matchingServerBasePaths.length) {
throw new ValidationError(ErrorCode.ServerNotFound);
}
const possiblePathnames = matchingServerBasePaths.map((basePath) =>
getPathnameWithoutBasePath(basePath, pathname),
);
const openApiPath = findOpenApiPathMatchingPossiblePathnames(
possiblePathnames,
this.paths(),
);
if (!openApiPath) {
throw new ValidationError(ErrorCode.PathNotFound);
}
return openApiPath;
}
findResponseDefinition(referenceString: string): ResponseObject {
const nameOfResponseDefinition = referenceString.split(
'#/components/responses/',
)[1];
return this.spec.components.responses[
nameOfResponseDefinition
] as ResponseObject;
}
getComponentDefinitions(): OpenAPIV3.ComponentsObject {
return this.spec.components as OpenAPIV3.ComponentsObject;
}
getComponentDefinitionsProperty(): Pick<OpenAPIV3.Document, 'components'> {
return { components: this.getComponentDefinitions() };
}
getSchemaObjects(): Pick<OpenAPIV3.ComponentsObject, 'schemas'> {
return this.getComponentDefinitions().schemas;
}
}