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 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 GetApiOndSignupInfoArgs = {guestRegistrationInfoStorage: GuestRegistrationInfoStorage}; export type GetApiOndSignupInfoOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const phoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber = z.object({value: z.string()}); export const point = z.object({latitude: z.number(), longitude: z.number()}); export const newStreetAddressDto = z.object({name: z.string().min(1).max(255), line1: z.string().min(1).max(255), line2: z.string().max(255).optional(), city: z.string().min(1).max(255), province: z.string().min(2).max(2), postalCode: z.string().min(6).max(6), telephone: phoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber, instructions: z.string().max(255).optional(), buildingType: z.number().int().optional(), location: point.optional()}); 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 googleRecaptchaServiceRecaptchaTypes = z.enum(["V3", "V2"]); export const registrationPropertiesOnboardingItem = z.object({}); export const featureFlag = z.object({id: z.string(), data: z.record(z.string(), z.unknown())}); export const registrationProperties = z.object({initialLandingPage: z.string().nullable(), conversionLandingPage: z.string().nullable(), hasBagSubscription: z.boolean(), hasProductSubscription: z.boolean(), preselectedItems: z.array(registrationPropertiesOnboardingItem), onboardingItems: z.array(registrationPropertiesOnboardingItem), ipv4Address: z.string(), featureFlags: z.array(featureFlag).nullable(), couponCode: z.string().nullable()}); export const guestRegistrationInfo = z.object({deliveryAddress: newStreetAddressDto, billingAddress: newStreetAddressDto, isBillingAddressSameAsDelivery: z.boolean(), deliveryDay: dayId, firstName: z.string().min(1), lastName: z.string().min(1), email: z.string().min(1), phone: phoneNumberServiceNormalizedPhoneNumberNormalizedE164PhoneNumber, password: z.string().min(1), stripeToken: z.string().min(1), captchaToken: z.string().min(1), captchaType: googleRecaptchaServiceRecaptchaTypes, couponCode: z.string(), referralMethodId: z.number().int(), subscribeToMarketingEmail: z.boolean(), subscribeToMarketingSms: z.boolean(), properties: registrationProperties}); 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 }; /** * GET /ond/signup/info */ export const getApiOndSignupInfo = async (args: GetApiOndSignupInfoArgs, options: GetApiOndSignupInfoOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.guestRegistrationInfoStorage != null) query.append("guestRegistrationInfoStorage", JSON.stringify(args.guestRegistrationInfoStorage)) const url = `${baseUrl.replace(/\/+$/, '')}/ond/signup/info${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(guestRegistrationInfo, await res.json()) };