@killb/openapi-alchemist 0.0.0-stage → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -0
- package/README.md +249 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.js +56 -0
- package/dist/lib/converters/openapi3_to_swagger2.d.ts +24 -0
- package/dist/lib/converters/openapi3_to_swagger2.js +551 -0
- package/dist/lib/formats/openapi_3.d.ts +15 -0
- package/dist/lib/formats/openapi_3.js +48 -0
- package/dist/lib/formats/swagger_2.d.ts +15 -0
- package/dist/lib/formats/swagger_2.js +57 -0
- package/dist/lib/refs.d.ts +6 -0
- package/dist/lib/refs.js +111 -0
- package/dist/lib/server-url.d.ts +7 -0
- package/dist/lib/server-url.js +45 -0
- package/dist/lib/util.d.ts +12 -0
- package/dist/lib/util.js +113 -0
- package/dist/types.d.ts +68 -0
- package/dist/types.js +306 -0
- package/package.json +100 -4
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function validateRefRoot(refRoot: unknown): string;
|
|
2
|
+
export declare function isInside(root: string, target: string): boolean;
|
|
3
|
+
export declare function resolveExternalFile(ref: string, file: string, baseDir: string, root: string): string;
|
|
4
|
+
export declare function walkOwn(start: unknown, keys: string[], ref: string): any;
|
|
5
|
+
export declare function decodePointer(pointer: string): string[];
|
|
6
|
+
export declare function assignOwn(target: any, key: string, value: unknown): void;
|
package/dist/lib/refs.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.validateRefRoot = validateRefRoot;
|
|
37
|
+
exports.isInside = isInside;
|
|
38
|
+
exports.resolveExternalFile = resolveExternalFile;
|
|
39
|
+
exports.walkOwn = walkOwn;
|
|
40
|
+
exports.decodePointer = decodePointer;
|
|
41
|
+
exports.assignOwn = assignOwn;
|
|
42
|
+
const fs = __importStar(require("node:fs"));
|
|
43
|
+
const path = __importStar(require("node:path"));
|
|
44
|
+
function validateRefRoot(refRoot) {
|
|
45
|
+
if (typeof refRoot !== 'string' || !path.isAbsolute(refRoot)) {
|
|
46
|
+
throw new Error(`Invalid refRoot ${JSON.stringify(refRoot)}: it must be an absolute path`);
|
|
47
|
+
}
|
|
48
|
+
let real;
|
|
49
|
+
try {
|
|
50
|
+
real = fs.realpathSync(refRoot);
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
throw new Error(`Invalid refRoot ${JSON.stringify(refRoot)}: the directory does not exist`);
|
|
54
|
+
}
|
|
55
|
+
if (!fs.statSync(real).isDirectory()) {
|
|
56
|
+
throw new Error(`Invalid refRoot ${JSON.stringify(refRoot)}: it is not a directory`);
|
|
57
|
+
}
|
|
58
|
+
return real;
|
|
59
|
+
}
|
|
60
|
+
function isInside(root, target) {
|
|
61
|
+
const relative = path.relative(root, target);
|
|
62
|
+
if (relative === '')
|
|
63
|
+
return true;
|
|
64
|
+
if (path.isAbsolute(relative))
|
|
65
|
+
return false;
|
|
66
|
+
return relative !== '..' && !relative.startsWith('..' + path.sep);
|
|
67
|
+
}
|
|
68
|
+
function resolveExternalFile(ref, file, baseDir, root) {
|
|
69
|
+
const refused = new Error(`$ref ${JSON.stringify(ref)} does not resolve to a file inside the reference root`);
|
|
70
|
+
const candidate = path.resolve(baseDir, file);
|
|
71
|
+
if (!isInside(root, candidate))
|
|
72
|
+
throw refused;
|
|
73
|
+
let real;
|
|
74
|
+
try {
|
|
75
|
+
real = fs.realpathSync(candidate);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw refused;
|
|
79
|
+
}
|
|
80
|
+
if (!isInside(root, real))
|
|
81
|
+
throw refused;
|
|
82
|
+
return real;
|
|
83
|
+
}
|
|
84
|
+
function walkOwn(start, keys, ref) {
|
|
85
|
+
let cur = start;
|
|
86
|
+
for (const key of keys) {
|
|
87
|
+
if (cur === null ||
|
|
88
|
+
typeof cur !== 'object' ||
|
|
89
|
+
!Object.prototype.hasOwnProperty.call(cur, key)) {
|
|
90
|
+
throw new Error(`Unresolvable $ref ${JSON.stringify(ref)}`);
|
|
91
|
+
}
|
|
92
|
+
cur = cur[key];
|
|
93
|
+
}
|
|
94
|
+
return cur;
|
|
95
|
+
}
|
|
96
|
+
function decodePointer(pointer) {
|
|
97
|
+
return pointer.split('/').map((k) => k.replace(/~1/g, '/').replace(/~0/g, '~'));
|
|
98
|
+
}
|
|
99
|
+
function assignOwn(target, key, value) {
|
|
100
|
+
if (key === '__proto__') {
|
|
101
|
+
Object.defineProperty(target, key, {
|
|
102
|
+
value,
|
|
103
|
+
enumerable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
target[key] = value;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.substituteVariables = substituteVariables;
|
|
4
|
+
exports.parseServerUrl = parseServerUrl;
|
|
5
|
+
function substituteVariables(serverUrl, variables) {
|
|
6
|
+
if (variables === null || typeof variables !== 'object')
|
|
7
|
+
return serverUrl;
|
|
8
|
+
return serverUrl.replace(/\{([^{}]*)\}/g, (token, name) => {
|
|
9
|
+
if (!Object.prototype.hasOwnProperty.call(variables, name))
|
|
10
|
+
return token;
|
|
11
|
+
const variable = variables[name];
|
|
12
|
+
const value = variable && typeof variable === 'object' ? variable.default : undefined;
|
|
13
|
+
return value ? String(value) : token;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
function stripQueryAndFragment(value) {
|
|
17
|
+
const end = value.search(/[?#]/);
|
|
18
|
+
return end === -1 ? value : value.slice(0, end);
|
|
19
|
+
}
|
|
20
|
+
function pathOf(value) {
|
|
21
|
+
const schemeEnd = value.indexOf('://');
|
|
22
|
+
if (schemeEnd === -1)
|
|
23
|
+
return stripQueryAndFragment(value);
|
|
24
|
+
const rest = value.slice(schemeEnd + 3);
|
|
25
|
+
const slash = rest.search(/[/?#]/);
|
|
26
|
+
if (slash === -1 || rest[slash] !== '/')
|
|
27
|
+
return '';
|
|
28
|
+
return stripQueryAndFragment(rest.slice(slash));
|
|
29
|
+
}
|
|
30
|
+
function parseServerUrl(value) {
|
|
31
|
+
if (/[{}]/.test(value))
|
|
32
|
+
return { basePath: pathOf(value) };
|
|
33
|
+
let parsed;
|
|
34
|
+
try {
|
|
35
|
+
parsed = new URL(value);
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
return { basePath: pathOf(value) };
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
host: parsed.host || undefined,
|
|
42
|
+
scheme: parsed.protocol.slice(0, -1),
|
|
43
|
+
basePath: parsed.pathname,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare class Util {
|
|
2
|
+
static joinPath(...args: string[]): string;
|
|
3
|
+
static parseJSON: (data: string) => Promise<any>;
|
|
4
|
+
static parseYAML: (data: string) => Promise<unknown>;
|
|
5
|
+
static resourceReaders: {
|
|
6
|
+
file: (filename: string) => Promise<unknown>;
|
|
7
|
+
object: (data: any) => Promise<any>;
|
|
8
|
+
string: (data: string) => Promise<string>;
|
|
9
|
+
};
|
|
10
|
+
static getSourceType(source: any): string | undefined;
|
|
11
|
+
static removeNonValues(obj: any): void;
|
|
12
|
+
}
|
package/dist/lib/util.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.Util = void 0;
|
|
37
|
+
const Yaml = __importStar(require("js-yaml"));
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
class Util {
|
|
40
|
+
static joinPath(...args) {
|
|
41
|
+
return args.join('/').replace(/\/\/+/g, '/');
|
|
42
|
+
}
|
|
43
|
+
static parseJSON = (data) => {
|
|
44
|
+
try {
|
|
45
|
+
return Promise.resolve(JSON.parse(data));
|
|
46
|
+
}
|
|
47
|
+
catch (err) {
|
|
48
|
+
return Promise.reject(err);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
static parseYAML = (data) => Promise.resolve(Yaml.load(data));
|
|
52
|
+
static resourceReaders = {
|
|
53
|
+
file: (filename) => {
|
|
54
|
+
return new Promise((resolve, reject) => {
|
|
55
|
+
fs.readFile(filename, 'utf8', (err, data) => {
|
|
56
|
+
if (err)
|
|
57
|
+
reject(err);
|
|
58
|
+
else
|
|
59
|
+
resolve(data);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
object: (data) => Promise.resolve(data),
|
|
64
|
+
string: (data) => Promise.resolve(data),
|
|
65
|
+
};
|
|
66
|
+
static getSourceType(source) {
|
|
67
|
+
if (typeof source === 'object' && source !== null) {
|
|
68
|
+
return 'object';
|
|
69
|
+
}
|
|
70
|
+
if (typeof source !== 'string') {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
if (fs.existsSync(source))
|
|
75
|
+
return 'file';
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
}
|
|
79
|
+
if (source.startsWith('./') || source.startsWith('../') || !source.includes('://')) {
|
|
80
|
+
return 'file';
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
return 'string';
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
static removeNonValues(obj) {
|
|
87
|
+
if (obj === null || typeof obj !== 'object') {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (Array.isArray(obj)) {
|
|
91
|
+
for (let i = obj.length - 1; i >= 0; i--) {
|
|
92
|
+
if (obj[i] === undefined) {
|
|
93
|
+
obj.splice(i, 1);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
this.removeNonValues(obj[i]);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
const keys = Object.keys(obj);
|
|
102
|
+
for (const key of keys) {
|
|
103
|
+
if (obj[key] === undefined) {
|
|
104
|
+
delete obj[key];
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this.removeNonValues(obj[key]);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.Util = Util;
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export interface ConvertOptions {
|
|
2
|
+
from: 'openapi_3';
|
|
3
|
+
to: 'swagger_2' | 'openapi_3';
|
|
4
|
+
source: string | object;
|
|
5
|
+
syntax?: 'json' | 'yaml';
|
|
6
|
+
order?: 'openapi' | 'alpha' | 'false';
|
|
7
|
+
passthrough?: any;
|
|
8
|
+
refRoot?: string;
|
|
9
|
+
}
|
|
10
|
+
export interface ConvertResult {
|
|
11
|
+
spec: any;
|
|
12
|
+
stringify(options?: StringifyOptions): string;
|
|
13
|
+
}
|
|
14
|
+
export interface StringifyOptions {
|
|
15
|
+
syntax?: 'json' | 'yaml';
|
|
16
|
+
order?: 'openapi' | 'alpha' | 'false';
|
|
17
|
+
}
|
|
18
|
+
export interface ValidationResult {
|
|
19
|
+
errors: any[] | null;
|
|
20
|
+
warnings: any[] | null;
|
|
21
|
+
}
|
|
22
|
+
export interface ResourceReaders {
|
|
23
|
+
file: (filename: string) => Promise<string>;
|
|
24
|
+
object: (data: any) => Promise<any>;
|
|
25
|
+
string: (data: string) => Promise<string>;
|
|
26
|
+
}
|
|
27
|
+
export declare abstract class BaseFormat {
|
|
28
|
+
spec: any;
|
|
29
|
+
format: string;
|
|
30
|
+
converters: {
|
|
31
|
+
[key: string]: (spec: BaseFormat, passthrough?: any) => Promise<any>;
|
|
32
|
+
};
|
|
33
|
+
sourceType?: string;
|
|
34
|
+
source?: string;
|
|
35
|
+
subResources?: {
|
|
36
|
+
[key: string]: any;
|
|
37
|
+
};
|
|
38
|
+
refRoot?: string;
|
|
39
|
+
constructor(spec?: any);
|
|
40
|
+
abstract formatName: string;
|
|
41
|
+
abstract supportedVersions: string[];
|
|
42
|
+
abstract getFormatVersion(): string;
|
|
43
|
+
abstract checkFormat(spec: any): boolean;
|
|
44
|
+
stringify(options?: StringifyOptions): string;
|
|
45
|
+
fillMissing(_dummyData?: any): void;
|
|
46
|
+
validate(callback?: (err: any, result: ValidationResult) => void): Promise<ValidationResult>;
|
|
47
|
+
listSubResources(): {
|
|
48
|
+
[key: string]: string;
|
|
49
|
+
};
|
|
50
|
+
resolveSubResources(): Promise<void>;
|
|
51
|
+
parse(data: any): Promise<any>;
|
|
52
|
+
readSpec(source: string | object): Promise<[any, string]>;
|
|
53
|
+
resolveResources(source: string | object): Promise<void>;
|
|
54
|
+
convertTo(format: string, passthrough?: any, callback?: (err: any, result: BaseFormat) => void): Promise<BaseFormat>;
|
|
55
|
+
convertTransitive(intermediaries: string[], passthrough?: any): Promise<any>;
|
|
56
|
+
protected abstract parsers: {
|
|
57
|
+
[key: string]: (data: string) => Promise<any>;
|
|
58
|
+
};
|
|
59
|
+
protected abstract fixSpec(): void;
|
|
60
|
+
}
|
|
61
|
+
export declare class Util {
|
|
62
|
+
static joinPath(...args: string[]): string;
|
|
63
|
+
static parseJSON: (data: string) => Promise<any>;
|
|
64
|
+
static parseYAML: (data: string) => Promise<unknown>;
|
|
65
|
+
static resourceReaders: ResourceReaders;
|
|
66
|
+
static getSourceType(source: any): string | undefined;
|
|
67
|
+
static removeNonValues(obj: any): void;
|
|
68
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.Util = exports.BaseFormat = void 0;
|
|
37
|
+
const Yaml = __importStar(require("js-yaml"));
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const refs_1 = require("./lib/refs");
|
|
40
|
+
function deepSortObject(obj, compareFn) {
|
|
41
|
+
if (obj === null || typeof obj !== 'object') {
|
|
42
|
+
return obj;
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(obj)) {
|
|
45
|
+
return obj.map(item => deepSortObject(item, compareFn));
|
|
46
|
+
}
|
|
47
|
+
const sortedKeys = Object.keys(obj).sort(compareFn);
|
|
48
|
+
const sortedObj = {};
|
|
49
|
+
for (const key of sortedKeys) {
|
|
50
|
+
(0, refs_1.assignOwn)(sortedObj, key, deepSortObject(obj[key], compareFn));
|
|
51
|
+
}
|
|
52
|
+
return sortedObj;
|
|
53
|
+
}
|
|
54
|
+
class BaseFormat {
|
|
55
|
+
spec;
|
|
56
|
+
format;
|
|
57
|
+
converters;
|
|
58
|
+
sourceType;
|
|
59
|
+
source;
|
|
60
|
+
subResources;
|
|
61
|
+
refRoot;
|
|
62
|
+
constructor(spec) {
|
|
63
|
+
if (spec)
|
|
64
|
+
this.spec = spec;
|
|
65
|
+
this.format = 'base_format';
|
|
66
|
+
this.converters = {};
|
|
67
|
+
}
|
|
68
|
+
stringify(options) {
|
|
69
|
+
const syntax = options?.syntax || 'json';
|
|
70
|
+
const order = options?.order || 'openapi';
|
|
71
|
+
let sortedSpecs;
|
|
72
|
+
if (order === 'false') {
|
|
73
|
+
sortedSpecs = this.spec;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const attr = [
|
|
77
|
+
'externalDocs',
|
|
78
|
+
'tags',
|
|
79
|
+
'security',
|
|
80
|
+
'securityDefinitions',
|
|
81
|
+
'responses',
|
|
82
|
+
'parameters',
|
|
83
|
+
'definitions',
|
|
84
|
+
'components',
|
|
85
|
+
'paths',
|
|
86
|
+
'produces',
|
|
87
|
+
'consumes',
|
|
88
|
+
'schemes',
|
|
89
|
+
'basePath',
|
|
90
|
+
'host',
|
|
91
|
+
'servers',
|
|
92
|
+
'info',
|
|
93
|
+
'swagger',
|
|
94
|
+
'openapi',
|
|
95
|
+
];
|
|
96
|
+
sortedSpecs = deepSortObject(this.spec, (a, b) => {
|
|
97
|
+
let aIdx = -1;
|
|
98
|
+
let bIdx = -1;
|
|
99
|
+
if (order !== 'alpha') {
|
|
100
|
+
aIdx = attr.indexOf(a);
|
|
101
|
+
bIdx = attr.indexOf(b);
|
|
102
|
+
}
|
|
103
|
+
if (aIdx === -1 && bIdx === -1) {
|
|
104
|
+
if (a === b)
|
|
105
|
+
return 0;
|
|
106
|
+
return a < b ? -1 : 1;
|
|
107
|
+
}
|
|
108
|
+
if (aIdx === bIdx)
|
|
109
|
+
return 0;
|
|
110
|
+
return aIdx < bIdx ? 1 : -1;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (syntax === 'yaml') {
|
|
114
|
+
return Yaml.dump(sortedSpecs);
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
return JSON.stringify(sortedSpecs, null, 2);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
fillMissing(_dummyData) {
|
|
121
|
+
}
|
|
122
|
+
validate(callback) {
|
|
123
|
+
const result = Promise.resolve({ errors: null, warnings: null });
|
|
124
|
+
if (callback) {
|
|
125
|
+
result.then(result => callback(null, result)).catch(err => callback(err, null));
|
|
126
|
+
}
|
|
127
|
+
return result;
|
|
128
|
+
}
|
|
129
|
+
listSubResources() {
|
|
130
|
+
return {};
|
|
131
|
+
}
|
|
132
|
+
resolveSubResources() {
|
|
133
|
+
const sources = this.listSubResources();
|
|
134
|
+
return Promise.all(Object.values(sources).map((url) => this.readSpec(url))).then((resources) => {
|
|
135
|
+
const refs = Object.keys(sources);
|
|
136
|
+
this.subResources = Object.fromEntries(refs.map((key, index) => [key, resources[index]]));
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
parse(data) {
|
|
140
|
+
if (typeof data !== 'string') {
|
|
141
|
+
return Promise.resolve(data);
|
|
142
|
+
}
|
|
143
|
+
const parsePromises = Object.values(this.parsers).map((parser) => parser(data).catch((err) => {
|
|
144
|
+
return Promise.reject(err);
|
|
145
|
+
}));
|
|
146
|
+
return Promise.allSettled(parsePromises).then(results => {
|
|
147
|
+
const successful = results.find(result => result.status === 'fulfilled');
|
|
148
|
+
if (successful) {
|
|
149
|
+
return successful.value;
|
|
150
|
+
}
|
|
151
|
+
const errors = results
|
|
152
|
+
.filter(result => result.status === 'rejected')
|
|
153
|
+
.map(result => result.reason);
|
|
154
|
+
throw new Error('Failed to parse spec: ' + errors.map((e) => e.message).join(', '));
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
readSpec(source) {
|
|
158
|
+
const sourceType = Util.getSourceType(source);
|
|
159
|
+
if (!sourceType) {
|
|
160
|
+
throw new Error('Spec source should be object, string, or filename.');
|
|
161
|
+
}
|
|
162
|
+
return Util.resourceReaders[sourceType](source)
|
|
163
|
+
.then((data) => this.parse(data))
|
|
164
|
+
.then((spec) => [spec, sourceType]);
|
|
165
|
+
}
|
|
166
|
+
resolveResources(source) {
|
|
167
|
+
return this.readSpec(source)
|
|
168
|
+
.then(([spec, sourceType]) => {
|
|
169
|
+
if (!this.checkFormat(spec)) {
|
|
170
|
+
throw new Error(sourceType + ' ' + source + ' is not valid ' + this.format);
|
|
171
|
+
}
|
|
172
|
+
this.spec = spec;
|
|
173
|
+
this.sourceType = sourceType;
|
|
174
|
+
if (sourceType === 'file') {
|
|
175
|
+
this.source = source;
|
|
176
|
+
}
|
|
177
|
+
})
|
|
178
|
+
.then(() => this.resolveSubResources())
|
|
179
|
+
.then(() => {
|
|
180
|
+
this.fixSpec();
|
|
181
|
+
const version = this.getFormatVersion();
|
|
182
|
+
if (this.supportedVersions.indexOf(version) === -1) {
|
|
183
|
+
throw new Error('Unsupported version');
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
convertTo(format, passthrough, callback) {
|
|
188
|
+
if (format === this.format) {
|
|
189
|
+
const result = Promise.resolve(this);
|
|
190
|
+
if (callback) {
|
|
191
|
+
result.then(result => callback(null, result)).catch(err => callback(err, null));
|
|
192
|
+
}
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
const convert = this.converters[format];
|
|
196
|
+
if (!convert) {
|
|
197
|
+
const error = new Error(`Unable to convert from ${this.format} to ${format}`);
|
|
198
|
+
if (callback) {
|
|
199
|
+
callback(error, null);
|
|
200
|
+
return Promise.resolve(this);
|
|
201
|
+
}
|
|
202
|
+
return Promise.reject(error);
|
|
203
|
+
}
|
|
204
|
+
const result = convert(this, passthrough).then((spec) => {
|
|
205
|
+
const FormatClass = global.Formats?.[format];
|
|
206
|
+
if (!FormatClass) {
|
|
207
|
+
throw new Error(`Format ${format} not found in registry`);
|
|
208
|
+
}
|
|
209
|
+
const result = new FormatClass(spec);
|
|
210
|
+
result.fixSpec();
|
|
211
|
+
return result;
|
|
212
|
+
}, (err) => {
|
|
213
|
+
err.message = 'Error during conversion: ' + err.message;
|
|
214
|
+
throw err;
|
|
215
|
+
});
|
|
216
|
+
if (callback) {
|
|
217
|
+
result.then(result => callback(null, result)).catch(err => callback(err, null));
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
}
|
|
221
|
+
convertTransitive(intermediaries, passthrough) {
|
|
222
|
+
let prom = Promise.resolve(this);
|
|
223
|
+
intermediaries.forEach(intermediary => {
|
|
224
|
+
prom = prom.then((spec) => {
|
|
225
|
+
return spec.convertTo(intermediary, passthrough);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
return prom.then((spec) => spec.spec);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
exports.BaseFormat = BaseFormat;
|
|
232
|
+
class Util {
|
|
233
|
+
static joinPath(...args) {
|
|
234
|
+
return args.join('/').replace(/\/\/+/g, '/');
|
|
235
|
+
}
|
|
236
|
+
static parseJSON = (data) => {
|
|
237
|
+
try {
|
|
238
|
+
return Promise.resolve(JSON.parse(data));
|
|
239
|
+
}
|
|
240
|
+
catch (err) {
|
|
241
|
+
return Promise.reject(err);
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
static parseYAML = (data) => Promise.resolve(Yaml.load(data));
|
|
245
|
+
static resourceReaders = {
|
|
246
|
+
file: (filename) => {
|
|
247
|
+
return new Promise((resolve, reject) => {
|
|
248
|
+
fs.readFile(filename, 'utf8', (err, data) => {
|
|
249
|
+
if (err)
|
|
250
|
+
reject(err);
|
|
251
|
+
else
|
|
252
|
+
resolve(data);
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
},
|
|
256
|
+
object: (data) => Promise.resolve(data),
|
|
257
|
+
string: (data) => Promise.resolve(data),
|
|
258
|
+
};
|
|
259
|
+
static getSourceType(source) {
|
|
260
|
+
if (typeof source === 'object' && source !== null) {
|
|
261
|
+
return 'object';
|
|
262
|
+
}
|
|
263
|
+
if (typeof source !== 'string') {
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
try {
|
|
267
|
+
if (fs.existsSync(source))
|
|
268
|
+
return 'file';
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
}
|
|
272
|
+
if (source.startsWith('./') || source.startsWith('../') || !source.includes('://')) {
|
|
273
|
+
return 'file';
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
return 'string';
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
static removeNonValues(obj) {
|
|
280
|
+
if (obj === null || typeof obj !== 'object') {
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
if (Array.isArray(obj)) {
|
|
284
|
+
for (let i = obj.length - 1; i >= 0; i--) {
|
|
285
|
+
if (obj[i] === undefined) {
|
|
286
|
+
obj.splice(i, 1);
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
this.removeNonValues(obj[i]);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
const keys = Object.keys(obj);
|
|
295
|
+
for (const key of keys) {
|
|
296
|
+
if (obj[key] === undefined) {
|
|
297
|
+
delete obj[key];
|
|
298
|
+
}
|
|
299
|
+
else {
|
|
300
|
+
this.removeNonValues(obj[key]);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
exports.Util = Util;
|