import {z} from 'zod' export type ImageBean = {buffer?: Array | undefined, originalFormat?: ('RAW' | 'RGB' | 'BMP' | 'BMP24BITS' | 'GIF' | 'JPEG' | 'JPEG2000' | 'PNG' | 'TIFF' | 'WSQ') | undefined, toFormat?: ('RAW' | 'RGB' | 'BMP' | 'BMP24BITS' | 'GIF' | 'JPEG' | 'JPEG2000' | 'PNG' | 'TIFF' | 'WSQ') | undefined, resolution?: number | undefined}; export type CreateApiImageConvertArgs = {body: ImageBean}; export type CreateApiImageConvertOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const imageBean = z.object({buffer: z.array(z.string()).optional(), originalFormat: z.enum(["RAW", "RGB", "BMP", "BMP24BITS", "GIF", "JPEG", "JPEG2000", "PNG", "TIFF", "WSQ"]).optional(), toFormat: z.enum(["RAW", "RGB", "BMP", "BMP24BITS", "GIF", "JPEG", "JPEG2000", "PNG", "TIFF", "WSQ"]).optional(), resolution: z.number().int().optional()}); export const imageResponse = z.object({status: z.enum(["OK", "ERROR", "BAD_REQUEST", "CREATED", "NO_CONTENT", "DELETED", "CANCELLED", "CLOSED", "REMOVED", "NOT_FOUND", "AUTHENTICATION_FAILED", "AUTHENTICATION_OK", "INVALID_SESSION_GUID", "UNAUTHORIZED", "FORBIDDEN", "RECEIVED", "ACCEPTED", "ENQUEUED", "INVALID_PARAMETERS", "FIELD_VALIDATION_ERROR", "ILLEGAL_STATE", "NOT_ENOUGH_DATA", "VALIDATION_ERROR", "IMPORTED", "UPDATED", "IGNORED", "SERVICE_STARTED", "SERVICE_CANCELLED", "SERVICE_SUBMITTED"]).optional(), code: z.number().int().optional(), image: imageBean.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 }; /** * convertImage * * POST /image/convert */ export const createApiImageConvert = async (args: CreateApiImageConvertArgs, options?: CreateApiImageConvertOptions) => { const {baseUrl = "http://:8085/gbds/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/image/convert${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(imageResponse, await res.json()) };