import {z} from 'zod' export type CreateApiApiV1ReposUserIdRepoPackagesJsonArgs = {user_id: string, repo: string, body: BodyInit}; export type CreateApiApiV1ReposUserIdRepoPackagesJsonOptions = {baseUrl?: string | undefined, apiToken?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const packageDetails = z.object({name: z.string(), distro_version: z.string(), architecture: z.string().nullable(), repository: z.string(), size: z.string(), summary: z.string().nullable(), filename: z.string(), description: z.string().nullable(), md5sum: z.string().nullable(), sha1sum: z.string().nullable(), sha256sum: z.string().nullable(), sha512sum: z.string().nullable(), private: z.boolean(), uploader_name: z.string(), created_at: z.string(), licenses: z.array(z.string()), version: z.string(), release: z.string().nullable(), epoch: z.number().int().nullable(), indexed: z.boolean(), scope: z.string().nullable(), repository_html_url: z.string(), versions_url: z.string(), promote_url: z.string(), total_downloads_count: z.number().int(), download_url: z.string(), package_html_url: z.string(), downloads_detail_url: z.string(), downloads_series_url: z.string(), downloads_count_url: z.string(), destroy_url: z.string(), self_url: z.string(), dependencies: z.array(z.object({context: z.string().optional(), dependencies: z.array(z.string()).optional()})).nullable(), repository_url: z.string(), package_url: z.string()}); 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 * * POST /api/v1/repos/{user_id}/{repo}/packages.json */ export const createApiApiV1ReposUserIdRepoPackagesJson = async (args: CreateApiApiV1ReposUserIdRepoPackagesJsonArgs, options?: CreateApiApiV1ReposUserIdRepoPackagesJsonOptions) => { const {baseUrl = "https://packagecloud.io", fetch: fetchFn = fetch, apiToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiToken !== undefined) headers.set('authorization', `Basic ${btoa(`${apiToken.username}:${apiToken.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/repos/${encodeURIComponent(String(args.user_id))}/${encodeURIComponent(String(args.repo))}/packages.json${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(packageDetails, await res.json()) };