import {z} from 'zod' export type CreateCompanyRequestComercialInfo = {website?: string | undefined, mcc?: string | undefined, merchant_support_email?: string | undefined, merchant_support_phone?: string | undefined}; export type CreateCompanyRequestFiscalInfo = {business_phone?: string | undefined, fiscal_type?: string | undefined}; export type CreateCompanyRequestBankAccountInfo = {clabe?: string | undefined}; export type CreateCompanyRequest = {name?: string | undefined, type_company?: string | undefined, comercial_info?: CreateCompanyRequestComercialInfo | undefined, fiscal_info?: CreateCompanyRequestFiscalInfo | undefined, bank_account_info?: CreateCompanyRequestBankAccountInfo | undefined, users?: Array | undefined}; export type CreateApiCompaniesArgs = {body: CreateCompanyRequest}; export type CreateApiCompaniesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const companyDocumentResponse = z.object({file_classification: z.enum(["id_legal_representative", "id_legal_representative_back", "cfdi", "constitutive_act_basic", "proof_of_address", "power_of_attonery", "deposit_account_cover", "permit_casino", "license_sanitation", "registration_tourism"]).optional(), status: z.string().optional(), file_name: z.string().nullable().optional()}); export const companyResponse = z.object({id: z.string(), name: z.string(), active: z.boolean(), account_status: z.string(), card_payments_enabled: z.boolean().optional(), parent_company_id: z.string().nullable().optional(), onboarding_status: z.string(), documents: z.array(companyDocumentResponse), created_at: z.number().int(), object: z.string(), three_ds_enabled: z.boolean().optional(), three_ds_mode: z.enum(["smart", "strict"]).nullable().optional(), onboarding_url: 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 Company * * POST /companies */ export const createApiCompanies = async (args: CreateApiCompaniesArgs, options?: CreateApiCompaniesOptions) => { const {baseUrl = "https://api.conekta.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/vnd.conekta-v2.3.0+json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies${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(companyResponse, await res.json()) };