import {z} from 'zod' export type StateCode = 'AL' | 'AK' | 'AZ' | 'AR' | 'CA' | 'CO' | 'CT' | 'DE' | 'DC' | 'FL' | 'GA' | 'HI' | 'ID' | 'IL' | 'IN' | 'IA' | 'KS' | 'KY' | 'LA' | 'ME' | 'MD' | 'MA' | 'MI' | 'MN' | 'MS' | 'MO' | 'MT' | 'NE' | 'NV' | 'NH' | 'NJ' | 'NM' | 'NY' | 'NC' | 'ND' | 'OH' | 'OK' | 'OR' | 'PA' | 'RI' | 'SC' | 'SD' | 'TN' | 'TX' | 'UT' | 'VT' | 'VA' | 'WA' | 'WV' | 'WI' | 'WY'; 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 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 AppPlatformPublicHostedSignupRequestBusinessInformation = {incorporation_region?: (StateCode | null) | undefined, entity_type?: (EntityTypeEnum | null) | undefined, tax_classification?: (TaxClassEnum | null) | undefined, llc_managed_type?: (LLCManagedTypeEnum | null) | undefined, business_name?: (string | null) | undefined, business_purpose?: (string | null) | undefined, ein?: (string | null) | undefined, incorporation_date?: (string | 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}; export type AppPlatformPublicHostedSignupRequest = {business_name: string, user_email: string, callback_url: string, legal_entity_id?: (string | null) | undefined, business_information?: (AppPlatformPublicHostedSignupRequestBusinessInformation | null) | undefined, external_company_id?: (string | null) | undefined}; export type CreateApiV2SignupSessionsArgs = {authorization?: string | undefined, body: AppPlatformPublicHostedSignupRequest}; export type CreateApiV2SignupSessionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const appPlatformPublicHostedSignupResponse = z.object({url: z.string(), created_at: z.string(), expires_at: z.string(), legal_entity_id: 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 }; /** * Create Hosted Signup Session * * POST /v2/signup/sessions */ export const createApiV2SignupSessions = async (args: CreateApiV2SignupSessionsArgs, options: CreateApiV2SignupSessionsOptions) => { 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.authorization != null) headers.set("authorization", String(args.authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/signup/sessions${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(appPlatformPublicHostedSignupResponse, await res.json()) };