Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: enable TS strict mode #889

Merged
merged 28 commits into from
Sep 14, 2023
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bd38f4d
chore: let's go baby
kanadgupta Sep 7, 2023
45b4195
Merge branch 'next' into ts-strict-mode
kanadgupta Sep 11, 2023
d37d141
refactor: add new AuthenticatedCommandOptions type
kanadgupta Sep 11, 2023
fe7d9d1
chore: bring AuthenticatedCommandOptions into createGHA file
kanadgupta Sep 11, 2023
d22e3cc
refactor: version parameter in `cleanHeaders`
kanadgupta Sep 11, 2023
64b085a
chore: other little typefixes in fetch file
kanadgupta Sep 11, 2023
2a91415
chore: more little TS fixes
kanadgupta Sep 11, 2023
69ed44c
chore: more straightforward type fixes
kanadgupta Sep 11, 2023
a71a0bb
Merge branch 'next' into ts-strict-mode
kanadgupta Sep 13, 2023
f7a2249
fix: various version typings
kanadgupta Sep 13, 2023
d21785f
chore: improved typing on oas prompt
kanadgupta Sep 13, 2023
ea2e542
fix: enable another tsconfig flag
kanadgupta Sep 14, 2023
b38e3a9
chore: add overdue stricter type
kanadgupta Sep 14, 2023
edf1240
refactor: backfill some types so we can re-enable lib check
kanadgupta Sep 14, 2023
bc36971
chore: temporarily disable strict mode to see if tests pass
kanadgupta Sep 14, 2023
c87861e
chore: remove some unnecessary overrides
kanadgupta Sep 14, 2023
5100923
revert: actually bring back skipLibCheck
kanadgupta Sep 14, 2023
fa2f524
fix: do not fallback to json
kanadgupta Sep 14, 2023
9c549d7
Merge branch 'next' into ts-strict-mode
kanadgupta Sep 14, 2023
f181e5b
Merge branch 'next' into ts-strict-mode
kanadgupta Sep 14, 2023
3c7305f
test: fix test
kanadgupta Sep 14, 2023
f6c64ce
revert: re-enable strictness
kanadgupta Sep 14, 2023
529e96f
chore: stricter option typing
kanadgupta Sep 14, 2023
dd326bd
refactor: remove function nesting
kanadgupta Sep 14, 2023
fd1c619
chore: fix typing in analyzer
kanadgupta Sep 14, 2023
b7dfaba
refactor: clearer type names
kanadgupta Sep 14, 2023
3bac0e2
chore: pr feedback
kanadgupta Sep 14, 2023
d0a55c6
fix: lint
kanadgupta Sep 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: version parameter in cleanHeaders
  • Loading branch information
kanadgupta committed Sep 11, 2023
commit d22e3ccb93f1bfbb2727f923cba6cc6415a4bc88
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';

@@ -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'],
]);
});

@@ -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'],
8 changes: 1 addition & 7 deletions src/cmds/categories/create.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the diff on this file is a bit wild but there aren't any actual changes to the code — just some refactoring to get rid of the unnecessary nested functions because TS doesn't like those (and neither do i lol)

Original file line number Diff line number Diff line change
@@ -90,13 +90,7 @@ export default class CategoriesCreateCommand extends Command {
}
return readmeAPIFetch('/api/v1/categories', {
method: 'post',
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({
title,
type: categoryType,
16 changes: 2 additions & 14 deletions src/cmds/docs/edit.ts
Original file line number Diff line number Diff line change
@@ -64,13 +64,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);
@@ -87,13 +81,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,
14 changes: 3 additions & 11 deletions src/cmds/openapi/index.ts
Original file line number Diff line number Diff line change
@@ -222,11 +222,8 @@ export default class OpenAPICommand extends Command {
const options: RequestInit = {
headers: cleanHeaders(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the signature of our cleanHeaders function so the second parameter is a version which we use to construct our x-readme-version request header. You'll see a lot of refactors related to that in this PR.

key,
new Headers({
Accept: 'application/json',
'Content-Type': 'application/json',
'x-readme-version': selectedVersion,
}),
selectedVersion,
new Headers({ Accept: 'application/json', 'Content-Type': 'application/json' }),
),
body: JSON.stringify({ registryUUID }),
};
@@ -283,12 +280,7 @@ export default class OpenAPICommand extends Command {
function getSpecs(url: string) {
return readmeAPIFetch(url, {
method: 'get',
headers: cleanHeaders(
key,
new Headers({
'x-readme-version': selectedVersion,
}),
),
headers: cleanHeaders(key, selectedVersion),
});
}

6 changes: 2 additions & 4 deletions src/cmds/versions/create.ts
Original file line number Diff line number Diff line change
@@ -91,10 +91,8 @@ export default class CreateVersionCommand extends Command {
method: 'post',
headers: cleanHeaders(
key,
new Headers({
Accept: 'application/json',
'Content-Type': 'application/json',
}),
undefined,
new Headers({ Accept: 'application/json', 'Content-Type': 'application/json' }),
),
body: JSON.stringify(body),
})
6 changes: 2 additions & 4 deletions src/cmds/versions/update.ts
Original file line number Diff line number Diff line change
@@ -78,10 +78,8 @@ export default class UpdateVersionCommand extends Command {
method: 'put',
headers: cleanHeaders(
key,
new Headers({
Accept: 'application/json',
'Content-Type': 'application/json',
}),
undefined,
new Headers({ Accept: 'application/json', 'Content-Type': 'application/json' }),
),
body: JSON.stringify(body),
})
8 changes: 1 addition & 7 deletions src/lib/deleteDoc.ts
Original file line number Diff line number Diff line change
@@ -26,13 +26,7 @@ export default async function deleteDoc(
}
return readmeAPIFetch(`/api/v1/${type}/${slug}`, {
method: 'delete',
headers: cleanHeaders(
key,
new Headers({
'x-readme-version': selectedVersion,
'Content-Type': 'application/json',
}),
),
headers: cleanHeaders(key, selectedVersion, new Headers({ 'Content-Type': 'application/json' })),
})
.then(handleRes)
.then(() => `🗑️ successfully deleted \`${slug}\`.`);
16 changes: 2 additions & 14 deletions src/lib/getCategories.ts
Original file line number Diff line number Diff line change
@@ -14,13 +14,7 @@ export default async function getCategories(key: string, selectedVersion: string
let totalCount = 0;
return readmeAPIFetch('/api/v1/categories?perPage=20&page=1', {
method: 'get',
headers: cleanHeaders(
key,
new Headers({
'x-readme-version': selectedVersion,
Accept: 'application/json',
}),
),
headers: cleanHeaders(key, selectedVersion, new Headers({ Accept: 'application/json' })),
})
.then(res => {
totalCount = Math.ceil(parseInt(res.headers.get('x-total-count'), 10) / 20);
@@ -39,13 +33,7 @@ export default async function getCategories(key: string, selectedVersion: string
[...new Array(totalCount + 1).keys()].slice(2).map(async page => {
return readmeAPIFetch(`/api/v1/categories?perPage=20&page=${page}`, {
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);
}),
)),
8 changes: 1 addition & 7 deletions src/lib/getDocs.ts
Original file line number Diff line number Diff line change
@@ -34,13 +34,7 @@ function flatten(data: Document[][]): Document[] {
async function getCategoryDocs(key: string, selectedVersion: string, category: string): Promise<Document[]> {
return readmeAPIFetch(`/api/v1/categories/${category}/docs`, {
method: 'get',
headers: cleanHeaders(
key,
new Headers({
'x-readme-version': selectedVersion,
'Content-Type': 'application/json',
}),
),
headers: cleanHeaders(key, selectedVersion, new Headers({ 'Content-Type': 'application/json' })),
}).then(handleRes);
}

11 changes: 10 additions & 1 deletion src/lib/readmeAPIFetch.ts
Original file line number Diff line number Diff line change
@@ -242,12 +242,21 @@ async function handleRes(res: Response, rejectOnJsonError = true) {
* Returns the basic auth header and any other defined headers for use in `node-fetch` API calls.
*
*/
function cleanHeaders(key: string, inputHeaders: Headers = new Headers()) {
function cleanHeaders(
key: string,
/** used for `x-readme-header` */
version?: string,
inputHeaders: Headers = new Headers(),
) {
const encodedKey = Buffer.from(`${key}:`).toString('base64');
const headers = new Headers({
Authorization: `Basic ${encodedKey}`,
});

if (version) {
headers.set('x-readme-version', version);
}

for (const header of inputHeaders.entries()) {
// If you supply `undefined` or `null` to the `Headers` API it'll convert that those to a
// string.
24 changes: 3 additions & 21 deletions src/lib/syncDocsPath.ts
Original file line number Diff line number Diff line change
@@ -66,13 +66,7 @@ async function pushDoc(
`/api/v1/${type}`,
{
method: 'post',
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({
slug,
...payload,
@@ -103,13 +97,7 @@ async function pushDoc(
`/api/v1/${type}/${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(payload),
},
{ filePath, fileType: 'path' },
@@ -120,13 +108,7 @@ async function pushDoc(

return readmeAPIFetch(`/api/v1/${type}/${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(async res => {
const body = await handleRes(res, false);