GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: don't send header values that are 'undefined' (#360) · readmeio/rdme@ae03c1e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • __tests__/lib
    • cleanHeaders.test.js
  • src
    • cmds
      • docs
        • edit.js
        • index.js
      • openapi.js
      • versions
        • create.js
        • delete.js
        • index.js
        • update.js
    • lib
      • cleanHeaders.js
      • versionSelect.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎__tests__/lib/cleanHeaders.test.js‎

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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

‎src/cmds/docs/edit.js‎

Copy file name to clipboardExpand all lines: src/cmds/docs/edit.js
+5-7Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs');
33
const editor = require('editor');
44
const { promisify } = require('util');
55
const APIError = require('../../lib/apiError');
6+
const { cleanHeaders } = require('../../lib/cleanHeaders');
67
const { getProjectVersion } = require('../../lib/versionSelect');
78
const { handleRes } = require('../../lib/handleRes');
89
const fetch = require('node-fetch');
@@ -52,15 +53,13 @@ exports.run = async function (opts) {
5253
});
5354

5455
const filename = `${slug}.md`;
55-
const encodedString = Buffer.from(`${key}:`).toString('base64');
5656

5757
const existingDoc = await fetch(`${config.host}/api/v1/docs/${slug}`, {
5858
method: 'get',
59-
headers: {
59+
headers: cleanHeaders(key, {
6060
'x-readme-version': selectedVersion,
61-
Authorization: `Basic ${encodedString}`,
6261
Accept: 'application/json',
63-
},
62+
}),
6463
}).then(res => handleRes(res));
6564

6665
await writeFile(filename, existingDoc.body);
@@ -72,11 +71,10 @@ exports.run = async function (opts) {
7271

7372
return fetch(`${config.host}/api/v1/docs/${slug}`, {
7473
method: 'put',
75-
headers: {
74+
headers: cleanHeaders(key, {
7675
'x-readme-version': selectedVersion,
77-
Authorization: `Basic ${encodedString}`,
7876
'Content-Type': 'application/json',
79-
},
77+
}),
8078
body: JSON.stringify(
8179
Object.assign(existingDoc, {
8280
body: updatedDoc,
Collapse file

‎src/cmds/docs/index.js‎

Copy file name to clipboardExpand all lines: src/cmds/docs/index.js
+7-11Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const config = require('config');
55
const crypto = require('crypto');
66
const frontMatter = require('gray-matter');
77
const { promisify } = require('util');
8+
const { cleanHeaders } = require('../../lib/cleanHeaders');
89
const { getProjectVersion } = require('../../lib/versionSelect');
910
const { handleRes } = require('../../lib/handleRes');
1011
const fetch = require('node-fetch');
@@ -70,18 +71,15 @@ exports.run = async function (opts) {
7071
return Promise.reject(new Error(`We were unable to locate Markdown files in ${folder}.`));
7172
}
7273

73-
const encodedString = Buffer.from(`${key}:`).toString('base64');
74-
7574
function createDoc(slug, file, hash, err) {
7675
if (err.error !== 'DOC_NOTFOUND') return Promise.reject(err);
7776

7877
return fetch(`${config.host}/api/v1/docs`, {
7978
method: 'post',
80-
headers: {
79+
headers: cleanHeaders(key, {
8180
'x-readme-version': selectedVersion,
82-
Authorization: `Basic ${encodedString}`,
8381
'Content-Type': 'application/json',
84-
},
82+
}),
8583
body: JSON.stringify({
8684
slug,
8785
body: file.content,
@@ -98,11 +96,10 @@ exports.run = async function (opts) {
9896

9997
return fetch(`${config.host}/api/v1/docs/${slug}`, {
10098
method: 'put',
101-
headers: {
99+
headers: cleanHeaders(key, {
102100
'x-readme-version': selectedVersion,
103-
Authorization: `Basic ${encodedString}`,
104101
'Content-Type': 'application/json',
105-
},
102+
}),
106103
body: JSON.stringify(
107104
Object.assign(existingDoc, {
108105
body: file.content,
@@ -124,11 +121,10 @@ exports.run = async function (opts) {
124121

125122
return fetch(`${config.host}/api/v1/docs/${slug}`, {
126123
method: 'get',
127-
headers: {
124+
headers: cleanHeaders(key, {
128125
'x-readme-version': selectedVersion,
129-
Authorization: `Basic ${encodedString}`,
130126
Accept: 'application/json',
131-
},
127+
}),
132128
})
133129
.then(res => res.json())
134130
.then(res => {
Collapse file

‎src/cmds/openapi.js‎

Copy file name to clipboardExpand all lines: src/cmds/openapi.js
+5-8Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { prompt } = require('enquirer');
55
const OASNormalize = require('oas-normalize');
66
const promptOpts = require('../lib/prompts');
77
const APIError = require('../lib/apiError');
8+
const { cleanHeaders } = require('../lib/cleanHeaders');
89
const { getProjectVersion } = require('../lib/versionSelect');
910
const fetch = require('node-fetch');
1011
const FormData = require('form-data');
@@ -61,8 +62,6 @@ exports.run = async function (opts) {
6162
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
6263
}
6364

64-
const encodedString = Buffer.from(`${key}:`).toString('base64');
65-
6665
async function callApi(specPath, versionCleaned) {
6766
// @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.
6867

@@ -122,12 +121,11 @@ exports.run = async function (opts) {
122121
formData.append('spec', bundledSpec);
123122

124123
const options = {
125-
headers: {
124+
headers: cleanHeaders(key, {
126125
'x-readme-version': versionCleaned,
127126
'x-readme-source': 'cli',
128-
Authorization: `Basic ${encodedString}`,
129127
Accept: 'application/json',
130-
},
128+
}),
131129
body: formData,
132130
};
133131

@@ -163,10 +161,9 @@ exports.run = async function (opts) {
163161
function getSpecs(url) {
164162
return fetch(`${config.host}${url}`, {
165163
method: 'get',
166-
headers: {
164+
headers: cleanHeaders(key, {
167165
'x-readme-version': versionCleaned,
168-
Authorization: `Basic ${encodedString}`,
169-
},
166+
}),
170167
});
171168
}
172169

Collapse file

‎src/cmds/versions/create.js‎

Copy file name to clipboardExpand all lines: src/cmds/versions/create.js
+4-7Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const semver = require('semver');
33
const { prompt } = require('enquirer');
44
const promptOpts = require('../../lib/prompts');
55
const APIError = require('../../lib/apiError');
6+
const { cleanHeaders } = require('../../lib/cleanHeaders');
67
const { handleRes } = require('../../lib/handleRes');
78
const fetch = require('node-fetch');
89

@@ -54,7 +55,6 @@ exports.args = [
5455
exports.run = async function (opts) {
5556
let versionList;
5657
const { key, version, codename, fork, main, beta, isPublic } = opts;
57-
const encodedString = Buffer.from(`${key}:`).toString('base64');
5858

5959
if (!key) {
6060
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
@@ -69,9 +69,7 @@ exports.run = async function (opts) {
6969
if (!fork) {
7070
versionList = await fetch(`${config.host}/api/v1/version`, {
7171
method: 'get',
72-
headers: {
73-
Authorization: `Basic ${encodedString}`,
74-
},
72+
headers: cleanHeaders(key),
7573
}).then(res => handleRes(res));
7674
}
7775

@@ -84,11 +82,10 @@ exports.run = async function (opts) {
8482

8583
return fetch(`${config.host}/api/v1/version`, {
8684
method: 'post',
87-
headers: {
85+
headers: cleanHeaders(key, {
8886
Accept: 'application/json',
8987
'Content-Type': 'application/json',
90-
Authorization: `Basic ${encodedString}`,
91-
},
88+
}),
9289
body: JSON.stringify({
9390
version,
9491
codename: codename || '',
Collapse file

‎src/cmds/versions/delete.js‎

Copy file name to clipboardExpand all lines: src/cmds/versions/delete.js
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const config = require('config');
22
const APIError = require('../../lib/apiError');
33
const { getProjectVersion } = require('../../lib/versionSelect');
4+
const { cleanHeaders } = require('../../lib/cleanHeaders');
45
const fetch = require('node-fetch');
56

67
exports.command = 'versions:delete';
@@ -25,7 +26,6 @@ exports.args = [
2526

2627
exports.run = async function (opts) {
2728
const { key, version } = opts;
28-
const encodedString = Buffer.from(`${key}:`).toString('base64');
2929

3030
if (!key) {
3131
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
@@ -37,9 +37,7 @@ exports.run = async function (opts) {
3737

3838
return fetch(`${config.host}/api/v1/version/${selectedVersion}`, {
3939
method: 'delete',
40-
headers: {
41-
Authorization: `Basic ${encodedString}`,
42-
},
40+
headers: cleanHeaders(key),
4341
}).then(res => {
4442
if (res.error) {
4543
return Promise.reject(new APIError(res));
Collapse file

‎src/cmds/versions/index.js‎

Copy file name to clipboardExpand all lines: src/cmds/versions/index.js
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const Table = require('cli-table');
22
const config = require('config');
33
const versionsCreate = require('./create');
44
const APIError = require('../../lib/apiError');
5+
const { cleanHeaders } = require('../../lib/cleanHeaders');
56
const fetch = require('node-fetch');
67

78
exports.command = 'versions';
@@ -82,7 +83,6 @@ const getVersionFormatted = version => {
8283

8384
exports.run = function (opts) {
8485
const { key, version, raw } = opts;
85-
const encodedString = Buffer.from(`${key}:`).toString('base64');
8686

8787
if (!key) {
8888
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
@@ -92,9 +92,7 @@ exports.run = function (opts) {
9292

9393
return fetch(uri, {
9494
method: 'get',
95-
headers: {
96-
Authorization: `Basic ${encodedString}`,
97-
},
95+
headers: cleanHeaders(key),
9896
})
9997
.then(res => res.json())
10098
.then(data => {
Collapse file

‎src/cmds/versions/update.js‎

Copy file name to clipboardExpand all lines: src/cmds/versions/update.js
+4-7Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const config = require('config');
22
const { prompt } = require('enquirer');
33
const promptOpts = require('../../lib/prompts');
44
const APIError = require('../../lib/apiError');
5+
const { cleanHeaders } = require('../../lib/cleanHeaders');
56
const { getProjectVersion } = require('../../lib/versionSelect');
67
const fetch = require('node-fetch');
78
const { handleRes } = require('../../lib/handleRes');
@@ -47,7 +48,6 @@ exports.args = [
4748

4849
exports.run = async function (opts) {
4950
const { key, version, codename, newVersion, main, beta, isPublic, deprecated } = opts;
50-
const encodedString = Buffer.from(`${key}:`).toString('base64');
5151

5252
if (!key) {
5353
return Promise.reject(new Error('No project API key provided. Please use `--key`.'));
@@ -59,20 +59,17 @@ exports.run = async function (opts) {
5959

6060
const foundVersion = await fetch(`${config.host}/api/v1/version/${selectedVersion}`, {
6161
method: 'get',
62-
headers: {
63-
Authorization: `Basic ${encodedString}`,
64-
},
62+
headers: cleanHeaders(key),
6563
}).then(res => handleRes(res));
6664

6765
const promptResponse = await prompt(promptOpts.createVersionPrompt([{}], opts, foundVersion));
6866

6967
return fetch(`${config.host}/api/v1/version/${selectedVersion}`, {
7068
method: 'put',
71-
headers: {
69+
headers: cleanHeaders(key, {
7270
Accept: 'application/json',
7371
'Content-Type': 'application/json',
74-
Authorization: `Basic ${encodedString}`,
75-
},
72+
}),
7673
body: JSON.stringify({
7774
codename: codename || '',
7875
version: newVersion || promptResponse.newVersion,
Collapse file

‎src/lib/cleanHeaders.js‎

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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

‎src/lib/versionSelect.js‎

Copy file name to clipboardExpand all lines: src/lib/versionSelect.js
+4-5Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
const { prompt } = require('enquirer');
22
const promptOpts = require('./prompts');
3+
const { cleanHeaders } = require('./cleanHeaders');
34
const fetch = require('node-fetch');
45
const config = require('config');
56
const APIError = require('./apiError');
67

78
async function getProjectVersion(versionFlag, key, allowNewVersion) {
8-
const encodedString = Buffer.from(`${key}:`).toString('base64');
9-
109
try {
1110
if (versionFlag) {
1211
return await fetch(`${config.host}/api/v1/version/${versionFlag}`, {
1312
method: 'get',
14-
headers: { Authorization: `Basic ${encodedString}` },
13+
headers: cleanHeaders(key),
1514
})
1615
.then(res => res.json())
1716
.then(res => res.version);
1817
}
1918

2019
const versionList = await fetch(`${config.host}/api/v1/version`, {
2120
method: 'get',
22-
headers: { Authorization: `Basic ${encodedString}` },
21+
headers: cleanHeaders(key),
2322
}).then(res => res.json());
2423

2524
if (allowNewVersion) {
@@ -29,7 +28,7 @@ async function getProjectVersion(versionFlag, key, allowNewVersion) {
2928

3029
await fetch(`${config.host}/api/v1/version`, {
3130
method: 'post',
32-
headers: { Authorization: `Basic ${encodedString}`, 'Content-Type': 'application/json' },
31+
headers: cleanHeaders(key, { 'Content-Type': 'application/json' }),
3332
body: JSON.stringify({
3433
from: versionList[0].version,
3534
version: newVersion,

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.