import {z} from 'zod' export type EntityTypeEnum = 'unset' | 'c_corp' | 'p_corp' | 'llc' | 'llp' | 'lp' | 'gp'; export type TaxClassEnum = 'unset' | 'corp' | 's_corp' | 'partnership' | 'disregarded' | 'tax_exempt'; export type LLCManagedTypeEnum = 'member_managed' | 'manager_managed'; export type Address = {id?: (string | null) | undefined, address_line_1: string, address_line_2?: (string | null) | undefined, city: string, state: string, country: string, postal_code: string}; export type AccountingMethod = 'accrual' | 'cash'; export type AccountingPeriod = 'fiscal' | 'calendar'; export type AccountingYearEndMonthNumber = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12; export type EmbeddedSignupRequest = {user_email: string, business_name: string, entity_type: EntityTypeEnum, tax_classification: TaxClassEnum, incorporation_location: string, incorporation_date: string, ein?: (string | null) | undefined, llc_managed_type?: (LLCManagedTypeEnum | null) | undefined, business_purpose?: (string | null) | undefined, business_phone?: (string | null) | undefined, business_email?: (string | null) | undefined, business_address?: (Address | null) | undefined, mailing_address?: (Address | null) | undefined, naics_code?: (string | null) | undefined, accounting_method?: (AccountingMethod | null) | undefined, accounting_period?: (AccountingPeriod | null) | undefined, accounting_year_end?: (AccountingYearEndMonthNumber | null) | undefined, futa_liability_year?: (string | null) | undefined, external_company_id?: (string | null) | undefined, authorization_callback_url?: (string | null) | undefined}; export type CreateApiAccountsSignupArgs = {verbose?: boolean | undefined, authorization?: (string | null) | undefined, body: EmbeddedSignupRequest}; export type CreateApiAccountsSignupOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const embeddedSignupResponse = z.object({legal_entity_id: z.string(), authorization_url: z.string().nullable().optional(), url: z.string().nullable().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 }; /** * Embedded Signup * * POST /accounts/signup */ export const createApiAccountsSignup = async (args: CreateApiAccountsSignupArgs, options: CreateApiAccountsSignupOptions) => { 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") if (args.verbose != null) query.append("verbose", String(args.verbose)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/accounts/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(embeddedSignupResponse, await res.json()) };