import {z} from 'zod' export type HealthConfig = {Test?: Array | undefined, Interval?: number | undefined, Timeout?: number | undefined, Retries?: number | undefined, StartPeriod?: number | undefined, StartInterval?: number | undefined}; export type ContainerConfig = {Hostname?: string | undefined, Domainname?: string | undefined, User?: string | undefined, AttachStdin?: boolean | undefined, AttachStdout?: boolean | undefined, AttachStderr?: boolean | undefined, ExposedPorts?: ({[key: string]: Record} | null) | undefined, Tty?: boolean | undefined, OpenStdin?: boolean | undefined, StdinOnce?: boolean | undefined, Env?: Array | undefined, Cmd?: Array | undefined, Healthcheck?: HealthConfig | undefined, ArgsEscaped?: (boolean | null) | undefined, Image?: string | undefined, Volumes?: {[key: string]: Record} | undefined, WorkingDir?: string | undefined, Entrypoint?: Array | undefined, NetworkDisabled?: (boolean | null) | undefined, MacAddress?: (string | null) | undefined, OnBuild?: (Array | null) | undefined, Labels?: {[key: string]: string} | undefined, StopSignal?: (string | null) | undefined, StopTimeout?: (number | null) | undefined, Shell?: (Array | null) | undefined}; export type CreateApiCommitArgs = {container?: string | undefined, repo?: string | undefined, tag?: string | undefined, comment?: string | undefined, author?: string | undefined, pause?: boolean | undefined, changes?: string | undefined, body: ContainerConfig}; export type CreateApiCommitOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const iDResponse = z.object({Id: 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 a new image from a container * * POST /commit */ export const createApiCommit = async (args: CreateApiCommitArgs, options: CreateApiCommitOptions) => { 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.container != null) query.append("container", String(args.container)) if (args.repo != null) query.append("repo", String(args.repo)) if (args.tag != null) query.append("tag", String(args.tag)) if (args.comment != null) query.append("comment", String(args.comment)) if (args.author != null) query.append("author", String(args.author)) if (args.pause != null) query.append("pause", String(args.pause)) if (args.changes != null) query.append("changes", String(args.changes)) const url = `${baseUrl.replace(/\/+$/, '')}/commit${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(iDResponse, await res.json()) };