-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Ed Vinyard <[email protected]>
- Loading branch information
1 parent
752bc8b
commit ed41dca
Showing
17 changed files
with
434 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,2 @@ | ||
import { transformOas3Operations } from '@stoplight/http-spec/oas3/operation'; | ||
import { transformOas2Operations } from '@stoplight/http-spec/oas2/operation'; | ||
import { transformPostmanCollectionOperations } from '@stoplight/http-spec/postman/operation'; | ||
import * as $RefParser from '@stoplight/json-schema-ref-parser'; | ||
import { HTTPResolverOptions } from '@stoplight/json-schema-ref-parser'; | ||
import { bundleTarget, decycle } from '@stoplight/json'; | ||
import { IHttpOperation } from '@stoplight/types'; | ||
import { get } from 'lodash'; | ||
import * as os from 'os'; | ||
import type { Spec } from 'swagger-schema-official'; | ||
import type { OpenAPIObject } from 'openapi3-ts'; | ||
import type { CollectionDefinition } from 'postman-collection'; | ||
|
||
export async function getHttpOperationsFromSpec(specFilePathOrObject: string | object): Promise<IHttpOperation[]> { | ||
const prismVersion = require('../package.json').version; | ||
const httpResolverOpts: HTTPResolverOptions = { | ||
headers: { | ||
'User-Agent': `PrismMockServer/${prismVersion} (${os.type()} ${os.arch()} ${os.release()})`, | ||
}, | ||
}; | ||
const result = decycle( | ||
await new $RefParser().dereference(specFilePathOrObject, { resolve: { http: httpResolverOpts } }) | ||
); | ||
|
||
let operations: IHttpOperation[] = []; | ||
if (isOpenAPI2(result)) operations = transformOas2Operations(result); | ||
else if (isOpenAPI3(result)) operations = transformOas3Operations(result); | ||
else if (isPostmanCollection(result)) operations = transformPostmanCollectionOperations(result); | ||
else throw new Error('Unsupported document format'); | ||
|
||
operations.forEach((op, i, ops) => { | ||
ops[i] = bundleTarget({ | ||
document: { | ||
...result, | ||
__target__: op, | ||
}, | ||
path: '#/__target__', | ||
cloneDocument: false, | ||
}); | ||
}); | ||
|
||
return operations; | ||
} | ||
|
||
function isOpenAPI2(document: unknown): document is Spec { | ||
return get(document, 'swagger') !== undefined; | ||
} | ||
|
||
function isOpenAPI3(document: unknown): document is OpenAPIObject { | ||
return get(document, 'openapi') !== undefined; | ||
} | ||
|
||
function isPostmanCollection(document: unknown): document is CollectionDefinition { | ||
return Array.isArray(get(document, 'item')) && get(document, 'info.name') !== undefined; | ||
} | ||
// add to keep this from being a breaking change. | ||
export { getHttpOperationsFromSpec } from '@stoplight/prism-http'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.