Skip to content

Commit 8651fe1

Browse files
authored
refactor: fix preset api types (renovatebot#37137)
1 parent c8203e6 commit 8651fe1

File tree

10 files changed

+50
-28
lines changed

10 files changed

+50
-28
lines changed

lib/config/presets/forgejo/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { logger } from '../../../logger';
22
import { getRepoContents } from '../../../modules/platform/forgejo/forgejo-helper';
33
import type { RepoContents } from '../../../modules/platform/forgejo/types';
4+
import type { Nullish } from '../../../types';
45
import { ExternalHostError } from '../../../types/errors/external-host-error';
56
import type { Preset, PresetConfig } from '../types';
67
import { PRESET_DEP_NOT_FOUND, fetchPreset, parsePreset } from '../util';
@@ -12,7 +13,7 @@ export async function fetchJSONFile(
1213
fileName: string,
1314
endpoint: string,
1415
tag?: string | null,
15-
): Promise<Preset> {
16+
): Promise<Nullish<Preset>> {
1617
let res: RepoContents;
1718
try {
1819
res = await getRepoContents(repo, fileName, tag, {
@@ -28,8 +29,7 @@ export async function fetchJSONFile(
2829
throw new Error(PRESET_DEP_NOT_FOUND);
2930
}
3031

31-
// TODO: null check #22198
32-
return parsePreset(res.contentString!, fileName);
32+
return parsePreset(res.contentString, fileName);
3333
}
3434

3535
export function getPresetFromEndpoint(
@@ -38,7 +38,7 @@ export function getPresetFromEndpoint(
3838
presetPath?: string,
3939
endpoint = Endpoint,
4040
tag?: string,
41-
): Promise<Preset | undefined> {
41+
): Promise<Nullish<Preset>> {
4242
return fetchPreset({
4343
repo,
4444
filePreset,
@@ -54,6 +54,6 @@ export function getPreset({
5454
presetName = 'default',
5555
presetPath,
5656
tag = undefined,
57-
}: PresetConfig): Promise<Preset | undefined> {
57+
}: PresetConfig): Promise<Nullish<Preset>> {
5858
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
5959
}

lib/config/presets/gitea/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { logger } from '../../../logger';
22
import { getRepoContents } from '../../../modules/platform/gitea/gitea-helper';
33
import type { RepoContents } from '../../../modules/platform/gitea/types';
4+
import type { Nullish } from '../../../types';
45
import { ExternalHostError } from '../../../types/errors/external-host-error';
56
import type { Preset, PresetConfig } from '../types';
67
import { PRESET_DEP_NOT_FOUND, fetchPreset, parsePreset } from '../util';
@@ -12,7 +13,7 @@ export async function fetchJSONFile(
1213
fileName: string,
1314
endpoint: string,
1415
tag?: string | null,
15-
): Promise<Preset> {
16+
): Promise<Nullish<Preset>> {
1617
let res: RepoContents;
1718
try {
1819
res = await getRepoContents(repo, fileName, tag, {
@@ -28,8 +29,7 @@ export async function fetchJSONFile(
2829
throw new Error(PRESET_DEP_NOT_FOUND);
2930
}
3031

31-
// TODO: null check #22198
32-
return parsePreset(res.contentString!, fileName);
32+
return parsePreset(res.contentString, fileName);
3333
}
3434

3535
export function getPresetFromEndpoint(
@@ -38,7 +38,7 @@ export function getPresetFromEndpoint(
3838
presetPath?: string,
3939
endpoint = Endpoint,
4040
tag?: string,
41-
): Promise<Preset | undefined> {
41+
): Promise<Nullish<Preset>> {
4242
return fetchPreset({
4343
repo,
4444
filePreset,
@@ -54,6 +54,6 @@ export function getPreset({
5454
presetName = 'default',
5555
presetPath,
5656
tag = undefined,
57-
}: PresetConfig): Promise<Preset | undefined> {
57+
}: PresetConfig): Promise<Nullish<Preset>> {
5858
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
5959
}

lib/config/presets/github/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import is from '@sindresorhus/is';
22
import { logger } from '../../../logger';
3+
import type { Nullish } from '../../../types';
34
import { ExternalHostError } from '../../../types/errors/external-host-error';
45
import { repoCacheProvider } from '../../../util/http/cache/repository-http-cache-provider';
56
import { GithubHttp } from '../../../util/http/github';
@@ -16,7 +17,7 @@ export async function fetchJSONFile(
1617
fileName: string,
1718
endpoint: string,
1819
tag?: string,
19-
): Promise<Preset> {
20+
): Promise<Nullish<Preset>> {
2021
let ref = '';
2122
if (is.nonEmptyString(tag)) {
2223
ref = `?ref=${tag}`;
@@ -47,7 +48,7 @@ export function getPresetFromEndpoint(
4748
presetPath?: string,
4849
endpoint = Endpoint,
4950
tag?: string,
50-
): Promise<Preset | undefined> {
51+
): Promise<Nullish<Preset>> {
5152
return fetchPreset({
5253
repo,
5354
filePreset,
@@ -63,6 +64,6 @@ export function getPreset({
6364
presetName = 'default',
6465
presetPath,
6566
tag,
66-
}: PresetConfig): Promise<Preset | undefined> {
67+
}: PresetConfig): Promise<Nullish<Preset>> {
6768
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
6869
}

lib/config/presets/gitlab/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import is from '@sindresorhus/is';
22
import { logger } from '../../../logger';
3+
import type { Nullish } from '../../../types';
34
import { ExternalHostError } from '../../../types/errors/external-host-error';
45
import type { GitlabProject } from '../../../types/platform/gitlab';
56
import { GitlabHttp } from '../../../util/http/gitlab';
@@ -25,7 +26,7 @@ export async function fetchJSONFile(
2526
fileName: string,
2627
endpoint: string,
2728
tag?: string,
28-
): Promise<Preset> {
29+
): Promise<Nullish<Preset>> {
2930
let url = endpoint;
3031
let ref = '';
3132
let res: HttpResponse;
@@ -63,7 +64,7 @@ export function getPresetFromEndpoint(
6364
presetPath?: string,
6465
endpoint = Endpoint,
6566
tag?: string,
66-
): Promise<Preset | undefined> {
67+
): Promise<Nullish<Preset>> {
6768
return fetchPreset({
6869
repo,
6970
filePreset: presetName,
@@ -79,6 +80,6 @@ export function getPreset({
7980
presetPath,
8081
presetName = 'default',
8182
tag = undefined,
82-
}: PresetConfig): Promise<Preset | undefined> {
83+
}: PresetConfig): Promise<Nullish<Preset>> {
8384
return getPresetFromEndpoint(repo, presetName, presetPath, Endpoint, tag);
8485
}

lib/config/presets/local/common.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { logger } from '../../../logger';
22
import { platform } from '../../../modules/platform';
3+
import type { Nullish } from '../../../types';
34
import { ExternalHostError } from '../../../types/errors/external-host-error';
45
import type { Preset } from '../types';
56
import { PRESET_DEP_NOT_FOUND, fetchPreset, parsePreset } from '../util';
@@ -9,7 +10,7 @@ export async function fetchJSONFile(
910
fileName: string,
1011
_endpoint?: string,
1112
tag?: string,
12-
): Promise<Preset> {
13+
): Promise<Nullish<Preset>> {
1314
let raw: string | null;
1415
try {
1516
raw = await platform.getRawFile(fileName, repo, tag ?? undefined);
@@ -38,7 +39,7 @@ export function getPresetFromEndpoint(
3839
presetPath: string | undefined,
3940
endpoint: string,
4041
tag?: string,
41-
): Promise<Preset | undefined> {
42+
): Promise<Nullish<Preset>> {
4243
return fetchPreset({
4344
repo,
4445
filePreset,

lib/config/presets/local/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { PlatformId } from '../../../constants';
2+
import type { Nullish } from '../../../types';
23
import { GlobalConfig } from '../../global';
34
import * as forgejo from '../forgejo';
45
import * as gitea from '../gitea';
@@ -14,7 +15,7 @@ interface Resolver {
1415
presetPath?: string,
1516
endpoint?: string,
1617
tag?: string,
17-
): Promise<Preset | undefined>;
18+
): Promise<Nullish<Preset>>;
1819
}
1920

2021
const resolvers = {
@@ -35,7 +36,7 @@ export function getPreset({
3536
presetName = 'default',
3637
presetPath,
3738
tag,
38-
}: PresetConfig): Promise<Preset | undefined> {
39+
}: PresetConfig): Promise<Nullish<Preset>> {
3940
const platform = GlobalConfig.get('platform');
4041
if (!platform) {
4142
throw new Error(`Missing platform config for local preset.`);

lib/config/presets/types.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { MaybePromise, Nullish } from '../../types';
12
import type { RenovateConfig } from '../types';
23

34
// TODO: Proper typing
@@ -11,9 +12,7 @@ export interface PresetConfig {
1112
}
1213

1314
export interface PresetApi {
14-
getPreset(
15-
config: PresetConfig,
16-
): Promise<Preset | null | undefined> | Preset | null | undefined;
15+
getPreset(config: PresetConfig): MaybePromise<Nullish<Preset>>;
1716
}
1817

1918
export interface ParsedPreset {
@@ -31,7 +30,7 @@ export type PresetFetcher = (
3130
fileName: string,
3231
endpoint: string,
3332
tag?: string,
34-
) => Promise<Preset | null | undefined>;
33+
) => Promise<Nullish<Preset>>;
3534

3635
export interface FetchPresetConfig {
3736
repo: string;

lib/config/presets/util.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { logger } from '../../logger';
2+
import type { Nullish } from '../../types';
23
import { parseJson } from '../../util/common';
34
import { regEx } from '../../util/regex';
45
import { ensureTrailingSlash } from '../../util/url';
@@ -19,7 +20,7 @@ export async function fetchPreset({
1920
endpoint: _endpoint,
2021
tag,
2122
fetch,
22-
}: FetchPresetConfig): Promise<Preset | undefined> {
23+
}: FetchPresetConfig): Promise<Nullish<Preset>> {
2324
// TODO: fix me, can be undefiend #22198
2425
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
2526
const endpoint = ensureTrailingSlash(_endpoint!);
@@ -87,9 +88,12 @@ export async function fetchPreset({
8788
return jsonContent;
8889
}
8990

90-
export function parsePreset(content: string, fileName: string): Preset {
91+
export function parsePreset(
92+
content: Nullish<string>,
93+
fileName: string,
94+
): Nullish<Preset> {
9195
try {
92-
return parseJson(content, fileName) as Preset;
96+
return parseJson(content, fileName) as Nullish<Preset>;
9397
} catch {
9498
throw new Error(PRESET_INVALID_JSON);
9599
}

lib/types/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,14 @@ export type { PrState } from './pr-state';
1313
export type { ModuleApi, RenovatePackageJson } from './base';
1414

1515
export type AutoMergeType = 'branch' | 'pr' | 'pr-comment';
16+
17+
type Val = NonNullable<unknown>;
18+
19+
/**
20+
* A type that can be null or undefined.
21+
*
22+
* @see https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing
23+
*/
24+
export type Nullish<T extends Val> = T | null | undefined;
25+
26+
export type MaybePromise<T> = T | Promise<T>;

lib/util/common.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
GITLAB_API_USING_HOST_TYPES,
1111
} from '../constants';
1212
import { logger } from '../logger';
13+
import type { Nullish } from '../types';
1314
import * as hostRules from './host-rules';
1415
import { parseUrl } from './url';
1516

@@ -91,7 +92,10 @@ export function noLeadingAtSymbol(input: string): string {
9192
return input.startsWith('@') ? input.slice(1) : input;
9293
}
9394

94-
export function parseJson(content: string | null, filename: string): JsonValue {
95+
export function parseJson(
96+
content: Nullish<string>,
97+
filename: string,
98+
): JsonValue {
9599
if (!content) {
96100
return null;
97101
}

0 commit comments

Comments
 (0)