From 7c0cdc9796f778cd91e7109514994443b8a82cc2 Mon Sep 17 00:00:00 2001 From: Kanad Gupta Date: Tue, 19 Nov 2024 10:18:40 -0600 Subject: [PATCH] refactor(fetch): use named function, add v1 --- __tests__/lib/fetch.test.ts | 34 +++++++++++++++---------------- src/commands/categories/create.ts | 4 ++-- src/commands/openapi/index.ts | 8 ++++---- src/commands/versions/create.ts | 6 +++--- src/commands/versions/delete.ts | 4 ++-- src/commands/versions/index.ts | 4 ++-- src/commands/versions/update.ts | 6 +++--- src/lib/deleteDoc.ts | 4 ++-- src/lib/getCategories.ts | 6 +++--- src/lib/getDocs.ts | 4 ++-- src/lib/loginFlow.ts | 4 ++-- src/lib/readmeAPIFetch.ts | 2 +- src/lib/streamSpecToRegistry.ts | 4 ++-- src/lib/syncDocsPath.ts | 8 ++++---- src/lib/versionSelect.ts | 6 +++--- 15 files changed, 52 insertions(+), 52 deletions(-) diff --git a/__tests__/lib/fetch.test.ts b/__tests__/lib/fetch.test.ts index 213c63940..5cf2f7b49 100644 --- a/__tests__/lib/fetch.test.ts +++ b/__tests__/lib/fetch.test.ts @@ -3,11 +3,11 @@ import nock from 'nock'; import { describe, beforeEach, afterEach, it, expect, vi, beforeAll, type MockInstance } from 'vitest'; import pkg from '../../package.json' with { type: 'json' }; -import readmeAPIFetch, { cleanHeaders, handleRes } from '../../src/lib/readmeAPIFetch.js'; +import { cleanHeaders, handleRes, readmeAPIV1Fetch } from '../../src/lib/readmeAPIFetch.js'; import getAPIMock from '../helpers/get-api-mock.js'; import { after, before } from '../helpers/setup-gha-env.js'; -describe('#fetch()', () => { +describe('#readmeAPIV1Fetch()', () => { beforeAll(() => { nock.disableNetConnect(); }); @@ -29,7 +29,7 @@ describe('#fetch()', () => { return this.req.headers; }); - const headers = await readmeAPIFetch('/api/v1', { + const headers = await readmeAPIV1Fetch('/api/v1', { method: 'get', headers: cleanHeaders(key), }).then(handleRes); @@ -56,7 +56,7 @@ describe('#fetch()', () => { return this.req.headers; }); - const headers = await readmeAPIFetch( + const headers = await readmeAPIV1Fetch( '/api/v1', { method: 'get', @@ -81,7 +81,7 @@ describe('#fetch()', () => { return this.req.headers; }); - const headers = await readmeAPIFetch( + const headers = await readmeAPIV1Fetch( '/api/v1', { method: 'get', @@ -107,7 +107,7 @@ describe('#fetch()', () => { return this.req.headers; }); - const headers = await readmeAPIFetch( + const headers = await readmeAPIV1Fetch( '/api/v1', { method: 'get', @@ -130,7 +130,7 @@ describe('#fetch()', () => { return this.req.headers; }); - const headers = await readmeAPIFetch( + const headers = await readmeAPIV1Fetch( '/api/v1', { method: 'get', @@ -156,7 +156,7 @@ describe('#fetch()', () => { return this.req.headers; }); - const headers = await readmeAPIFetch( + const headers = await readmeAPIV1Fetch( '/api/v1', { method: 'get', @@ -181,7 +181,7 @@ describe('#fetch()', () => { return this.req.headers; }); - const headers = await readmeAPIFetch('/api/v1', { + const headers = await readmeAPIV1Fetch('/api/v1', { method: 'get', headers: cleanHeaders(key), }).then(handleRes); @@ -203,7 +203,7 @@ describe('#fetch()', () => { return this.req.headers; }); - const headers = await readmeAPIFetch('/api/v1/doesnt-need-auth').then(handleRes); + const headers = await readmeAPIV1Fetch('/api/v1/doesnt-need-auth').then(handleRes); expect(headers['user-agent']).toBe(`rdme/${pkg.version}`); expect(headers['x-readme-source']).toBe('cli'); @@ -235,7 +235,7 @@ describe('#fetch()', () => { Warning: '', }); - await readmeAPIFetch('/api/v1/some-warning'); + await readmeAPIV1Fetch('/api/v1/some-warning'); expect(console.warn).toHaveBeenCalledTimes(0); expect(getWarningCommandOutput()).toBe(''); @@ -248,7 +248,7 @@ describe('#fetch()', () => { Warning: '199 - "some error"', }); - await readmeAPIFetch('/api/v1/some-warning'); + await readmeAPIV1Fetch('/api/v1/some-warning'); expect(console.warn).toHaveBeenCalledTimes(1); expect(getWarningCommandOutput()).toBe('⚠️ ReadMe API Warning: some error'); @@ -261,7 +261,7 @@ describe('#fetch()', () => { Warning: '199 - "some error" 199 - "another error"', }); - await readmeAPIFetch('/api/v1/some-warning'); + await readmeAPIV1Fetch('/api/v1/some-warning'); expect(console.warn).toHaveBeenCalledTimes(2); expect(getWarningCommandOutput()).toBe( @@ -276,7 +276,7 @@ describe('#fetch()', () => { Warning: 'some garbage error', }); - await readmeAPIFetch('/api/v1/some-warning'); + await readmeAPIV1Fetch('/api/v1/some-warning'); expect(console.warn).toHaveBeenCalledTimes(1); expect(getWarningCommandOutput()).toBe('⚠️ ReadMe API Warning: some garbage error'); @@ -302,7 +302,7 @@ describe('#fetch()', () => { const mock = getAPIMock({}).get('/api/v1/proxy').reply(200); - await readmeAPIFetch('/api/v1/proxy'); + await readmeAPIV1Fetch('/api/v1/proxy'); mock.done(); }); @@ -314,7 +314,7 @@ describe('#fetch()', () => { const mock = getAPIMock({}).get('/api/v1/proxy').reply(200); - await readmeAPIFetch('/api/v1/proxy'); + await readmeAPIV1Fetch('/api/v1/proxy'); mock.done(); }); @@ -326,7 +326,7 @@ describe('#fetch()', () => { const mock = getAPIMock({}).get('/api/v1/proxy').reply(200); - await readmeAPIFetch('/api/v1/proxy'); + await readmeAPIV1Fetch('/api/v1/proxy'); mock.done(); }); diff --git a/src/commands/categories/create.ts b/src/commands/categories/create.ts index cdcde1eb7..798e10d33 100644 --- a/src/commands/categories/create.ts +++ b/src/commands/categories/create.ts @@ -4,7 +4,7 @@ import chalk from 'chalk'; import BaseCommand from '../../lib/baseCommand.js'; import { keyFlag, versionFlag } from '../../lib/flags.js'; import getCategories from '../../lib/getCategories.js'; -import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch.js'; +import { cleanHeaders, handleRes, readmeAPIV1Fetch } from '../../lib/readmeAPIFetch.js'; import { getProjectVersion } from '../../lib/versionSelect.js'; interface Category { @@ -57,7 +57,7 @@ export default class CategoriesCreateCommand extends BaseCommand { options.method = 'post'; spinner.start('Creating your API docs in ReadMe...'); - return readmeAPIFetch('/api/v1/api-specification', options, { + return readmeAPIV1Fetch('/api/v1/api-specification', options, { filePath: specPath, fileType: specFileType, }).then(res => { @@ -214,7 +214,7 @@ export default class OpenAPICommand extends BaseCommand { isUpdate = true; options.method = 'put'; spinner.start('Updating your API docs in ReadMe...'); - return readmeAPIFetch(`/api/v1/api-specification/${specId}`, options, { + return readmeAPIV1Fetch(`/api/v1/api-specification/${specId}`, options, { filePath: specPath, fileType: specFileType, }).then(res => { @@ -237,7 +237,7 @@ export default class OpenAPICommand extends BaseCommand { function getSpecs(url: string) { if (url) { - return readmeAPIFetch(url, { + return readmeAPIV1Fetch(url, { method: 'get', headers: cleanHeaders(key, selectedVersion), }); diff --git a/src/commands/versions/create.ts b/src/commands/versions/create.ts index 59e38a6bc..a95f50181 100644 --- a/src/commands/versions/create.ts +++ b/src/commands/versions/create.ts @@ -9,7 +9,7 @@ import castStringOptToBool from '../../lib/castStringOptToBool.js'; import { baseVersionFlags, keyFlag } from '../../lib/flags.js'; import * as promptHandler from '../../lib/prompts.js'; import promptTerminal from '../../lib/promptWrapper.js'; -import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch.js'; +import { cleanHeaders, handleRes, readmeAPIV1Fetch } from '../../lib/readmeAPIFetch.js'; export default class CreateVersionCommand extends BaseCommand { static description = 'Create a new version for your project.'; @@ -42,7 +42,7 @@ export default class CreateVersionCommand extends BaseCommand { @@ -26,7 +26,7 @@ export default class DeleteVersionCommand extends BaseCommand const uri = version ? `/api/v1/version/${version}` : '/api/v1/version'; - return readmeAPIFetch(uri, { + return readmeAPIV1Fetch(uri, { method: 'get', headers: cleanHeaders(key), }) diff --git a/src/commands/versions/update.ts b/src/commands/versions/update.ts index 145fd7ebc..bc78be8b5 100644 --- a/src/commands/versions/update.ts +++ b/src/commands/versions/update.ts @@ -8,7 +8,7 @@ import castStringOptToBool from '../../lib/castStringOptToBool.js'; import { baseVersionFlags, keyFlag } from '../../lib/flags.js'; import * as promptHandler from '../../lib/prompts.js'; import promptTerminal from '../../lib/promptWrapper.js'; -import readmeAPIFetch, { cleanHeaders, handleRes } from '../../lib/readmeAPIFetch.js'; +import { cleanHeaders, handleRes, readmeAPIV1Fetch } from '../../lib/readmeAPIFetch.js'; import { getProjectVersion } from '../../lib/versionSelect.js'; export default class UpdateVersionCommand extends BaseCommand { @@ -34,7 +34,7 @@ export default class UpdateVersionCommand extends BaseCommand { - return readmeAPIFetch(`/api/v1/categories?perPage=20&page=${page}`, { + return readmeAPIV1Fetch(`/api/v1/categories?perPage=20&page=${page}`, { method: 'get', headers: cleanHeaders(key, selectedVersion, new Headers({ Accept: 'application/json' })), }).then(handleRes); diff --git a/src/lib/getDocs.ts b/src/lib/getDocs.ts index d74f2d569..b675bfa17 100644 --- a/src/lib/getDocs.ts +++ b/src/lib/getDocs.ts @@ -1,5 +1,5 @@ import getCategories from './getCategories.js'; -import readmeAPIFetch, { cleanHeaders, handleRes } from './readmeAPIFetch.js'; +import { cleanHeaders, handleRes, readmeAPIV1Fetch } from './readmeAPIFetch.js'; interface Document { _id: string; @@ -34,7 +34,7 @@ async function getCategoryDocs( selectedVersion: string | undefined, category: string, ): Promise { - return readmeAPIFetch(`/api/v1/categories/${category}/docs`, { + return readmeAPIV1Fetch(`/api/v1/categories/${category}/docs`, { method: 'get', headers: cleanHeaders(key, selectedVersion, new Headers({ 'Content-Type': 'application/json' })), }).then(handleRes); diff --git a/src/lib/loginFlow.ts b/src/lib/loginFlow.ts index 2eca60dfa..091cd0d26 100644 --- a/src/lib/loginFlow.ts +++ b/src/lib/loginFlow.ts @@ -5,7 +5,7 @@ import configStore from './configstore.js'; import getCurrentConfig from './getCurrentConfig.js'; import { debug } from './logger.js'; import promptTerminal from './promptWrapper.js'; -import readmeAPIFetch, { handleRes } from './readmeAPIFetch.js'; +import { handleRes, readmeAPIV1Fetch } from './readmeAPIFetch.js'; import { validateSubdomain } from './validatePromptInput.js'; interface LoginBody { @@ -16,7 +16,7 @@ interface LoginBody { } function loginFetch(body: LoginBody) { - return readmeAPIFetch('/api/v1/login', { + return readmeAPIV1Fetch('/api/v1/login', { method: 'post', headers: { Accept: 'application/json', 'Content-Type': 'application/json' }, body: JSON.stringify(body), diff --git a/src/lib/readmeAPIFetch.ts b/src/lib/readmeAPIFetch.ts index b41ddf662..c2b9d1751 100644 --- a/src/lib/readmeAPIFetch.ts +++ b/src/lib/readmeAPIFetch.ts @@ -128,7 +128,7 @@ function sanitizeHeaders(headers: Headers) { * @param fileOpts optional object containing information about the file being sent. * We use this to construct a full source URL for the file. */ -export default async function readmeAPIFetch( +export async function readmeAPIV1Fetch( pathname: string, options: RequestInit = { headers: new Headers() }, fileOpts: FilePathDetails = { filePath: '', fileType: false }, diff --git a/src/lib/streamSpecToRegistry.ts b/src/lib/streamSpecToRegistry.ts index 7f8525078..ba2d46ea5 100644 --- a/src/lib/streamSpecToRegistry.ts +++ b/src/lib/streamSpecToRegistry.ts @@ -4,7 +4,7 @@ import ora from 'ora'; import { file as tmpFile } from 'tmp-promise'; import { debug, oraOptions } from './logger.js'; -import readmeAPIFetch, { handleRes } from './readmeAPIFetch.js'; +import { handleRes, readmeAPIV1Fetch } from './readmeAPIFetch.js'; /** * Uploads a spec to the API registry for usage in ReadMe @@ -40,7 +40,7 @@ export default async function streamSpecToRegistry(spec: string) { method: 'POST', }; - return readmeAPIFetch('/api/v1/api-registry', options) + return readmeAPIV1Fetch('/api/v1/api-registry', options) .then(handleRes) .then(body => { spinner.stop(); diff --git a/src/lib/syncDocsPath.ts b/src/lib/syncDocsPath.ts index f58d22d7d..8fd7f32e8 100644 --- a/src/lib/syncDocsPath.ts +++ b/src/lib/syncDocsPath.ts @@ -12,7 +12,7 @@ import toposort from 'toposort'; import APIError from './apiError.js'; import readdirRecursive from './readdirRecursive.js'; import readDoc from './readDoc.js'; -import readmeAPIFetch, { cleanHeaders, handleRes } from './readmeAPIFetch.js'; +import { cleanHeaders, handleRes, readmeAPIV1Fetch } from './readmeAPIFetch.js'; /** API path within ReadMe to update (e.g. `docs`, `changelogs`, etc.) */ type PageType = 'changelogs' | 'custompages' | 'docs'; @@ -65,7 +65,7 @@ async function pushDoc( } return ( - readmeAPIFetch( + readmeAPIV1Fetch( `/api/v1/${type}`, { method: 'post', @@ -96,7 +96,7 @@ async function pushDoc( )}`; } - return readmeAPIFetch( + return readmeAPIV1Fetch( `/api/v1/${type}/${slug}`, { method: 'put', @@ -109,7 +109,7 @@ async function pushDoc( .then(res => `✏️ successfully updated '${res.slug}' with contents from ${filePath}`); } - return readmeAPIFetch(`/api/v1/${type}/${slug}`, { + return readmeAPIV1Fetch(`/api/v1/${type}/${slug}`, { method: 'get', headers: cleanHeaders(key, selectedVersion, new Headers({ Accept: 'application/json' })), }) diff --git a/src/lib/versionSelect.ts b/src/lib/versionSelect.ts index 063bc5490..5ee79cb9f 100644 --- a/src/lib/versionSelect.ts +++ b/src/lib/versionSelect.ts @@ -5,7 +5,7 @@ import APIError from './apiError.js'; import isCI from './isCI.js'; import { warn } from './logger.js'; import promptTerminal from './promptWrapper.js'; -import readmeAPIFetch, { cleanHeaders, handleRes } from './readmeAPIFetch.js'; +import { cleanHeaders, handleRes, readmeAPIV1Fetch } from './readmeAPIFetch.js'; /** * Validates and returns a project version. @@ -21,7 +21,7 @@ export async function getProjectVersion( ): Promise { try { if (versionFlag) { - return await readmeAPIFetch(`/api/v1/version/${versionFlag}`, { + return await readmeAPIV1Fetch(`/api/v1/version/${versionFlag}`, { method: 'get', headers: cleanHeaders(key), }) @@ -34,7 +34,7 @@ export async function getProjectVersion( return undefined; } - const versionList: Version[] = await readmeAPIFetch('/api/v1/version', { + const versionList: Version[] = await readmeAPIV1Fetch('/api/v1/version', { method: 'get', headers: cleanHeaders(key), }).then(handleRes);