-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(openapi): specify spec path and type in success response (#480)
* feat(openapi): specify spec path in success response * docs: corresponding docs update * refactor: separate OAS prep into its own function This way we can reuse it in our `validate` and `openapi` commands and allow for a shared way to specify whether the file is OpenAPI or Swagger. This also ended up fixing some inconsistencies with our debugging and error handling * fix: remove old `oas` guidance * feat: specify the spec type in another place * docs: update docs accordingly * fix(prepareOas): error handling + debugging
- Loading branch information
1 parent
7ffc1f5
commit 3a32cc2
Showing
6 changed files
with
109 additions
and
82 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const OASNormalize = require('oas-normalize'); | ||
const { debug } = require('./logger'); | ||
|
||
/** | ||
* Normalizes, validates, and (optionally) bundles an OpenAPI definition. | ||
* | ||
* @param {String} path path to spec file | ||
* @param {Boolean} bundle boolean to indicate whether or not to | ||
* return a bundled spec (defaults to false) | ||
*/ | ||
module.exports = async function prepare(path, bundle = false) { | ||
debug(`about to normalize spec located at ${path}`); | ||
const oas = new OASNormalize(path, { colorizeErrors: true, enablePaths: true }); | ||
debug('spec normalized'); | ||
|
||
const api = await oas.validate(false).catch(err => { | ||
debug(`raw validation error object: ${JSON.stringify(err)}`); | ||
throw err; | ||
}); | ||
debug('👇👇👇👇👇 spec validated! logging spec below 👇👇👇👇👇'); | ||
debug(api); | ||
debug('👆👆👆👆👆 finished logging spec 👆👆👆👆👆'); | ||
|
||
const specType = api.swagger ? 'Swagger' : 'OpenAPI'; | ||
debug(`spec type: ${specType}`); | ||
|
||
let bundledSpec = ''; | ||
|
||
if (bundle) { | ||
bundledSpec = await oas | ||
.bundle() | ||
.then(res => { | ||
return JSON.stringify(res); | ||
}) | ||
.catch(err => { | ||
debug(`raw bundling error object: ${JSON.stringify(err)}`); | ||
throw err; | ||
}); | ||
debug('spec bundled'); | ||
} | ||
|
||
return { bundledSpec, specType }; | ||
}; |