From 9d61bd9678afcb4c33b4c7198abb9fb4c7b48f69 Mon Sep 17 00:00:00 2001 From: Michael Kriese <michael.kriese@visualon.de> Date: Wed, 12 Jan 2022 22:08:09 +0100 Subject: [PATCH] refactor: strong type http headers (#13520) --- lib/datasource/docker/index.ts | 5 +++-- lib/datasource/maven/util.ts | 2 +- lib/platform/github/index.ts | 5 +++-- lib/util/http/gitlab.ts | 2 +- lib/util/http/index.ts | 7 ++++++- 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/datasource/docker/index.ts b/lib/datasource/docker/index.ts index a4756390b6..bf7f8f7b92 100644 --- a/lib/datasource/docker/index.ts +++ b/lib/datasource/docker/index.ts @@ -81,7 +81,7 @@ async function getDockerApiTags( noAuth: true, }); tags = tags.concat(res.body.tags); - const linkHeader = parseLinkHeader(res.headers.link as string); + const linkHeader = parseLinkHeader(res.headers.link); url = linkHeader?.next ? URL.resolve(url, linkHeader.next.url) : null; page += 1; } while (url && page < 20); @@ -200,7 +200,8 @@ export async function getDigest( ); if (manifestResponse) { if (hasKey('docker-content-digest', manifestResponse.headers)) { - digest = manifestResponse.headers['docker-content-digest'] || null; + digest = + (manifestResponse.headers['docker-content-digest'] as string) || null; } else { logger.debug( { registryHost }, diff --git a/lib/datasource/maven/util.ts b/lib/datasource/maven/util.ts index a0e6061014..d3180042c2 100644 --- a/lib/datasource/maven/util.ts +++ b/lib/datasource/maven/util.ts @@ -113,7 +113,7 @@ export async function checkHttpResource( try { const httpClient = httpByHostType(hostType); const res = await httpClient.head(pkgUrl.toString()); - const timestamp = res?.headers?.['last-modified'] as string; + const timestamp = res?.headers?.['last-modified']; if (timestamp) { const isoTimestamp = normalizeDate(timestamp); if (isoTimestamp) { diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index 9168f725ef..4c06175bbb 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -89,12 +89,13 @@ export async function detectGhe(token: string): Promise<void> { if (platformConfig.isGhe) { const gheHeaderKey = 'x-github-enterprise-version'; const gheQueryRes = await githubApi.headJson('/', { token }); - const gheHeaders: Record<string, string> = gheQueryRes?.headers || {}; + const gheHeaders: Record<string, string | string[]> = + gheQueryRes?.headers || {}; const [, gheVersion] = Object.entries(gheHeaders).find( ([k]) => k.toLowerCase() === gheHeaderKey ) ?? []; - platformConfig.gheVersion = semver.valid(gheVersion) ?? null; + platformConfig.gheVersion = semver.valid(gheVersion as string) ?? null; } } diff --git a/lib/util/http/gitlab.ts b/lib/util/http/gitlab.ts index 538837d2f1..0dc537279d 100644 --- a/lib/util/http/gitlab.ts +++ b/lib/util/http/gitlab.ts @@ -40,7 +40,7 @@ export class GitlabHttp extends Http<GitlabHttpOptions, GitlabHttpOptions> { if (opts.paginate && is.array(result.body)) { // Check if result is paginated try { - const linkHeader = parseLinkHeader(result.headers.link as string); + const linkHeader = parseLinkHeader(result.headers.link); const nextUrl = linkHeader?.next?.url ? parseUrl(linkHeader.next.url) : null; diff --git a/lib/util/http/index.ts b/lib/util/http/index.ts index 553e912672..63f67fa73a 100644 --- a/lib/util/http/index.ts +++ b/lib/util/http/index.ts @@ -1,4 +1,5 @@ import crypto from 'crypto'; +import type { IncomingHttpHeaders } from 'http'; import merge from 'deepmerge'; import got, { Options, Response } from 'got'; import { HOST_DISABLED } from '../../constants/error-messages'; @@ -48,10 +49,14 @@ export interface InternalHttpOptions extends HttpOptions { method?: 'get' | 'post' | 'put' | 'patch' | 'delete' | 'head'; } +export interface HttpHeaders extends IncomingHttpHeaders { + link?: string | undefined; +} + export interface HttpResponse<T = string> { statusCode: number; body: T; - headers: any; + headers: HttpHeaders; authorization?: boolean; } -- GitLab