Skip to content

Commit c8ed5cf

Browse files
committedDec 10, 2024·
refactor: more strict type checks
1 parent a400ba8 commit c8ed5cf

16 files changed

+69
-35
lines changed
 

‎src/drivers/azure-key-vault.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { createError, createRequiredError, defineDriver } from "./utils";
2-
import { SecretClient, SecretClientOptions } from "@azure/keyvault-secrets";
2+
import {
3+
SecretClient,
4+
type SecretClientOptions,
5+
} from "@azure/keyvault-secrets";
36
import { DefaultAzureCredential } from "@azure/identity";
47

58
export interface AzureKeyVaultOptions {
@@ -118,7 +121,7 @@ function encode(value: string): string {
118121
for (const key in base64Map) {
119122
encoded = encoded.replace(
120123
new RegExp(key.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&"), "g"),
121-
base64Map[key]
124+
base64Map[key]!
122125
);
123126
}
124127
return encoded;

‎src/drivers/azure-storage-table.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
TableClient,
44
AzureNamedKeyCredential,
55
AzureSASCredential,
6-
TableEntity,
6+
type TableEntity,
77
} from "@azure/data-tables";
88
import { DefaultAzureCredential } from "@azure/identity";
99

‎src/drivers/cloudflare-kv-http.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export default defineDriver<KVHTTPOptions>((opts) => {
198198
if (i % 10_000 === 0) {
199199
acc.push([]);
200200
}
201-
acc[acc.length - 1].push(key);
201+
acc[acc.length - 1]!.push(key);
202202
return acc;
203203
},
204204
[[]]

‎src/drivers/fs.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { existsSync, promises as fsp, Stats } from "node:fs";
22
import { resolve, relative, join } from "node:path";
3-
import { FSWatcher, WatchOptions as ChokidarOptions, watch } from "chokidar";
3+
import {
4+
FSWatcher,
5+
type WatchOptions as ChokidarOptions,
6+
watch,
7+
} from "chokidar";
48
import { createError, createRequiredError, defineDriver } from "./utils";
59
import {
610
readFile,

‎src/drivers/redis.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { defineDriver, joinKeys } from "./utils";
22
import Redis, {
33
Cluster,
4-
ClusterNode,
5-
ClusterOptions,
6-
RedisOptions as _RedisOptions,
4+
type ClusterNode,
5+
type ClusterOptions,
6+
type RedisOptions as _RedisOptions,
77
} from "ioredis";
88

99
export interface RedisOptions extends _RedisOptions {

‎src/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
getRequestHeader,
99
setResponseHeader,
1010
readRawBody,
11-
EventHandler,
11+
type EventHandler,
1212
H3Event,
1313
} from "h3";
1414
import type { Storage, TransactionOptions, StorageMeta } from "./types";

‎src/storage.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ export function createStorage<T extends StorageValue>(
4141
return {
4242
base,
4343
relativeKey: key.slice(base.length),
44-
driver: context.mounts[base],
44+
driver: context.mounts[base]!,
4545
};
4646
}
4747
}
4848
return {
4949
base: "",
5050
relativeKey: key,
51-
driver: context.mounts[""],
51+
driver: context.mounts[""]!,
5252
};
5353
};
5454

@@ -65,7 +65,7 @@ export function createStorage<T extends StorageValue>(
6565
? base!.slice(mountpoint.length)
6666
: undefined,
6767
mountpoint,
68-
driver: context.mounts[mountpoint],
68+
driver: context.mounts[mountpoint]!,
6969
}));
7070
};
7171

@@ -86,7 +86,7 @@ export function createStorage<T extends StorageValue>(
8686
context.watching = true;
8787
for (const mountpoint in context.mounts) {
8888
context.unwatch[mountpoint] = await watch(
89-
context.mounts[mountpoint],
89+
context.mounts[mountpoint]!,
9090
onChange,
9191
mountpoint
9292
);
@@ -98,7 +98,7 @@ export function createStorage<T extends StorageValue>(
9898
return;
9999
}
100100
for (const mountpoint in context.unwatch) {
101-
await context.unwatch[mountpoint]();
101+
await context.unwatch[mountpoint]!();
102102
}
103103
context.unwatch = {};
104104
context.watching = false;
@@ -426,11 +426,11 @@ export function createStorage<T extends StorageValue>(
426426
return;
427427
}
428428
if (context.watching && base in context.unwatch) {
429-
context.unwatch[base]();
429+
context.unwatch[base]?.();
430430
delete context.unwatch[base];
431431
}
432432
if (_dispose) {
433-
await dispose(context.mounts[base]);
433+
await dispose(context.mounts[base]!);
434434
}
435435
context.mountpoints = context.mountpoints.filter((key) => key !== base);
436436
delete context.mounts[base];

‎src/utils.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ export function normalizeKey(key?: string) {
4646
if (!key) {
4747
return "";
4848
}
49-
return key
50-
.split("?")[0]
51-
.replace(/[/\\]/g, ":")
52-
.replace(/:+/g, ":")
53-
.replace(/^:|:$/g, "");
49+
return (
50+
key
51+
.split("?")[0]
52+
?.replace(/[/\\]/g, ":")
53+
.replace(/:+/g, ":")
54+
.replace(/^:|:$/g, "") || ""
55+
);
5456
}
5557

5658
export function joinKeys(...keys: string[]) {

‎test/drivers/cloudflare-kv-binding.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { getPlatformProxy } from "wrangler";
77

88
describe("drivers: cloudflare-kv", async () => {
99
const cfProxy = await getPlatformProxy();
10-
globalThis.__env__ = cfProxy.env;
10+
(globalThis as any).__env__ = cfProxy.env;
1111
afterAll(async () => {
12-
globalThis.__env__ = undefined;
12+
(globalThis as any).__env__ = undefined;
1313
await cfProxy.dispose();
1414
});
1515
testDriver({

‎test/drivers/cloudflare-kv-http.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { afterAll, beforeAll, describe, expect, test } from "vitest";
2-
import driver, { KVHTTPOptions } from "../../src/drivers/cloudflare-kv-http";
2+
import driver, {
3+
type KVHTTPOptions,
4+
} from "../../src/drivers/cloudflare-kv-http";
35
import { testDriver } from "./utils";
46
import { rest } from "msw";
57
import { setupServer } from "msw/node";
@@ -78,7 +80,8 @@ const mockOptions: KVHTTPOptions = {
7880
};
7981

8082
// TODO: Fix msw compatibility with Node 18
81-
const isNode18 = Number.parseInt(process.version.slice(1).split(".")[0]) >= 18;
83+
const isNode18 =
84+
Number.parseInt(process.version.slice(1).split(".")[0] || "") >= 18;
8285
describe.skipIf(isNode18)("drivers: cloudflare-kv-http", () => {
8386
beforeAll(() => {
8487
// Establish requests interception layer before all tests.

‎test/drivers/cloudflare-r2-binding.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { getPlatformProxy } from "wrangler";
77

88
describe("drivers: cloudflare-r2-binding", async () => {
99
const cfProxy = await getPlatformProxy();
10-
globalThis.__env__ = cfProxy.env;
10+
(globalThis as any).__env__ = cfProxy.env;
1111
afterAll(async () => {
12-
globalThis.__env__ = undefined;
12+
(globalThis as any).__env__ = undefined;
1313
await cfProxy.dispose();
1414
});
1515

‎test/drivers/lru-cache.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { testDriver } from "./utils";
44

55
describe("drivers: lru-cache", () => {
66
testDriver({
7-
driver: driver(),
7+
driver: driver({}),
88
});
99
});
1010

‎test/drivers/redis.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe("drivers: redis", () => {
4242
});
4343

4444
it("exposes instance", () => {
45-
expect(driver.getInstance()).toBeInstanceOf(ioredis.default);
45+
expect(driver.getInstance?.()).toBeInstanceOf(ioredis.default);
4646
});
4747
},
4848
});

‎test/drivers/utils.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { it, expect } from "vitest";
2-
import { Storage, Driver, createStorage, restoreSnapshot } from "../../src";
2+
import {
3+
type Storage,
4+
type Driver,
5+
createStorage,
6+
restoreSnapshot,
7+
} from "../../src";
38

49
export interface TestContext {
510
storage: Storage;

‎tsconfig.json

+13-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
{
22
"compilerOptions": {
33
"target": "ESNext",
4-
"module": "ESNext",
5-
"moduleResolution": "Node",
4+
"module": "preserve",
5+
"moduleDetection": "force",
66
"esModuleInterop": true,
7+
"allowSyntheticDefaultImports": true,
8+
"allowJs": true,
9+
"resolveJsonModule": true,
710
"strict": true,
8-
"skipLibCheck": true
9-
},
10-
"include": ["src"]
11+
"isolatedModules": true,
12+
"verbatimModuleSyntax": true,
13+
"noUncheckedIndexedAccess": true,
14+
"forceConsistentCasingInFileNames": true,
15+
"noImplicitOverride": true,
16+
"noEmit": true,
17+
"allowImportingTsExtensions": true,
18+
}
1119
}

‎vite.config.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
typecheck: {
6+
enabled: true,
7+
},
8+
},
9+
});

0 commit comments

Comments
 (0)
Please sign in to comment.