-
-
Notifications
You must be signed in to change notification settings - Fork 168
/
SpecificationFile.ts
215 lines (184 loc) · 5.26 KB
/
SpecificationFile.ts
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { promises as fs } from 'fs';
import path from 'path';
import { URL } from 'url';
import fetch from 'node-fetch';
import yaml from 'js-yaml';
import { loadContext } from './Context';
import { ErrorLoadingSpec } from '../errors/specification-file';
import { MissingContextFileError } from '../errors/context-error';
const { readFile, lstat } = fs;
const allowedFileNames: string[] = [
'asyncapi.json',
'asyncapi.yml',
'asyncapi.yaml'
];
const TYPE_CONTEXT_NAME = 'context-name';
const TYPE_FILE_PATH = 'file-path';
const TYPE_URL = 'url-path';
export class Specification {
private readonly spec: string;
private readonly filePath?: string;
private readonly fileURL?: string;
private readonly kind?: 'file' | 'url';
constructor(spec: string, options: { filepath?: string, fileURL?: string } = {}) {
this.spec = spec;
if (options.filepath) {
this.filePath = options.filepath;
this.kind = 'file';
} else if (options.fileURL) {
this.fileURL = options.fileURL;
this.kind = 'url';
}
}
isAsyncAPI3() {
const jsObj = this.toJson();
return jsObj.asyncapi === '3.0.0';
}
toJson(): Record<string, any> {
try {
return yaml.load(this.spec, {json: true}) as Record<string, any>;
} catch (e) {
return JSON.parse(this.spec);
}
}
text() {
return this.spec;
}
getFilePath() {
return this.filePath;
}
getFileURL() {
return this.fileURL;
}
getKind() {
return this.kind;
}
getSource() {
return this.getFilePath() || this.getFileURL();
}
toSourceString() {
if (this.kind === 'file') {
return `File ${this.filePath}`;
}
return `URL ${this.fileURL}`;
}
static async fromFile(filepath: string) {
let spec;
try {
spec = await readFile(filepath, { encoding: 'utf8' });
} catch (error) {
throw new ErrorLoadingSpec('file', filepath);
}
return new Specification(spec, { filepath });
}
static async fromURL(URLpath: string) {
let response;
try {
response = await fetch(URLpath, { method: 'GET' });
if (!response.ok) {
throw new ErrorLoadingSpec('url', URLpath);
}
} catch (error) {
throw new ErrorLoadingSpec('url', URLpath);
}
return new Specification(await response?.text() as string, { fileURL: URLpath });
}
}
export default class SpecificationFile {
private readonly pathToFile: string;
constructor(filePath: string) {
this.pathToFile = filePath;
}
getPath(): string {
return this.pathToFile;
}
async read(): Promise<string> {
return readFile(this.pathToFile, { encoding: 'utf8' });
}
}
interface LoadType {
file?: boolean
url?: boolean
context?: boolean
}
/* eslint-disable sonarjs/cognitive-complexity */
export async function load(filePathOrContextName?: string, loadType?: LoadType): Promise<Specification> { // NOSONAR
if (filePathOrContextName) {
if (loadType?.file) { return Specification.fromFile(filePathOrContextName); }
if (loadType?.context) { return loadFromContext(filePathOrContextName); }
if (loadType?.url) { return Specification.fromURL(filePathOrContextName); }
const type = await nameType(filePathOrContextName);
if (type === TYPE_CONTEXT_NAME) {
return loadFromContext(filePathOrContextName);
}
if (type === TYPE_URL) {
return Specification.fromURL(filePathOrContextName);
}
await fileExists(filePathOrContextName);
return Specification.fromFile(filePathOrContextName);
}
try {
return await loadFromContext();
} catch (e) {
const autoDetectedSpecFile = await detectSpecFile();
if (autoDetectedSpecFile) {
return Specification.fromFile(autoDetectedSpecFile);
}
if (e instanceof MissingContextFileError) {
throw new ErrorLoadingSpec();
}
throw e;
}
}
export async function nameType(name: string): Promise<string> {
if (name.startsWith('.')) {
return TYPE_FILE_PATH;
}
try {
if (await fileExists(name)) {
return TYPE_FILE_PATH;
}
return TYPE_CONTEXT_NAME;
} catch (e) {
if (await isURL(name)) { return TYPE_URL; }
return TYPE_CONTEXT_NAME;
}
}
export async function isURL(urlpath: string): Promise<boolean> {
try {
const url = new URL(urlpath);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch (error) {
return false;
}
}
export async function fileExists(name: string): Promise<boolean> {
try {
if ((await lstat(name)).isFile()) {
return true;
}
throw new ErrorLoadingSpec('file', name);
} catch (e) {
throw new ErrorLoadingSpec('file', name);
}
}
async function loadFromContext(contextName?: string): Promise<Specification> {
try {
const context = await loadContext(contextName);
return Specification.fromFile(context);
} catch (error) {
if (error instanceof MissingContextFileError) {throw new ErrorLoadingSpec();}
throw error;
}
}
async function detectSpecFile(): Promise<string | undefined> {
const existingFileNames = await Promise.all(allowedFileNames.map(async filename => {
try {
const exists = await fileExists(path.resolve(process.cwd(), filename));
return exists ? filename : undefined;
} catch (e) {
// We did our best...
}
}));
return existingFileNames.find(filename => filename !== undefined);
}