Skip to content
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

fix: remove extra call to accounts endpoint across cli #6947

Merged
merged 9 commits into from
Dec 18, 2024
4 changes: 2 additions & 2 deletions bin/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { argv } from 'process'

import updateNotifier from 'update-notifier'

import { runProgram } from '../dist/utils/run-program.js'
import { createMainCommand } from '../dist/commands/main.js'
import { error } from '../dist/utils/command-helpers.js'
import getPackageJson from '../dist/utils/get-package-json.js'
import { createMainCommand } from '../dist/commands/main.js'
import { runProgram } from '../dist/utils/run-program.js'

// 12 hours
const UPDATE_CHECK_INTERVAL = 432e5
Expand Down
3 changes: 2 additions & 1 deletion src/commands/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ export default class BaseCommand extends Command {
token,
...apiUrlOpts,
})
const { buildDir, config, configPath, repositoryRoot, siteInfo } = cachedConfig
const { accounts, buildDir, config, configPath, repositoryRoot, siteInfo } = cachedConfig
let { env } = cachedConfig
if (flags.offlineEnv) {
env = {}
Expand Down Expand Up @@ -642,6 +642,7 @@ export default class BaseCommand extends Command {
const configFilePath = configPath || join(this.workingDir, 'netlify.toml')

actionCommand.netlify = {
accounts,
// api methods
api,
apiOpts,
Expand Down
8 changes: 4 additions & 4 deletions src/commands/sites/sites-create-template.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import { OptionValues } from 'commander'
import inquirer from 'inquirer'
import pick from 'lodash/pick.js'
import { render } from 'prettyjson'
import { v4 as uuid } from 'uuid'
import path from 'node:path'
import { fileURLToPath } from 'node:url'

import {
chalk,
Expand All @@ -30,7 +31,7 @@ import BaseCommand from '../base-command.js'
import { getSiteNameInput } from './sites-create.js'

export const sitesCreateTemplate = async (repository: string, options: OptionValues, command: BaseCommand) => {
const { api } = command.netlify
const { accounts, api } = command.netlify
await command.authenticate()

const { globalConfig } = command.netlify
Expand All @@ -52,7 +53,6 @@ export const sitesCreateTemplate = async (repository: string, options: OptionVal
error(`${getTerminalLink(chalk.bold(templateName), githubLink)} is not a valid GitHub template`)
return
}
const accounts = await api.listAccountsForUser()

let { accountSlug } = options

Expand Down
4 changes: 1 addition & 3 deletions src/commands/sites/sites-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ export const getSiteNameInput = async (name: string | undefined): Promise<{ name
}

export const sitesCreate = async (options: OptionValues, command: BaseCommand) => {
const { api } = command.netlify
const { accounts, api } = command.netlify

await command.authenticate()

const accounts: Account[] = await api.listAccountsForUser()

let { accountSlug }: { accountSlug?: string } = options
if (!accountSlug) {
const { accountSlug: accountSlugInput }: { accountSlug: string } = await inquirer.prompt<
Expand Down
7 changes: 2 additions & 5 deletions src/commands/status/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { chalk, error, exit, getToken, log, logJson, warn, APIError } from '../.
import BaseCommand from '../base-command.js'

export const status = async (options: OptionValues, command: BaseCommand) => {
const { api, globalConfig, site, siteInfo } = command.netlify
const { accounts, api, globalConfig, site, siteInfo } = command.netlify
const current = globalConfig.get('userId')
const [accessToken] = await getToken()

Expand All @@ -23,12 +23,10 @@ export const status = async (options: OptionValues, command: BaseCommand) => {
Current Netlify User β”‚
β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜`)

let accounts
let user

try {
// eslint-disable-next-line @typescript-eslint/no-extra-semi
;[accounts, user] = await Promise.all([api.listAccountsForUser(), api.getCurrentUser()])
user = await api.getCurrentUser()
} catch (error_) {
if ((error_ as APIError).status === 401) {
error('Your session has expired. Please try to re-authenticate by running `netlify logout` and `netlify login`.')
Expand All @@ -45,7 +43,6 @@ export const status = async (options: OptionValues, command: BaseCommand) => {
}
const teamsData = {}

// @ts-expect-error TS(7006) FIXME: Parameter 'team' implicitly has an 'any' type.
accounts.forEach((team) => {
// @ts-expect-error TS(7053) FIXME: Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
teamsData[team.name] = team.roles_allowed.join(' ')
Expand Down
2 changes: 2 additions & 0 deletions src/commands/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { NetlifyAPI } from 'netlify'

import type { FrameworksAPIPaths } from "../utils/frameworks-api.ts";
import StateConfig from '../utils/state-config.js'
import { Account } from "../utils/types.ts";


// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -57,6 +58,7 @@ export type EnvironmentVariables = Record<string, { sources: EnvironmentVariable
* The netlify object inside each command with the state
*/
export type NetlifyOptions = {
accounts: Account[]
// poorly duck type the missing api functions
api: NetlifyAPI & Record<string, (...args: $TSFixMe) => Promise<$TSFixMe>>
apiOpts: $TSFixMe
Expand Down
11 changes: 1 addition & 10 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,7 @@ export interface Account {
name: string
slug: string
type: string
capabilities: {
sites: {
included: number
used: number
}
collaborators: {
included: number
used: number
}
}
capabilities: Record<string, { included: string; used: string }>
billing_name: string
billing_email: string
billing_details: string
Expand Down
Loading