Skip to content

Commit 452be3a

Browse files
authored
refactor(fetch): readmeAPIFetch, consolidate base URL (#742)
* refactor: rename fetch to `readmeAPIFetch` * refactor: move API base URL into fetch function itself
1 parent 7574752 commit 452be3a

File tree

18 files changed

+64
-76
lines changed

18 files changed

+64
-76
lines changed

__tests__/helpers/get-api-mock.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import config from 'config';
22
import nock from 'nock';
33

4-
import { getUserAgent } from '../../src/lib/fetch';
4+
import { getUserAgent } from '../../src/lib/readmeAPIFetch';
55

66
/**
77
* Nock wrapper that adds required `user-agent` request header

__tests__/lib/fetch.test.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/* eslint-disable @typescript-eslint/ban-ts-comment, no-console */
2-
import config from 'config';
32
import { Headers } from 'node-fetch';
43

54
import pkg from '../../package.json';
6-
import fetch, { cleanHeaders, handleRes } from '../../src/lib/fetch';
5+
import readmeAPIFetch, { cleanHeaders, handleRes } from '../../src/lib/readmeAPIFetch';
76
import getAPIMock from '../helpers/get-api-mock';
87
import { after, before } from '../helpers/setup-gha-env';
98

@@ -23,7 +22,7 @@ describe('#fetch()', () => {
2322
return this.req.headers;
2423
});
2524

26-
const headers = await fetch(`${config.get('host')}/api/v1`, {
25+
const headers = await readmeAPIFetch('/api/v1', {
2726
method: 'get',
2827
headers: cleanHeaders(key),
2928
}).then(handleRes);
@@ -50,8 +49,8 @@ describe('#fetch()', () => {
5049
return this.req.headers;
5150
});
5251

53-
const headers = await fetch(
54-
`${config.get('host')}/api/v1`,
52+
const headers = await readmeAPIFetch(
53+
'/api/v1',
5554
{
5655
method: 'get',
5756
headers: cleanHeaders(key),
@@ -75,8 +74,8 @@ describe('#fetch()', () => {
7574
return this.req.headers;
7675
});
7776

78-
const headers = await fetch(
79-
`${config.get('host')}/api/v1`,
77+
const headers = await readmeAPIFetch(
78+
'/api/v1',
8079
{
8180
method: 'get',
8281
headers: cleanHeaders(key),
@@ -101,8 +100,8 @@ describe('#fetch()', () => {
101100
return this.req.headers;
102101
});
103102

104-
const headers = await fetch(
105-
`${config.get('host')}/api/v1`,
103+
const headers = await readmeAPIFetch(
104+
'/api/v1',
106105
{
107106
method: 'get',
108107
headers: cleanHeaders(key),
@@ -126,7 +125,7 @@ describe('#fetch()', () => {
126125
return this.req.headers;
127126
});
128127

129-
const headers = await fetch(`${config.get('host')}/api/v1`, {
128+
const headers = await readmeAPIFetch('/api/v1', {
130129
method: 'get',
131130
headers: cleanHeaders(key),
132131
}).then(handleRes);
@@ -148,7 +147,7 @@ describe('#fetch()', () => {
148147
return this.req.headers;
149148
});
150149

151-
const headers = await fetch(`${config.get('host')}/api/v1/doesnt-need-auth`).then(handleRes);
150+
const headers = await readmeAPIFetch('/api/v1/doesnt-need-auth').then(handleRes);
152151

153152
expect(headers['user-agent'].shift()).toBe(`rdme/${pkg.version}`);
154153
expect(headers['x-readme-source'].shift()).toBe('cli');
@@ -180,7 +179,7 @@ describe('#fetch()', () => {
180179
Warning: '',
181180
});
182181

183-
await fetch(`${config.get('host')}/api/v1/some-warning`);
182+
await readmeAPIFetch('/api/v1/some-warning');
184183

185184
expect(console.warn).toHaveBeenCalledTimes(0);
186185
expect(getWarningCommandOutput()).toBe('');
@@ -193,7 +192,7 @@ describe('#fetch()', () => {
193192
Warning: '199 - "some error"',
194193
});
195194

196-
await fetch(`${config.get('host')}/api/v1/some-warning`);
195+
await readmeAPIFetch('/api/v1/some-warning');
197196

198197
expect(console.warn).toHaveBeenCalledTimes(1);
199198
expect(getWarningCommandOutput()).toBe('⚠️ ReadMe API Warning: some error');
@@ -206,7 +205,7 @@ describe('#fetch()', () => {
206205
Warning: '199 - "some error" 199 - "another error"',
207206
});
208207

209-
await fetch(`${config.get('host')}/api/v1/some-warning`);
208+
await readmeAPIFetch('/api/v1/some-warning');
210209

211210
expect(console.warn).toHaveBeenCalledTimes(2);
212211
expect(getWarningCommandOutput()).toBe(
@@ -221,7 +220,7 @@ describe('#fetch()', () => {
221220
Warning: 'some garbage error',
222221
});
223222

224-
await fetch(`${config.get('host')}/api/v1/some-warning`);
223+
await readmeAPIFetch('/api/v1/some-warning');
225224

226225
expect(console.warn).toHaveBeenCalledTimes(1);
227226
expect(getWarningCommandOutput()).toBe('⚠️ ReadMe API Warning: some garbage error');
@@ -243,7 +242,7 @@ describe('#fetch()', () => {
243242

244243
const mock = getAPIMock({}, `${proxy}/`).get('/api/v1/proxy').reply(200);
245244

246-
await fetch(`${config.get('host')}/api/v1/proxy`);
245+
await readmeAPIFetch('/api/v1/proxy');
247246

248247
expect(mock.isDone()).toBe(true);
249248
});
@@ -255,7 +254,7 @@ describe('#fetch()', () => {
255254

256255
const mock = getAPIMock({}, `${proxy}/`).get('/api/v1/proxy').reply(200);
257256

258-
await fetch(`${config.get('host')}/api/v1/proxy`);
257+
await readmeAPIFetch('/api/v1/proxy');
259258

260259
expect(mock.isDone()).toBe(true);
261260
});
@@ -267,7 +266,7 @@ describe('#fetch()', () => {
267266

268267
const mock = getAPIMock({}, proxy).get('/api/v1/proxy').reply(200);
269268

270-
await fetch(`${config.get('host')}/api/v1/proxy`);
269+
await readmeAPIFetch('/api/v1/proxy');
271270

272271
expect(mock.isDone()).toBe(true);
273272
});

src/cmds/categories/create.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import config from 'config';
55
import { Headers } from 'node-fetch';
66

77
import Command, { CommandCategories } from '../../lib/baseCommand';
8-
import fetch, { cleanHeaders, handleRes } from '../../lib/fetch';
98
import getCategories from '../../lib/getCategories';
9+
import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch';
1010
import { getProjectVersion } from '../../lib/versionSelect';
1111

1212
interface Category {
@@ -88,7 +88,7 @@ export default class CategoriesCreateCommand extends Command {
8888
);
8989
}
9090
}
91-
return fetch(`${config.get('host')}/api/v1/categories`, {
91+
return readmeAPIFetch('/api/v1/categories', {
9292
method: 'post',
9393
headers: cleanHeaders(
9494
key,

src/cmds/docs/edit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import editor from 'editor';
1111
import APIError from '../../lib/apiError';
1212
import Command, { CommandCategories } from '../../lib/baseCommand';
1313
import isHidden from '../../lib/decorators/isHidden';
14-
import fetch, { cleanHeaders, handleRes } from '../../lib/fetch';
14+
import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch';
1515
import { getProjectVersion } from '../../lib/versionSelect';
1616

1717
const writeFile = promisify(fs.writeFile);
@@ -61,7 +61,7 @@ export default class DocsEditCommand extends Command {
6161

6262
const filename = `${slug}.md`;
6363

64-
const existingDoc = await fetch(`${config.get('host')}/api/v1/docs/${slug}`, {
64+
const existingDoc = await readmeAPIFetch(`/api/v1/docs/${slug}`, {
6565
method: 'get',
6666
headers: cleanHeaders(
6767
key,
@@ -84,7 +84,7 @@ export default class DocsEditCommand extends Command {
8484

8585
Command.debug(`read edited contents of ${filename}, sending to ReadMe`);
8686

87-
return fetch(`${config.get('host')}/api/v1/docs/${slug}`, {
87+
return readmeAPIFetch(`/api/v1/docs/${slug}`, {
8888
method: 'put',
8989
headers: cleanHeaders(
9090
key,

src/cmds/openapi/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ import type { CommandOptions } from '../../lib/baseCommand';
22
import type { RequestInit, Response } from 'node-fetch';
33

44
import chalk from 'chalk';
5-
import config from 'config';
65
import { Headers } from 'node-fetch';
76
import ora from 'ora';
87
import parse from 'parse-link-header';
98

109
import Command, { CommandCategories } from '../../lib/baseCommand';
1110
import createGHA from '../../lib/createGHA';
12-
import fetch, { cleanHeaders, handleRes } from '../../lib/fetch';
1311
import { oraOptions } from '../../lib/logger';
1412
import prepareOas from '../../lib/prepareOas';
1513
import * as promptHandler from '../../lib/prompts';
1614
import promptTerminal from '../../lib/promptWrapper';
15+
import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch';
1716
import streamSpecToRegistry from '../../lib/streamSpecToRegistry';
1817
import { getProjectVersion } from '../../lib/versionSelect';
1918

@@ -237,7 +236,7 @@ export default class OpenAPICommand extends Command {
237236

238237
options.method = 'post';
239238
spinner.start('Creating your API docs in ReadMe...');
240-
return fetch(`${config.get('host')}/api/v1/api-specification`, options, {
239+
return readmeAPIFetch('/api/v1/api-specification', options, {
241240
filePath: specPath,
242241
fileType: specFileType,
243242
}).then(res => {
@@ -258,7 +257,7 @@ export default class OpenAPICommand extends Command {
258257
isUpdate = true;
259258
options.method = 'put';
260259
spinner.start('Updating your API docs in ReadMe...');
261-
return fetch(`${config.get('host')}/api/v1/api-specification/${specId}`, options, {
260+
return readmeAPIFetch(`/api/v1/api-specification/${specId}`, options, {
262261
filePath: specPath,
263262
fileType: specFileType,
264263
}).then(res => {
@@ -280,7 +279,7 @@ export default class OpenAPICommand extends Command {
280279
*/
281280

282281
function getSpecs(url: string) {
283-
return fetch(`${config.get('host')}${url}`, {
282+
return readmeAPIFetch(url, {
284283
method: 'get',
285284
headers: cleanHeaders(
286285
key,

src/cmds/versions/create.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { Headers } from 'node-fetch';
66
import semver from 'semver';
77

88
import Command, { CommandCategories } from '../../lib/baseCommand';
9-
import fetch, { cleanHeaders, handleRes } from '../../lib/fetch';
109
import * as promptHandler from '../../lib/prompts';
1110
import promptTerminal from '../../lib/promptWrapper';
11+
import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch';
1212

1313
export interface Options extends CommonOptions {
1414
fork?: string;
@@ -60,7 +60,7 @@ export default class CreateVersionCommand extends Command {
6060
}
6161

6262
if (!fork) {
63-
versionList = await fetch(`${config.get('host')}/api/v1/version`, {
63+
versionList = await readmeAPIFetch('/api/v1/version', {
6464
method: 'get',
6565
headers: cleanHeaders(key),
6666
}).then(handleRes);
@@ -82,7 +82,7 @@ export default class CreateVersionCommand extends Command {
8282
is_hidden: promptResponse.is_stable ? false : !(isPublic === 'true' || promptResponse.is_hidden),
8383
};
8484

85-
return fetch(`${config.get('host')}/api/v1/version`, {
85+
return readmeAPIFetch('/api/v1/version', {
8686
method: 'post',
8787
headers: cleanHeaders(
8888
key,

src/cmds/versions/delete.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { CommandOptions } from '../../lib/baseCommand';
22

3-
import config from 'config';
4-
53
import Command, { CommandCategories } from '../../lib/baseCommand';
6-
import fetch, { cleanHeaders, handleRes } from '../../lib/fetch';
4+
import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch';
75
import { getProjectVersion } from '../../lib/versionSelect';
86

97
export default class DeleteVersionCommand extends Command {
@@ -35,7 +33,7 @@ export default class DeleteVersionCommand extends Command {
3533

3634
Command.debug(`selectedVersion: ${selectedVersion}`);
3735

38-
return fetch(`${config.get('host')}/api/v1/version/${selectedVersion}`, {
36+
return readmeAPIFetch(`/api/v1/version/${selectedVersion}`, {
3937
method: 'delete',
4038
headers: cleanHeaders(key),
4139
})

src/cmds/versions/index.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import type { CommandOptions } from '../../lib/baseCommand';
22

3-
import config from 'config';
4-
53
import Command, { CommandCategories } from '../../lib/baseCommand';
6-
import fetch, { cleanHeaders, handleRes } from '../../lib/fetch';
4+
import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch';
75

86
export interface Version {
97
codename?: string;
@@ -40,9 +38,9 @@ export default class VersionsCommand extends Command {
4038

4139
const { key, version } = opts;
4240

43-
const uri = version ? `${config.get('host')}/api/v1/version/${version}` : `${config.get('host')}/api/v1/version`;
41+
const uri = version ? `/api/v1/version/${version}` : '/api/v1/version';
4442

45-
return fetch(uri, {
43+
return readmeAPIFetch(uri, {
4644
method: 'get',
4745
headers: cleanHeaders(key),
4846
})

src/cmds/versions/update.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ import type { Version } from '.';
22
import type { CommonOptions } from './create';
33
import type { CommandOptions } from '../../lib/baseCommand';
44

5-
import config from 'config';
65
import { Headers } from 'node-fetch';
76

87
import Command, { CommandCategories } from '../../lib/baseCommand';
9-
import fetch, { cleanHeaders, handleRes } from '../../lib/fetch';
108
import * as promptHandler from '../../lib/prompts';
119
import promptTerminal from '../../lib/promptWrapper';
10+
import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch';
1211
import { getProjectVersion } from '../../lib/versionSelect';
1312

1413
export interface Options extends CommonOptions {
@@ -56,7 +55,7 @@ export default class UpdateVersionCommand extends Command {
5655

5756
Command.debug(`selectedVersion: ${selectedVersion}`);
5857

59-
const foundVersion = await fetch(`${config.get('host')}/api/v1/version/${selectedVersion}`, {
58+
const foundVersion = await readmeAPIFetch(`/api/v1/version/${selectedVersion}`, {
6059
method: 'get',
6160
headers: cleanHeaders(key),
6261
}).then(handleRes);
@@ -72,7 +71,7 @@ export default class UpdateVersionCommand extends Command {
7271
is_hidden: promptResponse.is_stable ? false : !(isPublic === 'true' || promptResponse.is_hidden),
7372
};
7473

75-
return fetch(`${config.get('host')}/api/v1/version/${selectedVersion}`, {
74+
return readmeAPIFetch(`/api/v1/version/${selectedVersion}`, {
7675
method: 'put',
7776
headers: cleanHeaders(
7877
key,

src/lib/deleteDoc.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { CommandCategories } from './baseCommand';
22

3-
import config from 'config';
43
import { Headers } from 'node-fetch';
54

6-
import fetch, { cleanHeaders, handleRes } from './fetch';
5+
import readmeAPIFetch, { cleanHeaders, handleRes } from './readmeAPIFetch';
76

87
/**
98
* Delete a document from ReadMe
@@ -25,7 +24,7 @@ export default async function deleteDoc(
2524
if (dryRun) {
2625
return Promise.resolve(`🎭 dry run! This will delete \`${slug}\`.`);
2726
}
28-
return fetch(`${config.get('host')}/api/v1/${type}/${slug}`, {
27+
return readmeAPIFetch(`/api/v1/${type}/${slug}`, {
2928
method: 'delete',
3029
headers: cleanHeaders(
3130
key,

0 commit comments

Comments
 (0)