import {z} from 'zod' export type PartnerId = number; export type BusinessUnitId = number; export type MerchantIdentifier = string; export type MerchantName = string; export type LegalName = string; export type TaxNumber = string; export type Sector = string; export type Phone = string; export type Email = string; export type ConstitutionDate = string; export type Reference = {name: string, phone: string}; export type Neighborhood = string; export type City = string; export type State = 'AC' | 'AL' | 'AP' | 'AM' | 'BA' | 'CE' | 'DF' | 'ES' | 'GO' | 'MA' | 'MT' | 'MS' | 'MG' | 'PA' | 'PB' | 'PR' | 'PE' | 'PI' | 'RJ' | 'RN' | 'RS' | 'RO' | 'RR' | 'SC' | 'SP' | 'SE' | 'TO'; export type PostalCode = string; export type Line = string; export type Country = string; export type CompanyAddress = {Number?: string | undefined, Complement?: string | undefined, Reference?: Reference | undefined, Neighborhood?: Neighborhood | undefined, City?: City | undefined, State?: State | undefined, PostalCode?: PostalCode | undefined, Line?: Line | undefined, Country?: Country | undefined}; export type DailyLimit = {TotalAmount?: number | undefined}; export type CreateApiUpdateMerchantClosedLoopInfoArgs = {body: {Method: string, PartnerId: PartnerId, BusinessUnitId: BusinessUnitId, MerchantIdentifier: MerchantIdentifier, MerchantName?: MerchantName | undefined, LegalName?: LegalName | undefined, TaxNumber?: TaxNumber | undefined, Sector?: Sector | undefined, Phone?: Phone | undefined, Email?: Email | undefined, ConstitutionDate?: ConstitutionDate | undefined, CompanyAddress?: CompanyAddress | undefined, DailyLimit?: DailyLimit | undefined}}; export type CreateApiUpdateMerchantClosedLoopInfoOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const success = z.boolean(); export const message = z.string(); export const updateMerchantClosedLoopInfo = z.object({Success: success.optional(), Message: message.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 }; /** * Merchant Update * * POST /UpdateMerchantClosedLoopInfo */ export const createApiUpdateMerchantClosedLoopInfo = async (args: CreateApiUpdateMerchantClosedLoopInfoArgs, options?: CreateApiUpdateMerchantClosedLoopInfoOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, BASE64UsernamePassword} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BASE64UsernamePassword !== undefined) headers.set('authorization', `Basic ${btoa(`${BASE64UsernamePassword.username}:${BASE64UsernamePassword.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/UpdateMerchantClosedLoopInfo${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(updateMerchantClosedLoopInfo, await res.json()) };