Skip to content

Commit 983fe57

Browse files
Refactor miscellaneous source files to pass strict-null checks
1 parent 65f9904 commit 983fe57

File tree

7 files changed

+53
-29
lines changed

7 files changed

+53
-29
lines changed

.changeset/heavy-days-cheat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Refactor miscellaneous source files to pass strict-null checks

packages/wrangler/src/__tests__/kv.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,16 @@ describe("wrangler", () => {
335335
expect(namespaceId).toEqual(expectedNamespaceId);
336336
expect(key).toEqual(expectedKey);
337337
expect(body).toEqual(expectedValue);
338-
expect(query.get("expiration")).toEqual(`${expiration}`);
339-
expect(query.get("expiration_ttl")).toEqual(`${expirationTtl}`);
338+
if (expiration !== undefined) {
339+
expect(query.get("expiration")).toEqual(`${expiration}`);
340+
} else {
341+
expect(query.has("expiration")).toBe(false);
342+
}
343+
if (expirationTtl) {
344+
expect(query.get("expiration_ttl")).toEqual(`${expirationTtl}`);
345+
} else {
346+
expect(query.has("expiration_ttl")).toBe(false);
347+
}
340348
return null;
341349
}
342350
);

packages/wrangler/src/api/form_data.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export function toMimeType(type: CfModuleType): string {
2323
}
2424
}
2525

26-
function toModule(module: CfModule, entryType?: CfModuleType): Blob {
26+
function toModule(module: CfModule, entryType: CfModuleType): Blob {
2727
const { type: moduleType, content } = module;
2828
const type = toMimeType(moduleType ?? entryType);
2929

packages/wrangler/src/cfetch/internal.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fetch from "node-fetch";
2-
import type { RequestInit, HeadersInit } from "node-fetch";
1+
import fetch, { Headers } from "node-fetch";
2+
import type { RequestInit } from "node-fetch";
33
import { getAPIToken, loginOrRefreshIfRequired } from "../user";
44

55
export const CF_API_BASE_URL =
@@ -21,7 +21,7 @@ export async function fetchInternal<ResponseType>(
2121
): Promise<ResponseType> {
2222
await requireLoggedIn();
2323
const apiToken = requireApiToken();
24-
const headers = cloneHeaders(init.headers);
24+
const headers = new Headers(init.headers);
2525
addAuthorizationHeader(headers, apiToken);
2626

2727
const queryString = queryParams ? `?${queryParams.toString()}` : "";
@@ -55,11 +55,7 @@ function requireApiToken(): string {
5555
return apiToken;
5656
}
5757

58-
function cloneHeaders(headers: HeadersInit): HeadersInit {
59-
return { ...headers };
60-
}
61-
62-
function addAuthorizationHeader(headers: HeadersInit, apiToken: string): void {
58+
function addAuthorizationHeader(headers: Headers, apiToken: string): void {
6359
if (headers["Authorization"]) {
6460
throw new Error(
6561
"The request already specifies an authorisation header - cannot add a new one."

packages/wrangler/src/kv.tsx

+18-9
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export interface NamespaceKeyInfo {
8282
export async function listNamespaceKeys(
8383
accountId: string,
8484
namespaceId: string,
85-
prefix?: string
85+
prefix = ""
8686
) {
8787
return await fetchListResult<NamespaceKeyInfo>(
8888
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/keys`,
@@ -98,15 +98,20 @@ export async function putKeyValue(
9898
value: string,
9999
args?: { expiration?: number; expiration_ttl?: number }
100100
) {
101+
let searchParams: URLSearchParams | undefined;
102+
if (args) {
103+
searchParams = new URLSearchParams();
104+
if (args.expiration) {
105+
searchParams.set("expiration", `${args.expiration}`);
106+
}
107+
if (args.expiration_ttl) {
108+
searchParams.set("expiration_ttl", `${args.expiration_ttl}`);
109+
}
110+
}
101111
return await fetchResult(
102112
`/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${key}`,
103113
{ method: "PUT", body: value },
104-
args
105-
? new URLSearchParams({
106-
expiration: args.expiration?.toString(),
107-
expiration_ttl: args.expiration_ttl?.toString(),
108-
})
109-
: undefined
114+
searchParams
110115
);
111116
}
112117

@@ -262,6 +267,10 @@ export function getNamespaceId({
262267
/**
263268
* KV namespace binding names must be valid JS identifiers.
264269
*/
265-
export function isValidNamespaceBinding(binding: string): boolean {
266-
return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(binding);
270+
export function isValidNamespaceBinding(
271+
binding: string | undefined
272+
): binding is string {
273+
return (
274+
typeof binding === "string" && /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(binding)
275+
);
267276
}

packages/wrangler/src/sites.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ export async function syncAssets(
8989
);
9090

9191
const manifest = {};
92-
const upload = [];
92+
const upload: {
93+
key: string;
94+
value: string;
95+
base64: boolean;
96+
}[] = [];
9397
// TODO: this can be more efficient by parallelising
9498
for await (const file of getFilesInFolder(dirPath)) {
9599
// TODO: "exclude:" config

packages/wrangler/src/user.tsx

+10-8
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ let initialised = false;
298298

299299
// we do this because we have some async stuff
300300
// TODO: this should just happen in the top level
301-
// abd we should fiure out how to do top level await
301+
// and we should figure out how to do top level await
302302
export async function initialise(): Promise<void> {
303-
// get refreshtoken/accesstoken from fs if exists
303+
// get refreshToken/accessToken from fs if exists
304304
try {
305305
// if CF_API_TOKEN available, use that
306306
if (process.env.CF_API_TOKEN) {
@@ -350,7 +350,9 @@ export function getAPIToken(): string {
350350
}
351351

352352
throwIfNotInitialised();
353-
return LocalState.accessToken?.value;
353+
// `throwIfNotInitialised()` ensures that the accessToken is guaranteed to be defined.
354+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
355+
return LocalState.accessToken!.value;
354356
}
355357

356358
interface AccessContext {
@@ -971,14 +973,14 @@ export async function getAccountId() {
971973
});
972974
} catch (err) {
973975
// probably offline
976+
return;
974977
}
975-
if (!response) return;
976-
let accountId: string;
977-
// @ts-expect-error need to type this response
978-
const responseJSON: {
978+
979+
let accountId: string | undefined;
980+
const responseJSON = (await response.json()) as {
979981
success: boolean;
980982
result: { id: string; account: { id: string; name: string } }[];
981-
} = await response.json();
983+
};
982984

983985
if (responseJSON.success === true) {
984986
if (responseJSON.result.length === 1) {

0 commit comments

Comments
 (0)