import {z} from 'zod' export type PkgApiV1UpgradePluginsRequest = {allow_ref_change?: boolean | undefined, allow_signer_change?: boolean | undefined, clients?: Array | undefined, fail_on_changes?: boolean | undefined, names?: Array | undefined, preview?: boolean | undefined, project_root?: string | undefined, public_key?: string | undefined}; export type CreateApiApiV1betaPluginsUpgradeArgs = {body: Record | PkgApiV1UpgradePluginsRequest}; export type CreateApiApiV1betaPluginsUpgradeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const githubComStacklokToolhivePkgSkillsFailureReason = z.enum(["registry-unreachable", "digest-missing", "validation-rejected", "lock-write-failed", "signature-invalid", "signer-mismatch", "provenance-field-mismatch", "key-signed-unverifiable", "unsigned-rejected", "unknown"]); export const githubComStacklokToolhivePkgSkillsUpgradeStatus = z.enum(["upgraded", "up-to-date", "trust-updated", "not-upgradable", "ref-change-blocked", "signer-change-blocked", "failed"]); export const githubComStacklokToolhivePkgSkillsUpgradeOutcome = z.object({error: z.string().optional(), name: z.string().optional(), new_digest: z.string().optional(), new_resolved_reference: z.string().optional(), new_signer_identity: z.string().optional(), old_digest: z.string().optional(), reason: githubComStacklokToolhivePkgSkillsFailureReason.optional(), status: githubComStacklokToolhivePkgSkillsUpgradeStatus.optional(), trust_anchor_changed: z.boolean().optional()}); export const githubComStacklokToolhivePkgPluginsUpgradeResult = z.object({outcomes: z.array(githubComStacklokToolhivePkgSkillsUpgradeOutcome).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Upgrade project plugins * * POST /api/v1beta/plugins/upgrade */ export const createApiApiV1betaPluginsUpgrade = async (args: CreateApiApiV1betaPluginsUpgradeArgs, options: CreateApiApiV1betaPluginsUpgradeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1beta/plugins/upgrade${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(githubComStacklokToolhivePkgPluginsUpgradeResult, await res.json()) };