declastruct-stripe-sdk
Version:
declarative control of stripe constructs, via declastruct
40 lines • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getInvoiceItems = void 0;
const helpful_errors_1 = require("helpful-errors");
const castToDeclaredStripeInvoiceItem_1 = require("../cast/castToDeclaredStripeInvoiceItem");
const getInvoice_1 = require("../invoice/getInvoice");
/**
* .what = gets all invoice items from stripe
*/
const getInvoiceItems = async (input, context) => {
// handle by invoice
if (input.by.invoice) {
// resolve the invoice ref
const invoiceFound = await (0, getInvoice_1.getInvoice)({
by: {
ref: input.by.invoice.by.ref,
},
}, context);
if (!invoiceFound)
throw new helpful_errors_1.BadRequestError('can not resolve item.invoiceRef', { input });
// lookup the items by invoice
const { data: items } = await context.stripe.invoiceItems.list({
invoice: invoiceFound.id,
limit: 100,
expand: ['data.price', 'data.discounts'], // required to cast to declared item
});
if (items.length >= 90)
// 90 because that's too close for comfort
throw new helpful_errors_1.UnexpectedCodePathError('todo: handle pagination on invoice.items', {
found: items.length,
limit: 100,
});
// return each
return items.map(castToDeclaredStripeInvoiceItem_1.castToDeclaredStripeInvoiceItem);
}
// otherwise, unexpected input
throw new helpful_errors_1.UnexpectedCodePathError('invalid input', { input });
};
exports.getInvoiceItems = getInvoiceItems;
//# sourceMappingURL=getInvoiceItems.js.map