@killb/openapi-alchemist 0.0.0-stage → 2.0.0
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 +87 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Kill-B
|
|
4
|
+
|
|
5
|
+
Portions of this software are derived from api-spec-converter
|
|
6
|
+
(https://github.com/LucyBot-Inc/api-spec-converter),
|
|
7
|
+
Copyright (c) the api-spec-converter authors (LucyBot), also under the MIT License.
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,250 @@
|
|
|
1
|
-
#
|
|
1
|
+
# OpenAPI Alchemist 🧪
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/Kill-B/openapi-alchemist/actions/workflows/ci.yml)
|
|
4
|
+
[](https://www.npmjs.com/package/@killb/openapi-alchemist)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
> ⚠️ **Disclaimer**: This is a fork of [LucyBot-Inc/api-spec-converter](https://github.com/LucyBot-Inc/api-spec-converter), converted to TypeScript and focused on OpenAPI 3 to Swagger 2 conversion. For the original full-featured converter supporting multiple formats, please visit the [original repository](https://github.com/LucyBot-Inc/api-spec-converter).
|
|
8
|
+
|
|
9
|
+
**Transform OpenAPI 3 to Swagger 2 with alchemical precision** ✨
|
|
10
|
+
|
|
11
|
+
A TypeScript-converted minimal and focused API specification transformer that converts OpenAPI 3 to Swagger 2 with surgical accuracy.
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
This package provides two main functionalities:
|
|
16
|
+
|
|
17
|
+
1. **OpenAPI 3 → Swagger 2 transformation** ✨
|
|
18
|
+
2. **OpenAPI 3 JSON ↔ YAML format conversion** (semantic preservation, format-only conversion)
|
|
19
|
+
|
|
20
|
+
This version has been converted to TypeScript for Node.js v22+ with modern Promise-based APIs and improved type safety.
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @killb/openapi-alchemist
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
This package used to be published as `openapi-alchemist`. That name is deprecated and gets no new versions; see [Migrating from 1.x](#migrating-from-1x).
|
|
29
|
+
|
|
30
|
+
## Usage
|
|
31
|
+
|
|
32
|
+
### OpenAPI 3 to Swagger 2 Conversion
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
const openapiAlchemist = require('@killb/openapi-alchemist');
|
|
36
|
+
|
|
37
|
+
// Transform OpenAPI 3 specification to Swagger 2
|
|
38
|
+
openapiAlchemist.convert(
|
|
39
|
+
{
|
|
40
|
+
syntax: 'yaml',
|
|
41
|
+
order: 'openapi',
|
|
42
|
+
from: 'openapi_3',
|
|
43
|
+
to: 'swagger_2',
|
|
44
|
+
source: './path/to/your/openapi3.yaml', // or object
|
|
45
|
+
},
|
|
46
|
+
function (err, converted) {
|
|
47
|
+
if (err) {
|
|
48
|
+
console.error('Error converting:', err);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Output as YAML
|
|
53
|
+
const yamlString = converted.stringify({syntax: 'yaml'});
|
|
54
|
+
console.log(yamlString);
|
|
55
|
+
|
|
56
|
+
// Output as JSON
|
|
57
|
+
const jsonString = converted.stringify({syntax: 'json'});
|
|
58
|
+
console.log(jsonString);
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### OpenAPI 3 JSON to YAML Conversion
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
const openapiAlchemist = require('@killb/openapi-alchemist');
|
|
67
|
+
|
|
68
|
+
// Transform OpenAPI 3 JSON to YAML (format conversion only)
|
|
69
|
+
openapiAlchemist.convert(
|
|
70
|
+
{
|
|
71
|
+
syntax: 'yaml',
|
|
72
|
+
order: 'openapi',
|
|
73
|
+
from: 'openapi_3',
|
|
74
|
+
to: 'openapi_3', // Same format, different syntax
|
|
75
|
+
source: './path/to/your/openapi3.json',
|
|
76
|
+
},
|
|
77
|
+
function (err, converted) {
|
|
78
|
+
if (err) {
|
|
79
|
+
console.error('Error converting:', err);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Output as YAML
|
|
84
|
+
const yamlString = converted.stringify({syntax: 'yaml'});
|
|
85
|
+
console.log(yamlString);
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Using with Objects
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
const openapiAlchemist = require('@killb/openapi-alchemist');
|
|
94
|
+
|
|
95
|
+
const openApiSpec = {
|
|
96
|
+
openapi: '3.0.0',
|
|
97
|
+
info: {
|
|
98
|
+
title: 'My API',
|
|
99
|
+
version: '1.0.0'
|
|
100
|
+
},
|
|
101
|
+
paths: {
|
|
102
|
+
'/users': {
|
|
103
|
+
get: {
|
|
104
|
+
responses: {
|
|
105
|
+
'200': {
|
|
106
|
+
description: 'Success',
|
|
107
|
+
content: {
|
|
108
|
+
'application/json': {
|
|
109
|
+
schema: {
|
|
110
|
+
type: 'object',
|
|
111
|
+
properties: {
|
|
112
|
+
users: {
|
|
113
|
+
type: 'array',
|
|
114
|
+
items: {
|
|
115
|
+
type: 'string'
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
openapiAlchemist.convert(
|
|
130
|
+
{
|
|
131
|
+
from: 'openapi_3',
|
|
132
|
+
to: 'swagger_2',
|
|
133
|
+
source: openApiSpec, // Direct object
|
|
134
|
+
},
|
|
135
|
+
function (err, converted) {
|
|
136
|
+
if (err) {
|
|
137
|
+
console.error('Error:', err);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
console.log('Converted successfully!');
|
|
142
|
+
console.log('Swagger version:', converted.spec.swagger);
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## API Reference
|
|
148
|
+
|
|
149
|
+
### `convert(options, callback)`
|
|
150
|
+
|
|
151
|
+
Transforms API specifications between supported formats.
|
|
152
|
+
|
|
153
|
+
#### Parameters
|
|
154
|
+
|
|
155
|
+
- `options` (Object):
|
|
156
|
+
- `from` (String): Source format (`'openapi_3'`)
|
|
157
|
+
- `to` (String): Target format (`'swagger_2'` or `'openapi_3'`)
|
|
158
|
+
- `source` (String|Object): Source specification: a file path, the spec's text, or an object (see [Sources](#sources))
|
|
159
|
+
- `refRoot` (String, optional): Absolute directory that external `$ref` files must stay inside (see [External references](#external-references))
|
|
160
|
+
- `syntax` (String, optional): Output syntax (`'json'` or `'yaml'`, default: `'json'`)
|
|
161
|
+
- `order` (String, optional): Output ordering (`'openapi'`, `'alpha'`, or `'false'`, default: `'openapi'`)
|
|
162
|
+
|
|
163
|
+
- `callback` (Function): Callback function with signature `(err, result)`
|
|
164
|
+
|
|
165
|
+
#### Return Value
|
|
166
|
+
|
|
167
|
+
The callback receives a `result` object with:
|
|
168
|
+
- `spec`: The converted specification object
|
|
169
|
+
- `stringify(options)`: Method to convert the spec to string format
|
|
170
|
+
|
|
171
|
+
### `getSpec(source, format, callback?, options?)`
|
|
172
|
+
|
|
173
|
+
Reads and validates a spec without converting it. `options.refRoot` works as in `convert`. Call `convertTo(format)` on the result to convert it.
|
|
174
|
+
|
|
175
|
+
## Sources
|
|
176
|
+
|
|
177
|
+
- **Object**: used as is.
|
|
178
|
+
- **String**: read as a **file path** when it has no `://`, starts with `./` or `../`, or names a file that exists. Any other string is parsed as the spec's JSON or YAML text. A spec's text usually contains `://` (in `servers`), but one that doesn't is read as a path.
|
|
179
|
+
|
|
180
|
+
Do not pass untrusted input as a string: a string can make the converter read a local file. Parse untrusted specs yourself and pass the object.
|
|
181
|
+
|
|
182
|
+
## External references
|
|
183
|
+
|
|
184
|
+
A `$ref` to another file (`./schemas/pet.yaml#/Pet`) resolves only to files inside the **reference root**, after resolving `..` and symbolic links. A reference outside the root fails the conversion with an error that names the reference.
|
|
185
|
+
|
|
186
|
+
- By default the root is the directory of the source file.
|
|
187
|
+
- `refRoot` sets another root: an absolute path to an existing directory. Anything else fails the conversion before any file is read.
|
|
188
|
+
- With an object or string source, relative references resolve against `refRoot`. Without `refRoot`, external references in an object or string source are not supported.
|
|
189
|
+
- Absolute references are allowed when they point inside the root.
|
|
190
|
+
- Remote (`http`) references are not supported.
|
|
191
|
+
|
|
192
|
+
A `$ref` the converter resolves (path items, operations, parameters, request bodies, responses, headers) must point to an existing key, or the conversion fails with an error that names it. References only walk the spec's own keys, never `__proto__`, `constructor` or `prototype`. An unresolvable `discriminator.mapping` entry is skipped with a warning.
|
|
193
|
+
|
|
194
|
+
## Migrating from 1.x
|
|
195
|
+
|
|
196
|
+
`@killb/openapi-alchemist` 2.0.0 is the first version under the new name. It has two breaking changes:
|
|
197
|
+
|
|
198
|
+
1. An external `$ref` that points outside the source file's directory now fails. Pass `refRoot` with a directory that contains it.
|
|
199
|
+
2. A resolved `$ref` that points to a missing key now fails with a clear error. 1.x left it dangling or threw a `TypeError`.
|
|
200
|
+
|
|
201
|
+
It also fixes server URLs: a URL that still holds a `{variable}` with no default, or that is not a valid absolute URL, now converts with `host` and `schemes` left out and `basePath` taken from its path. Server variable names are replaced literally.
|
|
202
|
+
|
|
203
|
+
## Supported Node.js Versions
|
|
204
|
+
|
|
205
|
+
- Node.js >= 22.0.0 (TypeScript version with modern Promise APIs)
|
|
206
|
+
|
|
207
|
+
## What's Not Supported
|
|
208
|
+
|
|
209
|
+
This minimal version does **not** support:
|
|
210
|
+
- RAML specifications
|
|
211
|
+
- API Blueprint specifications
|
|
212
|
+
- WADL specifications
|
|
213
|
+
- Google Discovery documents
|
|
214
|
+
- I/O Docs specifications
|
|
215
|
+
- Swagger 1.x specifications
|
|
216
|
+
- URL-based sources (only file paths, spec text and objects)
|
|
217
|
+
- Remote `$ref` URLs
|
|
218
|
+
- Swagger 2 → OpenAPI 3 conversion
|
|
219
|
+
|
|
220
|
+
## Development
|
|
221
|
+
|
|
222
|
+
This package is built with TypeScript and compiled to CommonJS for Node.js compatibility. The source code is in the `src/` directory and compiled output is in `dist/`.
|
|
223
|
+
|
|
224
|
+
### Building
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
npm run build
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Testing
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
npm test
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
## Contributing
|
|
237
|
+
|
|
238
|
+
1. Fork the repository
|
|
239
|
+
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
|
|
240
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
241
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
242
|
+
5. Open a Pull Request
|
|
243
|
+
|
|
244
|
+
## Releases
|
|
245
|
+
|
|
246
|
+
Versions are published by the `release` workflow on `main`, after CI passes, through npm trusted publishing with a provenance attestation. Commit messages follow [Conventional Commits](https://www.conventionalcommits.org/); semantic-release picks the version from them. Nobody publishes from a laptop or with an npm token.
|
|
247
|
+
|
|
248
|
+
## License
|
|
249
|
+
|
|
250
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ConvertOptions, ConvertResult, BaseFormat } from './types';
|
|
2
|
+
interface GetSpecOptions {
|
|
3
|
+
refRoot?: string;
|
|
4
|
+
}
|
|
5
|
+
declare const Converter: {
|
|
6
|
+
Formats: {
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
};
|
|
9
|
+
BaseFormat: typeof BaseFormat;
|
|
10
|
+
ResourceReaders: import("./types").ResourceReaders;
|
|
11
|
+
getSpec(source: string | object, format: string, callback?: (err: any, result: BaseFormat) => void, options?: GetSpecOptions): Promise<BaseFormat>;
|
|
12
|
+
getFormatName(name: string, version: string): string | undefined;
|
|
13
|
+
convert(options: ConvertOptions, callback?: (err: any, result: ConvertResult) => void): Promise<ConvertResult>;
|
|
14
|
+
};
|
|
15
|
+
export = Converter;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const types_1 = require("./types");
|
|
3
|
+
const openapi_3_1 = require("./lib/formats/openapi_3");
|
|
4
|
+
const refs_1 = require("./lib/refs");
|
|
5
|
+
const swagger_2_1 = require("./lib/formats/swagger_2");
|
|
6
|
+
const Formats = {};
|
|
7
|
+
Formats['swagger_2'] = swagger_2_1.Swagger2;
|
|
8
|
+
Formats['openapi_3'] = openapi_3_1.OpenApi3;
|
|
9
|
+
global.Formats = Formats;
|
|
10
|
+
const Converter = {
|
|
11
|
+
Formats,
|
|
12
|
+
BaseFormat: types_1.BaseFormat,
|
|
13
|
+
ResourceReaders: types_1.Util.resourceReaders,
|
|
14
|
+
getSpec(source, format, callback, options) {
|
|
15
|
+
if (!Object.prototype.hasOwnProperty.call(Formats, format)) {
|
|
16
|
+
throw new Error('Unknown format ' + format + ', you might have forgotten installing an optional dependency');
|
|
17
|
+
}
|
|
18
|
+
const FormatClass = Formats[format];
|
|
19
|
+
const spec = new FormatClass();
|
|
20
|
+
let result;
|
|
21
|
+
try {
|
|
22
|
+
if (options?.refRoot !== undefined) {
|
|
23
|
+
spec.refRoot = (0, refs_1.validateRefRoot)(options.refRoot);
|
|
24
|
+
}
|
|
25
|
+
result = spec.resolveResources(source).then(() => spec);
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
result = Promise.reject(err);
|
|
29
|
+
}
|
|
30
|
+
if (callback) {
|
|
31
|
+
result.then(result => callback(null, result)).catch(err => callback(err, null));
|
|
32
|
+
}
|
|
33
|
+
return result;
|
|
34
|
+
},
|
|
35
|
+
getFormatName(name, version) {
|
|
36
|
+
let result;
|
|
37
|
+
Object.entries(Formats).forEach(([formatName, format]) => {
|
|
38
|
+
const formatPrototype = format.prototype;
|
|
39
|
+
if (formatPrototype.formatName === name &&
|
|
40
|
+
formatPrototype.supportedVersions.indexOf(version) !== -1) {
|
|
41
|
+
result = formatName;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return result;
|
|
45
|
+
},
|
|
46
|
+
convert(options, callback) {
|
|
47
|
+
const result = Converter.getSpec(options.source, options.from, undefined, {
|
|
48
|
+
refRoot: options.refRoot,
|
|
49
|
+
}).then((fromSpec) => fromSpec.convertTo(options.to, options.passthrough));
|
|
50
|
+
if (callback) {
|
|
51
|
+
result.then(result => callback(null, result)).catch(err => callback(err, null));
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
module.exports = Converter;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BaseFormat } from '../../types';
|
|
2
|
+
export declare class OpenApi3ToSwagger2Converter {
|
|
3
|
+
private readonly spec;
|
|
4
|
+
private readonly directory?;
|
|
5
|
+
private readonly root?;
|
|
6
|
+
constructor(data: BaseFormat);
|
|
7
|
+
convert(): any;
|
|
8
|
+
resolveReference(base: any, obj: any, shouldClone: boolean): any;
|
|
9
|
+
private fixRef;
|
|
10
|
+
private fixRefs;
|
|
11
|
+
private convertInfos;
|
|
12
|
+
private convertOperations;
|
|
13
|
+
private convertOperationParameters;
|
|
14
|
+
private convertParameters;
|
|
15
|
+
private copySchemaProperties;
|
|
16
|
+
private copySchemaXProperties;
|
|
17
|
+
private convertResponses;
|
|
18
|
+
private convertSchema;
|
|
19
|
+
private convertSchemas;
|
|
20
|
+
private convertDiscriminatorMapping;
|
|
21
|
+
private convertSecurityDefinitions;
|
|
22
|
+
private isJsonMimeType;
|
|
23
|
+
private getSupportedMimeTypes;
|
|
24
|
+
}
|