import {z} from 'zod' export type OrderByPixel = 'created_at' | 'updated_at'; export type OrderDirection = 'asc' | 'desc'; export type PixelType = 'tiktok' | 'facebook' | 'google' | 'twitter'; export type GetApiApiV1PixelArgs = {order_by?: OrderByPixel | undefined, order_direction?: OrderDirection | undefined, filter_by_type?: PixelType | undefined, filterByCompanyUserId?: (Array | null) | undefined, all_available?: boolean | undefined, page?: number | undefined, size?: number | undefined}; export type GetApiApiV1PixelOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pixelType = z.enum(["tiktok", "facebook", "google", "twitter"]); export const ownerDTO = z.object({ownerCompanyUserId: z.string().nullable().optional(), email: z.string(), logoUrl: z.string().nullable().optional()}); export const pixelListDTO = z.object({pixel: z.string(), token: z.string(), comment: z.string().nullable().optional(), pixelType: pixelType, lastEditedBy: z.string().nullable().optional(), id: z.string(), createdAt: z.string(), updatedAt: z.string().nullable().optional(), owner: ownerDTO.nullable().optional()}); export const pagePixelListDTO = z.object({items: z.array(pixelListDTO), 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 my pixels * * GET /api/v1/pixel */ export const getApiApiV1Pixel = async (args: GetApiApiV1PixelArgs, options: GetApiApiV1PixelOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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_type != null) query.append("filter_by_type", String(args.filter_by_type)) 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/pixel${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(pagePixelListDTO, await res.json()) };