Skip to content

refactor: convert remaining JavaScript test files to TypeScript #7199

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

Merged
merged 2 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 52 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
"@sindresorhus/slugify": "2.2.1",
"@tsconfig/node18": "^18.2.4",
"@tsconfig/recommended": "^1.0.8",
"@types/backoff": "^2.5.5",
"@types/content-type": "1.1.8",
"@types/debug": "4.1.12",
"@types/envinfo": "7.8.4",
Expand Down Expand Up @@ -206,6 +207,7 @@
"strip-ansi": "7.1.0",
"temp-dir": "3.0.0",
"tree-kill": "1.2.2",
"tsx": "^4.19.3",
"typescript": "5.8.3",
"typescript-eslint": "^8.26.0",
"verdaccio": "6.1.2",
Expand Down
1 change: 1 addition & 0 deletions src/commands/env/env-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const envGet = async (name: string, options: OptionValues, command: BaseC
const { siteInfo } = cachedConfig
const env = await getEnvelopeEnv({ api, context, env: cachedConfig.env, key: name, scope, siteInfo })

// @ts-expect-error FIXME(ndhoule)
const { value } = env[name] || {}

// Return json response for piping commands
Expand Down
26 changes: 10 additions & 16 deletions src/lib/completion/get-autocompletion.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
/**
* @typedef CompletionItem
* @type import('tabtab').CompletionItem
*/
/**
*
* @param {import('tabtab').TabtabEnv} env
* @param {Record<string, CompletionItem & {options: CompletionItem[]}>} program
* @returns {CompletionItem[]|void}
*/
// @ts-expect-error TS(7006) FIXME: Parameter 'env' implicitly has an 'any' type.
const getAutocompletion = function (env, program) {
import type { CompletionItem } from '@pnpm/tabtab'

const getAutocompletion = function (
env: { complete: boolean; lastPartial: string; line: string; words: number },
program: Record<
string,
CompletionItem & { description?: string | undefined; name?: string | undefined; options: CompletionItem[] }
>,
): CompletionItem[] | undefined {
if (!env.complete) {
return
}
// means that we are currently in the first command (the root command)
if (env.words === 1) {
// @ts-expect-error TS(2345) FIXME: Argument of type '({ description, name }: { descri... Remove this comment to see the full error message
const rootCommands = Object.values(program).map(({ description, name }) => ({ name, description }))

// suggest all commands
Expand All @@ -26,20 +22,18 @@ const getAutocompletion = function (env, program) {

// $ netlify add<cursor>
// we can now check if a command starts with the last partial
// @ts-expect-error TS(2769) FIXME: No overload matches this call.
const autocomplete = rootCommands.filter(({ name }) => name.startsWith(env.lastPartial))
return autocomplete
}

const [, command, ...args] = env.line.split(' ')

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (program[command]) {
const usedArgs = new Set(args)
// @ts-expect-error TS(7031) FIXME: Binding element 'name' implicitly has an 'any' typ... Remove this comment to see the full error message
const unusedOptions = program[command].options.filter(({ name }) => !usedArgs.has(name))

if (env.lastPartial.length !== 0) {
// @ts-expect-error TS(7031) FIXME: Binding element 'name' implicitly has an 'any' typ... Remove this comment to see the full error message
return unusedOptions.filter(({ name }) => name.startsWith(env.lastPartial))
}

Expand Down
9 changes: 6 additions & 3 deletions src/lib/edge-functions/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { BlobsContextWithEdgeAccess } from '../blobs/blobs.js'
import { getGeoLocation } from '../geo-location.js'
import { getPathInProject } from '../settings.js'
import { type Spinner, startSpinner, stopSpinner } from '../spinner.js'
import type { CLIState, ServerSettings, SiteInfo } from '../../utils/types.js'
import type { CLIState, ServerSettings } from '../../utils/types.js'

import { getBootstrapURL } from './bootstrap.js'
import { DIST_IMPORT_MAP_PATH, EDGE_FUNCTIONS_SERVE_FOLDER } from './consts.js'
Expand Down Expand Up @@ -51,13 +51,16 @@ const getDownloadUpdateFunctions = () => {
}
}

export const handleProxyRequest = (req: ExtendedIncomingMessage, proxyReq: ClientRequest) => {
export const handleProxyRequest = (req: ExtendedIncomingMessage, proxyReq: ClientRequest): void => {
Object.entries(req[headersSymbol]).forEach(([header, value]) => {
proxyReq.setHeader(header, value)
})
}

export const createSiteInfoHeader = (siteInfo: SiteInfo, localURL: string) => {
export const createSiteInfoHeader = (
siteInfo: { id?: string | undefined; name?: string | undefined; url?: string | undefined },
localURL?: string,
): string => {
const { id, name, url } = siteInfo
const site = { id, name, url: localURL ?? url }
const siteString = JSON.stringify(site)
Expand Down
68 changes: 37 additions & 31 deletions src/lib/geo-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@ const CACHE_TTL = 8.64e7
// 10 seconds
const REQUEST_TIMEOUT = 1e4

/**
* @typedef GeoLocation
* @type {object}
* @property {string} city
* @property {object} country
* @property {string} country.code
* @property {string} country.name
* @property {object} subdivision
* @property {string} subdivision.code
* @property {string} subdivision.name
* @property {number} longitude
* @property {number} latitude
* @property {string} timezone
*/
export const mockLocation = {
export type Geolocation = {
city: string
country: {
code: string
name: string
}
subdivision: {
code: string
name: string
}
longitude: number
latitude: number
timezone: string
}

interface State {
get(key: string): unknown
set(key: string, value: unknown): void
}

export const mockLocation: Geolocation = {
city: 'San Francisco',
country: { code: 'US', name: 'United States' },
subdivision: { code: 'CA', name: 'California' },
Expand All @@ -32,19 +38,21 @@ export const mockLocation = {
}

/**
* Returns geolocation data from a remote API, the local cache, or a mock
* location, depending on the mode selected.
*
* @param {object} params
* @param {"cache"|"update"|"mock"} params.mode
* @param {string} params.geoCountry
* @param {boolean} params.offline
* @param {import('../utils/cli-state.js').default} params.state
* @returns {Promise<GeoLocation>}
* Returns geolocation data from a remote API, the local cache, or a mock location, depending on the
* specified mode.
*/
// @ts-expect-error TS(7031) FIXME: Binding element 'geoCountry' implicitly has an 'an... Remove this comment to see the full error message
export const getGeoLocation = async ({ geoCountry, mode, offline, state }) => {
const cacheObject = state.get(STATE_GEO_PROPERTY)
export const getGeoLocation = async ({
geoCountry,
mode,
offline = false,
state,
}: {
mode: 'cache' | 'update' | 'mock'
geoCountry?: string | undefined
offline?: boolean | undefined
state: State
}): Promise<Geolocation> => {
const cacheObject = state.get(STATE_GEO_PROPERTY) as { data: Geolocation; timestamp: number } | undefined

// If `--country` was used, we also set `--mode=mock`.
if (geoCountry) {
Expand Down Expand Up @@ -103,11 +111,9 @@ export const getGeoLocation = async ({ geoCountry, mode, offline, state }) => {
}

/**
* Returns geolocation data from a remote API
*
* @returns {Promise<GeoLocation>}
* Returns geolocation data from a remote API.
*/
const getGeoLocationFromAPI = async () => {
const getGeoLocationFromAPI = async (): Promise<Geolocation> => {
const res = await fetch(API_URL, {
method: 'GET',
signal: AbortSignal.timeout(REQUEST_TIMEOUT),
Expand Down
19 changes: 17 additions & 2 deletions src/lib/http-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,23 @@ const DEFAULT_HTTPS_PORT = 443
// 50 seconds
const AGENT_PORT_TIMEOUT = 50

// @ts-expect-error TS(7031) FIXME: Binding element 'certificateFile' implicitly has a... Remove this comment to see the full error message
export const tryGetAgent = async ({ certificateFile, httpProxy }) => {
export const tryGetAgent = async ({
certificateFile,
httpProxy,
}: {
httpProxy?: string | undefined
certificateFile?: string | undefined
}): Promise<
| {
error?: string | undefined
warning?: string | undefined
message?: string | undefined
}
| {
agent: HttpsProxyAgentWithCA
response: unknown
}
> => {
if (!httpProxy) {
return {}
}
Expand Down
5 changes: 0 additions & 5 deletions src/utils/copy-template-dir/copy-template-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import assert from 'assert'
import fs from 'fs'
import path from 'path'
import { pipeline } from 'stream'
Expand Down Expand Up @@ -53,10 +52,6 @@ async function writeFile(outDir: string, vars: Record<string, string>, file: Ent

// High throughput template dir writes
export async function copyTemplateDir(srcDir: string, outDir: string, vars: Record<string, string>): Promise<string[]> {
assert.strictEqual(typeof srcDir, 'string')
assert.strictEqual(typeof outDir, 'string')
assert.strictEqual(typeof vars, 'object')

await fs.promises.mkdir(outDir, { recursive: true })

const rs: ReaddirpStream = readdirp(srcDir)
Expand Down
16 changes: 10 additions & 6 deletions src/utils/deploy/hash-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import { pipeline } from 'stream/promises'
import walker from 'folder-walker'

import { fileFilterCtor, fileNormalizerCtor, hasherCtor, manifestCollectorCtor } from './hasher-segments.js'
import { $TSFixMe } from '../../commands/types.js'

const hashFiles = async ({
assetType = 'file',
// @ts-expect-error TS(7031) FIXME: Binding element 'concurrentHash' implicitly has an... Remove this comment to see the full error message
concurrentHash,
// @ts-expect-error TS(7031) FIXME: Binding element 'directories' implicitly has an 'a... Remove this comment to see the full error message
directories,
// @ts-expect-error TS(7031) FIXME: Binding element 'filter' implicitly has an 'any' t... Remove this comment to see the full error message
filter,
hashAlgorithm = 'sha1',
// @ts-expect-error TS(7031) FIXME: Binding element 'normalizer' implicitly has an 'an... Remove this comment to see the full error message
normalizer,
// @ts-expect-error TS(7031) FIXME: Binding element 'statusCb' implicitly has an 'any'... Remove this comment to see the full error message
statusCb,
}) => {
}: {
assetType?: string | undefined
concurrentHash: $TSFixMe
directories: $TSFixMe
filter: $TSFixMe
hashAlgorithm?: string | undefined
normalizer?: $TSFixMe
statusCb: $TSFixMe
}): Promise<{ files: Record<string, string>; filesShaMap: Record<string, $TSFixMe[]> }> => {
if (!filter) throw new Error('Missing filter function option')

const fileStream = walker(directories, { filter })
Expand Down
Loading
Loading