import {z} from 'zod' export type GetApiDamFoldersFolderIdFilesFileIdArgs = {folder_id: string, file_id: string, relationships?: string | undefined, includes?: string | undefined}; export type GetApiDamFoldersFolderIdFilesFileIdOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const fileAttributes = z.object({name: z.string().optional(), description: z.string().nullable().optional(), size: z.number().int().optional(), sort: z.number().int().optional(), created_at: z.string().optional(), updated_at: z.string().nullable().optional(), extension: z.string().optional(), width: z.number().int().nullable().optional(), height: z.number().int().nullable().optional(), orientation: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7), z.literal(8)]).optional(), rotation: z.union([z.literal(0), z.literal(90), z.literal(180), z.literal(270)]).optional(), version: z.number().int().optional(), is_archived: z.boolean().optional(), hasDuplicates: z.boolean().optional(), consent_status: z.string().optional(), consent_rights: z.array(z.string()).optional(), consent_party_ids: z.array(z.number().int()).optional(), consent_expire_at: z.string().nullable().optional()}); export const file = z.object({type: z.literal("file"), id: z.string(), attributes: fileAttributes}); 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 }; /** * Returns a single DAM file within a folder * * GET /dam/folders/{folder_id}/files/{file_id} */ export const getApiDamFoldersFolderIdFilesFileId = async (args: GetApiDamFoldersFolderIdFilesFileIdArgs, options: GetApiDamFoldersFolderIdFilesFileIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.api+json") if (args.relationships != null) query.append("relationships", String(args.relationships)) if (args.includes != null) query.append("includes", String(args.includes)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/dam/folders/${encodeURIComponent(String(args.folder_id))}/files/${encodeURIComponent(String(args.file_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(file, await res.json()) };