Skip to content

Commit

Permalink
refactor: Fix type errors with @types/node
Browse files Browse the repository at this point in the history
See also cloudflare/workerd#1298 for more
information
  • Loading branch information
kulla committed Nov 20, 2023
1 parent ea313d2 commit f6b4fa1
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 28 deletions.
Binary file not shown.
Binary file not shown.
10 changes: 3 additions & 7 deletions __tests__/__utils__/test-environment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CfProperties } from '@cloudflare/workers-types'
import TOML from '@iarna/toml'
import fs from 'fs'
import path from 'path'
Expand Down Expand Up @@ -63,8 +64,8 @@ export abstract class TestEnvironment {

public abstract fetchRequest(request: Request): Promise<Response>

public createRequest(spec: UrlSpec, init?: RequestInit) {
return new Request(this.createUrl(spec), init)
public createRequest(spec: UrlSpec, init?: RequestInit<CfProperties>) {
return new Request<unknown, CfProperties>(this.createUrl(spec), init)
}

public createUrl({
Expand Down Expand Up @@ -179,11 +180,6 @@ class RemoteEnvironment extends TestEnvironment {
}
}

// There seem to be conflicting type definitions of `RequestInit` in `node`,
// `cloudflare-worker` and `undici-types`. With the following code the always
// use the `RequestInit` definition coresponding to the `Request` class.
type RequestInit = ConstructorParameters<typeof Request>[1]

interface Config {
env: {
[Environment in RemoteEnvironmentName]: {
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"@testing-library/jest-dom": "^6.1.4",
"@types/iarna__toml": "^2.0.5",
"@types/jest": "^29.5.8",
"@types/node": "^18.18.9",
"@typescript-eslint/eslint-plugin": "^6.11.0",
"@typescript-eslint/parser": "^6.11.0",
"cross-env": "^7.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/frontend-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function fetchBackend({
backendUrl.pathname = backendUrl.pathnameWithoutTrailingSlash
}

const response = await fetch(backendUrl.toString(), {
const response = await fetch(new Request(backendUrl.toString(), request), {
redirect: route.__typename === 'Frontend' ? route.redirect : 'manual',
})

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ async function semanticFileNames(request: Request) {

url.pathname = `${prefix}${hash}.${extension}`
}
return await fetch(url.href, { headers: request.headers })
return await fetch(url.href, request)
}
7 changes: 6 additions & 1 deletion src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ export class Url extends URL {
return Response.redirect(this.toString(), status)
}

public static fromRequest(request: Request): Url {
// This avoids type errors in the case there are differen incompatible type
// definitions for `Request` (we only need the `url` parameter of the
// `request` object anyways).
//
// See also https://github.com/cloudflare/workerd/issues/1298
public static fromRequest(request: { url: string }): Url {
return new Url(request.url)
}
}
17 changes: 0 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,6 @@ __metadata:
"@testing-library/jest-dom": ^6.1.4
"@types/iarna__toml": ^2.0.5
"@types/jest": ^29.5.8
"@types/node": ^18.18.9
"@typescript-eslint/eslint-plugin": ^6.11.0
"@typescript-eslint/parser": ^6.11.0
cross-env: ^7.0.3
Expand Down Expand Up @@ -1464,15 +1463,6 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:^18.18.9":
version: 18.18.9
resolution: "@types/node@npm:18.18.9"
dependencies:
undici-types: ~5.26.4
checksum: 629ce20357586144031cb43d601617eef45e59460dea6b1e123f708e6885664f44d54f65e5b72b2614af5b8613f3652ced832649c0b991accbc6a85139befa51
languageName: node
linkType: hard

"@types/parse-json@npm:^4.0.0":
version: 4.0.0
resolution: "@types/parse-json@npm:4.0.0"
Expand Down Expand Up @@ -7605,13 +7595,6 @@ __metadata:
languageName: node
linkType: hard

"undici-types@npm:~5.26.4":
version: 5.26.5
resolution: "undici-types@npm:5.26.5"
checksum: 3192ef6f3fd5df652f2dc1cd782b49d6ff14dc98e5dced492aa8a8c65425227da5da6aafe22523c67f035a272c599bb89cfe803c1db6311e44bed3042fc25487
languageName: node
linkType: hard

"undici@npm:^5.22.1":
version: 5.26.3
resolution: "undici@npm:5.26.3"
Expand Down

0 comments on commit f6b4fa1

Please sign in to comment.