import {z} from 'zod' export type CreateApiClientsArgs = {body: {name: string, companyName: string, legalNature: 'LTDA' | 'EIRELI' | 'ME' | 'Associação' | 'Cooperativa' | 'MEI' | 'SA' | 'Outros', commercialPhone: string, email: string, nire?: string | undefined, documentNumber: string, address: {street: string, number: string, complement?: string | undefined, neighborhood: string, city: string, state: string, country: string, zipCode: string, proof?: {filename: string, content: string} | undefined}, administrator: {name: string, email: string, phone: string, documentNumber: string, identityDocument?: {filename: string, content: string} | undefined}, articleOfAssociation: {filename: string, content: string}, corporateStructureEvidence?: {filename: string, content: string} | undefined, boardOfDirectorsElectionDocument?: {filename: string, content: string} | undefined, simplifiedCommercialCertificate?: {filename: string, content: string} | undefined, revenue?: ({informed: boolean, value: string, periodStartAt: string, periodEndAt: string, billingEvidence?: {filename: string, content: string} | undefined, taxRegime?: ('national_simple' | 'presumed_profit' | 'real_profit') | undefined}) | undefined, extraAttachments?: Array<{filename: string, content: string}> | undefined, requiredSigners: number}}; export type CreateApiClientsOptions = {baseUrl?: string | undefined, sec0?: {username: string, password: string} | undefined, sec1?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiClientsResponse = z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({status: z.string().optional(), documentNumber: z.string().optional(), legalNature: z.string().optional(), nire: z.string().optional(), name: z.string().optional(), companyName: z.string().optional(), email: z.string().optional(), commercialPhone: z.string().optional(), requiredSigners: z.number().int().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), articleOfAssociation: z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({filename: z.string().optional(), content: z.string().optional()}).optional()}).optional()}).optional(), corporateStructureEvidence: z.unknown(), boardOfDirectorsElectionDocument: z.unknown(), simplifiedCommercialCertificate: z.unknown(), extraAttachments: z.unknown(), administrator: z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({documentNumber: z.string().optional(), name: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), identityDocument: z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({filename: z.string().optional(), content: z.string().optional()}).optional()}).optional()}).optional()}).optional()}).optional()}).optional(), revenue: z.object({data: z.object({id: z.unknown(), type: z.string().optional(), attributes: z.object({informed: z.boolean().optional(), taxRegime: z.string().optional(), value: z.string().optional(), periodStartAt: z.string().optional(), periodEndAt: z.string().optional(), billingEvidence: z.object({data: z.object({id: z.string().optional(), type: z.string().optional(), attributes: z.object({filename: z.string().optional(), content: z.string().optional()}).optional()}).optional()}).optional()}).optional()}).optional()}).optional(), address: z.object({data: z.object({id: z.unknown(), type: z.string().optional(), attributes: z.object({zipCode: z.string().optional(), street: z.string().optional(), number: z.string().optional(), complement: z.string().optional(), country: z.string().optional(), state: z.string().optional(), city: z.string().optional(), neighborhood: z.string().optional()}).optional()}).optional()}).optional()}).optional()}).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 }; /** * Criar onboarding * * POST /clients */ export const createApiClients = async (args: CreateApiClientsArgs, options?: CreateApiClientsOptions) => { const {baseUrl = "https://pagamentos.grafeno.be/api/v2", fetch: fetchFn = fetch, sec0, sec1} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set('authorization', `Basic ${btoa(`${sec0.username}:${sec0.password}`)}`) if (sec1 !== undefined) headers.set("Authorization", sec1) const url = `${baseUrl.replace(/\/+$/, '')}/clients${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(createApiClientsResponse, await res.json()) };