Skip to content

Commit 08ce662

Browse files
dylanspyerbenhancockDanielSLew
authored
chore: fix TypeScript errors in login and logout files (#6880)
* 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>
1 parent e9623e6 commit 08ce662

File tree

8 files changed

+23
-18
lines changed

8 files changed

+23
-18
lines changed

src/commands/build/build.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export const build = async (options: OptionValues, command: BaseCommand) => {
2727
const { cachedConfig, siteInfo } = command.netlify
2828
command.setAnalyticsPayload({ dry: options.dry })
2929
// Retrieve Netlify Build options
30-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
3130
const [token] = await getToken()
3231
const settings = await detectFrameworkSettings(command, 'build')
3332

src/commands/deploy/deploy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,17 @@ const uploadDeployBlobs = async ({
371371
phase: 'start',
372372
})
373373

374-
const [token] = await getToken(false)
374+
const [token] = await getToken()
375375

376+
const blobsToken = token || undefined
376377
const { success } = await runCoreSteps(['blobs_upload'], {
377378
...options,
378379
quiet: silent,
379380
cachedConfig,
380381
packagePath,
381382
deployId,
382383
siteId,
383-
token,
384+
token: blobsToken,
384385
})
385386

386387
if (!success) {
@@ -565,7 +566,6 @@ const handleBuild = async ({ cachedConfig, currentDir, defaultConfig, deployHand
565566
if (!options.build) {
566567
return {}
567568
}
568-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
569569
const [token] = await getToken()
570570
const resolvedOptions = await getBuildOptions({
571571
cachedConfig,

src/commands/integration/deploy.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,6 @@ export const getConfiguration = (workingDir) => {
394394
export const deploy = async (options: OptionValues, command: BaseCommand) => {
395395
const { api, cachedConfig, site, siteInfo } = command.netlify
396396
const { id: siteId } = site
397-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
398397
const [token] = await getToken()
399398
const workingDir = resolve(command.workingDir)
400399
const buildOptions = await getBuildOptions({
@@ -412,6 +411,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
412411
const { description, integrationLevel, name, scopes, slug } = await getConfiguration(command.workingDir)
413412
const localIntegrationConfig = { name, description, scopes, slug, integrationLevel }
414413

414+
const headers = token ? { 'netlify-token': token } : undefined
415415
// @ts-expect-error TS(2345) FIXME: Argument of type '{ api: any; site: any; siteInfo:... Remove this comment to see the full error message
416416
const { accountId } = await getSiteInformation({
417417
api,
@@ -422,9 +422,7 @@ export const deploy = async (options: OptionValues, command: BaseCommand) => {
422422
const { body: registeredIntegration, statusCode } = await fetch(
423423
`${getIntegrationAPIUrl()}/${accountId}/integrations?site_id=${siteId}`,
424424
{
425-
headers: {
426-
'netlify-token': token,
427-
},
425+
headers,
428426
},
429427
).then(async (res) => {
430428
const body = await res.json()

src/commands/login/login.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { OptionValues } from 'commander'
22

33
import { chalk, exit, getToken, log } from '../../utils/command-helpers.js'
4+
import { TokenLocation } from '../../utils/types.js'
45
import BaseCommand from '../base-command.js'
56

6-
// @ts-expect-error TS(7006) FIXME: Parameter 'location' implicitly has an 'any' type.
7-
const msg = function (location) {
7+
const msg = function (location: TokenLocation) {
88
switch (location) {
99
case 'env':
1010
return 'via process.env.NETLIFY_AUTH_TOKEN set in your terminal session'
@@ -18,7 +18,6 @@ const msg = function (location) {
1818
}
1919

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

2423
command.setAnalyticsPayload({ new: options.new })

src/commands/logout/logout.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { track } from '../../utils/telemetry/index.js'
55
import BaseCommand from '../base-command.js'
66

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

1110
if (!accessToken) {

src/commands/status/status.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import BaseCommand from '../base-command.js'
88
export const status = async (options: OptionValues, command: BaseCommand) => {
99
const { api, globalConfig, site, siteInfo } = command.netlify
1010
const current = globalConfig.get('userId')
11-
// @ts-expect-error TS(2554) FIXME: Expected 1 arguments, but got 0.
1211
const [accessToken] = await getToken()
1312

1413
if (!accessToken) {

src/utils/command-helpers.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import chokidar from 'chokidar'
88
import decache from 'decache'
99
import WSL from 'is-wsl'
1010
import debounce from 'lodash/debounce.js'
11+
import { NetlifyAPI } from 'netlify'
1112
import terminalLink from 'terminal-link'
1213

1314
import { clearSpinner, startSpinner } from '../lib/spinner.js'
1415

1516
import getGlobalConfig from './get-global-config.js'
1617
import getPackageJson from './get-package-json.js'
1718
import { reportError } from './telemetry/report-error.js'
19+
import { TokenLocation } from './types.js'
1820

1921
/** The parsed process argv without the binary only arguments and flags */
2022
const argv = process.argv.slice(2)
@@ -92,8 +94,14 @@ const TOKEN_TIMEOUT = 3e5
9294
* @param {object} config.ticket
9395
* @returns
9496
*/
95-
// @ts-expect-error TS(7031) FIXME: Binding element 'api' implicitly has an 'any' type... Remove this comment to see the full error message
96-
export const pollForToken = async ({ api, ticket }) => {
97+
98+
export const pollForToken = async ({
99+
api,
100+
ticket,
101+
}: {
102+
api: NetlifyAPI
103+
ticket: { id: string; client_id: string; authorized: boolean; created_at: string }
104+
}) => {
97105
const spinner = startSpinner({ text: 'Waiting for authorization...' })
98106
try {
99107
const accessToken = await api.getAccessToken(ticket, { timeout: TOKEN_TIMEOUT })
@@ -118,14 +126,15 @@ export const pollForToken = async ({ api, ticket }) => {
118126
clearSpinner({ spinner })
119127
}
120128
}
121-
122129
/**
123130
* Get a netlify token
124131
* @param {string} [tokenFromOptions] optional token from the provided --auth options
125132
* @returns {Promise<[null|string, 'flag' | 'env' |'config' |'not found']>}
126133
*/
127-
// @ts-expect-error TS(7006) FIXME: Parameter 'tokenFromOptions' implicitly has an 'an... Remove this comment to see the full error message
128-
export const getToken = async (tokenFromOptions) => {
134+
135+
export type tokenTuple = [string | null, TokenLocation]
136+
137+
export const getToken = async (tokenFromOptions?: string): Promise<tokenTuple> => {
129138
// 1. First honor command flag --auth
130139
if (tokenFromOptions) {
131140
return [tokenFromOptions, 'flag']

src/utils/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ export interface Request extends IncomingMessage {
6262
}
6363

6464
export type Rewriter = (req: Request) => Match | null
65+
66+
export type TokenLocation = 'env' | 'flag' | 'config' | 'not found'

0 commit comments

Comments
 (0)