import {z} from 'zod' export type RegistrationSchema = {base_url?: string | undefined, company_name?: (string | null) | undefined, country: string, date_created?: string | undefined, email: string, email_marketing_consent?: (boolean | null) | undefined, first_name: string, id?: string | undefined, last_name: string, marketplace_signup_nonce?: string | undefined, ordway_customer_id?: string | undefined, ordway_subscription_id?: string | undefined, password: string, referral_code?: string | undefined, stripe_id?: string | undefined}; export type CreateApiV1RegistrationsArgs = {body: RegistrationSchema}; export type CreateApiV1RegistrationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const registrationSchema = z.object({base_url: z.string().optional(), company_name: z.string().nullable().optional(), country: z.string(), date_created: z.string().optional(), email: z.string(), email_marketing_consent: z.boolean().nullable().optional(), first_name: z.string(), id: z.string().optional(), last_name: z.string(), marketplace_signup_nonce: z.string().optional(), ordway_customer_id: z.string().optional(), ordway_subscription_id: z.string().optional(), password: z.string(), referral_code: z.string().optional(), stripe_id: z.string().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 a new registration * * POST /v1/registrations/ */ export const createApiV1Registrations = async (args: CreateApiV1RegistrationsArgs, options: CreateApiV1RegistrationsOptions) => { 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(/\/+$/, '')}/v1/registrations/${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(registrationSchema, await res.json()) };