diff --git a/.gitignore b/.gitignore index c2658d7d..9cf6fffa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules/ +.idea diff --git a/index.js b/index.js index ae90b8c6..83fb0103 100644 --- a/index.js +++ b/index.js @@ -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); } diff --git a/lib/conformance-to-swagger.js b/lib/conformance-to-swagger.js index 4321ec94..781ea170 100644 --- a/lib/conformance-to-swagger.js +++ b/lib/conformance-to-swagger.js @@ -23,14 +23,31 @@ 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}; @@ -38,42 +55,49 @@ Converter.convert = function(baseURL, conf) { 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 @@ -81,9 +105,9 @@ Converter.convert = function(baseURL, conf) { = {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) { @@ -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); diff --git a/options.js b/options.js index e67d0484..e2c5d56e 100644 --- a/options.js +++ b/options.js @@ -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; -} diff --git a/server.js b/server.js index a2b8fca0..e2c7165a 100644 --- a/server.js +++ b/server.js @@ -1,6 +1,5 @@ var App = require('express')(); -var Request = require('request'); var Convert = require('./index.js'); var LucyConsole = require('lucy-console'); @@ -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);