Skip to content

Commit

Permalink
chore: fix TypeScript errors in login and logout files (#6880)
Browse files Browse the repository at this point in the history
* chore: fix TypeScript errors in login and logout files

Fixed TS errors in login and logout.
Created `tokenTuple` type for the `getToken` function's return type
Updated `getToken` function calls to remove TypeScript ignore comments

Co-authored-by: Ben Hancock<benhancock859@gmail.com>

* fix: corrected tuple type

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

* fix: type narrowed in /integration/deploy.ts to avoid passing undefined tokens

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

* chore: extracted Location to its own type

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

* fix: fix to building headers object for fetch request in deploy.ts

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

* fix: reverted `getToken` changes back to previous

In cases where `getToken` can't find a token,
changed it back to previous behavior of returning `[null, 'not found']`.
Changed `tokenTuple` type to reflect changes.
This preserves previous behavior while ensuring type safety for
function calls that use `getToken`'s return value. This required a
new variable `blobsToken` to satisfy `runCoreSteps` function argument type requirements.

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

* Update src/utils/types.ts

Co-authored-by: Daniel Lew <51924260+DanielSLew@users.noreply.github.com>

* fix: updated Location type to TokenLocation to be more specific

Co-authored-by: Ben Hancock <benhancock859@gmail.com>

---------

Co-authored-by: Ben Hancock <benhancock859@gmail.com>
Co-authored-by: Daniel Lew <51924260+DanielSLew@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 19, 2024
1 parent e9623e6 commit 08ce662
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 18 deletions.
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
6 changes: 3 additions & 3 deletions src/commands/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,17 @@ const uploadDeployBlobs = async ({
phase: 'start',
})

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

const blobsToken = token || undefined
const { success } = await runCoreSteps(['blobs_upload'], {
...options,
quiet: silent,
cachedConfig,
packagePath,
deployId,
siteId,
token,
token: blobsToken,
})

if (!success) {
Expand Down Expand Up @@ -565,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
6 changes: 2 additions & 4 deletions src/commands/integration/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@ 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()
const workingDir = resolve(command.workingDir)
const buildOptions = await getBuildOptions({
Expand All @@ -412,6 +411,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
const { description, integrationLevel, name, scopes, slug } = await getConfiguration(command.workingDir)
const localIntegrationConfig = { name, description, scopes, slug, integrationLevel }

const headers = token ? { 'netlify-token': token } : undefined
// @ts-expect-error TS(2345) FIXME: Argument of type '{ api: any; site: any; siteInfo:... Remove this comment to see the full error message
const { accountId } = await getSiteInformation({
api,
Expand All @@ -422,9 +422,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
const { body: registeredIntegration, statusCode } = await fetch(
`${getIntegrationAPIUrl()}/${accountId}/integrations?site_id=${siteId}`,
{
headers: {
'netlify-token': token,
},
headers,
},
).then(async (res) => {
const body = await res.json()
Expand Down
5 changes: 2 additions & 3 deletions src/commands/login/login.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OptionValues } from 'commander'

import { chalk, exit, getToken, log } from '../../utils/command-helpers.js'
import { TokenLocation } from '../../utils/types.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: TokenLocation) {
switch (location) {
case 'env':
return 'via process.env.NETLIFY_AUTH_TOKEN set in your terminal session'
Expand All @@ -18,7 +18,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
19 changes: 14 additions & 5 deletions src/utils/command-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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'

import getGlobalConfig from './get-global-config.js'
import getPackageJson from './get-package-json.js'
import { reportError } from './telemetry/report-error.js'
import { TokenLocation } from './types.js'

/** The parsed process argv without the binary only arguments and flags */
const argv = process.argv.slice(2)
Expand Down Expand Up @@ -92,8 +94,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 @@ -118,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 | null, TokenLocation]

export const getToken = async (tokenFromOptions?: string): Promise<tokenTuple> => {
// 1. First honor command flag --auth
if (tokenFromOptions) {
return [tokenFromOptions, 'flag']
Expand Down
2 changes: 2 additions & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ export interface Request extends IncomingMessage {
}

export type Rewriter = (req: Request) => Match | null

export type TokenLocation = 'env' | 'flag' | 'config' | 'not found'

0 comments on commit 08ce662

Please sign in to comment.