UNPKG

991 BJavaScriptView Raw
1const { paths } = require('@netlify/open-api')
2
3const { omit } = require('./utils/omit')
4
5// Retrieve all OpenAPI operations
6const getOperations = function() {
7 return [].concat(
8 ...Object.entries(paths).map(([path, pathItem]) => {
9 const operations = omit(pathItem, ['parameters'])
10 return Object.entries(operations).map(([method, operation]) => {
11 const parameters = getParameters(pathItem.parameters, operation.parameters)
12 return Object.assign({}, operation, { verb: method, path, parameters })
13 })
14 })
15 )
16}
17
18const getParameters = function(pathParameters = [], operationParameters = []) {
19 const parameters = [...pathParameters, ...operationParameters]
20 return parameters.reduce(addParameter, { path: {}, query: {}, body: {} })
21}
22
23const addParameter = function(parameters, param) {
24 return Object.assign({}, parameters, {
25 [param.in]: Object.assign({}, parameters[param.in], { [param.name]: param })
26 })
27}
28
29module.exports = { getOperations }