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

chore: fix TypeScript errors in login and logout files #6880

Merged
merged 11 commits into from
Oct 19, 2024
1 change: 0 additions & 1 deletion src/commands/build/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export const build = async (options: OptionValues, command: BaseCommand) => {
const { cachedConfig, siteInfo } = command.netlify
command.setAnalyticsPayload({ dry: options.dry })
// Retrieve Netlify Build options
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [token] = await getToken()
const settings = await detectFrameworkSettings(command, 'build')

Expand Down
3 changes: 1 addition & 2 deletions src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ const uploadDeployBlobs = async ({
phase: 'start',
})

const [token] = await getToken(false)
const [token] = await getToken()

const { success } = await runCoreSteps(['blobs_upload'], {
...options,
Expand Down Expand Up @@ -566,7 +566,6 @@ const handleBuild = async ({ cachedConfig, currentDir, defaultConfig, deployHand
if (!options.build) {
return {}
}
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [token] = await getToken()
const resolvedOptions = await getBuildOptions({
cachedConfig,
Expand Down
7 changes: 4 additions & 3 deletions src/commands/integration/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,10 @@ export const getConfiguration = (workingDir) => {
export const deploy = async (options: OptionValues, command: BaseCommand) => {
const { api, cachedConfig, site, siteInfo } = command.netlify
const { id: siteId } = site
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [token] = await getToken()
let [token] = await getToken()
if (!token) {
token = ''
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did this need to change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prior to our changes, getToken returned an any type. In reality, this value would be a string or undefined. The fetch request below it (see screenshot) relies on token being a string type. Because getToken can return undefined, and we don't want to pass undefined to the fetch request, we reassigned it to an empty string.

image
image

It does seem that our change may have unintended consequences as a result of changing token from undefined to an empty string in cases where there is no token. We are pushing a change that goes about fixing this in a way that does not involve reassigning token.

const workingDir = resolve(command.workingDir)
const buildOptions = await getBuildOptions({
cachedConfig,
Expand All @@ -418,7 +420,6 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
site,
siteInfo,
})

const { body: registeredIntegration, statusCode } = await fetch(
`${getIntegrationAPIUrl()}/${accountId}/integrations?site_id=${siteId}`,
{
Expand Down
4 changes: 1 addition & 3 deletions src/commands/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { OptionValues } from 'commander'
import { chalk, exit, getToken, log } from '../../utils/command-helpers.js'
import BaseCommand from '../base-command.js'

// @ts-expect-error TS(7006) FIXME: Parameter 'location' implicitly has an 'any' type.
const msg = function (location) {
const msg = function (location: 'env' | 'flag' | 'config' | 'not found') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can extract this type and move it into the types file as it's used later as well

switch (location) {
case 'env':
return 'via process.env.NETLIFY_AUTH_TOKEN set in your terminal session'
Expand All @@ -18,7 +17,6 @@ const msg = function (location) {
}

export const login = async (options: OptionValues, command: BaseCommand) => {
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [accessToken, location] = await getToken()

command.setAnalyticsPayload({ new: options.new })
Expand Down
1 change: 0 additions & 1 deletion src/commands/logout/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { track } from '../../utils/telemetry/index.js'
import BaseCommand from '../base-command.js'

export const logout = async (options: OptionValues, command: BaseCommand) => {
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [accessToken, location] = await getToken()

if (!accessToken) {
Expand Down
1 change: 0 additions & 1 deletion src/commands/status/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import BaseCommand from '../base-command.js'
export const status = async (options: OptionValues, command: BaseCommand) => {
const { api, globalConfig, site, siteInfo } = command.netlify
const current = globalConfig.get('userId')
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
const [accessToken] = await getToken()

if (!accessToken) {
Expand Down
20 changes: 14 additions & 6 deletions src/utils/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import chokidar from 'chokidar'
import decache from 'decache'
import WSL from 'is-wsl'
import debounce from 'lodash/debounce.js'
import { NetlifyAPI } from 'netlify'
import terminalLink from 'terminal-link'

import { clearSpinner, startSpinner } from '../lib/spinner.js'
Expand Down Expand Up @@ -92,8 +93,14 @@ const TOKEN_TIMEOUT = 3e5
* @param {object} config.ticket
* @returns
*/
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
export const pollForToken = async ({ api, ticket }) => {

export const pollForToken = async ({
api,
ticket,
}: {
api: NetlifyAPI
ticket: { id: string; client_id: string; authorized: boolean; created_at: string }
}) => {
const spinner = startSpinner({ text: 'Waiting for authorization...' })
try {
const accessToken = await api.getAccessToken(ticket, { timeout: TOKEN_TIMEOUT })
Expand All @@ -119,14 +126,15 @@ export const pollForToken = async ({ api, ticket }) => {
clearSpinner({ spinner })
}
}

/**
* Get a netlify token
* @param {string} [tokenFromOptions] optional token from the provided --auth options
* @returns {Promise<[null|string, 'flag' | 'env' |'config' |'not found']>}
*/
// @ts-expect-error TS(7006) FIXME: Parameter 'tokenFromOptions' implicitly has an 'an... Remove this comment to see the full error message
export const getToken = async (tokenFromOptions) => {

export type tokenTuple = [string | undefined, 'flag' | 'env' | 'config' | 'not found']

export const getToken = async (tokenFromOptions?: string): Promise<tokenTuple> => {
// 1. First honor command flag --auth
if (tokenFromOptions) {
return [tokenFromOptions, 'flag']
Expand All @@ -143,7 +151,7 @@ export const getToken = async (tokenFromOptions) => {
if (tokenFromConfig) {
return [tokenFromConfig, 'config']
}
return [null, 'not found']
return [undefined, 'not found']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did this change as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was originally intended to satisfy the type requirements of a pair of functions that use getToken's return value. Our updated commit restores the original return value and creates a new variable whose value is set based on getToken's return value to preserve type safety for these function calls.

}

// 'api' command uses JSON output by default
Expand Down