Skip to content

Commit 4c72743

Browse files
erunionkanadgupta
andauthored
feat: minor tweaks to plugin extensibility (#1221)
## 🧰 Changes Minor updates to make plugin creation a little friendlier. * [x] Ability to supply `handleAPIRes` a type generic for the response coming out of our public API. * [x] Ability to load in and extend our base command class, which has a number of helpful utilities and error handling. --------- Co-authored-by: Kanad Gupta <git@kanad.dev>
1 parent 6ff5e41 commit 4c72743

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

src/lib/baseCommand.ts

Lines changed: 3 additions & 3 deletions
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 } from './readmeAPIFetch.js';
12+
import { handleAPIv2Res, readmeAPIv2Fetch, type ResponseBody } 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,8 +116,8 @@ 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(...args: Parameters<typeof handleAPIv2Res>) {
120-
return handleAPIv2Res.call(this, ...args);
119+
public async handleAPIRes<R extends ResponseBody = ResponseBody>(...args: Parameters<typeof handleAPIv2Res>) {
120+
return handleAPIv2Res.call(this, ...args) as Promise<R>;
121121
}
122122

123123
/**

src/lib/readmeAPIFetch.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ export interface Mappings {
5757
parentPages: Record<string, string>;
5858
}
5959

60+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
61+
export interface ResponseBody extends Record<string, any> {}
62+
6063
export const emptyMappings: Mappings = { categories: {}, parentPages: {} };
6164

6265
/**
@@ -371,7 +374,7 @@ export async function handleAPIv1Res(
371374
*
372375
* If we receive non-JSON responses, we consider them errors and throw them.
373376
*/
374-
export async function handleAPIv2Res<T extends Hook.Context = Hook.Context>(
377+
export async function handleAPIv2Res<R extends ResponseBody = ResponseBody, T extends Hook.Context = Hook.Context>(
375378
/**
376379
* `this` does not have to be a hook, it can also be a Command class.
377380
* This type ensures that `this` has the `config` and `debug` properties.
@@ -383,25 +386,23 @@ export async function handleAPIv2Res<T extends Hook.Context = Hook.Context>(
383386
* we can skip parsing the JSON body using this flag.
384387
*/
385388
skipJsonParsing = false,
386-
) {
389+
): Promise<R> {
387390
const contentType = res.headers.get('content-type') || '';
388391
const extension = mime.extension(contentType) || contentType.includes('json') ? 'json' : false;
389392
if (res.status === SUCCESS_NO_CONTENT) {
390393
// to prevent a memory leak, we should still consume the response body? even though we don't use it?
391394
// https://x.com/cramforce/status/1762142087930433999
392395
const body = await res.text();
393396
this.debug(`received status code ${res.status} from ${res.url} with no content: ${body}`);
394-
return {};
397+
return {} as R;
395398
} else if (skipJsonParsing) {
396399
// to prevent a memory leak, we should still consume the response body? even though we don't use it?
397400
// https://x.com/cramforce/status/1762142087930433999
398401
const body = await res.text();
399402
this.debug(`received status code ${res.status} from ${res.url} and not parsing JSON b/c of flag: ${body}`);
400-
return {};
403+
return {} as R;
401404
} else if (extension === 'json') {
402-
// TODO: type this better
403-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
404-
const body = (await res.json()) as any;
405+
const body = (await res.json()) as R;
405406
this.debug(`received status code ${res.status} from ${res.url} with JSON response: ${JSON.stringify(body)}`);
406407
if (!res.ok) {
407408
throw new APIv2Error(body as APIv2ErrorResponse);

src/lib/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type guidesRequestBodySchema =
1515

1616
type projectSchema =
1717
(typeof readmeAPIv2Oas)['paths']['/projects/me']['get']['responses']['200']['content']['application/json']['schema'];
18+
1819
/**
1920
* Derived from our API documentation, this is the schema for the `guides` object
2021
* as we send it to the ReadMe API.

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export type { GuidesRequestRepresentation } from './lib/types/index.js';
1+
export type { GuidesRequestRepresentation, ProjectRepresentation } from './lib/types/index.js';
22
export type { PageMetadata } from './lib/readPage.js';
33
export type { PluginHooks } from './lib/hooks/exported.js';
4+
export type { ResponseBody } from './lib/readmeAPIFetch.js';

src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
import BaseCommand from './lib/baseCommand.js';
2+
3+
export { BaseCommand };
14
export { readmeAPIv2Fetch, handleAPIv2Res } from './lib/readmeAPIFetch.js';

0 commit comments

Comments
 (0)