import {z} from 'zod' export type PhoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber = {value: string}; export type Point = {latitude: number, longitude: number}; export type NewStreetAddressDto = {name: string, line1: string, line2?: string | undefined, city: string, province: string, postalCode: string, telephone: PhoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber, instructions?: string | undefined, buildingType?: number | undefined, location?: Point | undefined}; export type DayId = 1 | 2 | 3 | 4 | 5 | 6 | 7; export type GoogleRecaptchaServiceRecaptchaTypes = 'V3' | 'V2'; export type RegistrationPropertiesOnboardingItem = Record; export type FeatureFlag = {id: string, data: {[key: string]: unknown}}; export type RegistrationProperties = {initialLandingPage: string | null, conversionLandingPage: string | null, hasBagSubscription: boolean, hasProductSubscription: boolean, preselectedItems: Array, onboardingItems: Array, ipv4Address: string, featureFlags: Array | null, couponCode: string | null}; export type ShoppingCartItem = Record; export type Coupon = Record; export type GuestShoppingCart = {sessionId?: string | undefined, date?: string | undefined, items?: Array | undefined, itemsSubtotal?: number | undefined, coupon?: (Coupon | null) | undefined, preselectedCartItems?: Array | undefined}; export type CustomerFactValue = Record; export type GuestCustomerFacts = {sessionId?: string | undefined, facts?: {[key: string]: CustomerFactValue} | undefined}; export type GuestRegistrationInfo = {deliveryAddress: NewStreetAddressDto, billingAddress: NewStreetAddressDto, isBillingAddressSameAsDelivery: boolean, deliveryDay: DayId, firstName: string, lastName: string, email: string, phone: PhoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber, password: string, stripeToken: string, captchaToken: string, captchaType: GoogleRecaptchaServiceRecaptchaTypes, couponCode: string, referralMethodId: number, subscribeToMarketingEmail: boolean, subscribeToMarketingSms: boolean, properties: RegistrationProperties}; export type GuestRegistrationInfoStorage = {sessionId?: string | undefined, guestRegistrationInfo?: GuestRegistrationInfo | undefined}; export type CreateApiOndSignupArgs = {body: {deliveryAddress: NewStreetAddressDto, billingAddress: NewStreetAddressDto, isBillingAddressSameAsDelivery: boolean, deliveryDay: DayId, firstName: string, lastName: string, email: string, phone: PhoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber, password: string, stripeToken: string, captchaToken: string, captchaType: GoogleRecaptchaServiceRecaptchaTypes, couponCode: string, referralMethodId: number, subscribeToMarketingEmail: boolean, subscribeToMarketingSms: boolean, properties: RegistrationProperties, guestCart?: (GuestShoppingCart | null) | undefined, guestFacts?: GuestCustomerFacts | undefined, guestRegistrationInfoStorage?: GuestRegistrationInfoStorage | undefined}}; export type CreateApiOndSignupOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const originPlatform = z.object({id: z.number().int(), name: z.string()}); export const userMembership = z.object({id: z.number().int(), userId: z.number().int(), fee: z.number(), isTrial: z.boolean(), startDate: z.string(), endDate: z.string(), renewedAt: z.string().nullable(), renewalCancelledAt: z.string().nullable(), renewalFailures: z.number().int()}); export const dayId = z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5), z.literal(6), z.literal(7)]); export const userCustomer = z.object({id: z.number().int(), originPlatform: originPlatform.nullable(), email: z.string(), firstName: z.string(), lastName: z.string(), membership: userMembership.nullable(), defaultDay: dayId, deliveryZoneId: z.number().int(), deliveryAddressId: z.number().int(), billingAddressId: z.number().int(), isActive: z.boolean(), hasBagSubscription: z.boolean(), hasProductSubscription: z.boolean(), lastLoginAt: 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 }; /** * POST /ond/signup */ export const createApiOndSignup = async (args: CreateApiOndSignupArgs, options: CreateApiOndSignupOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/ond/signup${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(userCustomer, await res.json()) };