import {z} from 'zod' export type GetApiV1StylesBasicOptions = {baseUrl?: string | undefined, auth0?: string | undefined, fetch?: typeof fetch | undefined}; export const transformModel = z.enum(["recraftv2", "recraftv2_raster", "recraftv2_vector", "recraftv3", "recraftv3_raster", "recraftv3_vector", "recraftv4", "recraftv4_raster", "recraftv4_vector", "recraftv4_pro", "recraftv4_pro_raster", "recraftv4_pro_vector", "recraftv4_1", "recraftv4_1_raster", "recraftv4_1_vector", "recraftv4_1_pro", "recraftv4_1_pro_raster", "recraftv4_1_pro_vector", "recraftv4_1_utility", "recraftv4_1_utility_raster", "recraftv4_1_utility_vector", "recraftv4_1_utility_pro", "recraftv4_1_utility_pro_raster", "recraftv4_1_utility_pro_vector", "recraftv4_styles", "recraftv4_styles_raster", "recraftv4_styles_vector", "recraftv4_styles_pro", "recraftv4_styles_pro_raster", "recraftv4_styles_pro_vector", "recraftv4_1_flash", "recraftv4_1_flash_raster"]); export const basicStyle = z.object({model: transformModel, style: z.string(), style_id: z.string()}); export const listBasicStylesResponse = z.object({styles: z.array(basicStyle)}); 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 }; /** * List Basic Styles * * GET /v1/styles/basic */ export const getApiV1StylesBasic = async (options?: GetApiV1StylesBasicOptions) => { const {baseUrl = "https://external.api.recraft.ai", fetch: fetchFn = fetch, auth0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (auth0 !== undefined) headers.set('authorization', `Bearer ${auth0}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/styles/basic${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(listBasicStylesResponse, await res.json()) };