2022-03-03 09:35:26 +00:00
|
|
|
import { ExternalHostError } from '../../types/errors/external-host-error';
|
|
|
|
import { Http } from '../../util/http';
|
|
|
|
import type { HttpError } from '../../util/http';
|
2021-05-21 05:40:09 +00:00
|
|
|
import type {
|
|
|
|
DatasourceApi,
|
|
|
|
DigestConfig,
|
|
|
|
GetReleasesConfig,
|
|
|
|
ReleaseResult,
|
|
|
|
} from './types';
|
|
|
|
|
|
|
|
export abstract class Datasource implements DatasourceApi {
|
|
|
|
protected constructor(public readonly id: string) {
|
|
|
|
this.http = new Http(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
caching: boolean | undefined;
|
|
|
|
|
|
|
|
customRegistrySupport = true;
|
|
|
|
|
|
|
|
defaultConfig: Record<string, unknown> | undefined;
|
|
|
|
|
2022-06-16 11:00:11 +00:00
|
|
|
defaultRegistryUrls?: string[] | (() => string[]);
|
2021-05-21 05:40:09 +00:00
|
|
|
|
|
|
|
defaultVersioning: string | undefined;
|
|
|
|
|
|
|
|
registryStrategy: 'first' | 'hunt' | 'merge' | undefined = 'first';
|
|
|
|
|
|
|
|
protected http: Http;
|
|
|
|
|
|
|
|
abstract getReleases(
|
|
|
|
getReleasesConfig: GetReleasesConfig
|
|
|
|
): Promise<ReleaseResult | null>;
|
|
|
|
|
2022-02-05 05:54:18 +00:00
|
|
|
getDigest?(config: DigestConfig, newValue?: string): Promise<string | null>;
|
2021-05-21 05:40:09 +00:00
|
|
|
|
2021-11-08 19:20:03 +00:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
2021-05-21 05:40:09 +00:00
|
|
|
handleSpecificErrors(err: HttpError): void {}
|
|
|
|
|
|
|
|
protected handleGenericErrors(err: HttpError): never {
|
2021-07-09 07:06:50 +00:00
|
|
|
// istanbul ignore if: not easy testable with nock
|
|
|
|
if (err instanceof ExternalHostError) {
|
|
|
|
throw err;
|
|
|
|
}
|
2021-05-21 05:40:09 +00:00
|
|
|
this.handleSpecificErrors(err);
|
|
|
|
if (err.response?.statusCode !== undefined) {
|
|
|
|
if (
|
|
|
|
err.response?.statusCode === 429 ||
|
|
|
|
(err.response?.statusCode >= 500 && err.response?.statusCode < 600)
|
|
|
|
) {
|
|
|
|
throw new ExternalHostError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|