import {z} from 'zod' export type CreateCustomerForm = {emailAddress: string}; export type CreateApiStoresStoreIdCustomersArgs = {storeId: string, body: CreateCustomerForm}; export type CreateApiStoresStoreIdCustomersOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const storeAffiliate = z.object({id: z.string(), dateCreated: z.string(), storeId: z.string(), customerId: z.string(), isEnabled: z.boolean(), link: z.string().min(1).max(256), returnPercentage: z.number().int().nullable().optional(), percentageOff: z.number().int().nullable().optional(), balance: z.number()}); export const externalIntegration = z.object({id: z.string(), dateCreated: z.string(), accountId: z.string().max(256), platformId: z.string().max(64), expirationDate: z.string().nullable().optional()}); export const ticketStatus = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const ticketEntry = z.object({id: z.string(), dateCreated: z.string(), name: z.string().min(1).max(50), message: z.string().min(1).max(2048), ticketId: z.string()}); export const ticket = z.object({id: z.string(), dateCreated: z.string(), subject: z.string().min(1).max(128), status: ticketStatus, customerId: z.string(), userId: z.string().min(1).max(36), storeId: z.string(), entries: z.array(ticketEntry)}); export const subscriptionStatus = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const billingInterval = z.union([z.literal(0), z.literal(1), z.literal(2), z.literal(3), z.literal(4)]); export const customerSubscription = z.object({id: z.string(), dateCreated: z.string(), customerId: z.string(), customerStoreDataId: z.string(), storeId: z.string(), productId: z.string(), variantId: z.string(), status: subscriptionStatus, billingInterval: billingInterval, customIntervalDays: z.number().int().nullable().optional(), price: z.number(), currencyCode: z.string().max(4), currentPeriodStart: z.string(), currentPeriodEnd: z.string(), cancelAtPeriodEnd: z.boolean(), canceledAt: z.string().nullable().optional(), pausedAt: z.string().nullable().optional(), pauseResumeDate: z.string().nullable().optional(), pauseCount: z.number().int(), gateway: z.string().max(64), gatewaySubscriptionId: z.string().max(256).nullable().optional(), gatewayCustomerId: z.string().max(256).nullable().optional(), autoRenew: z.boolean(), failedPaymentCount: z.number().int(), lastPaymentFailureDate: z.string().nullable().optional(), nextRetryDate: z.string().nullable().optional(), lastReminderSentAt: z.string().nullable().optional(), metadataJson: z.string().max(4096).nullable().optional()}); export const emailMarketingCustomerOptions = z.object({id: z.string(), dateCreated: z.string(), isEnabled: z.boolean(), disabledAt: z.string().nullable().optional(), consentSource: z.string().max(64).nullable().optional(), consentTimestamp: z.string().nullable().optional(), consentIpAddress: z.string().max(45).nullable().optional(), consentWithdrawnAt: z.string().nullable().optional(), customerId: z.string(), customerStoreDataId: z.string()}); export const customerStoreData = z.object({id: z.string(), dateCreated: z.string(), balance: z.number(), affiliate: storeAffiliate.optional(), externalIntegrations: z.array(externalIntegration), tickets: z.array(ticket), subscriptions: z.array(customerSubscription), gatewayCustomerIds: z.record(z.string(), z.string()), stripeCustomerId: z.string().max(64).nullable().optional(), mollieCustomerId: z.string().max(64).nullable().optional(), storeId: z.string(), customerId: z.string(), customer: z.lazy(() => customer), marketingOptions: emailMarketingCustomerOptions.optional(), isBlocked: z.boolean(), blockedNote: z.string().max(2000).nullable().optional(), blockedAt: z.string().nullable().optional(), ipAddress: z.string().max(45)}); export const customer: z.ZodType = z.object({id: z.string(), dateCreated: z.string(), emailAddress: z.string().min(3).max(256), ipAddress: z.string().max(45), storeData: z.array(customerStoreData)}); export const customerResponse = z.object({success: z.boolean(), message: z.string().nullable().optional(), code: z.string().nullable().optional(), data: customer.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 }; /** * Create customer * * POST /stores/{storeId}/customers */ export const createApiStoresStoreIdCustomers = async (args: CreateApiStoresStoreIdCustomersArgs, options?: CreateApiStoresStoreIdCustomersOptions) => { const {baseUrl = "https://api.komerza.com", 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))}/customers${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(customerResponse, await res.json()) };