import {z} from 'zod' export type GetApiAssetsAssetIdDynamicSegmentsArgs = {asset_id: string, 'filter[scope]'?: ('local' | 'global' | 'all') | undefined, page?: number | undefined, per_page?: number | undefined}; export type GetApiAssetsAssetIdDynamicSegmentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const dynamicSegmentCondition = z.object({type: z.literal("condition"), merge_tag_id: z.string().nullable(), merge_tag_type: z.enum(["string", "number", "date", "relative_date", "boolean"]).optional(), operator: z.string(), value: z.union([z.string(), z.number()]).optional(), category: z.string().optional()}); export const dynamicSegmentConjunction: z.ZodType = z.object({type: z.literal("condition_set"), conjunction: z.enum(["and", "or"]).optional(), children: z.array(z.union([dynamicSegmentCondition, z.lazy(() => dynamicSegmentConjunction)]))}); export const platformDynamicSegment = z.object({id: z.string().optional(), platform: z.string().optional(), integration_id: z.string().optional(), conditional_logic: dynamicSegmentConjunction.optional()}); export const dynamicSegmentAttributes = z.object({id: z.string().optional(), name: z.string().optional(), asset_id: z.string().nullable().optional(), is_global: z.boolean().optional(), platform_dynamic_segments: z.array(platformDynamicSegment).optional()}); export const dynamicSegment = z.object({type: z.string().optional(), id: z.string().optional(), attributes: dynamicSegmentAttributes.optional()}); export const dynamicSegmentList = z.object({data: z.array(dynamicSegment).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 dynamic segments of an asset * * GET /assets/{asset_id}/dynamic-segments */ export const getApiAssetsAssetIdDynamicSegments = async (args: GetApiAssetsAssetIdDynamicSegmentsArgs, options?: GetApiAssetsAssetIdDynamicSegmentsOptions) => { 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[scope]'] != null) query.append("filter[scope]", String(args['filter[scope]'])) 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(/\/+$/, '')}/assets/${encodeURIComponent(String(args.asset_id))}/dynamic-segments${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(dynamicSegmentList, await res.json()) };