import {z} from 'zod' export type GetApiDamFoldersFolderIdArgs = {folder_id: string, relationships?: string | undefined, includes?: string | undefined}; export type GetApiDamFoldersFolderIdOptions = {baseUrl: string, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const folderAttributes = z.object({name: z.string().optional(), description: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), sort: z.number().int().optional(), child_count: z.number().int().optional(), files_count: z.number().int().optional(), files_size: z.number().int().optional(), path: z.string().optional(), show_large_folder_name: z.boolean().optional(), hide_folder_name: z.boolean().optional(), thumbs_scale_to_fit: z.boolean().optional(), apply_folder_image_hue: z.boolean().optional(), is_archived: z.boolean().optional(), is_synced_mirror: z.boolean().nullable().optional(), mirror_design: z.boolean().optional()}); export const resourceIdentifierObject = z.object({type: z.string().optional(), id: z.string().optional()}); export const folderRelationships = z.object({folder_thumbnail_file: z.object({data: resourceIdentifierObject.optional()}).nullable().optional()}); export const folderLinks = z.object({self: z.string().optional()}); export const folderMeta = z.object({folder_type: z.enum(["folder", "album", "smart_folder", "mirror_folder", "archive_folder"]).optional(), is_locked: z.boolean().optional()}); export const folder = z.object({type: z.literal("folder"), id: z.string(), attributes: folderAttributes, relationships: folderRelationships.optional(), links: folderLinks.optional(), meta: folderMeta.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 }; /** * Returns a single DAM folder * * GET /dam/folders/{folder_id} */ export const getApiDamFoldersFolderId = async (args: GetApiDamFoldersFolderIdArgs, options: GetApiDamFoldersFolderIdOptions) => { 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))}${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(folder, await res.json()) };