Skip to content

Commit 75ae118

Browse files
authored
chore: smol refactors (#1149)
## 🧰 Changes - [x] forks `syncsDocsPath` logic and deprecates it (in favor of TBA) - [x] cleaner arg definitions in `baseCommand.ts` - [x] cleanups + improved response handling flexibility in `handleAPIv2Res` ## 🧬 QA & Testing does the build + tests still pass?
1 parent ebe766b commit 75ae118

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

src/commands/changelogs.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Args, Flags } from '@oclif/core';
22

33
import BaseCommand from '../lib/baseCommand.js';
44
import { githubFlag, keyFlag } from '../lib/flags.js';
5-
import syncDocsPath from '../lib/syncDocsPath.js';
5+
import syncDocsPath from '../lib/syncDocsPath.legacy.js';
66

77
export default class ChangelogsCommand extends BaseCommand<typeof ChangelogsCommand> {
88
// we need this as a const for syncDocsPath

src/lib/baseCommand.ts

+5-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import chalk from 'chalk';
99
import debugPkg from 'debug';
1010

1111
import { isGHA, isTest } from './isCI.js';
12-
import { handleAPIv2Res, readmeAPIv2Fetch, type FilePathDetails } from './readmeAPIFetch.js';
12+
import { handleAPIv2Res, readmeAPIv2Fetch } from './readmeAPIFetch.js';
1313

1414
type Flags<T extends typeof OclifCommand> = Interfaces.InferredFlags<(typeof BaseCommand)['baseFlags'] & T['flags']>;
1515
type Args<T extends typeof OclifCommand> = Interfaces.InferredArgs<T['args']>;
@@ -116,19 +116,15 @@ export default abstract class BaseCommand<T extends typeof OclifCommand> extends
116116
/**
117117
* Wrapper around `handleAPIv2Res` that binds the context of the class to the function.
118118
*/
119-
public async handleAPIRes(res: Response) {
120-
return handleAPIv2Res.call(this, res);
119+
public async handleAPIRes(...args: Parameters<typeof handleAPIv2Res>) {
120+
return handleAPIv2Res.call(this, ...args);
121121
}
122122

123123
/**
124124
* Wrapper around `readmeAPIv2Fetch` that binds the context of the class to the function.
125125
*/
126-
public async readmeAPIFetch(
127-
pathname: string,
128-
options: RequestInit = { headers: new Headers() },
129-
fileOpts: FilePathDetails = { filePath: '', fileType: false },
130-
) {
131-
return readmeAPIv2Fetch.call(this, pathname, options, fileOpts);
126+
public async readmeAPIFetch(...args: Parameters<typeof readmeAPIv2Fetch>) {
127+
return readmeAPIv2Fetch.call(this, ...args);
132128
}
133129

134130
async runCreateGHAHook(opts: CreateGHAHookOptsInClass) {

src/lib/readmeAPIFetch.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const SUCCESS_NO_CONTENT = 204;
1919
* This contains a few pieces of information about a file so
2020
* we can properly construct a source URL for it.
2121
*/
22-
export interface FilePathDetails {
22+
interface FilePathDetails {
2323
/** The URL or local file path */
2424
filePath: string;
2525
/** This is derived from the `oas-normalize` `type` property. */
@@ -348,10 +348,30 @@ export async function handleAPIv1Res(res: Response, rejectOnJsonError = true) {
348348
*
349349
* If we receive non-JSON responses, we consider them errors and throw them.
350350
*/
351-
export async function handleAPIv2Res<T extends Command>(this: T, res: Response) {
351+
export async function handleAPIv2Res<T extends Command>(
352+
this: T,
353+
res: Response,
354+
/**
355+
* If we're making a request where we don't care about the body (e.g. a HEAD or DELETE request),
356+
* we can skip parsing the JSON body using this flag.
357+
*/
358+
skipJsonParsing = false,
359+
) {
352360
const contentType = res.headers.get('content-type') || '';
353361
const extension = mime.extension(contentType) || contentType.includes('json') ? 'json' : false;
354-
if (extension === 'json') {
362+
if (res.status === SUCCESS_NO_CONTENT) {
363+
// to prevent a memory leak, we should still consume the response body? even though we don't use it?
364+
// https://x.com/cramforce/status/1762142087930433999
365+
const body = await res.text();
366+
this.debug(`received status code ${res.status} from ${res.url} with no content: ${body}`);
367+
return {};
368+
} else if (skipJsonParsing) {
369+
// to prevent a memory leak, we should still consume the response body? even though we don't use it?
370+
// https://x.com/cramforce/status/1762142087930433999
371+
const body = await res.text();
372+
this.debug(`received status code ${res.status} from ${res.url} and not parsing JSON b/c of flag: ${body}`);
373+
return {};
374+
} else if (extension === 'json') {
355375
// TODO: type this better
356376
// eslint-disable-next-line @typescript-eslint/no-explicit-any
357377
const body = (await res.json()) as any;
@@ -361,10 +381,6 @@ export async function handleAPIv2Res<T extends Command>(this: T, res: Response)
361381
}
362382
return body;
363383
}
364-
if (res.status === SUCCESS_NO_CONTENT) {
365-
this.debug(`received status code ${res.status} from ${res.url} with no content`);
366-
return {};
367-
}
368384

369385
// If we receive a non-JSON response, it's likely an error.
370386
// Let's debug the raw response body and throw it.

src/lib/syncDocsPath.ts src/lib/syncDocsPath.legacy.ts

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ function sortFiles(filePaths: string[]): ReadDocMetadata[] {
144144
* Takes a path (either to a directory of files or to a single file)
145145
* and syncs those (either via POST or PUT) to ReadMe.
146146
* @returns A promise-wrapped string with the results
147+
*
148+
* @deprecated This is for APIv1 only. Use `syncDocsPath.ts` instead, if possible.
147149
*/
148150
export default async function syncDocsPath(
149151
this: ChangelogsCommand,

0 commit comments

Comments
 (0)