Skip to content
← Public packages

@kody/onetimefax

Head-start stub for sending faxes from Kody with the Onetime Fax developer API. Fork it and add your API key.

FollowFork

Files

account.ts

47 lines · 1.4 KB · TypeScript
import { object, parseSafe } from 'remix/data-schema'
import { inputError, onetimefaxRequest } from './client.ts'

const accountInput = object({}, { unknownKeys: 'error' })

export type OnetimeFaxAccount = {
	account: {
		name?: string
		creditBalanceCents: number
		creditBalance?: string
	}
	apiKey?: {
		name?: string
		keyPrefix?: string
		lastUsedAt?: string | null
	}
}

/**
 * Read the Onetime Fax developer account behind the saved API key: name,
 * prepaid credit balance, and key metadata (never the key itself).
 * Use as the first smoke test after you save `onetimefax-api-token`, and
 * before sending to see whether credits cover a quote. Read-only; spends nothing.
 *
 * Wraps `GET /api/v1/account` (https://www.onetimefax.com/developers/).
 *
 * @param input - Empty object. Unknown keys are rejected.
 * @returns `{ account: { name, creditBalanceCents, creditBalance }, apiKey: { name, keyPrefix, lastUsedAt } }`
 *
 * @example
 * import getAccount from 'kody:@kody/onetimefax/account'
 *
 * const { account } = await getAccount({})
 */
export default async function getAccount(
	raw: unknown = {},
): Promise<OnetimeFaxAccount> {
	const parsed = parseSafe(accountInput, raw ?? {})
	if (!parsed.success) {
		throw inputError('account', '(none)', parsed.issues)
	}
	const { data } = await onetimefaxRequest<OnetimeFaxAccount>(
		'GET',
		'/api/v1/account',
	)
	return data
}