Skip to content
This repository was archived by the owner on Jun 28, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
.idea
29 changes: 5 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
var Request = require('request');
var Converter = require('./lib/conformance-to-swagger.js');
var FS = require('fs');

module.exports = function(options, callback) {
callback = callback || function(err) {if (err) throw err};
var headers = {};
var auth = options.authorization;
if (auth) {
var authString = auth.username + ':' + auth.password;
authString = new Buffer(authString).toString('base64');
headers.Authorization = 'Basic ' + authString;
}

Request({
rejectUnauthorized: options.reject_unauthorized,
url: options.fhir_url + options.conformance_path,
headers: headers,
json: true,
}, function(err, resp, body) {
if (err) return callback(err);
var swagger = Converter.convert(options.fhir_url, body);
if (auth) {
swagger.securityDefinitions = swagger.securityDefinitions || {};
swagger.securityDefinitions.Basic = {type: 'basic'};
}
callback(null, swagger);
})
var body = FS.readFileSync(options.fhir_cs_path, "utf-8");
var json = JSON.parse(body);
var swagger = Converter.convert(options.fhir_url, json);
return callback(null, swagger);
}
91 changes: 60 additions & 31 deletions lib/conformance-to-swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,67 +23,91 @@ var getDefaultOp = function(res) {
}

Converter.convert = function(baseURL, conf) {
var swagger = {swagger: '2.0'};
swagger.definitions = {};
var swagger = {openapi: '3.0.0'};
var definitions = {};
swagger.paths = {};
swagger.info = {description: DESCRIPTION, title: conf.id || 'Untitled', version: 'unspecified'};
var url = URL.parse(baseURL);
swagger.host = url.host;
swagger.schemes = [url.protocol.substring(0, url.protocol.length - 1)];
swagger.basePath = url.pathname;
swagger.servers = [
{
url: url.href,
description: "staging"
}
];
swagger.security = [{
bearerAuth: []
}]
swagger.components = {
schemas: definitions,
securitySchemes: {
bearerAuth: {
type: "http",
scheme: "bearer",
bearerFormat: "JWT"
}
}
}

var resources = conf.rest[0].resource;
swagger.tags = resources.map(function(res) {
return {name: res.type};
})

resources.forEach(function(res) {
if (!res.searchParam) return;
var schema = swagger.definitions[res.type] = getSchema(res);
var schemaRef = '#/definitions/' + res.type;
var schema = definitions[res.type] = getSchema(res);
var schemaRef = '#/components/schemas/' + res.type;
var typeOps = swagger.paths['/' + res.type] = {};
var instOps = swagger.paths['/' + res.type + '/{id}'] = {parameters: [{in: 'path', required: true, name: 'id', type: 'string'}]}
var instOps = swagger.paths['/' + res.type + '/{id}'] = {parameters: [{in: 'path', required: true, name: 'id', schema: {type: 'string'}}]}
res.interaction = res.interaction || DEFAULT_INTERACTIONS;
var interactions = res.interaction.map(s => s.code);
if (interactions.indexOf('create') !== -1) {
typeOps.post = getDefaultOp(res);
typeOps.post.parameters.push({
name: 'body',
in: 'body',
schema: {$ref: schemaRef},
})
typeOps.post.requestBody = {
content: {
"application/json": {
schema: {
$ref: schemaRef
},
}
},
required: true
}
}
if (interactions.indexOf('search-type') !== -1) {
typeOps.get = getDefaultOp(res);
typeOps.get.parameters = res.searchParam.map(function(param) {
var swagParam = {
name: param.name,
type: convertType(param.type),
schema: {type: convertType(param.type)},
in: 'query',
description: param.documentation
}
var format = getFormat(param.type);
if (format) swagParam.format = format;
if (format) swagParam.schema.format = format;
return swagParam;
});
var formatParam = {
name: '_format',
in: 'query',
type: 'string',
schema: {
type: 'string'
},
}
formatParam['x-consoleDefault'] = 'application/json';
typeOps.get.parameters = typeOps.get.parameters.filter((v,i,a)=>a.findIndex(t=>(t.name === v.name))===i);
typeOps.get.parameters.push(formatParam);
typeOps.get.responses['200'].schema = {type: 'array', items: {$ref: schemaRef}}
typeOps.get.responses['200'].content = {'*/*': {schema: {type: 'array', items: {$ref: schemaRef}}}}
}
if (interactions.indexOf('history-instance') !== -1) {
var histOp
= swagger.paths['/' + res.type + '/{id}/_history']
= {get: getDefaultOp(res)}
histOp = histOp.get;
histOp.parameters = [
{name: 'id', in: 'path', required: true, type: 'string'},
{name: '_count', in: 'query', type: 'string'},
{name: '_since', in: 'query', type: 'string'},
{name: 'id', in: 'path', required: true, schema: {type: 'string'}},
{name: '_count', in: 'query', schema: {type: 'string'}},
{name: '_since', in: 'query', schema: {type: 'string'}},
]
}
if (interactions.indexOf('history-type') !== -1) {
Expand All @@ -92,31 +116,36 @@ Converter.convert = function(baseURL, conf) {
= {get: getDefaultOp(res)}
histOp = histOp.get;
histOp.parameters = [
{name: '_count', in: 'query', type: 'string'},
{name: '_since', in: 'query', type: 'string'},
{name: '_count', in: 'query', schema: {type: 'string'}},
{name: '_since', in: 'query', schema: {type: 'string'}},
]
}

if (interactions.indexOf('read') !== -1) {
instOps.get = getDefaultOp(res);
instOps.get.responses['200'].schema = {$ref: schemaRef};
instOps.get.responses['200'].content = {'*/*': {schema: {$ref: schemaRef}}};
}
if (interactions.indexOf('vread') !== -1) {
var versionOp = swagger.paths['/' + res.type + '/{id}/_history/{vid}'] = {get: getDefaultOp(res)};
versionOp = versionOp.get;
versionOp.parameters = [
{name: 'id', in: 'path', required: true, type: 'string'},
{name: 'vid', in: 'path', required: true, type: 'string'},
{name: 'id', in: 'path', required: true, schema: {type: 'string'}},
{name: 'vid', in: 'path', required: true, schema: {type: 'string'}},
];
versionOp.responses['200'].schema = {$ref: schemaRef};
versionOp.responses['200'].content = {'*/*': {schema: {$ref: schemaRef}}};
}
if (interactions.indexOf('update') !== -1) {
instOps.put = getDefaultOp(res);
instOps.put.parameters.push({
in: 'body',
name: 'body',
schema: {$ref: schemaRef}
})
instOps.put.requestBody = {
content: {
"application/json": {
schema: {
$ref: schemaRef
},
}
},
required: true
}
}
if (interactions.indexOf('delete') !== -1) {
instOps.delete = getDefaultOp(res);
Expand Down
9 changes: 0 additions & 9 deletions options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,4 @@ args = module.exports = JSON.parse(JSON.stringify(args));
args.fhir_url = args.fhir_url || 'http://argonaut.healthintersections.com.au/open';
args.conformance_path = args.conformance_path || '/metadata?_format=application/json';
args.output = args.output || '-';
if (args.username) {
args.authorization = {
username: args.username,
password: args.password,
}
}
if (args.reject_unauthorized === 'false' || args.reject_unauthorized === '0') {
args.reject_unauthorized = false;
}

3 changes: 1 addition & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var App = require('express')();

var Request = require('request');
var Convert = require('./index.js');
var LucyConsole = require('lucy-console');

Expand All @@ -11,7 +10,7 @@ Convert(args, function(err, s) {
swagger = s;
var portal = new LucyConsole({
swagger: swagger,
proxy: args.proxy_host || true,
proxy: true,
development: process.env.DEVELOPMENT,
})
App.use(portal.router);
Expand Down