import {z} from 'zod' export type GetApiApiV1FolderSearchArgs = {name?: string | undefined, path?: string | undefined, recursive?: boolean | undefined, siteId?: string | undefined, orderby?: ('name' | 'mod_date') | undefined, direction?: ('ASC' | 'DESC') | undefined, page?: number | undefined, per_page?: number | undefined, includePermissions?: boolean | undefined}; export type GetApiApiV1FolderSearchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const errorEntity = z.object({errorCode: z.string().optional(), message: z.string().optional(), fieldName: z.string().optional()}); export const folderSearchView = z.object({id: z.string().optional(), inode: z.string().optional(), name: z.string().optional(), path: z.string().optional(), addChildrenAllowed: z.boolean().optional(), hasChildren: z.boolean().optional(), defaultBaseType: z.string().optional(), title: z.string().optional(), sortOrder: z.number().int().optional(), filesMasks: z.string().optional(), defaultFileType: z.string().optional(), showOnMenu: z.boolean().optional(), permissions: z.array(z.enum(["READ", "EDIT", "PUBLISH", "EDIT_PERMISSIONS", "CAN_ADD_CHILDREN"])).optional()}); export const messageEntity = z.object({message: z.string().optional()}); export const pagination = z.object({currentPage: z.number().int().optional(), perPage: z.number().int().optional(), totalEntries: z.number().int().optional()}); export const responseEntityFolderSearchView = z.object({errors: z.array(errorEntity).optional(), entity: z.array(folderSearchView).optional(), messages: z.array(messageEntity).optional(), i18nMessagesMap: z.record(z.string(), z.string()).optional(), permissions: z.array(z.string()).optional(), pagination: pagination.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 }; /** * Search folders * * GET /api/v1/folder/search */ export const getApiApiV1FolderSearch = async (args: GetApiApiV1FolderSearchArgs, options: GetApiApiV1FolderSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.name != null) query.append("name", String(args.name)) if (args.path != null) query.append("path", String(args.path)) if (args.recursive != null) query.append("recursive", String(args.recursive)) if (args.siteId != null) query.append("siteId", String(args.siteId)) if (args.orderby != null) query.append("orderby", String(args.orderby)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.page != null) query.append("page", String(args.page)) if (args.per_page != null) query.append("per_page", String(args.per_page)) if (args.includePermissions != null) query.append("includePermissions", String(args.includePermissions)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/folder/search${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(responseEntityFolderSearchView, await res.json()) };