import {z} from 'zod' export type ArtifactGrantRequest = {artifact_id: string, version: string}; export type CreateApiV1LicenseArtifactsDownloadArgs = {'X-Kugel-License-Key': string, body: ArtifactGrantRequest}; export type CreateApiV1LicenseArtifactsDownloadOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const artifactGrantResponse = z.object({artifact_id: z.string(), version: z.string(), download_url: z.string(), expires_in_seconds: z.number().int(), ciphertext_size: z.number().int(), ciphertext_sha256: z.string(), archive_format: z.string(), compression: z.string(), encryption_format: z.string(), decryption_key_b64: z.string(), associated_data_b64: z.string(), recommended_part_size_bytes: z.number().int(), recommended_concurrency: z.number().int()}); 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 }; /** * Create Artifact Grant * * POST /v1/license/artifacts/download */ export const createApiV1LicenseArtifactsDownload = async (args: CreateApiV1LicenseArtifactsDownloadArgs, options: CreateApiV1LicenseArtifactsDownloadOptions) => { 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") if (args['X-Kugel-License-Key'] != null) headers.set("X-Kugel-License-Key", String(args['X-Kugel-License-Key'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/license/artifacts/download${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(artifactGrantResponse, await res.json()) };