import {z} from 'zod' export type ReceiveRemittanceRequest = {mgi_transaction_session_id?: string | undefined, reference?: string | undefined, amount?: string | undefined, currency?: string | undefined, country?: string | undefined, state?: string | undefined, city?: string | undefined, address?: string | undefined, zip_code?: string | undefined, phone_country_code?: string | undefined, phone?: string | undefined, photo_id_country?: string | undefined, photo_id_type?: string | undefined, photo_id_number?: string | undefined, birth_country?: string | undefined, date_of_birth?: string | undefined, occupation?: string | undefined, purpose_of_transaction?: string | undefined, relationship_to_sender?: string | undefined, intended_use?: string | undefined, id_state?: string | undefined, gender?: string | undefined, id_expiration_year?: string | undefined, operator_name?: string | undefined, id_issue_year?: string | undefined}; export type CreateApiV2RemittancesReceiveArgs = {body: ReceiveRemittanceRequest}; export type CreateApiV2RemittancesReceiveOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const receiveRemittanceResponse = z.object({status: z.string().optional(), mgi_transaction_session_id: z.string().optional(), ready_for_commit: z.boolean().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 private account and receive remittance * * POST /v2/remittances/receive */ export const createApiV2RemittancesReceive = async (args: CreateApiV2RemittancesReceiveArgs, options: CreateApiV2RemittancesReceiveOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/remittances/receive${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(receiveRemittanceResponse, await res.json()) };