import {z} from 'zod' export type GetApiV2ContactsDeletionDeletionRequestIdArgs = {deletionRequestId: unknown}; export type GetApiV2ContactsDeletionDeletionRequestIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiBulkContactDeletionStatuses = z.enum(["Started", "Completed", "Failed", "NotAvailableInThisVersion"]); export const apiBulkContactDeletion = z.object({id: z.string().optional(), status: apiBulkContactDeletionStatuses.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 }; /** * Gets the status of a previously started request to delete contacts from a list. * * GET /v2/contacts/deletion/{deletionRequestId} */ export const getApiV2ContactsDeletionDeletionRequestId = async (args: GetApiV2ContactsDeletionDeletionRequestIdArgs, options?: GetApiV2ContactsDeletionDeletionRequestIdOptions) => { const {baseUrl = "https://api.dotdigital.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/contacts/deletion/${encodeURIComponent(String(args.deletionRequestId))}${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(apiBulkContactDeletion, await res.json()) };