import {z} from 'zod' export type GetApiThemesArgs = {'filter[name]'?: string | undefined, 'filter[tags]'?: string | undefined, 'filter[published]'?: boolean | undefined, sort?: string | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiThemesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const theme = z.object({type: z.string().optional(), id: z.string().optional(), attributes: z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), published: z.boolean().optional(), tags: z.array(z.unknown()).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional()}); export const themeList = z.object({data: z.array(theme).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 all themes * * GET /themes */ export const getApiThemes = async (args: GetApiThemesArgs, options?: GetApiThemesOptions) => { 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[name]'] != null) query.append("filter[name]", String(args['filter[name]'])) if (args['filter[tags]'] != null) query.append("filter[tags]", String(args['filter[tags]'])) if (args['filter[published]'] != null) query.append("filter[published]", String(args['filter[published]'])) if (args.sort != null) query.append("sort", String(args.sort)) 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(/\/+$/, '')}/themes${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(themeList, await res.json()) };