import {z} from 'zod' export type CreateApiV1CustomersCustomerIdPermanentDeletionOptions = {baseUrl?: string | undefined, XAppId?: string | undefined, XAppToken?: string | undefined, fetch?: typeof fetch | undefined}; export const customersPermanentDeletionCreateResponseBody = z.object({id: z.string(), created_at: z.string(), related_object_id: z.string(), related_object: z.string(), status: z.string(), data_json: z.object({events: z.number().int(), customer_events: z.number().int(), daily_events: z.number().int(), segments: z.number().int(), orders: z.number().int(), order_events: z.number().int(), customer: z.number().int()}), object: z.string()}); 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 Customer Permanently * * POST /v1/customers/{customerId}/permanent-deletion * * Also accepts auth: X-Voucherify-OAuth */ export const createApiV1CustomersCustomerIdPermanentDeletion = async (options?: CreateApiV1CustomersCustomerIdPermanentDeletionOptions) => { const {baseUrl = "https://api.voucherify.io", fetch: fetchFn = fetch, XAppId, XAppToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (XAppId !== undefined) headers.set("X-App-Id", XAppId) if (XAppToken !== undefined) headers.set("X-App-Token", XAppToken) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customers/${encodeURIComponent(String(args.customerId))}/permanent-deletion${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(customersPermanentDeletionCreateResponseBody, await res.json()) };