declastruct-stripe-sdk
Version:
declarative control of stripe constructs, via declastruct
40 lines • 2.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.castToDeclaredStripeProduct = void 0;
const domain_glossary_price_1 = require("domain-glossary-price");
const helpful_errors_1 = require("helpful-errors");
const type_fns_1 = require("type-fns");
const DeclaredStripeProduct_1 = require("../../domain/objects/DeclaredStripeProduct");
const castToDeclaredStripeProduct = (input) => {
const product = new DeclaredStripeProduct_1.DeclaredStripeProduct({
id: input.id,
url: input.url ?? null,
name: input.name,
description: input.description,
active: input.active,
price: new domain_glossary_price_1.Price({
amount: typeof input.default_price === 'string'
? helpful_errors_1.UnexpectedCodePathError.throw('product.default_price was defined as a string. please have stripe hydrate this into an object', { input })
: !(0, type_fns_1.isPresent)(input.default_price)
? helpful_errors_1.UnexpectedCodePathError.throw('product.default_price was not defined. why?', { input })
: input.default_price.deleted
? helpful_errors_1.UnexpectedCodePathError.throw('product.default_price has a deleted status. why?', { input })
: input.default_price.unit_amount ??
helpful_errors_1.UnexpectedCodePathError.throw('product.default_price.unit_amount is not defined. why?', { input }),
currency: 'USD',
}),
metadata: (() => {
const obj = input.metadata ? (0, type_fns_1.omit)(input.metadata, ['exid']) : {};
if (Object.keys(obj).length === 0)
return null;
return obj;
})(),
});
if ((0, type_fns_1.hasMetadata)(product, ['id']))
return product;
throw new helpful_errors_1.UnexpectedCodePathError('expected product to have metadata', {
product,
});
};
exports.castToDeclaredStripeProduct = castToDeclaredStripeProduct;
//# sourceMappingURL=castToDeclaredStripeProduct.js.map