import {z} from 'zod' export type UserRequestPreferences = {dateFormat?: string | undefined, timeZone?: string | undefined, currency?: ('AUD' | 'BRL' | 'CAD' | 'EUR' | 'GBP' | 'HKD' | 'IDR' | 'INR' | 'JPY' | 'NZD' | 'SGD' | 'USD' | 'ZAR' | 'CNY' | 'VND' | 'MYR' | 'CHF') | undefined, locale?: ('en_US' | 'en_ES' | 'fr_CA' | 'zh_CN') | undefined}; export type UserAddress = {zip?: string | undefined, country?: string | undefined, address3?: string | undefined, address2?: string | undefined, city?: string | undefined, address1?: string | undefined, state?: string | undefined}; export type Name = {middle?: string | undefined, last?: string | undefined, fullName?: string | undefined, first?: string | undefined}; export type UserRegistration = {preferences?: UserRequestPreferences | undefined, address?: UserAddress | undefined, loginName: string, name?: Name | undefined, email?: string | undefined, segmentName?: string | undefined}; export type UserRequest = {user: UserRegistration}; export type CreateApiUserRegisterArgs = {body: UserRequest}; export type CreateApiUserRegisterOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const userResponsePreferences = z.object({dateFormat: z.string().min(1).max(2147483647).optional(), timeZone: z.string().min(1).max(2147483647).optional(), currency: z.enum(["AUD", "BRL", "CAD", "EUR", "GBP", "HKD", "IDR", "INR", "JPY", "NZD", "SGD", "USD", "ZAR", "CNY", "VND", "MYR", "CHF"]).optional(), locale: z.enum(["en_US", "en_ES", "fr_CA", "zh_CN"]).optional()}); export const userSession = z.object({userSession: z.string().optional()}); export const name = z.object({middle: z.string().optional(), last: z.string().optional(), fullName: z.string().optional(), first: z.string().optional()}); export const user = z.object({preferences: userResponsePreferences.optional(), session: userSession.optional(), loginName: z.string().optional(), name: name.optional(), id: z.number().int().optional(), roleType: z.literal("INDIVIDUAL").optional()}); export const userResponse = z.object({user: user.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 }; /** * Register User * * POST /user/register */ export const createApiUserRegister = async (args: CreateApiUserRegisterArgs, options: CreateApiUserRegisterOptions) => { 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;charset=UTF-8") const url = `${baseUrl.replace(/\/+$/, '')}/user/register${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(userResponse, await res.json()) };