Skip to content

Commit 10c2346

Browse files
committed
Enable strict mode
1 parent aab8b46 commit 10c2346

8 files changed

Lines changed: 19 additions & 19 deletions

File tree

src/routes/library.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ describe('/libraries/:library', () => {
348348
describe('Assets array', () => {
349349
it('has \'version\', \'files\', \'rawFiles\' and \'sri\' properties for each entry', async () => {
350350
const body = await response.json<LibraryResponse>();
351-
for (const result of body.assets) {
351+
for (const result of body.assets!) {
352352
expect(result).to.have.property('version').that.is.a('string');
353353
expect(result).to.have.property('files').that.is.an('array');
354354
expect(result).to.have.property('rawFiles').that.is.an('array');

src/routes/library.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const extensions = Object.keys(files);
1717
*
1818
* @param file Filename to check.
1919
*/
20-
const whitelisted = (file: string) => extensions.includes(file.split('.').slice(-1)[0]);
20+
const whitelisted = (file: string) => extensions.includes(file.split('.').slice(-1)[0] || '');
2121

2222
/**
2323
* Handle GET /libraries/:library/:version requests.
@@ -81,15 +81,16 @@ const handleGetLibrary = async (ctx: Context) => {
8181

8282
// Generate the initial filtered response (without SRI, versions or assets data)
8383
const requestedFields = queryCheck(ctx.req.queries('fields'));
84+
const { name, ...rest } = lib;
8485
const response: LibraryResponse = filter(
8586
{
8687
// Ensure name is first prop
87-
name: lib.name,
88+
name,
8889
// Custom latest prop (and SRI value)
8990
latest: lib.filename && lib.version ? 'https://cdnjs.cloudflare.com/ajax/libs/' + lib.name + '/' + lib.version + '/' + lib.filename : null,
9091
sri: null,
9192
// All other lib props
92-
...lib,
93+
...rest,
9394
},
9495
requestedFields,
9596
);
@@ -122,7 +123,7 @@ const handleGetLibrary = async (ctx: Context) => {
122123
if (requestedFields('sri')) {
123124
if (lib.filename && lib.version) {
124125
// Handle if we've already fetched SRI
125-
if ('assets' in response) {
126+
if (response.assets) {
126127
const latestVersion = response.assets.find(entry => entry.version === lib.version);
127128
if (latestVersion) {
128129
if (lib.filename in latestVersion.sri) {

src/routes/whitelist.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,22 @@ describe('/whitelist', () => {
3333
describe('Extensions array', () => {
3434
it('only has string elements', async () => {
3535
const body = await response.json<WhitelistResponse>();
36-
for (const result of body.extensions) {
36+
for (const result of body.extensions!) {
3737
expect(result).to.be.a('string');
3838
}
3939
});
4040
});
4141
describe('Categories object', () => {
4242
it('has a key for each value in \'extensions\'', async () => {
4343
const body = await response.json<WhitelistResponse>();
44-
const keys = Object.keys(body.categories);
45-
for (const result of body.extensions) {
44+
const keys = Object.keys(body.categories!);
45+
for (const result of body.extensions!) {
4646
expect(keys).to.include(result);
4747
}
4848
});
4949
it('has a string value for each key', async () => {
5050
const body = await response.json<WhitelistResponse>();
51-
for (const result of Object.values(body.categories)) {
51+
for (const result of Object.values(body.categories!)) {
5252
expect(result).to.be.a('string');
5353
}
5454
});

src/utils/fetchJson.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,21 @@ class FetchError extends Error {
2727
*/
2828
export default async (url: RequestInfo, options: RequestInit = {}, timeout: number = 30) => {
2929
// Create the timeout
30-
const hasTimeout = typeof timeout === 'number' && timeout > 0;
31-
const controller = hasTimeout && new AbortController();
32-
const timer = hasTimeout && setTimeout(() => controller.abort(), timeout * 1000);
30+
const controller = typeof timeout === 'number' && timeout > 0 && new AbortController();
31+
const timer = controller && setTimeout(() => controller.abort(), timeout * 1000);
3332

3433
// Run the request
3534
let resp: Response;
3635
try {
3736
resp = await fetch(url, {
3837
...options,
39-
...(hasTimeout
38+
...(controller
4039
? { signal: controller.signal }
4140
: {}),
4241
});
4342
} catch (error) {
4443
// If the request was aborted, throw a nice error
45-
if (error.name === 'AbortError') {
44+
if (error instanceof Error && error.name === 'AbortError') {
4645
throw new FetchError(`Timed out after ${timeout}s`, {
4746
method: options?.method || 'GET',
4847
url,
@@ -54,7 +53,7 @@ export default async (url: RequestInfo, options: RequestInit = {}, timeout: numb
5453
throw error;
5554
} finally {
5655
// Clear the timeout
57-
if (hasTimeout) clearTimeout(timer);
56+
if (timer) clearTimeout(timer);
5857
}
5958

6059
// Handle failures

src/utils/sort.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('utils/sort', () => {
4848
});
4949

5050
it('handles empty arrays', () => {
51-
const versions = [];
51+
const versions: string[] = [];
5252
const sorted = sortVersions([ ...versions ]);
5353
expect(sorted).toEqual([]);
5454
});

src/utils/sriForVersion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default (library: string, version: string, files: string[], sriData: Reco
1515
const fullFile = `${library}/${version}/${file}`;
1616

1717
// If we have an SRI entry for this, add it
18-
if (sriData && fullFile in sriData) {
18+
if (sriData && sriData[fullFile]) {
1919
sri[file] = sriData[fullFile];
2020
continue;
2121
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"verbatimModuleSyntax": true,
1111
"esModuleInterop": true,
1212
"noEmit": true,
13-
"strict": false,
13+
"strict": true,
1414
"skipLibCheck": true,
1515
"types": ["@cloudflare/vitest-pool-workers"],
1616
},

vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default defineWorkersConfig({
66
reporters: [
77
'verbose',
88
process.env.GITHUB_ACTIONS && 'github-actions',
9-
].filter(Boolean),
9+
].filter((x): x is string => !!x),
1010
poolOptions: {
1111
workers: {
1212
wrangler: { configPath: './wrangler.toml' },

0 commit comments

Comments
 (0)