import {z} from 'zod' export type Address = {line1: string, line2?: (string | null) | undefined, city: string, state?: (string | null) | undefined, postalCode: string, country: string} | null; export type CreateLegalEntityApplicationRequest = {applicationData: {residencyType: 'e-Resident' | 'Resident', entityType: 'llc', name: string, extension: 'LLC' | 'L.L.C.' | 'Limited Liability Company' | 'S. de R.L.' | 'SRL' | 'Limited Company' | 'L.C.' | 'LC' | 'Limited Liability Co.' | 'Limited Co.' | 'Ltd. Co.', principalOffice: Address, contactEmail: string, registeredAgentProvider: 'prospera_employment_solutions' | null, registeredAgentDetails: {attn: string, residentPermitNumber: string, officeAddress: Address, mailingAddress: Address} | null, analytics?: {[key: string]: string} | undefined}, referralCode?: string | undefined, redirectUrl?: string | undefined}; export type CreateApiApiV1LegalEntityApplicationsArgs = {body: CreateLegalEntityApplicationRequest}; export type CreateApiApiV1LegalEntityApplicationsOptions = {baseUrl?: string | undefined, standardApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const legalEntityApplication = z.object({id: z.string(), statusId: z.enum(["Draft", "Pending Review", "Approved", "Rejected"]), applicationData: z.unknown(), applicationVersion: z.string(), submittedAt: z.string().nullable(), createdAt: z.string(), approvedAt: z.string().nullable(), rejectedAt: z.string().nullable(), legalEntityId: z.string().nullable()}); export const createLegalEntityApplicationResponse = z.object({data: legalEntityApplication, nextSteps: z.object({signature: z.string().nullable()})}); 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 legal-entity application * * POST /api/v1/legal_entity_applications * * Also accepts auth: agentKey */ export const createApiApiV1LegalEntityApplications = async (args: CreateApiApiV1LegalEntityApplicationsArgs, options?: CreateApiApiV1LegalEntityApplicationsOptions) => { const {baseUrl = "https://portal.eprospera.com", fetch: fetchFn = fetch, standardApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (standardApiKey !== undefined) headers.set('authorization', `Bearer ${standardApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/legal_entity_applications${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(createLegalEntityApplicationResponse, await res.json()) };