splitwise
Version:
A TypeScript SDK for the Splitwise API.
93 lines • 3.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Expenses = void 0;
const errors_js_1 = require("../errors.js");
const pagination_js_1 = require("../pagination.js");
const base_js_1 = require("./base.js");
class Expenses extends base_js_1.BaseResource {
list(params, overrides) {
const { limit, offset, ...query } = params ?? {};
return (0, pagination_js_1.createPagedResult)(this.http, '/get_expenses', 'expenses', {
...(limit !== undefined && { limit }),
...(offset !== undefined && { offset }),
query,
...(overrides ?? {}),
});
}
async get(params, overrides) {
return this.http.get(`/get_expense/${params.id}`, {
unwrapKey: 'expense',
...overrides,
});
}
async create(params, overrides) {
const expenses = await this.http.post('/create_expense', {
body: { ...params },
unwrapKey: 'expenses',
...overrides,
});
return firstExpenseOrThrow(expenses);
}
async update(params, overrides) {
const { id, ...body } = params;
const expenses = await this.http.post(`/update_expense/${id}`, {
body: { ...body },
unwrapKey: 'expenses',
...overrides,
});
return firstExpenseOrThrow(expenses);
}
/**
* Deletes an expense. Throws SplitwiseConstraintError if the API returns
* `success: false` (e.g. you don't have permission). Returns nothing on
* success -- the absence of an exception is the signal.
*/
async delete(params, overrides) {
await this.http.post(`/delete_expense/${params.id}`, overrides);
}
/** Restores a previously-deleted expense. Throws on failure. */
async restore(params, overrides) {
await this.http.post(`/undelete_expense/${params.id}`, overrides);
}
/**
* Convenience helper for "person A owes person B X amount" — wraps create()
* with the right `users` shape so callers don't have to remember the
* paid_share / owed_share dance.
*/
async createDebt(params, overrides) {
const { paidBy, owedBy, amount, description, groupId, date } = params;
const cost = typeof amount === 'number' ? String(amount) : amount;
return this.create({
payment: false,
cost,
// ExpenseCreateParams requires `description`, but for createDebt
// it's optional from the user's perspective. Default to a generic
// string rather than '' because some Splitwise-side validation
// rejects empty descriptions.
description: description ?? 'IOU',
...(groupId !== undefined && { groupId }),
...(date !== undefined && { date }),
// v1 only set paidShare/owedShare on the relevant side. We mirror that
// to minimize the request payload and avoid surprising server-side
// validation.
users: [
{ userId: paidBy, paidShare: cost },
{ userId: owedBy, owedShare: cost },
],
}, overrides);
}
}
exports.Expenses = Expenses;
/**
* Pulls the first expense from a create/update response, or throws a
* descriptive SplitwiseError if the array is missing or empty. We do this
* defensively because the http client returns `undefined` (cast as the
* generic `T`) when the unwrap key is absent from the response.
*/
function firstExpenseOrThrow(expenses) {
if (!Array.isArray(expenses) || expenses.length === 0) {
throw new errors_js_1.SplitwiseError('Splitwise returned no expense (response was missing or empty `expenses` array)');
}
return expenses[0];
}
//# sourceMappingURL=expenses.js.map