import {z} from 'zod' export type AddressPostDto = {state?: string | undefined, city?: string | undefined, zip?: string | undefined, phoneWork?: string | undefined, address1?: string | undefined, address2?: string | undefined}; export type FeaturesPostDto = {bankTransactions?: boolean | undefined, bills?: boolean | undefined, bookkeeping?: boolean | undefined, imports?: boolean | undefined, invoicing?: boolean | undefined, payroll?: boolean | undefined, reporting?: boolean | undefined, taxes?: boolean | undefined, timeTracking?: boolean | undefined, mileage?: boolean | undefined}; export type CompanyPostDto = {id?: string | undefined, name: string, apiCompanyId?: string | undefined, entityType: 'SOLE_PROPRIETORSHIP' | 'LLP' | 'LLC' | 'S_CORP' | 'C_CORP', owners: Array<{userId: string, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined}> | null, address?: AddressPostDto | undefined, accountingMethod?: ('ACCRUAL' | 'CASH') | undefined, businessTypeId: string, availableFeatures?: FeaturesPostDto | undefined}; export type CreateApiV6CompanyArgs = {body: CompanyPostDto}; export type CreateApiV6CompanyOptions = {baseUrl?: string | undefined, PartnerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const ownerGetDto = z.object({userId: z.string(), email: z.string().optional(), firstName: z.string().optional(), lastName: z.string().optional(), language: z.enum(["EN", "ES", "FR", "ZH", "ID"]).optional()}); export const addressGetDto = z.object({countryCode: z.enum(["USA", "CAN", "GBR"]).optional(), state: z.string().optional(), city: z.string().optional(), zip: z.string().optional(), phoneWork: z.string().optional(), address1: z.string().optional(), address2: z.string().optional()}).nullable(); export const businessTypeInfo = z.object({id: z.string().optional(), name: z.string().optional()}); export const featuresGetDto = z.object({bankTransactions: z.boolean().optional(), bills: z.boolean().optional(), bookkeeping: z.boolean().optional(), imports: z.boolean().optional(), invoicing: z.boolean().optional(), payroll: z.boolean().optional(), reporting: z.boolean().optional(), taxes: z.boolean().optional(), timeTracking: z.boolean().optional(), mileage: z.boolean().optional()}); export const companyGetDto = z.object({id: z.string().optional(), name: z.string(), apiCompanyId: z.string().optional(), entityType: z.enum(["SOLE_PROPRIETORSHIP", "LLP", "LLC", "S_CORP", "C_CORP"]).optional(), owners: z.array(ownerGetDto), address: addressGetDto.optional(), accountingMethod: z.enum(["ACCRUAL", "CASH"]).optional(), businessType: businessTypeInfo.optional(), availableFeatures: featuresGetDto.optional(), accessLevel: z.enum(["READ_WRITE", "READ_ONLY", "NONE", "READ_WRITE", "READ_ONLY", "NONE"]).nullable().optional(), status: z.enum(["ACTIVE", "INACTIVE", "REMOVED"]).optional()}); export const companyRtlResponse = z.object({result: z.enum(["SUCCESS", "FAILURE"]).optional(), data: companyGetDto.optional(), readyToLaunchUrl: 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 company * * POST /v6/company */ export const createApiV6Company = async (args: CreateApiV6CompanyArgs, options?: CreateApiV6CompanyOptions) => { const {baseUrl = "https://sandbox.tight.com", fetch: fetchFn = fetch, PartnerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (PartnerAuth !== undefined) headers.set('authorization', `Bearer ${PartnerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v6/company${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(companyRtlResponse, await res.json()) };