import {z} from 'zod' export type UpdateApiBatchBatchIdNewsIdArgs = {body: {data: {id: string, type: 'news', attributes: {category?: ('combat-operations' | 'miscellaneous' | 'afghanistan' | 'horn-of-africa' | 'us' | 'press-release') | undefined, title: string, description: string, body: string, notes?: string | undefined, tags?: Array | undefined, created_at: string, subdiv?: string | undefined, country: string, city?: string | undefined, status?: ('unpublished' | 'published' | 'needs-approval' | 'archived') | undefined}, relationships: {authors?: {data: Array<{id: string, type: 'author'}>} | undefined, batch?: {data: {id: string, type: 'batch'}} | undefined, hometowns?: {data: Array<{id: string, type: 'location'}>} | undefined, themes?: {data: Array<{id: string, type: 'theme'}>} | undefined, service_unit: {data: {id: string, type: 'service-unit'}}}}}}; export type UpdateApiBatchBatchIdNewsIdOptions = {baseUrl?: string | undefined, DVIDS?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiBatchBatchIdNewsIdResponse = z.object({data: z.object({id: z.string(), type: z.literal("news"), attributes: z.object({category: z.enum(["combat-operations", "miscellaneous", "afghanistan", "horn-of-africa", "us", "press-release"]).optional(), title: z.string().max(255), description: z.string().max(65535), body: z.string().max(65535), notes: z.string().max(65535).optional(), tags: z.array(z.string().max(100)).optional(), created_at: z.string(), subdiv: z.string().max(50).optional(), country: z.string().max(2), city: z.string().max(75).optional(), status: z.enum(["unpublished", "published", "needs-approval", "archived"]).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(), hometowns: z.object({data: z.array(z.object({id: z.string(), type: z.literal("location")}))}).optional(), 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 news story by id * * PUT /batch/{batch_id}/news/{id} */ export const updateApiBatchBatchIdNewsId = async (args: UpdateApiBatchBatchIdNewsIdArgs, options?: UpdateApiBatchBatchIdNewsIdOptions) => { 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))}/news/${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(updateApiBatchBatchIdNewsIdResponse, await res.json()) };