UNPKG

@marceloclp/monzojs

Version:

Unofficial wrapper for the Monzo API written in TypeScript.

67 lines (66 loc) 2.71 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.safelyAnnotateTransaction = exports.annotateTransaction = exports.getTransactions = exports.getTransaction = void 0; const create_request_1 = __importDefault(require("../utils/create-request")); const remap_object_1 = __importDefault(require("../utils/remap-object")); /** * Returns an individual transaction, fetched by its id. * * @see https://docs.monzo.com/#retrieve-transaction */ const getTransaction = async (accessToken, { transactionId, expand }) => (0, create_request_1.default)(accessToken) .withQuery({ 'expand[]': expand }) .get(`transactions/${transactionId}`) .then(({ transaction }) => transaction); exports.getTransaction = getTransaction; /** * Returns a list of transactions on the user’s account. * * @see https://docs.monzo.com/#list-transactions */ const getTransactions = async (accessToken, { accountId, expand, limit, since, before, }) => (0, create_request_1.default)(accessToken) .withQuery({ account_id: accountId, 'expand[]': expand, limit, since, before, }) .get(`transactions`) .then(({ transactions }) => transactions); exports.getTransactions = getTransactions; /** * Update a transaction's metadata. * * NOTE: * * It seems Monzo's documentation is out-of-date, and only the notes metadata * param can be updated. Attempting to update any other field will result in no * changes to the transaction object returned by Monzo. * * Keep in mind that the notes parameter will be seen on the app by the user, * under each transaction's name, so attempting to store a stringified object or * similar structure is not a good solution. * * Refer to the safelyAnnotateTransaction method to update the notes directly. * * @see https://docs.monzo.com/#annotate-transaction */ const annotateTransaction = async (accessToken, { transactionId, prefix = '', metadata }) => (0, create_request_1.default)(accessToken) .withFormData((0, remap_object_1.default)(metadata, (k) => `metadata[${prefix}${k}]`)) .patch(`transactions/${transactionId}`) .then(({ transaction }) => transaction); exports.annotateTransaction = annotateTransaction; /** * Update a transaction's notes. */ const safelyAnnotateTransaction = async (accessToken, { transactionId, notes }) => (0, exports.annotateTransaction)(accessToken, { transactionId, metadata: { notes: typeof notes === 'string' ? notes : notes.join('\n'), }, }); exports.safelyAnnotateTransaction = safelyAnnotateTransaction;