Skip to content

Commit

Permalink
refactor: enable TS strict mode (#889)
Browse files Browse the repository at this point in the history
* chore: let's go baby

* refactor: add new AuthenticatedCommandOptions type

* chore: bring AuthenticatedCommandOptions into createGHA file

* refactor: version parameter in `cleanHeaders`

* chore: other little typefixes in fetch file

* chore: more little TS fixes

* chore: more straightforward type fixes

* fix: various version typings

* chore: improved typing on oas prompt

* fix: enable another tsconfig flag

* chore: add overdue stricter type

i don't feel great about this but progress is progress lol

* refactor: backfill some types so we can re-enable lib check

* chore: temporarily disable strict mode to see if tests pass

* chore: remove some unnecessary overrides

* revert: actually bring back skipLibCheck

* fix: do not fallback to json

* test: fix test

* revert: re-enable strictness

* chore: stricter option typing

* refactor: remove function nesting

this diff is kinda crazy but no actual changes here. an external contributor just did a bunch of unnecessary function nesting and TS didn't like that.

* chore: fix typing in analyzer

* refactor: clearer type names

* chore: pr feedback

Co-Authored-By: Jon Ursenbach <[email protected]>

* fix: lint

---------

Co-authored-by: Jon Ursenbach <[email protected]>
  • Loading branch information
kanadgupta and erunion authored Sep 14, 2023
1 parent cf3b785 commit 0007f4f
Show file tree
Hide file tree
Showing 46 changed files with 244 additions and 276 deletions.
2 changes: 1 addition & 1 deletion __tests__/cmds/docs/edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('rdme docs:edit', () => {
fs.appendFile(filename, edits, cb.bind(null, 0));
}

await expect(docsEdit.run({ slug, key, version: '1.0.0', mockEditor })).resolves.toBeUndefined();
await expect(docsEdit.run({ slug, key, version: '1.0.0', mockEditor })).resolves.toBe('');

getMock.done();
putMock.done();
Expand Down
16 changes: 7 additions & 9 deletions __tests__/lib/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/ban-ts-comment, no-console */
/* eslint-disable no-console */
import { Headers } from 'node-fetch';
import { describe, beforeEach, afterEach, it, expect, vi } from 'vitest';

Expand Down Expand Up @@ -328,19 +328,17 @@ describe('#cleanHeaders()', () => {
});

it('should filter out undefined headers', () => {
expect(
// @ts-ignore Testing a quirk of `node-fetch`.
Array.from(cleanHeaders('test', new Headers({ 'x-readme-version': undefined }))),
).toStrictEqual([['authorization', 'Basic dGVzdDo=']]);
expect(Array.from(cleanHeaders('test', undefined, new Headers({ 'x-something': undefined })))).toStrictEqual([
['authorization', 'Basic dGVzdDo='],
]);
});

it('should filter out null headers', () => {
expect(
// @ts-ignore Testing a quirk of `node-fetch`.
Array.from(cleanHeaders('test', new Headers({ 'x-readme-version': '1234', Accept: null }))),
Array.from(cleanHeaders('test', undefined, new Headers({ 'x-something': '1234', Accept: null }))),
).toStrictEqual([
['authorization', 'Basic dGVzdDo='],
['x-readme-version', '1234'],
['x-something', '1234'],
]);
});

Expand All @@ -351,7 +349,7 @@ describe('#cleanHeaders()', () => {
'Content-Type': 'application/json',
});

expect(Array.from(cleanHeaders('test', headers))).toStrictEqual([
expect(Array.from(cleanHeaders('test', undefined, headers))).toStrictEqual([
['authorization', 'Basic dGVzdDo='],
['accept', 'text/plain'],
['content-type', 'application/json'],
Expand Down
56 changes: 22 additions & 34 deletions src/cmds/categories/create.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../../lib/baseCommand';

import chalk from 'chalk';
import { Headers } from 'node-fetch';
Expand Down Expand Up @@ -52,7 +52,7 @@ export default class CategoriesCreateCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: AuthenticatedCommandOptions<Options>) {
await super.run(opts);

const { categoryType, title, key, version, preventDuplicates } = opts;
Expand All @@ -69,45 +69,33 @@ export default class CategoriesCreateCommand extends Command {

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

async function matchCategory() {
if (preventDuplicates) {
const allCategories = await getCategories(key, selectedVersion);

return allCategories.find((category: Category) => {
const matchedCategory = allCategories.find((category: Category) => {
return category.title.trim().toLowerCase() === title.trim().toLowerCase() && category.type === categoryType;
});
}

async function createCategory() {
if (preventDuplicates) {
const matchedCategory = await matchCategory();
if (typeof matchedCategory !== 'undefined') {
return Promise.reject(
new Error(
`The '${matchedCategory.title}' category with a type of '${matchedCategory.type}' already exists with an id of '${matchedCategory.id}'. A new category was not created.`,
),
);
}
if (typeof matchedCategory !== 'undefined') {
return Promise.reject(
new Error(
`The '${matchedCategory.title}' category with a type of '${matchedCategory.type}' already exists with an id of '${matchedCategory.id}'. A new category was not created.`,
),
);
}
return readmeAPIFetch('/api/v1/categories', {
method: 'post',
headers: cleanHeaders(
key,
new Headers({
'x-readme-version': selectedVersion,
'Content-Type': 'application/json',
}),
),
body: JSON.stringify({
title,
type: categoryType,
}),
})
.then(handleRes)
.then(res => `🌱 successfully created '${res.title}' with a type of '${res.type}' and an id of '${res.id}'`);
}

const createdCategory = chalk.green(await createCategory());

return Promise.resolve(createdCategory);
const createdCategory = await readmeAPIFetch('/api/v1/categories', {
method: 'post',
headers: cleanHeaders(key, selectedVersion, new Headers({ 'Content-Type': 'application/json' })),
body: JSON.stringify({
title,
type: categoryType,
}),
})
.then(handleRes)
.then(res => `🌱 successfully created '${res.title}' with a type of '${res.type}' and an id of '${res.id}'`);

return Promise.resolve(chalk.green(createdCategory));
}
}
4 changes: 2 additions & 2 deletions src/cmds/categories/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../../lib/baseCommand';

import Command, { CommandCategories } from '../../lib/baseCommand';
import getCategories from '../../lib/getCategories';
Expand All @@ -16,7 +16,7 @@ export default class CategoriesCommand extends Command {
this.args = [this.getKeyArg(), this.getVersionArg()];
}

async run(opts: CommandOptions<{}>) {
async run(opts: AuthenticatedCommandOptions) {
await super.run(opts);

const { key, version } = opts;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/changelogs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../lib/baseCommand';

import Command, { CommandCategories } from '../lib/baseCommand';
import supportsGHA from '../lib/decorators/supportsGHA';
Expand Down Expand Up @@ -37,7 +37,7 @@ export default class ChangelogsCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: AuthenticatedCommandOptions<Options>) {
await super.run(opts);

const { dryRun, filePath, key } = opts;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/custompages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../lib/baseCommand';

import Command, { CommandCategories } from '../lib/baseCommand';
import supportsGHA from '../lib/decorators/supportsGHA';
Expand Down Expand Up @@ -37,7 +37,7 @@ export default class CustomPagesCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: AuthenticatedCommandOptions<Options>) {
await super.run(opts);

const { dryRun, filePath, key } = opts;
Expand Down
22 changes: 5 additions & 17 deletions src/cmds/docs/edit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../../lib/baseCommand';

import fs from 'fs';
import { promisify } from 'util';
Expand Down Expand Up @@ -45,7 +45,7 @@ export default class DocsEditCommand extends Command {
];
}

async run(opts: CommandOptions<Options>): Promise<undefined> {
async run(opts: AuthenticatedCommandOptions<Options>): Promise<string> {
Command.warn('`rdme docs:edit` is now deprecated and will be removed in a future release.');
await super.run(opts);

Expand All @@ -63,13 +63,7 @@ export default class DocsEditCommand extends Command {

const existingDoc = await readmeAPIFetch(`/api/v1/docs/${slug}`, {
method: 'get',
headers: cleanHeaders(
key,
new Headers({
'x-readme-version': selectedVersion,
Accept: 'application/json',
}),
),
headers: cleanHeaders(key, selectedVersion, new Headers({ Accept: 'application/json' })),
}).then(handleRes);

await writeFile(filename, existingDoc.body);
Expand All @@ -86,13 +80,7 @@ export default class DocsEditCommand extends Command {

return readmeAPIFetch(`/api/v1/docs/${slug}`, {
method: 'put',
headers: cleanHeaders(
key,
new Headers({
'x-readme-version': selectedVersion,
'Content-Type': 'application/json',
}),
),
headers: cleanHeaders(key, selectedVersion, new Headers({ 'Content-Type': 'application/json' })),
body: JSON.stringify(
Object.assign(existingDoc, {
body: updatedDoc,
Expand All @@ -115,7 +103,7 @@ export default class DocsEditCommand extends Command {
// Normally we should resolve with a value that is logged to the console,
// but since we need to wait for the temporary file to be removed,
// it's okay to resolve the promise with no value.
return resolve(undefined);
return resolve('');
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/docs/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../../lib/baseCommand';

import Command, { CommandCategories } from '../../lib/baseCommand';
import createGHA from '../../lib/createGHA';
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class DocsCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: AuthenticatedCommandOptions<Options>) {
await super.run(opts);

const { dryRun, filePath, key, version } = opts;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/docs/prune.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../../lib/baseCommand';

import chalk from 'chalk';
import prompts from 'prompts';
Expand Down Expand Up @@ -56,7 +56,7 @@ export default class DocsPruneCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: AuthenticatedCommandOptions<Options>) {
await super.run(opts);

const { dryRun, folder, key, version } = opts;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/guides/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../../lib/baseCommand';
import type { Options } from '../docs';

import DocsCommand from '../docs';
Expand All @@ -12,7 +12,7 @@ export default class GuidesCommand extends DocsCommand {
this.description = 'Alias for `rdme docs`.';
}

async run(opts: CommandOptions<Options>) {
async run(opts: AuthenticatedCommandOptions<Options>) {
return super.run(opts);
}
}
4 changes: 2 additions & 2 deletions src/cmds/guides/prune.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { AuthenticatedCommandOptions } from '../../lib/baseCommand';
import type { Options } from '../docs/prune';

import DocsPruneCommand from '../docs/prune';
Expand All @@ -12,7 +12,7 @@ export default class GuidesPruneCommand extends DocsPruneCommand {
this.description = 'Alias for `rdme docs:prune`.';
}

async run(opts: CommandOptions<Options>) {
async run(opts: AuthenticatedCommandOptions<Options>) {
return super.run(opts);
}
}
4 changes: 2 additions & 2 deletions src/cmds/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import prompts from 'prompts';

Expand Down Expand Up @@ -45,7 +45,7 @@ export default class LoginCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

prompts.override(opts);
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/logout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import Command, { CommandCategories } from '../lib/baseCommand';
import config from '../lib/config';
Expand All @@ -16,7 +16,7 @@ export default class LogoutCommand extends Command {
this.args = [];
}

async run(opts: CommandOptions<{}>) {
async run(opts: ZeroAuthCommandOptions) {
await super.run(opts);

if (configStore.has('email') && configStore.has('project')) {
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/oas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import Command, { CommandCategories } from '../lib/baseCommand';
import isHidden from '../lib/decorators/isHidden';
Expand All @@ -16,7 +16,7 @@ export default class OASCommand extends Command {
this.args = [];
}

async run(opts: CommandOptions<{}>) {
async run(opts: ZeroAuthCommandOptions) {
await super.run(opts);

const message = [
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/open.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import chalk from 'chalk';
import open from 'open';
Expand Down Expand Up @@ -31,7 +31,7 @@ export default class OpenCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { dash } = opts;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/openapi/convert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../../lib/baseCommand';
import type { OASDocument } from 'oas/dist/rmoas.types';

import fs from 'fs';
Expand Down Expand Up @@ -43,7 +43,7 @@ export default class OpenAPIConvertCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, workingDirectory } = opts;
Expand Down
Loading

0 comments on commit 0007f4f

Please sign in to comment.