forked from postmanlabs/openapi-to-postman
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
206 lines (182 loc) · 6.46 KB
/
index.ts
File metadata and controls
206 lines (182 loc) · 6.46 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
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
'use strict';
import _ from 'lodash';
import type {
SpecificationInput,
Options,
Callback,
SyncOptions,
OptionsCriteria,
OptionDefinition,
OptionsRecord,
BundleResult,
FilesResult,
ValidationResult
} from './types';
export type {
Options,
SyncOptions,
Callback,
CollectionResult,
BundleResult,
FilesResult,
ValidationResult
} from './types';
const { MODULE_VERSION } = require('../lib/schemapack.js');
const SchemaPack = require('../lib/schemapack.js').SchemaPack;
const UserError = require('../lib/common/UserError');
const DEFAULT_INVALID_ERROR = 'Provided definition is invalid';
/**
* Converts an OpenAPI specification to a Postman Collection (v1 API)
* @param {SpecificationInput} input - The OpenAPI specification input
* @param {Options} options - Conversion options
* @param {Callback} cb - Callback function with conversion result
* @returns {void}
*/
export function convert(input: SpecificationInput, options: Options, cb: Callback): void {
var schema = new SchemaPack(input, options);
if (schema.validated) {
return schema.convert(cb);
}
return cb(new UserError(_.get(schema, 'validationResult.reason', DEFAULT_INVALID_ERROR)));
}
/**
* Converts an OpenAPI specification to a Postman Collection (v2 API)
* @param {SpecificationInput} input - The OpenAPI specification input
* @param {Options} options - Conversion options
* @param {Callback} cb - Callback function with conversion result
* @returns {void}
*/
export function convertV2(input: SpecificationInput, options: Options, cb: Callback): void {
var schema = new SchemaPack(input, options, MODULE_VERSION.V2);
if (schema.validated) {
return schema.convertV2(cb);
}
return cb(new UserError(_.get(schema, 'validationResult.reason', DEFAULT_INVALID_ERROR)));
}
/**
* Converts an OpenAPI specification to a Postman Collection (v2 API) with type fetching enabled
* @param {SpecificationInput} input - The OpenAPI specification input
* @param {Options} options - Conversion options
* @param {Callback} cb - Callback function with conversion result
* @returns {void}
*/
export function convertV2WithTypes(input: SpecificationInput, options: Options, cb: Callback): void {
const enableTypeFetching = true;
var schema = new SchemaPack(input, options, MODULE_VERSION.V2, enableTypeFetching);
if (schema.validated) {
return schema.convertV2(cb);
}
return cb(new UserError(_.get(schema, 'validationResult.reason', DEFAULT_INVALID_ERROR)));
}
/**
* Validates an OpenAPI specification
* @param {SpecificationInput} input - The OpenAPI specification input
* @returns {ValidationResult} Validation result
*/
export function validate(input: SpecificationInput): ValidationResult {
var schema = new SchemaPack(input);
return schema.validationResult;
}
/**
* Retrieves metadata from an OpenAPI specification
* @param {SpecificationInput} input - The OpenAPI specification input
* @param {Callback} cb - Callback function with metadata result
* @returns {void}
*/
export function getMetaData(input: SpecificationInput, cb: Callback): void {
var schema = new SchemaPack(input);
schema.getMetaData(cb);
}
/**
* Merges and validates an OpenAPI specification
* @param {SpecificationInput} input - The OpenAPI specification input
* @param {Callback} cb - Callback function with merge and validation result
* @returns {void}
*/
export function mergeAndValidate(input: SpecificationInput, cb: Callback): void {
var schema = new SchemaPack(input);
schema.mergeAndValidate(cb);
}
/**
* Gets conversion options
* @param {string} mode - Mode for options ('document' or 'use')
* @param {OptionsCriteria} criteria - Criteria for filtering options
* @returns {OptionDefinition[] | OptionsRecord} Option definitions or option values
*/
export function getOptions(mode?: string, criteria?: OptionsCriteria): OptionDefinition[] | OptionsRecord {
return SchemaPack.getOptions(mode, criteria);
}
/**
* Gets sync options
* @param {string} mode - Mode for options ('document' or 'use')
* @returns {OptionDefinition[] | OptionsRecord} Option definitions or option values
*/
export function getSyncOptions(mode?: string): OptionDefinition[] | OptionsRecord {
return SchemaPack.getSyncOptions(mode);
}
/**
* Detects root files in a multi-file OpenAPI specification
* @param {SpecificationInput} input - The OpenAPI specification input
* @returns {Promise<FilesResult>} Promise with detection result
*/
export async function detectRootFiles(input: SpecificationInput): Promise<FilesResult> {
var schema = new SchemaPack(input);
return schema.detectRootFiles();
}
/**
* Detects related files in a multi-file OpenAPI specification
* @param {SpecificationInput} input - The OpenAPI specification input
* @returns {Promise<FilesResult>} Promise with detection result
*/
export async function detectRelatedFiles(input: SpecificationInput): Promise<FilesResult> {
var schema = new SchemaPack(input);
return schema.detectRelatedFiles();
}
/**
* Bundles a multi-file OpenAPI specification into a single file
* @param {SpecificationInput} input - The OpenAPI specification input with optional bundling options
* @returns {Promise<BundleResult>} Promise with bundled specification
*/
export async function bundle(input: SpecificationInput & { options?: Options }): Promise<BundleResult> {
var schema = new SchemaPack(input, input.options ?? {});
return schema.bundle();
}
/**
* Syncs a Postman Collection with changes from an OpenAPI specification
* @param {SpecificationInput} input - The OpenAPI specification input
* @param {Options} options - Conversion options
* @param {object} currentCollection - The current Postman Collection to sync
* @param {SyncOptions | null} syncOptions - Sync options
* @param {Callback} cb - Callback function with sync result
* @returns {void}
*/
export function syncCollection(
input: SpecificationInput,
options: Options,
currentCollection: object,
syncOptions: SyncOptions | null,
cb: Callback
): void {
const enableTypeFetching = true;
var schema = new SchemaPack(input, options, MODULE_VERSION.V2, enableTypeFetching);
if (schema.validated) {
return schema.syncCollection(currentCollection, syncOptions, cb);
}
return cb(new UserError(_.get(schema, 'validationResult.reason', DEFAULT_INVALID_ERROR)));
}
export { SchemaPack };
export default {
convert,
convertV2,
convertV2WithTypes,
validate,
getMetaData,
mergeAndValidate,
getOptions,
getSyncOptions,
detectRootFiles,
detectRelatedFiles,
bundle,
syncCollection,
SchemaPack
};