import {z} from 'zod' export type GetApiEntitiesParentSegmentsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleType = z.enum(["cron", "daily", "hourly", "minutes_interval", "monthly", "none", "weekly"]); export const relationshipsFolderJsonApiResource = z.object({data: z.object({id: z.string(), type: z.literal("folder-segment")}).optional()}); export const parentSegmentJsonApiResource = z.object({id: z.string(), type: z.literal("parentSegment"), attributes: z.object({name: z.string(), description: z.string(), matrixUpdatedAt: z.string().nullable(), population: z.number().nullable(), scheduleType: scheduleType, timezone: z.string().optional(), createdAt: z.string(), updatedAt: z.string(), v4Id: z.number(), allowActivationBehavior: z.boolean().optional(), maxActivationBehaviorRow: z.number().int().optional(), customerGroup: z.object({name: z.string(), population: z.number()}).nullable().optional()}), relationships: z.object({parentFolder: relationshipsFolderJsonApiResource.optional(), parentSegmentFolder: relationshipsFolderJsonApiResource.optional()})}); export const entitiesGetParentSegmentJsonApiResponse = z.object({data: parentSegmentJsonApiResource}); export const getApiEntitiesParentSegmentsResponse = z.array(entitiesGetParentSegmentJsonApiResponse); 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 }; /** * Retrieve list of parent segments * * GET /entities/parent_segments */ export const getApiEntitiesParentSegments = async (options?: GetApiEntitiesParentSegmentsOptions) => { const {baseUrl = "https://api-cdp.treasuredata.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/vnd.treasuredata.v1+json") const url = `${baseUrl.replace(/\/+$/, '')}/entities/parent_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(getApiEntitiesParentSegmentsResponse, await res.json()) };