insomnia/plugins/insomnia-plugin-kong-kubernetes-config/src/generate.js
Jack Kavanagh d4df5ea5f7
Extend config generator plugin API and modal to handle documentation links (#4204)
* first pass

* capitalise Swagger and OpenAPI in an error message

* use docs from plugin scope

* extract variables

* check only OpenAPI spec version

* feedback

* fix config mapping

* refactor _generate

* remove bool casts

* revert refactor

* consume docs link

Co-authored-by: Opender Singh <opender.singh@konghq.com>
2021-11-18 10:54:19 +01:00

34 lines
944 B
JavaScript

const YAML = require('yaml');
const o2k = require('openapi-2-kong');
module.exports = {
label: 'Kong for Kubernetes',
docsLink: 'https://docs.insomnia.rest/insomnia/kong-for-kubernetes',
generate: async ({ contents, formatVersion }) => {
const isSupported = formatVersion && formatVersion.match(/^3./);
if (!isSupported) {
return {
document: null,
error: `Unsupported OpenAPI spec format ${formatVersion}`,
};
}
try {
const result = await o2k.generateFromString(contents, 'kong-for-kubernetes');
const yamlDocs = result.documents.map(d => YAML.stringify(d));
return {
// Join the YAML docs with "---" and strip any extra newlines surrounding them
document: yamlDocs.join('\n---\n').replace(/\n+---\n+/g, '\n---\n'),
error: null,
};
} catch (err) {
return {
document: null,
error: err.message,
};
}
},
};