export type TransferEntry = {toUsername: string, toUserId: number, credits: number, commentOnFrom?: string | undefined, commentOnTo?: string | undefined}; export type CreateApiCreditTransferArgs = {body: TransferEntry}; export type CreateApiCreditTransferOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; /** * Transfer credits to another account * * POST /credit/transfer */ export const createApiCreditTransfer = async (args: CreateApiCreditTransferArgs, options?: CreateApiCreditTransferOptions) => { const {baseUrl = "https://api.bulksms.com/v1", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/credit/transfer${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 undefined };