import {z} from 'zod' export type CreateApiApiSubscriptionTransfersIdRejectArgs = {id: string}; export type CreateApiApiSubscriptionTransfersIdRejectOptions = {baseUrl?: string | undefined, jwt?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiSubscriptionTransfersIdRejectResponse = z.object({_id: z.string(), sellerId: z.string(), subscriptionId: z.string(), subscriptionItemIds: z.array(z.string()), status: z.enum(["CREATED", "TRANSFERRED", "REJECTED", "CANCELED", "EXPIRED"]), sender: z.object({customerId: z.string()}), recipient: z.object({email: z.string(), customerId: z.string().optional()}), outcome: z.object({subscriptionId: z.string(), items: z.array(z.object({originSubscriptionItemId: z.string(), subscriptionItemId: z.string()}))}).optional(), history: z.array(z.object({type: z.enum(["created", "transferred", "rejected", "canceled", "expired"]), date: z.string(), userId: z.string().optional()})).optional(), expiresAt: z.string(), createdAt: z.string(), updatedAt: 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 }; /** * Reject a Subscription Transfer * * POST /api/subscription-transfers/{id}/reject * * Also accepts auth: apikey */ export const createApiApiSubscriptionTransfersIdReject = async (args: CreateApiApiSubscriptionTransfersIdRejectArgs, options?: CreateApiApiSubscriptionTransfersIdRejectOptions) => { const {baseUrl = "https://vivenu.com", fetch: fetchFn = fetch, jwt} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (jwt !== undefined) headers.set('authorization', `Bearer ${jwt}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/subscription-transfers/${encodeURIComponent(String(args.id))}/reject${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(createApiApiSubscriptionTransfersIdRejectResponse, await res.json()) };