Skip to content

Commit

Permalink
Built out security types, and reference potential for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
gratcliff committed Jan 24, 2020
1 parent 4a927d5 commit 96cad79
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/lib/get-reference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function findReference(reference, type, oas) {
const explodedRef = reference.split('/');
const key = explodedRef[explodedRef.length - 1];
return oas.components[type][key];
};
29 changes: 22 additions & 7 deletions src/oas.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const { pathToRegexp, match } = require('path-to-regexp');
const getPathOperation = require('./lib/get-path-operation');
const getUserVariable = require('./lib/get-user-variable');
const getReference = require('./lib/get-reference');

class Operation {
constructor(oas, path, method, operation) {
Expand Down Expand Up @@ -42,10 +43,9 @@ class Operation {
if (security.scheme === 'bearer') type = 'Bearer';
} else if (security.type === 'oauth2') {
type = 'OAuth2';
} else if (security.type === 'apiKey' && security.in === 'query') {
type = 'Query';
} else if (security.type === 'apiKey' && security.in === 'header') {
type = 'Header';
} else if (security.type === 'apiKey') {
if (security.in === 'query') type = 'Query';
else if (security.in === 'header' || security.in === 'cookie') type = 'Header';
} else {
return false;
}
Expand Down Expand Up @@ -74,13 +74,28 @@ class Operation {
};

const security = this.prepareSecurity();
if (security.Header && security.Header.length) {
this.headers.request = security.Header.map(h => h.name);
if (security.Header) {
this.headers.request = security.Header.map(h => {
if (h.in === 'cookie') return 'Cookie';
return h.name;
});
}
if (security.Bearer || security.Basic) {
this.headers.request.push('Authorization');
}

if (this.parameters) {
this.headers.request = this.headers.request.concat(
this.parameters.filter(p => p.in === 'header').map(p => p.name)
this.parameters
.map(p => {
if (p.in && p.in === 'header') return p.name;
if (p.$ref) {
const { name } = getReference(p.$ref, 'parameters', this.oas);
return name;
}
return undefined;
})
.filter(p => p)
);
}

Expand Down

0 comments on commit 96cad79

Please sign in to comment.