import {z} from 'zod' export type CreateApiSiloV1EntriesEntryIdFilesArgs = {entry_id: string, body: {category?: string | undefined, data?: string | undefined, desc?: string | undefined, embeddable?: boolean | undefined, key?: string | undefined, meta?: {[key: string]: string} | undefined, mime?: string | undefined, name: string, private?: boolean | undefined, sha256?: string | undefined, size?: number | undefined}}; export type CreateApiSiloV1EntriesEntryIdFilesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const siloFileVersion = z.object({created_at: z.string().optional(), hash: z.string().optional(), id: z.string().optional(), size: z.number().int().optional()}); export const siloFile = z.object({category: z.string().optional(), created_at: z.string().optional(), desc: z.string().optional(), embeddable: z.boolean().optional(), entry_id: z.string().optional(), hash: z.string().optional(), id: z.string().optional(), key: z.string().optional(), meta: z.record(z.string(), z.string()).optional(), mime: z.string().optional(), name: z.string().optional(), previous: z.array(siloFileVersion).optional(), private: z.boolean().optional(), size: z.number().int().optional(), stored: z.boolean().optional(), url: z.string().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 }; /** * Add a new file to a silo entry without an explicit UUID. The file data can be provided inline as base64 encoded data, or omitted to create a placeholder. When data is not provided, you must provide sha256, size, and mime fields, and the response will include a `url` field that can be used to stream/upload the file contents directly via a PUT request to that URL. For non-enrolled users, only the `format` and `attachment` categories are allowed. * * POST /silo/v1/entries/{entry_id}/files */ export const createApiSiloV1EntriesEntryIdFiles = async (args: CreateApiSiloV1EntriesEntryIdFilesArgs, options?: CreateApiSiloV1EntriesEntryIdFilesOptions) => { const {baseUrl = "https://api.invopop.com", 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(/\/+$/, '')}/silo/v1/entries/${encodeURIComponent(String(args.entry_id))}/files${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(siloFile, await res.json()) };