Files
Sebastian Poxhofer 15f4bd8eff chore: migrate from zod/v3 to zod/v4 (#43691)
* chore: migrate from zod/v3 to zod/v4

This commit contains the logic changes

* chore: migrate imports from zod/v3 to zod/v4

This contains import updates and adds explicit record keys as zod/v4 requires these now

* ci: enforce usage of zod/v4
2026-05-30 16:01:58 +00:00

81 lines
2.4 KiB
TypeScript

import urlJoin from 'url-join';
import { ZodError } from 'zod/v4';
import { logger } from '../../../logger/index.ts';
import { withCache } from '../../../util/cache/package/with-cache.ts';
import { Result } from '../../../util/result.ts';
import { Datasource } from '../datasource.ts';
import { ReleasesConfig } from '../schema.ts';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types.ts';
import { BuildpacksRegistryResponse } from './schema.ts';
export class BuildpacksRegistryDatasource extends Datasource {
static readonly id = 'buildpacks-registry';
constructor() {
super(BuildpacksRegistryDatasource.id);
}
override readonly customRegistrySupport = false;
override readonly defaultRegistryUrls = ['https://registry.buildpacks.io'];
override readonly releaseTimestampSupport = true;
override readonly releaseTimestampNote =
'The release timestamp is determined from the `published_at` field in the results.';
override readonly sourceUrlSupport = 'release';
override readonly sourceUrlNote =
'The source URL is determined from the `source_code_url` field of the release object in the results.';
private async _getReleases(
config: GetReleasesConfig,
): Promise<ReleaseResult | null> {
const result = Result.parse(config, ReleasesConfig)
.transform(({ packageName, registryUrl }) => {
const url = urlJoin(
registryUrl,
'api',
'v1',
'buildpacks',
packageName,
);
return this.http.getJsonSafe(url, BuildpacksRegistryResponse);
})
.transform(({ versions, latest }): ReleaseResult => {
const releases: Release[] = versions;
const res: ReleaseResult = { releases };
if (latest?.homepage) {
res.homepage = latest.homepage;
}
return res;
});
const { val, err } = await result.unwrap();
if (err instanceof ZodError) {
logger.debug({ err }, 'buildpacks: validation error');
return null;
}
if (err) {
this.handleGenericErrors(err);
}
return val;
}
getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null> {
return withCache(
{
namespace: `datasource-${BuildpacksRegistryDatasource.id}`,
key: `${config.registryUrl}:${config.packageName}`,
fallback: true,
},
() => this._getReleases(config),
);
}
}