Skip to content

Commit

Permalink
wip(oas2): preliminary support for server element
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Jun 18, 2019
1 parent 171067a commit 50dc494
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
30 changes: 27 additions & 3 deletions packages/fury-adapter-swagger/lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,31 @@ class Parser {

// Converts the Swagger hostname and schemes to a Refract host metadata entry.
handleSwaggerHost() {
const { Member: MemberElement } = this.namespace.elements;
const { Member, HrefVariables, Enum } = this.namespace.elements;
const Server = this.namespace.getElementClass('server');

const server = new Server();
server.element = 'server';
server.attributes.set('href', '{scheme}://{host}{basePath}');
const hrefVariables = new HrefVariables();
const scheme = new Member('scheme');
hrefVariables.push(scheme);
hrefVariables.push(new Member('host', this.swagger.host));
hrefVariables.push(new Member('basePath', this.basePath));
server.attributes.set('hrefVariables', hrefVariables);

if (this.swagger.schemes) {
if (this.swagger.schemes.length > 1) {
scheme.value = new Enum();
scheme.value.enumerations = this.swagger.schemes;
} else {
scheme.value = this.swagger.schemes[0];
}
}

this.api.push(server);

return

if (this.swagger.host) {
this.withPath('host', () => {
Expand Down Expand Up @@ -706,7 +730,7 @@ class Parser {
// by individual transition URI templates. When creating a transition
// below, we *only* set the transition URI template if it differs from
// the one we've generated here.
resource.href = uriTemplate(this.basePath, href, pathObjectParameters);
resource.href = uriTemplate(href, pathObjectParameters);

if (this.generateSourceMap) {
this.createSourceMap(resource.attributes.get('href'), this.path);
Expand Down Expand Up @@ -770,7 +794,7 @@ class Parser {
// Here we generate a URI template specific to this transition. If it
// is different from the resource URI template, then we set the
// transition's `href` attribute.
const hrefForTransition = uriTemplate(this.basePath, href, resourceParams, queryParams);
const hrefForTransition = uriTemplate(href, resourceParams, queryParams);

if (hrefForTransition !== resource.href.toValue()) {
transition.href = hrefForTransition;
Expand Down
6 changes: 3 additions & 3 deletions packages/fury-adapter-swagger/lib/uri-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const _ = require('lodash');
const escapeUriTemplateVariable = variable => encodeURIComponent(variable)
.replace(/[-.!~*'()]/g, c => `%${c.charCodeAt(0).toString(16)}`);

module.exports = (basePath, href, pathObjectParams = [], queryParams = []) => {
module.exports = (href, pathObjectParams = [], queryParams = []) => {
const parameterNames = _.chain(pathObjectParams)
.concat(queryParams)
.filter(parameter => parameter.in === 'query')
Expand All @@ -21,8 +21,8 @@ module.exports = (basePath, href, pathObjectParams = [], queryParams = []) => {

if (parameterNames.length > 0) {
const queryString = parameterNames.join(',');
return `${basePath}${href}{?${queryString}}`;
return `${href}{?${queryString}}`;
}

return basePath + href;
return href;
};

0 comments on commit 50dc494

Please sign in to comment.