import {z} from 'zod' export type UpdateApiBatchBatchIdGraphicIdArgs = {body: {data: {id: string, type: 'graphic', attributes: {title: string, description: string, instructions: string, caption_writer?: string | undefined, tags?: Array | undefined, created_at: string, virin: string, subdiv?: string | undefined, country: string, city?: string | undefined, status?: ('uploaded' | 'pending-processing' | 'needs-approval' | 'published' | 'archived') | undefined, thumbnail_url_template?: unknown | undefined}, relationships: {authors?: {data: Array<{id: string, type: 'author'}>} | undefined, batch?: {data: {id: string, type: 'batch'}} | undefined, batch_upload: {data: {id: string, type: 'batch-upload' | 'batch-multipart-upload'}}, category: {data: {id: string, type: 'graphic-category'}}, themes?: {data: Array<{id: string, type: 'theme'}>} | undefined, service_unit: {data: {id: string, type: 'service-unit'}}}}}}; export type UpdateApiBatchBatchIdGraphicIdOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiBatchBatchIdGraphicIdResponse = z.object({data: z.object({id: z.string(), type: z.literal("graphic"), attributes: z.object({title: z.string().max(255), description: z.string().max(65535), instructions: z.string().max(65535), caption_writer: z.string().max(255).optional(), tags: z.array(z.string().max(100)).optional(), created_at: z.string(), virin: z.string(), subdiv: z.string().max(50).optional(), country: z.string().max(2), city: z.string().max(75).optional(), status: z.enum(["uploaded", "pending-processing", "needs-approval", "published", "archived"]).optional(), thumbnail_url_template: z.string().optional()}), relationships: z.object({authors: z.object({data: z.array(z.object({id: z.string(), type: z.literal("author")}))}).optional(), batch: z.object({data: z.object({id: z.string(), type: z.literal("batch")})}).optional(), batch_upload: z.object({data: z.object({id: z.string(), type: z.enum(["batch-upload", "batch-multipart-upload"])})}), category: z.object({data: z.object({id: z.string(), type: z.literal("graphic-category")})}), themes: z.object({data: z.array(z.object({id: z.string(), type: z.literal("theme")}))}).optional(), service_unit: z.object({data: z.object({id: z.string(), type: z.literal("service-unit")})})})})}); 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 }; /** * Update a graphic by id * * PUT /batch/{batch_id}/graphic/{id} */ export const updateApiBatchBatchIdGraphicId = async (args: UpdateApiBatchBatchIdGraphicIdArgs, options?: UpdateApiBatchBatchIdGraphicIdOptions) => { const {baseUrl = "https://submitapi.dvidshub.net", fetch: fetchFn = fetch, DVIDS} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/vnd.api+json") headers.set('accept', "application/vnd.api+json") if (DVIDS !== undefined) headers.set('authorization', `Bearer ${DVIDS}`) const url = `${baseUrl.replace(/\/+$/, '')}/batch/${encodeURIComponent(String(args.batch_id))}/graphic/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiBatchBatchIdGraphicIdResponse, await res.json()) };