Skip to content

Commit 2aa2e44

Browse files
authored
refactor: fix Account.capabilities and parseAllRemoteImages types (#7219)
* refactor: fix Account capabilities types * refactor: fix parseAllRemoteImages type errors
1 parent a95b9e5 commit 2aa2e44

File tree

7 files changed

+30
-26
lines changed

7 files changed

+30
-26
lines changed

src/lib/account.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
/**
2-
* @param {any} account
3-
* @param {string} capability
4-
* @returns {boolean}
5-
*/
6-
// @ts-expect-error TS(7006) FIXME: Parameter 'account' implicitly has an 'any' type.
7-
const supportsBooleanCapability = (account, capability) => Boolean(account?.capabilities?.[capability]?.included)
1+
import type { Account, Capability } from '../utils/dev.js'
82

9-
/**
10-
* @param {any} account
11-
* @returns {boolean}
12-
*/
13-
// @ts-expect-error TS(7006) FIXME: Parameter 'account' implicitly has an 'any' type.
14-
export const supportsBackgroundFunctions = (account) => supportsBooleanCapability(account, 'background_functions')
3+
const supportsBooleanCapability = (account: Account | undefined, capability: Capability) =>
4+
Boolean(account?.capabilities?.[capability]?.included)
5+
6+
export const supportsBackgroundFunctions = (account?: Account): boolean =>
7+
supportsBooleanCapability(account, 'background_functions')

src/lib/functions/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ interface GetFunctionsServerOptions {
262262
functionsRegistry: FunctionsRegistry
263263
siteUrl: string
264264
siteInfo?: SiteInfo
265-
accountId: string
265+
accountId?: string | undefined
266266
geoCountry: string
267267
offline: boolean
268268
state: CLIState

src/lib/images/proxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface IpxParams {
3131
position?: string | null
3232
}
3333

34-
export const parseAllRemoteImages = function (config: NormalizedCachedConfigConfig): {
34+
export const parseAllRemoteImages = function (config: Pick<NormalizedCachedConfigConfig, 'images'>): {
3535
errors: ErrorObject[]
3636
remotePatterns: RegExp[]
3737
} {

src/utils/dev.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import process from 'process'
22

33
import getPort from 'get-port'
44
import isEmpty from 'lodash/isEmpty.js'
5+
import type { NetlifyAPI } from 'netlify'
56

67
import { supportsBackgroundFunctions } from '../lib/account.js'
78

89
import { NETLIFYDEVLOG, chalk, logAndThrowError, log, warn, APIError } from './command-helpers.js'
910
import { loadDotEnvFiles } from './dot-env.js'
11+
import type { SiteInfo } from './types.js'
1012

1113
// Possible sources of environment variables. For the purpose of printing log messages only. Order does not matter.
1214
const ENV_VAR_SOURCES = {
@@ -48,8 +50,21 @@ const validateSiteInfo = ({ site, siteInfo }) => {
4850
}
4951
}
5052

51-
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
52-
const getAccounts = async ({ api }) => {
53+
type ApiAccount = Awaited<ReturnType<NetlifyAPI['listAccountsForUser']>>[number]
54+
type Capabilities = NonNullable<ApiAccount['capabilities']> & {
55+
// FIXME(serhalp): `background_functions` is missing from Netlify API account capabilities type
56+
background_functions?:
57+
| {
58+
included?: boolean | undefined
59+
}
60+
| undefined
61+
}
62+
export type Capability = keyof Capabilities
63+
export type Account = ApiAccount & {
64+
capabilities?: Capabilities
65+
}
66+
67+
const getAccounts = async ({ api }: { api: NetlifyAPI }) => {
5368
try {
5469
const accounts = await api.listAccountsForUser()
5570
return accounts
@@ -83,13 +98,11 @@ const getAddonsInformation = ({ addons, siteInfo }) => {
8398
return { urls, env }
8499
}
85100

86-
// @ts-expect-error TS(7031) FIXME: Binding element 'accounts' implicitly has an 'any'... Remove this comment to see the full error message
87-
const getSiteAccount = ({ accounts, siteInfo }) => {
88-
// @ts-expect-error TS(7006) FIXME: Parameter 'account' implicitly has an 'any' type.
101+
const getSiteAccount = ({ accounts, siteInfo }: { accounts: Account[]; siteInfo: SiteInfo }): Account | undefined => {
89102
const siteAccount = accounts.find((account) => account.slug === siteInfo.account_slug)
90103
if (!siteAccount) {
91104
warn(`Could not find account for site '${siteInfo.name}' with account slug '${siteInfo.account_slug}'`)
92-
return {}
105+
return undefined
93106
}
94107
return siteAccount
95108
}
@@ -121,7 +134,7 @@ export const getSiteInformation = async ({ api, offline, site, siteInfo }) => {
121134
return {
122135
addonsUrls,
123136
siteUrl: siteInfo.ssl_url,
124-
accountId: account.id,
137+
accountId: account?.id,
125138
capabilities: {
126139
backgroundFunctions: supportsBackgroundFunctions(account),
127140
},

src/utils/proxy-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const startProxyServer = async ({
6363
siteInfo,
6464
state,
6565
}: {
66-
accountId: string
66+
accountId: string | undefined
6767
addonsUrls: $TSFixMe
6868
api?: NetlifyOptions['api']
6969
blobsContext?: BlobsContextWithEdgeAccess

tests/unit/lib/account.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { supportsBackgroundFunctions } from '../../../src/lib/account.js'
44

55
describe('supportsBackgroundFunctions', () => {
66
test(`should return false if no account`, () => {
7-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
87
expect(supportsBackgroundFunctions()).toEqual(false)
98
})
109

@@ -25,6 +24,7 @@ describe('supportsBackgroundFunctions', () => {
2524
})
2625

2726
test(`should return true if included property is truthy`, () => {
27+
// @ts-expect-error Intentional incorrect type. TODO(serhalp): Remove this test? Can this happen?
2828
expect(supportsBackgroundFunctions({ capabilities: { background_functions: { included: 'string' } } })).toEqual(
2929
true,
3030
)

tests/unit/lib/images/proxy.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ test('should parse all remote images correctly', () => {
88
remote_images: ['https://example.com/*', 'https://test.com/*'],
99
},
1010
}
11-
// @ts-expect-error TS(2345) FIXME: Argument of type '{ images: { remote_images: strin... Remove this comment to see the full error message
1211
const { errors, remotePatterns } = parseAllRemoteImages(config)
1312
expect(errors).toEqual([])
1413
expect(remotePatterns).toEqual([/https:\/\/example.com\/*/, /https:\/\/test.com\/*/])
@@ -20,7 +19,6 @@ test('should report invalid remote images', () => {
2019
remote_images: ['*'],
2120
},
2221
}
23-
// @ts-expect-error TS(2345) FIXME: Argument of type '{ images: { remote_images: strin... Remove this comment to see the full error message
2422
const { errors, remotePatterns } = parseAllRemoteImages(config)
2523
expect(errors).toEqual([
2624
{

0 commit comments

Comments
 (0)