import {z} from 'zod' export type DeleteApiEntitiesPredictiveSegmentsIdArgs = {id: number}; export type DeleteApiEntitiesPredictiveSegmentsIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const predictiveSegmentAccuracy = z.number().gte(0).lte(100).nullable(); export const predictiveSegmentAreaUnderRocCurve = z.number().gte(0).lte(1).nullable(); export const predictiveSegmentGradeThresholds = z.array(z.number().gte(0).lte(100)); export const predictiveSegmentJsonApiResourceAttr = z.object({audienceId: z.string().optional(), baseSegmentId: z.string().nullable().optional(), segmentId: z.string().optional(), scoredSegmentId: z.string().nullable().optional(), name: z.string().optional(), description: z.string().nullable().optional(), categoricalAsColumnNames: z.array(z.string()).optional(), categoricalArrayAsColumnNames: z.array(z.string()).optional(), quantitativeAsColumnNames: z.array(z.string()).optional(), accuracy: predictiveSegmentAccuracy.optional(), areaUnderRocCurve: predictiveSegmentAreaUnderRocCurve.optional(), gradeThresholds: predictiveSegmentGradeThresholds.optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), modelUpdatedAt: z.string().optional()}); export const userJsonApiResource = z.object({id: z.string(), type: z.literal("user"), attributes: z.object({tdUserId: z.string(), name: z.string()})}); export const entitiesGetPredictiveSegmentJsonApiResponse = z.object({data: predictiveSegmentJsonApiResourceAttr, included: z.array(userJsonApiResource)}); 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 }; /** * Delete predictive scoring model * * DELETE /entities/predictive_segments/{id} */ export const deleteApiEntitiesPredictiveSegmentsId = async (args: DeleteApiEntitiesPredictiveSegmentsIdArgs, options?: DeleteApiEntitiesPredictiveSegmentsIdOptions) => { 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/predictive_segments/${encodeURIComponent(String(args.id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(entitiesGetPredictiveSegmentJsonApiResponse, await res.json()) };