import {z} from 'zod' export type AppEndpointsExternalV1GenerateRotationImageSize = {width: number, height: number}; export type CameraView = 'side' | 'low top-down' | 'high top-down'; export type Direction = 'north' | 'north-east' | 'east' | 'south-east' | 'south' | 'south-west' | 'west' | 'north-west'; export type Base64Image = {type?: 'base64' | undefined, base64: string}; export type RotateRequest = {image_size: AppEndpointsExternalV1GenerateRotationImageSize, image_guidance_scale?: number | undefined, view_change?: (number | null) | undefined, direction_change?: (number | null) | undefined, from_view?: (CameraView | null) | undefined, to_view?: (CameraView | null) | undefined, from_direction?: (Direction | null) | undefined, to_direction?: (Direction | null) | undefined, isometric?: boolean | undefined, oblique_projection?: boolean | undefined, init_image?: (Base64Image | null) | undefined, init_image_strength?: number | undefined, mask_image?: (Base64Image | null) | undefined, from_image: Base64Image, color_image?: (Base64Image | null) | undefined, seed?: (number | null) | undefined}; export type CreateApiRotateArgs = {body: RotateRequest}; export type CreateApiRotateOptions = {baseUrl: string, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const usage = z.object({type: z.enum(["usd", "generations"]).optional(), usd: z.number().nullable().optional(), generations: z.number().nullable().optional()}); export const base64Image = z.object({type: z.literal("base64").optional(), base64: z.string()}); export const rotateResponse = z.object({usage: usage, image: base64Image}); 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 }; /** * Rotate character or object * * POST /rotate */ export const createApiRotate = async (args: CreateApiRotateArgs, options: CreateApiRotateOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/rotate${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(rotateResponse, await res.json()) };