import {z} from 'zod' export type IdeaInput = {title?: string | undefined, caption?: string | undefined, tags?: Array | undefined, mediaUrls?: Array | undefined, mediaType?: ('image' | 'video') | undefined, status?: ('unassigned' | 'todo' | 'in_progress' | 'done') | undefined}; export type PatchApiIdeasIdArgs = {body: IdeaInput}; export type PatchApiIdeasIdOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const idea = z.object({title: z.string().optional(), caption: z.string().optional(), tags: z.array(z.string()).optional(), mediaUrls: z.array(z.string()).optional(), mediaType: z.enum(["image", "video"]).optional(), status: z.enum(["unassigned", "todo", "in_progress", "done"]).optional(), id: z.string().optional(), position: z.number().optional()}); export const patchApiIdeasIdResponse = z.object({idea: idea.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 }; /** * Update an idea (only sent fields are touched) * * PATCH /ideas/{id} */ export const patchApiIdeasId = async (args: PatchApiIdeasIdArgs, options?: PatchApiIdeasIdOptions) => { const {baseUrl = "https://api.posteahora.com/functions/v1/api", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/ideas/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiIdeasIdResponse, await res.json()) };