import {z} from 'zod' export type GetApiAssetFoldersArgs = {'filter[brand_id]'?: string | undefined, 'filter[parent_folder_id]'?: string | undefined, 'filter[type]'?: ('folder' | 'campaign') | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiAssetFoldersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const assetFolder = z.object({type: z.string().optional(), id: z.string().optional(), attributes: z.object({id: z.string().optional(), name: z.string().optional(), parent_folder_id: z.string().nullable().optional(), brand_id: z.string().optional(), type: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional()}); export const assetFolderList = z.object({data: z.array(assetFolder).optional(), links: z.object({first: z.string().optional(), last: z.string().optional(), prev: z.string().nullable().optional(), next: z.string().nullable().optional()}).optional(), meta: z.object({current_page: z.number().int().optional(), from: z.number().int().optional(), last_page: z.number().int().optional(), links: z.array(z.object({url: z.string().nullable().optional(), label: z.string().optional(), active: z.boolean().optional()})).optional(), path: z.string().optional(), per_page: z.number().int().optional(), to: z.number().int().optional(), total: z.number().int().optional()}).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 }; /** * List asset folders * * GET /asset-folders */ export const getApiAssetFolders = async (args: GetApiAssetFoldersArgs, options?: GetApiAssetFoldersOptions) => { const {baseUrl = "https://enterprise.knak.io/api/published/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['filter[brand_id]'] != null) query.append("filter[brand_id]", String(args['filter[brand_id]'])) if (args['filter[parent_folder_id]'] != null) query.append("filter[parent_folder_id]", String(args['filter[parent_folder_id]'])) if (args['filter[type]'] != null) query.append("filter[type]", String(args['filter[type]'])) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) const url = `${baseUrl.replace(/\/+$/, '')}/asset-folders${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(assetFolderList, await res.json()) };