import {z} from 'zod' export type SfcApiImageId = string; export type CreateApiPreviewV2AdminImagesIdPublishArgs = {id: SfcApiImageId}; export type CreateApiPreviewV2AdminImagesIdPublishOptions = {baseUrl: string, sfcApiBearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const sfcApiImageId = z.string(); export const sfcApiImageResourcePathImageId = z.string(); export const sfcApiName = z.string().min(1).max(255); export const sfcApiImageUploadStatus = z.enum(["started", "uploading", "completed", "failed", "revoked"]); export const sfcApiUnixEpoch = z.number().int(); export const sfcApiImageListEntry = z.object({id: sfcApiImageId, resource_path: sfcApiImageResourcePathImageId, owner: sfcApiName, workspace: sfcApiName, name: sfcApiName, object: z.literal("image"), upload_status: sfcApiImageUploadStatus, created_at: sfcApiUnixEpoch, provider: z.string().nullable().optional(), sha256: z.string().min(64).max(64).nullable().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 }; /** * Publish image * * POST /preview/v2/admin/images/{id}/publish */ export const createApiPreviewV2AdminImagesIdPublish = async (args: CreateApiPreviewV2AdminImagesIdPublishArgs, options: CreateApiPreviewV2AdminImagesIdPublishOptions) => { const {baseUrl, fetch: fetchFn = fetch, sfcApiBearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (sfcApiBearerAuth !== undefined) headers.set('authorization', `Bearer ${sfcApiBearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/preview/v2/admin/images/${encodeURIComponent(String(args.id))}/publish${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(sfcApiImageListEntry, await res.json()) };