GeezerVison is showing github.com — fix: don't send header values that are 'undefined' (#360) · readmeio/rdme@ae03c1e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +const { cleanHeaders } = require('../../src/lib/cleanHeaders'); | | 2 | + | | 3 | +describe('cleanHeaders', () => { | | 4 | + it('should b64-encode key in ReadMe-friendly format', () => { | | 5 | + expect(cleanHeaders('test')).toStrictEqual({ Authorization: 'Basic dGVzdDo=' }); | | 6 | + }); | | 7 | + | | 8 | + it('should filter out undefined headers', () => { | | 9 | + expect(cleanHeaders('test', { 'x-readme-version': undefined })).toStrictEqual({ Authorization: 'Basic dGVzdDo=' }); | | 10 | + }); | | 11 | + | | 12 | + it('should filter out null headers', () => { | | 13 | + expect(cleanHeaders('test', { 'x-readme-version': undefined, Accept: null })).toStrictEqual({ | | 14 | + Authorization: 'Basic dGVzdDo=', | | 15 | + }); | | 16 | + }); | | 17 | + | | 18 | + it('should pass in properly defined headers', () => { | | 19 | + expect( | | 20 | + cleanHeaders('test', { 'x-readme-version': undefined, Accept: null, 'Content-Type': 'application/json' }) | | 21 | + ).toStrictEqual({ | | 22 | + Authorization: 'Basic dGVzdDo=', | | 23 | + 'Content-Type': 'application/json', | | 24 | + }); | | 25 | + }); | | 26 | +}); |
Collapse file Copy file name to clipboardExpand all lines: src/cmds/docs/edit.js +5-7Lines changed: 5 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ const fs = require('fs'); | 3 | 3 | const editor = require('editor'); | 4 | 4 | const { promisify } = require('util'); | 5 | 5 | const APIError = require('../../lib/apiError'); | | 6 | +const { cleanHeaders } = require('../../lib/cleanHeaders'); | 6 | 7 | const { getProjectVersion } = require('../../lib/versionSelect'); | 7 | 8 | const { handleRes } = require('../../lib/handleRes'); | 8 | 9 | const fetch = require('node-fetch'); | @@ -52,15 +53,13 @@ exports.run = async function (opts) { | 52 | 53 | }); | 53 | 54 |
| 54 | 55 | const filename = `${slug}.md`; | 55 | | - const encodedString = Buffer.from(`${key}:`).toString('base64'); | 56 | 56 |
| 57 | 57 | const existingDoc = await fetch(`${config.host}/api/v1/docs/${slug}`, { | 58 | 58 | method: 'get', | 59 | | - headers: { | | 59 | + headers: cleanHeaders(key, { | 60 | 60 | 'x-readme-version': selectedVersion, | 61 | | - Authorization: `Basic ${encodedString}`, | 62 | 61 | Accept: 'application/json', | 63 | | - }, | | 62 | + }), | 64 | 63 | }).then(res => handleRes(res)); | 65 | 64 |
| 66 | 65 | await writeFile(filename, existingDoc.body); | @@ -72,11 +71,10 @@ exports.run = async function (opts) { | 72 | 71 |
| 73 | 72 | return fetch(`${config.host}/api/v1/docs/${slug}`, { | 74 | 73 | method: 'put', | 75 | | - headers: { | | 74 | + headers: cleanHeaders(key, { | 76 | 75 | 'x-readme-version': selectedVersion, | 77 | | - Authorization: `Basic ${encodedString}`, | 78 | 76 | 'Content-Type': 'application/json', | 79 | | - }, | | 77 | + }), | 80 | 78 | body: JSON.stringify( | 81 | 79 | Object.assign(existingDoc, { | 82 | 80 | body: updatedDoc, | |
Collapse file Copy file name to clipboardExpand all lines: src/cmds/docs/index.js +7-11Lines changed: 7 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ const config = require('config'); | 5 | 5 | const crypto = require('crypto'); | 6 | 6 | const frontMatter = require('gray-matter'); | 7 | 7 | const { promisify } = require('util'); | | 8 | +const { cleanHeaders } = require('../../lib/cleanHeaders'); | 8 | 9 | const { getProjectVersion } = require('../../lib/versionSelect'); | 9 | 10 | const { handleRes } = require('../../lib/handleRes'); | 10 | 11 | const fetch = require('node-fetch'); | @@ -70,18 +71,15 @@ exports.run = async function (opts) { | 70 | 71 | return Promise.reject(new Error(`We were unable to locate Markdown files in ${folder}.`)); | 71 | 72 | } | 72 | 73 |
| 73 | | - const encodedString = Buffer.from(`${key}:`).toString('base64'); | 74 | | - | 75 | 74 | function createDoc(slug, file, hash, err) { | 76 | 75 | if (err.error !== 'DOC_NOTFOUND') return Promise.reject(err); | 77 | 76 |
| 78 | 77 | return fetch(`${config.host}/api/v1/docs`, { | 79 | 78 | method: 'post', | 80 | | - headers: { | | 79 | + headers: cleanHeaders(key, { | 81 | 80 | 'x-readme-version': selectedVersion, | 82 | | - Authorization: `Basic ${encodedString}`, | 83 | 81 | 'Content-Type': 'application/json', | 84 | | - }, | | 82 | + }), | 85 | 83 | body: JSON.stringify({ | 86 | 84 | slug, | 87 | 85 | body: file.content, | @@ -98,11 +96,10 @@ exports.run = async function (opts) { | 98 | 96 |
| 99 | 97 | return fetch(`${config.host}/api/v1/docs/${slug}`, { | 100 | 98 | method: 'put', | 101 | | - headers: { | | 99 | + headers: cleanHeaders(key, { | 102 | 100 | 'x-readme-version': selectedVersion, | 103 | | - Authorization: `Basic ${encodedString}`, | 104 | 101 | 'Content-Type': 'application/json', | 105 | | - }, | | 102 | + }), | 106 | 103 | body: JSON.stringify( | 107 | 104 | Object.assign(existingDoc, { | 108 | 105 | body: file.content, | @@ -124,11 +121,10 @@ exports.run = async function (opts) { | 124 | 121 |
| 125 | 122 | return fetch(`${config.host}/api/v1/docs/${slug}`, { | 126 | 123 | method: 'get', | 127 | | - headers: { | | 124 | + headers: cleanHeaders(key, { | 128 | 125 | 'x-readme-version': selectedVersion, | 129 | | - Authorization: `Basic ${encodedString}`, | 130 | 126 | Accept: 'application/json', | 131 | | - }, | | 127 | + }), | 132 | 128 | }) | 133 | 129 | .then(res => res.json()) | 134 | 130 | .then(res => { | |
Collapse file Copy file name to clipboardExpand all lines: src/cmds/openapi.js +5-8Lines changed: 5 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,7 @@ const { prompt } = require('enquirer'); | 5 | 5 | const OASNormalize = require('oas-normalize'); | 6 | 6 | const promptOpts = require('../lib/prompts'); | 7 | 7 | const APIError = require('../lib/apiError'); | | 8 | +const { cleanHeaders } = require('../lib/cleanHeaders'); | 8 | 9 | const { getProjectVersion } = require('../lib/versionSelect'); | 9 | 10 | const fetch = require('node-fetch'); | 10 | 11 | const FormData = require('form-data'); | @@ -61,8 +62,6 @@ exports.run = async function (opts) { | 61 | 62 | return Promise.reject(new Error('No project API key provided. Please use `--key`.')); | 62 | 63 | } | 63 | 64 |
| 64 | | - const encodedString = Buffer.from(`${key}:`).toString('base64'); | 65 | | - | 66 | 65 | async function callApi(specPath, versionCleaned) { | 67 | 66 | // @todo Tailor messaging to what is actually being handled here. If the user is uploading a Swagger file, never mention that they uploaded/updated an OpenAPI file. | 68 | 67 |
| @@ -122,12 +121,11 @@ exports.run = async function (opts) { | 122 | 121 | formData.append('spec', bundledSpec); | 123 | 122 |
| 124 | 123 | const options = { | 125 | | - headers: { | | 124 | + headers: cleanHeaders(key, { | 126 | 125 | 'x-readme-version': versionCleaned, | 127 | 126 | 'x-readme-source': 'cli', | 128 | | - Authorization: `Basic ${encodedString}`, | 129 | 127 | Accept: 'application/json', | 130 | | - }, | | 128 | + }), | 131 | 129 | body: formData, | 132 | 130 | }; | 133 | 131 |
| @@ -163,10 +161,9 @@ exports.run = async function (opts) { | 163 | 161 | function getSpecs(url) { | 164 | 162 | return fetch(`${config.host}${url}`, { | 165 | 163 | method: 'get', | 166 | | - headers: { | | 164 | + headers: cleanHeaders(key, { | 167 | 165 | 'x-readme-version': versionCleaned, | 168 | | - Authorization: `Basic ${encodedString}`, | 169 | | - }, | | 166 | + }), | 170 | 167 | }); | 171 | 168 | } | 172 | 169 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/cmds/versions/create.js +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,6 +3,7 @@ const semver = require('semver'); | 3 | 3 | const { prompt } = require('enquirer'); | 4 | 4 | const promptOpts = require('../../lib/prompts'); | 5 | 5 | const APIError = require('../../lib/apiError'); | | 6 | +const { cleanHeaders } = require('../../lib/cleanHeaders'); | 6 | 7 | const { handleRes } = require('../../lib/handleRes'); | 7 | 8 | const fetch = require('node-fetch'); | 8 | 9 |
| @@ -54,7 +55,6 @@ exports.args = [ | 54 | 55 | exports.run = async function (opts) { | 55 | 56 | let versionList; | 56 | 57 | const { key, version, codename, fork, main, beta, isPublic } = opts; | 57 | | - const encodedString = Buffer.from(`${key}:`).toString('base64'); | 58 | 58 |
| 59 | 59 | if (!key) { | 60 | 60 | return Promise.reject(new Error('No project API key provided. Please use `--key`.')); | @@ -69,9 +69,7 @@ exports.run = async function (opts) { | 69 | 69 | if (!fork) { | 70 | 70 | versionList = await fetch(`${config.host}/api/v1/version`, { | 71 | 71 | method: 'get', | 72 | | - headers: { | 73 | | - Authorization: `Basic ${encodedString}`, | 74 | | - }, | | 72 | + headers: cleanHeaders(key), | 75 | 73 | }).then(res => handleRes(res)); | 76 | 74 | } | 77 | 75 |
| @@ -84,11 +82,10 @@ exports.run = async function (opts) { | 84 | 82 |
| 85 | 83 | return fetch(`${config.host}/api/v1/version`, { | 86 | 84 | method: 'post', | 87 | | - headers: { | | 85 | + headers: cleanHeaders(key, { | 88 | 86 | Accept: 'application/json', | 89 | 87 | 'Content-Type': 'application/json', | 90 | | - Authorization: `Basic ${encodedString}`, | 91 | | - }, | | 88 | + }), | 92 | 89 | body: JSON.stringify({ | 93 | 90 | version, | 94 | 91 | codename: codename || '', | |
Collapse file Copy file name to clipboardExpand all lines: src/cmds/versions/delete.js +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | const config = require('config'); | 2 | 2 | const APIError = require('../../lib/apiError'); | 3 | 3 | const { getProjectVersion } = require('../../lib/versionSelect'); | | 4 | +const { cleanHeaders } = require('../../lib/cleanHeaders'); | 4 | 5 | const fetch = require('node-fetch'); | 5 | 6 |
| 6 | 7 | exports.command = 'versions:delete'; | @@ -25,7 +26,6 @@ exports.args = [ | 25 | 26 |
| 26 | 27 | exports.run = async function (opts) { | 27 | 28 | const { key, version } = opts; | 28 | | - const encodedString = Buffer.from(`${key}:`).toString('base64'); | 29 | 29 |
| 30 | 30 | if (!key) { | 31 | 31 | return Promise.reject(new Error('No project API key provided. Please use `--key`.')); | @@ -37,9 +37,7 @@ exports.run = async function (opts) { | 37 | 37 |
| 38 | 38 | return fetch(`${config.host}/api/v1/version/${selectedVersion}`, { | 39 | 39 | method: 'delete', | 40 | | - headers: { | 41 | | - Authorization: `Basic ${encodedString}`, | 42 | | - }, | | 40 | + headers: cleanHeaders(key), | 43 | 41 | }).then(res => { | 44 | 42 | if (res.error) { | 45 | 43 | return Promise.reject(new APIError(res)); | |
Collapse file Copy file name to clipboardExpand all lines: src/cmds/versions/index.js +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ const Table = require('cli-table'); | 2 | 2 | const config = require('config'); | 3 | 3 | const versionsCreate = require('./create'); | 4 | 4 | const APIError = require('../../lib/apiError'); | | 5 | +const { cleanHeaders } = require('../../lib/cleanHeaders'); | 5 | 6 | const fetch = require('node-fetch'); | 6 | 7 |
| 7 | 8 | exports.command = 'versions'; | @@ -82,7 +83,6 @@ const getVersionFormatted = version => { | 82 | 83 |
| 83 | 84 | exports.run = function (opts) { | 84 | 85 | const { key, version, raw } = opts; | 85 | | - const encodedString = Buffer.from(`${key}:`).toString('base64'); | 86 | 86 |
| 87 | 87 | if (!key) { | 88 | 88 | return Promise.reject(new Error('No project API key provided. Please use `--key`.')); | @@ -92,9 +92,7 @@ exports.run = function (opts) { | 92 | 92 |
| 93 | 93 | return fetch(uri, { | 94 | 94 | method: 'get', | 95 | | - headers: { | 96 | | - Authorization: `Basic ${encodedString}`, | 97 | | - }, | | 95 | + headers: cleanHeaders(key), | 98 | 96 | }) | 99 | 97 | .then(res => res.json()) | 100 | 98 | .then(data => { | |
Collapse file Copy file name to clipboardExpand all lines: src/cmds/versions/update.js +4-7Lines changed: 4 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -2,6 +2,7 @@ const config = require('config'); | 2 | 2 | const { prompt } = require('enquirer'); | 3 | 3 | const promptOpts = require('../../lib/prompts'); | 4 | 4 | const APIError = require('../../lib/apiError'); | | 5 | +const { cleanHeaders } = require('../../lib/cleanHeaders'); | 5 | 6 | const { getProjectVersion } = require('../../lib/versionSelect'); | 6 | 7 | const fetch = require('node-fetch'); | 7 | 8 | const { handleRes } = require('../../lib/handleRes'); | @@ -47,7 +48,6 @@ exports.args = [ | 47 | 48 |
| 48 | 49 | exports.run = async function (opts) { | 49 | 50 | const { key, version, codename, newVersion, main, beta, isPublic, deprecated } = opts; | 50 | | - const encodedString = Buffer.from(`${key}:`).toString('base64'); | 51 | 51 |
| 52 | 52 | if (!key) { | 53 | 53 | return Promise.reject(new Error('No project API key provided. Please use `--key`.')); | @@ -59,20 +59,17 @@ exports.run = async function (opts) { | 59 | 59 |
| 60 | 60 | const foundVersion = await fetch(`${config.host}/api/v1/version/${selectedVersion}`, { | 61 | 61 | method: 'get', | 62 | | - headers: { | 63 | | - Authorization: `Basic ${encodedString}`, | 64 | | - }, | | 62 | + headers: cleanHeaders(key), | 65 | 63 | }).then(res => handleRes(res)); | 66 | 64 |
| 67 | 65 | const promptResponse = await prompt(promptOpts.createVersionPrompt([{}], opts, foundVersion)); | 68 | 66 |
| 69 | 67 | return fetch(`${config.host}/api/v1/version/${selectedVersion}`, { | 70 | 68 | method: 'put', | 71 | | - headers: { | | 69 | + headers: cleanHeaders(key, { | 72 | 70 | Accept: 'application/json', | 73 | 71 | 'Content-Type': 'application/json', | 74 | | - Authorization: `Basic ${encodedString}`, | 75 | | - }, | | 72 | + }), | 76 | 73 | body: JSON.stringify({ | 77 | 74 | codename: codename || '', | 78 | 75 | version: newVersion || promptResponse.newVersion, | |
Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Returns the basic auth header and any other defined headers for use in node-fetch API calls. | | 3 | + * @param {string} key The ReadMe project API key | | 4 | + * @param {Object} inputHeaders Any additional headers to be cleaned | | 5 | + * @returns An object with cleaned request headers for usage in the node-fetch requests to the ReadMe API. | | 6 | + */ | | 7 | +function cleanHeaders(key, inputHeaders = {}) { | | 8 | + const encodedKey = Buffer.from(`${key}:`).toString('base64'); | | 9 | + const headers = { | | 10 | + Authorization: `Basic ${encodedKey}`, | | 11 | + }; | | 12 | + | | 13 | + Object.keys(inputHeaders).forEach(header => { | | 14 | + // For some reason, node-fetch will send in the string 'undefined' | | 15 | + // if you pass in an undefined value for a header, | | 16 | + // so that's why headers are added incrementally. | | 17 | + if (typeof inputHeaders[header] === 'string') headers[header] = inputHeaders[header]; | | 18 | + }); | | 19 | + | | 20 | + return headers; | | 21 | +} | | 22 | + | | 23 | +module.exports = { cleanHeaders }; |
Collapse file Copy file name to clipboardExpand all lines: src/lib/versionSelect.js +4-5Lines changed: 4 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | const { prompt } = require('enquirer'); | 2 | 2 | const promptOpts = require('./prompts'); | | 3 | +const { cleanHeaders } = require('./cleanHeaders'); | 3 | 4 | const fetch = require('node-fetch'); | 4 | 5 | const config = require('config'); | 5 | 6 | const APIError = require('./apiError'); | 6 | 7 |
| 7 | 8 | async function getProjectVersion(versionFlag, key, allowNewVersion) { | 8 | | - const encodedString = Buffer.from(`${key}:`).toString('base64'); | 9 | | - | 10 | 9 | try { | 11 | 10 | if (versionFlag) { | 12 | 11 | return await fetch(`${config.host}/api/v1/version/${versionFlag}`, { | 13 | 12 | method: 'get', | 14 | | - headers: { Authorization: `Basic ${encodedString}` }, | | 13 | + headers: cleanHeaders(key), | 15 | 14 | }) | 16 | 15 | .then(res => res.json()) | 17 | 16 | .then(res => res.version); | 18 | 17 | } | 19 | 18 |
| 20 | 19 | const versionList = await fetch(`${config.host}/api/v1/version`, { | 21 | 20 | method: 'get', | 22 | | - headers: { Authorization: `Basic ${encodedString}` }, | | 21 | + headers: cleanHeaders(key), | 23 | 22 | }).then(res => res.json()); | 24 | 23 |
| 25 | 24 | if (allowNewVersion) { | @@ -29,7 +28,7 @@ async function getProjectVersion(versionFlag, key, allowNewVersion) { | 29 | 28 |
| 30 | 29 | await fetch(`${config.host}/api/v1/version`, { | 31 | 30 | method: 'post', | 32 | | - headers: { Authorization: `Basic ${encodedString}`, 'Content-Type': 'application/json' }, | | 31 | + headers: cleanHeaders(key, { 'Content-Type': 'application/json' }), | 33 | 32 | body: JSON.stringify({ | 34 | 33 | from: versionList[0].version, | 35 | 34 | version: newVersion, | |
|