From 95a163abfccab4470c515604cc94fb0e7c48aa8c Mon Sep 17 00:00:00 2001 From: Alban Mouton Date: Wed, 31 May 2017 11:05:47 +0200 Subject: [PATCH] merge .js modules into .vue module for simpler integration --- src/OpenApi.vue | 130 +++++++++++++++++++++++++++++++++++++++++++++--- src/request.js | 42 ---------------- src/tags.js | 69 ------------------------- 3 files changed, 124 insertions(+), 117 deletions(-) delete mode 100644 src/request.js delete mode 100644 src/tags.js diff --git a/src/OpenApi.vue b/src/OpenApi.vue index 2dff9ed..fbd6855 100644 --- a/src/OpenApi.vue +++ b/src/OpenApi.vue @@ -143,13 +143,12 @@ diff --git a/src/request.js b/src/request.js deleted file mode 100644 index 7140721..0000000 --- a/src/request.js +++ /dev/null @@ -1,42 +0,0 @@ -import Vue from 'vue' - -exports.reset = (request, entry) => { - request.params = Object.assign({}, ...(entry.parameters || []).map(p => ({ - [p.name]: p.schema.enum ? p.schema.enum[0] : (p.schema.type === 'array' ? [] : null) - }))) - if (entry.requestBody) { - request.contentType = entry.requestBody.selectedType - const example = entry.requestBody.content[request.contentType].example - request.body = typeof example === 'string' ? example : JSON.stringify(example, null, 2) - } -} - -exports.fetch = (request, entry, api) => { - let params = Object.assign({}, ...(entry.parameters || []) - .filter(p => p.in === 'query' && (p.schema.type === 'array' ? request.params[p.name].length : request.params[p.name])) - .map(p => ({ - // TODO : join character for array should depend of p.style - [p.name]: p.schema.type === 'array' ? request.params[p.name].join(',') : request.params[p.name] - })) - ) - let headers = Object.assign({}, ...(entry.parameters || []) - .filter(p => p.in === 'header' && (p.schema.type === 'array' ? request.params[p.name].length : request.params[p.name])) - .map(p => ({ - // TODO : join character for array should depend of p.style - [p.name]: p.schema.type === 'array' ? request.params[p.name].join(',') : request.params[p.name] - })) - ) - const httpRequest = { - method: entry.method, - url: api.servers[0].url + entry.path.replace(/{(\w*)}/g, (m, key) => { - return request.params[key] - }), - params, - headers - } - if (entry.requestBody) { - httpRequest.headers['Content-type'] = request.contentType - httpRequest.body = request.body - } - return Vue.http(httpRequest) -} diff --git a/src/tags.js b/src/tags.js deleted file mode 100644 index 1f42f71..0000000 --- a/src/tags.js +++ /dev/null @@ -1,69 +0,0 @@ -import Vue from 'vue' -import deref from 'json-schema-deref-local' - -const defaultStyle = { - query: 'form', - path: 'simple', - header: 'simple', - cookie: 'form' -} - -function processContent(contentType, api) { - // Spec allow examples as an item or an array. In the API or in the schema - // we always fall back on an array - if (contentType.schema) { - contentType.examples = contentType.examples || contentType.schema.examples - contentType.example = contentType.example || contentType.schema.example - } - - if (contentType.example) { - contentType.examples = [contentType.example] - } -} - -exports.get = api => { - const derefAPI = deref(api) - var tags = {} - Object.keys(derefAPI.paths).forEach(function(path) { - Object.keys(derefAPI.paths[path]).forEach(function(method) { - let entry = derefAPI.paths[path][method] - entry.method = method - entry.path = path - // Filling tags entries - entry.tags = entry.tags || [] - if (!entry.tags.length) { - entry.tags.push('No category') - } - entry.tags.forEach(function(tag) { - tags[tag] = tags[tag] || [] - tags[tag].push(entry) - }) - if (entry.parameters) { - entry.parameters.forEach(p => { - p.style = p.style || defaultStyle[p.in] - p.explode = p.explode || (p.style === 'form') - p.schema = p.schema || { - type: 'string' - } - }) - } - if (entry.requestBody) { - if (entry.requestBody.content) { - Vue.set(entry.requestBody, 'selectedType', Object.keys(entry.requestBody.content)[0]) - entry.requestBody.required = true - Object.values(entry.requestBody.content).forEach(contentType => processContent(contentType, api)) - } - } - - // Some preprocessing with responses - Object.values(entry.responses).forEach(response => { - if (response.content) { - // preselecting responses mime-type - Vue.set(response, 'selectedType', Object.keys(response.content)[0]) - Object.values(response.content).forEach(contentType => processContent(contentType, api)) - } - }) - }) - }) - return tags -}