import {z} from 'zod' export type CustomerSubscriptionEmailSchema = {email: string, subscriptions: {marketing?: ({subscriptionStatus: 'subscribed' | 'unsubscribed', triggerAutomations?: boolean | undefined, updatedAt?: string | undefined}) | undefined}}; export type SubscriptionStatusMarketingSchema = {subscriptionStatus: 'subscribed' | 'confirmed' | 'unsubscribed', triggerAutomations?: boolean | undefined, updatedAt?: string | undefined}; export type SubscriptionStatusTransactionalSchema = {subscriptionStatus: 'subscribed' | 'unsubscribed', updatedAt?: string | undefined}; export type CustomerSubscriptionSmsSchema = {phoneNumber: string, subscriptions: {marketing?: SubscriptionStatusMarketingSchema | undefined, transactional?: SubscriptionStatusTransactionalSchema | undefined}}; export type CreateApiStoresStoreIdCustomerSubscriptionsArgs = {storeId: string, body: {email?: CustomerSubscriptionEmailSchema | undefined, sms?: CustomerSubscriptionSmsSchema | undefined}}; export type CreateApiStoresStoreIdCustomerSubscriptionsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const customerSubscriptionUpdateResponseSchema = z.object({message: z.string(), success: z.boolean(), updatedSubscriptions: z.object({email: z.object({marketing: z.boolean().optional()}).optional(), sms: z.object({marketing: z.boolean().optional(), transactional: z.boolean().optional()}).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 }; /** * Update Customer Subscriptions * * POST /stores/{storeId}/customer-subscriptions */ export const createApiStoresStoreIdCustomerSubscriptions = async (args: CreateApiStoresStoreIdCustomerSubscriptionsArgs, options?: CreateApiStoresStoreIdCustomerSubscriptionsOptions) => { const {baseUrl = "https://api.getredo.com/v2.2", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/stores/${encodeURIComponent(String(args.storeId))}/customer-subscriptions${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(customerSubscriptionUpdateResponseSchema, await res.json()) };