Skip to content

Commit b8c74ba

Browse files
committed
types: fix more misc. util types
1 parent 727e837 commit b8c74ba

File tree

12 files changed

+246
-154
lines changed

12 files changed

+246
-154
lines changed

src/commands/functions/functions-create.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ const handleAddonDidInstall = async ({ addonCreated, addonDidInstall, command, f
640640
return
641641
}
642642

643-
await injectEnvVariables({
643+
injectEnvVariables({
644644
devConfig: { ...config.dev },
645645
env: command.netlify.cachedConfig.env,
646646
site: command.netlify.site,

src/commands/types.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { NetlifyAPI } from 'netlify'
55
import type { FrameworksAPIPaths } from "../utils/frameworks-api.ts";
66
import type StateConfig from '../utils/state-config.js'
77
import type { Account } from "../utils/types.ts";
8-
import type { CachedConfig } from "../utils/build.js"
8+
import type { CachedConfig } from "../lib/build.js"
99

1010
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1111
type $TSFixMe = any;
@@ -15,7 +15,7 @@ export type NetlifySite = {
1515
configPath?: string
1616
siteId?: string
1717
get id(): string | undefined
18-
set id(id: string): void
18+
set id(id: string)
1919
}
2020

2121
type PatchedConfig = NetlifyTOML & Pick<NetlifyConfig, 'images'> & {

src/lib/functions/server.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ interface GetFunctionsServerOptions {
259259
functionsRegistry: FunctionsRegistry
260260
siteUrl: string
261261
siteInfo?: $TSFixMe
262-
accountId: string
262+
accountId?: string | undefined
263263
geoCountry: string
264264
offline: boolean
265265
state: $TSFixMe

src/utils/dev.ts

+38-45
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ import { supportsBackgroundFunctions } from '../lib/account.js'
77

88
import { NETLIFYDEVLOG, chalk, error, log, warn, APIError } from './command-helpers.js'
99
import { loadDotEnvFiles } from './dot-env.js'
10+
import type { NetlifyAPI } from 'netlify'
11+
import type { SiteInfo } from './types.js'
12+
import { CachedConfig } from '../lib/build.js'
13+
import { NetlifySite } from '../commands/types.js'
14+
import { DevConfig } from '../commands/dev/types.js'
1015

1116
// Possible sources of environment variables. For the purpose of printing log messages only. Order does not matter.
1217
const ENV_VAR_SOURCES = {
@@ -39,15 +44,13 @@ const ENV_VAR_SOURCES = {
3944
const ERROR_CALL_TO_ACTION =
4045
"Double-check your login status with 'netlify status' or contact support with details of your error."
4146

42-
// @ts-expect-error TS(7031) FIXME: Binding element 'site' implicitly has an 'any' typ... Remove this comment to see the full error message
43-
const validateSiteInfo = ({ site, siteInfo }) => {
47+
const validateSiteInfo = ({ site, siteInfo }: { site: NetlifySite; siteInfo: SiteInfo }): void => {
4448
if (isEmpty(siteInfo)) {
4549
error(`Failed retrieving site information for site ${chalk.yellow(site.id)}. ${ERROR_CALL_TO_ACTION}`)
4650
}
4751
}
4852

49-
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
50-
const getAccounts = async ({ api }) => {
53+
const getAccounts = async ({ api }: { api: NetlifyAPI }) => {
5154
try {
5255
const accounts = await api.listAccountsForUser()
5356
return accounts
@@ -56,9 +59,9 @@ const getAccounts = async ({ api }) => {
5659
}
5760
}
5861

59-
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
60-
const getAddons = async ({ api, site }) => {
62+
const getAddons = async ({ api, site }: { api: NetlifyAPI; site: NetlifySite }) => {
6163
try {
64+
// @ts-expect-error(serhalp) One of three types is incorrect here (is `site.id` optional?). Dig and fix.
6265
const addons = await api.listServiceInstancesForSite({ siteId: site.id })
6366
return addons
6467
} catch (error_) {
@@ -70,20 +73,17 @@ const getAddons = async ({ api, site }) => {
7073
}
7174
}
7275

73-
// @ts-expect-error TS(7031) FIXME: Binding element 'addons' implicitly has an 'any' t... Remove this comment to see the full error message
74-
const getAddonsInformation = ({ addons, siteInfo }) => {
76+
type Addons = Awaited<ReturnType<NetlifyAPI['listServiceInstancesForSite']>>
77+
const getAddonsInformation = ({ addons, siteInfo }: { addons: Addons; siteInfo: SiteInfo }) => {
7578
const urls = Object.fromEntries(
76-
// @ts-expect-error TS(7006) FIXME: Parameter 'addon' implicitly has an 'any' type.
7779
addons.map((addon) => [addon.service_slug, `${siteInfo.ssl_url}${addon.service_path}`]),
7880
)
79-
// @ts-expect-error TS(7006) FIXME: Parameter 'addon' implicitly has an 'any' type.
8081
const env = Object.assign({}, ...addons.map((addon) => addon.env))
8182
return { urls, env }
8283
}
8384

84-
// @ts-expect-error TS(7031) FIXME: Binding element 'accounts' implicitly has an 'any'... Remove this comment to see the full error message
85-
const getSiteAccount = ({ accounts, siteInfo }) => {
86-
// @ts-expect-error TS(7006) FIXME: Parameter 'account' implicitly has an 'any' type.
85+
type Accounts = Awaited<ReturnType<NetlifyAPI['listAccountsForUser']>>
86+
const getSiteAccount = ({ accounts, siteInfo }: { accounts: Accounts; siteInfo: SiteInfo }) => {
8787
const siteAccount = accounts.find((account) => account.slug === siteInfo.account_slug)
8888
if (!siteAccount) {
8989
warn(`Could not find account for site '${siteInfo.name}' with account slug '${siteInfo.account_slug}'`)
@@ -98,17 +98,17 @@ const SYNCHRONOUS_FUNCTION_TIMEOUT = 30
9898
// default 15 minutes for background functions
9999
const BACKGROUND_FUNCTION_TIMEOUT = 900
100100

101-
/**
102-
*
103-
* @param {object} config
104-
* @param {boolean} config.offline
105-
* @param {*} config.api
106-
* @param {*} config.site
107-
* @param {*} config.siteInfo
108-
* @returns
109-
*/
110-
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
111-
export const getSiteInformation = async ({ api, offline, site, siteInfo }) => {
101+
export const getSiteInformation = async ({
102+
api,
103+
offline,
104+
site,
105+
siteInfo,
106+
}: {
107+
api: NetlifyAPI
108+
offline: boolean
109+
site: NetlifySite
110+
siteInfo: SiteInfo
111+
}) => {
112112
if (site.id && !offline) {
113113
validateSiteInfo({ site, siteInfo })
114114
const [accounts, addons] = await Promise.all([getAccounts({ api }), getAddons({ api, site })])
@@ -142,22 +142,22 @@ export const getSiteInformation = async ({ api, offline, site, siteInfo }) => {
142142
}
143143
}
144144

145-
// @ts-expect-error TS(7006) FIXME: Parameter 'source' implicitly has an 'any' type.
146-
const getEnvSourceName = (source) => {
147-
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
148-
const { name = source, printFn = chalk.green } = ENV_VAR_SOURCES[source] || {}
145+
const getEnvSourceName = (source: string) => {
146+
const { name = source, printFn = chalk.green } = ENV_VAR_SOURCES[source] ?? {}
149147

150148
return printFn(name)
151149
}
152150

153-
/**
154-
* @param {{devConfig: any, env: Record<string, { sources: string[], value: string}>, site: any}} param0
155-
* @returns {Promise<Record<string, { sources: string[], value: string}>>}
156-
*/
157-
// @ts-expect-error TS(7031) FIXME: Binding element 'devConfig' implicitly has an 'any... Remove this comment to see the full error message
158-
export const getDotEnvVariables = async ({ devConfig, env, site }) => {
151+
export const getDotEnvVariables = async ({
152+
devConfig,
153+
env,
154+
site,
155+
}: {
156+
devConfig: DevConfig
157+
env: CachedConfig['env']
158+
site: NetlifySite
159+
}): Promise<Record<string, { sources: string[]; value: string }>> => {
159160
const dotEnvFiles = await loadDotEnvFiles({ envFiles: devConfig.envFiles, projectDir: site.root })
160-
// @ts-expect-error TS(2339) FIXME: Property 'env' does not exist on type '{ warning: ... Remove this comment to see the full error message
161161
dotEnvFiles.forEach(({ env: fileEnv, file }) => {
162162
const newSourceName = `${file} file`
163163

@@ -169,6 +169,7 @@ export const getDotEnvVariables = async ({ devConfig, env, site }) => {
169169
}
170170

171171
env[key] = {
172+
// @ts-expect-error(serhalp) Something isn't right with these types but it's a can of worms.
172173
sources,
173174
value: fileEnv[key],
174175
}
@@ -180,20 +181,14 @@ export const getDotEnvVariables = async ({ devConfig, env, site }) => {
180181

181182
/**
182183
* Takes a set of environment variables in the format provided by @netlify/config and injects them into `process.env`
183-
* @param {Record<string, { sources: string[], value: string}>} env
184-
* @return {void}
185184
*/
186-
// @ts-expect-error TS(7006) FIXME: Parameter 'env' implicitly has an 'any' type.
187-
export const injectEnvVariables = (env) => {
185+
export const injectEnvVariables = (env: Record<string, { sources: string[]; value: string }>): void => {
188186
for (const [key, variable] of Object.entries(env)) {
189187
const existsInProcess = process.env[key] !== undefined
190-
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
191188
const [usedSource, ...overriddenSources] = existsInProcess ? ['process', ...variable.sources] : variable.sources
192189
const usedSourceName = getEnvSourceName(usedSource)
193-
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
194190
const isInternal = variable.sources.includes('internal')
195191

196-
// @ts-expect-error TS(7006) FIXME: Parameter 'source' implicitly has an 'any' type.
197192
overriddenSources.forEach((source) => {
198193
const sourceName = getEnvSourceName(source)
199194

@@ -212,7 +207,6 @@ export const injectEnvVariables = (env) => {
212207
log(`${NETLIFYDEVLOG} Injected ${usedSourceName} env var: ${chalk.yellow(key)}`)
213208
}
214209

215-
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
216210
process.env[key] = variable.value
217211
}
218212
}
@@ -234,8 +228,7 @@ export const acquirePort = async ({
234228
return acquiredPort
235229
}
236230

237-
// @ts-expect-error TS(7006) FIXME: Parameter 'fn' implicitly has an 'any' type.
238-
export const processOnExit = (fn) => {
231+
export const processOnExit = (fn: (...args: unknown[]) => void) => {
239232
const signals = ['SIGINT', 'SIGTERM', 'SIGQUIT', 'SIGHUP', 'exit']
240233
signals.forEach((signal) => {
241234
process.on(signal, fn)

src/utils/dot-env.ts

+32-18
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
11
import { readFile } from 'fs/promises'
22
import path from 'path'
33

4-
import dotenv from 'dotenv'
4+
import dotenv, { type DotenvParseOutput } from 'dotenv'
55

66
import { isFileAsync } from '../lib/fs.js'
77

88
import { warn } from './command-helpers.js'
99

10-
// @ts-expect-error TS(7031) FIXME: Binding element 'envFiles' implicitly has an 'any'... Remove this comment to see the full error message
11-
export const loadDotEnvFiles = async function ({ envFiles, projectDir }) {
12-
const response = await tryLoadDotEnvFiles({ projectDir, dotenvFiles: envFiles })
10+
interface LoadedDotEnvFile {
11+
file: string
12+
env: DotenvParseOutput
13+
}
14+
15+
export const loadDotEnvFiles = async function ({
16+
envFiles,
17+
projectDir,
18+
}: {
19+
envFiles?: string[]
20+
projectDir?: string
21+
}): Promise<LoadedDotEnvFile[]> {
22+
const loadedDotEnvFiles = await tryLoadDotEnvFiles({ projectDir, dotenvFiles: envFiles })
1323

14-
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
15-
const filesWithWarning = response.filter((el) => el.warning)
16-
filesWithWarning.forEach((el) => {
17-
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
18-
warn(el.warning)
19-
})
24+
loadedDotEnvFiles
25+
.filter((el): el is { warning: string } => 'warning' in el)
26+
.forEach((el) => {
27+
warn(el.warning)
28+
})
2029

21-
// @ts-expect-error TS(2532) FIXME: Object is possibly 'undefined'.
22-
return response.filter((el) => el.file && el.env)
30+
return loadedDotEnvFiles.filter((el): el is LoadedDotEnvFile => 'file' in el && 'env' in el)
2331
}
2432

2533
// in the user configuration, the order is highest to lowest
2634
const defaultEnvFiles = ['.env.development.local', '.env.local', '.env.development', '.env']
2735

28-
// @ts-expect-error TS(7031) FIXME: Binding element 'projectDir' implicitly has an 'an... Remove this comment to see the full error message
29-
export const tryLoadDotEnvFiles = async ({ dotenvFiles = defaultEnvFiles, projectDir }) => {
36+
export const tryLoadDotEnvFiles = async ({
37+
dotenvFiles = defaultEnvFiles,
38+
projectDir,
39+
}: {
40+
dotenvFiles?: string[]
41+
projectDir?: string
42+
}): Promise<Array<LoadedDotEnvFile | { warning: string }>> => {
3043
const results = await Promise.all(
3144
dotenvFiles.map(async (file) => {
32-
const filepath = path.resolve(projectDir, file)
45+
const filepath = path.resolve(projectDir ?? '', file)
3346
try {
3447
const isFile = await isFileAsync(filepath)
3548
if (!isFile) {
3649
return
3750
}
3851
} catch (error) {
3952
return {
40-
// @ts-expect-error TS(2571) FIXME: Object is of type 'unknown'.
41-
warning: `Failed reading env variables from file: ${filepath}: ${error.message}`,
53+
warning: `Failed reading env variables from file: ${filepath}: ${
54+
error instanceof Error ? error.message : error?.toString()
55+
}`,
4256
}
4357
}
4458
const content = await readFile(filepath, 'utf-8')
@@ -48,5 +62,5 @@ export const tryLoadDotEnvFiles = async ({ dotenvFiles = defaultEnvFiles, projec
4862
)
4963

5064
// we return in order of lowest to highest priority
51-
return results.filter(Boolean).reverse()
65+
return results.filter((result): result is LoadedDotEnvFile => result != null).reverse()
5266
}

src/utils/proxy-server.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export const startProxyServer = async ({
6363
siteInfo,
6464
state,
6565
}: {
66-
accountId: string
67-
addonsUrls: $TSFixMe
66+
accountId?: string | undefined
67+
addonsUrls: Record<string, string>
6868
api?: NetlifyOptions['api']
6969
blobsContext?: BlobsContextWithEdgeAccess
7070
command: BaseCommand

0 commit comments

Comments
 (0)