declastruct-stripe-sdk
Version:
declarative control of stripe constructs, via declastruct
42 lines • 1.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProduct = void 0;
const domain_objects_1 = require("domain-objects");
const helpful_errors_1 = require("helpful-errors");
const DeclaredStripeProduct_1 = require("../../domain/objects/DeclaredStripeProduct");
const castToDeclaredStripeProduct_1 = require("../cast/castToDeclaredStripeProduct");
/**
* .what = gets a product from stripe
*/
const getProduct = async (input, context) => {
// handle by ref
if (input.by.ref)
return (0, domain_objects_1.isUniqueKeyRef)({ of: DeclaredStripeProduct_1.DeclaredStripeProduct })(input.by.ref)
? (0, exports.getProduct)({ by: { unique: input.by.ref } }, context)
: (0, exports.getProduct)({ by: { primary: input.by.ref } }, context);
// handle get by id
if (input.by.primary) {
try {
const product = await context.stripe.products.retrieve(input.by.primary.id, {
expand: ['default_price'],
});
if (product.deleted)
return null;
return (0, castToDeclaredStripeProduct_1.castToDeclaredStripeProduct)(product);
}
catch (error) {
if (!(error instanceof Error))
throw error;
if (error.message.includes('No such product'))
return null; // handle "null" responses without an error
throw error;
}
}
// handle get by unique
if (input.by.unique)
return await (0, exports.getProduct)({ by: { primary: { id: input.by.unique.id } } }, context); // unique key === primary key, in this scenario, cause stripe hates unique keys
// otherwise, unexpected input
throw new helpful_errors_1.UnexpectedCodePathError('invalid input', { input });
};
exports.getProduct = getProduct;
//# sourceMappingURL=getProduct.js.map