import {z} from 'zod' export type OrderByLanding = 'created_at' | 'updated_at'; export type OrderDirection = 'asc' | 'desc'; export type GetApiApiV1LandingArgs = {filter_by_name?: (string | null) | undefined, order_by?: (OrderByLanding | null) | undefined, order_direction?: OrderDirection | undefined, filter_by_template_id?: (string | null) | undefined, filterByCompanyUserId?: (Array | null) | undefined, all_available?: boolean | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiApiV1LandingOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const ownerDTO = z.object({ownerCompanyUserId: z.string().nullable().optional(), email: z.string(), logoUrl: z.string().nullable().optional()}); export const landingListDTO = z.object({id: z.string(), name: z.string(), comment: z.string().nullable(), htmlUrl: z.string(), userId: z.string(), createdAt: z.string(), updatedAt: z.string().nullable(), previewUrl: z.string().nullable().optional(), translations: z.array(z.string()).nullable().optional(), owner: ownerDTO.nullable().optional(), lastEditedBy: z.string().nullable().optional()}); export const pageLandingListDTO = z.object({items: z.array(landingListDTO), total: z.number().int().gte(0), page: z.number().int().gte(1), size: z.number().int().gte(1), pages: z.number().int().gte(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 list of custom landings * * GET /api/v1/landing */ export const getApiApiV1Landing = async (args: GetApiApiV1LandingArgs, options: GetApiApiV1LandingOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.filter_by_name != null) query.append("filter_by_name", String(args.filter_by_name)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.order_direction != null) query.append("order_direction", String(args.order_direction)) if (args.filter_by_template_id != null) query.append("filter_by_template_id", String(args.filter_by_template_id)) for (const item of args.filterByCompanyUserId ?? []) query.append("filterByCompanyUserId", String(item)) if (args.all_available != null) query.append("all_available", String(args.all_available)) if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/landing${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(pageLandingListDTO, await res.json()) };