import {z} from 'zod' export type CouponDeletionFilters = {createdBefore?: string | undefined, createdAfter?: string | undefined, startsAfter?: string | undefined, startsBefore?: string | undefined, valid?: ('expired' | 'validNow' | 'validFuture') | undefined, usable?: boolean | undefined, redeemed?: boolean | undefined, recipientIntegrationId?: string | undefined, exactMatch?: boolean | undefined, value?: string | undefined, batchId?: string | undefined, referralId?: number | undefined, expiresAfter?: string | undefined, expiresBefore?: string | undefined}; export type NewCouponDeletionJob = {filters: CouponDeletionFilters}; export type CreateApiV1ApplicationsApplicationIdCampaignsCampaignIdCouponsDeletionJobsArgs = {applicationId: number, campaignId: number, body: NewCouponDeletionJob}; export type CreateApiV1ApplicationsApplicationIdCampaignsCampaignIdCouponsDeletionJobsOptions = {baseUrl?: string | undefined, managerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const asyncCouponDeletionJobResponse = z.object({id: z.number().int()}); 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 }; /** * Creates a coupon deletion job * * POST /v1/applications/{applicationId}/campaigns/{campaignId}/coupons_deletion_jobs * * Also accepts auth: management_key */ export const createApiV1ApplicationsApplicationIdCampaignsCampaignIdCouponsDeletionJobs = async (args: CreateApiV1ApplicationsApplicationIdCampaignsCampaignIdCouponsDeletionJobsArgs, options?: CreateApiV1ApplicationsApplicationIdCampaignsCampaignIdCouponsDeletionJobsOptions) => { const {baseUrl = "https://yourbaseurl.talon.one", fetch: fetchFn = fetch, managerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (managerAuth !== undefined) headers.set("Authorization", managerAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/applications/${encodeURIComponent(String(args.applicationId))}/campaigns/${encodeURIComponent(String(args.campaignId))}/coupons_deletion_jobs${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(asyncCouponDeletionJobResponse, await res.json()) };