import {z} from 'zod' export type GetApiFolderArgs = {parent_uuid?: (string | null) | undefined, name_contains?: (string | null) | undefined, public?: (boolean | null) | undefined, starred?: (boolean | null) | undefined, project_uuid?: (string | null) | undefined, page?: number | undefined, size?: number | undefined, sort?: string | undefined, asc?: boolean | undefined, created_after?: (string | null) | undefined, created_before?: (string | null) | undefined, updated_after?: (string | null) | undefined, updated_before?: (string | null) | undefined, access_token?: (string | null) | undefined}; export type GetApiFolderOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const folder = z.object({name: z.string().nullable().optional(), parent_uuid: z.string().nullable().optional(), notes: z.string().optional(), starred: z.boolean().optional(), public: z.boolean().optional(), shared_with_rowan: z.boolean().optional(), created_at: z.string(), updated_at: z.string().nullable().optional(), public_until: z.string().nullable().optional(), is_root: z.boolean(), uuid: z.string(), ancestor_is_public: z.boolean().optional(), project_uuid: z.string().nullable().optional(), is_temporarily_public: z.boolean()}); export const folderList = z.object({folders: z.array(folder), num_pages: z.number().int().gt(0)}); 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 }; /** * Get Folders * * GET /folder */ export const getApiFolder = async (args: GetApiFolderArgs, options: GetApiFolderOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() const cookies: string[] = [] headers.set('accept', "application/json") if (args.parent_uuid != null) query.append("parent_uuid", String(args.parent_uuid)) if (args.name_contains != null) query.append("name_contains", String(args.name_contains)) if (args.public != null) query.append("public", String(args.public)) if (args.starred != null) query.append("starred", String(args.starred)) if (args.project_uuid != null) query.append("project_uuid", String(args.project_uuid)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.asc != null) query.append("asc", String(args.asc)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.updated_after != null) query.append("updated_after", String(args.updated_after)) if (args.updated_before != null) query.append("updated_before", String(args.updated_before)) if (args.access_token != null) cookies.push("access_token=" + encodeURIComponent(String(args.access_token))) if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) if (cookies.length > 0) headers.set('cookie', cookies.join('; ')) const url = `${baseUrl.replace(/\/+$/, '')}/folder${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(folderList, await res.json()) };