@shopify/app-bridge
Version:
> **Maintenance Mode:** Although apps using this package will continue to function, it is no longer receiving updates. For new projects, please use the [CDN version of App Bridge](https://shopify.dev/docs/api/app-home?accordionItem=getting-started-build-y
136 lines (135 loc) • 8.43 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Action = exports.validateAction = void 0;
var Cart_1 = require("../../actions/Cart");
Object.defineProperty(exports, "Action", { enumerable: true, get: function () { return Cart_1.Action; } });
var type_validate_1 = require("../type-validate");
var utils_1 = require("../utils");
var addressSchema = (0, type_validate_1.matchesObject)({
address1: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
address2: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
city: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
company: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
firstName: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
lastName: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
phone: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
province: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
country: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
zip: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
name: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
provinceCode: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
countryCode: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
});
var discountSchema = (0, type_validate_1.matchesObject)({
amount: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesPositiveInteger)()),
discountDescription: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
type: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
discountCode: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
});
var discountAmount = (0, type_validate_1.matchesObject)({
amount: (0, type_validate_1.matchesPositiveNumber)(),
discountDescription: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
type: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
});
var discountCode = (0, type_validate_1.matchesObject)({
discountCode: (0, type_validate_1.matchesString)(),
});
var lineItemSchema = (0, type_validate_1.matchesObject)({
price: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesPositiveNumber)()),
quantity: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesPositiveInteger)()),
title: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
variantId: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesPositiveInteger)()),
discount: (0, type_validate_1.makeOptional)(discountAmount),
});
var updateLineItemSchema = (0, type_validate_1.matchesObject)({
quantity: (0, type_validate_1.matchesPositiveInteger)(),
});
var customerSchema = (0, type_validate_1.matchesObject)({
id: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesPositiveInteger)()),
email: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
firstName: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
lastName: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
note: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
addresses: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesArray)(addressSchema)),
});
var noteSchema = (0, type_validate_1.matchesObject)({
name: (0, type_validate_1.matchesString)(),
value: (0, type_validate_1.matchesString)(),
});
var cartSchema = (0, type_validate_1.matchesObject)({
cartDiscount: (0, type_validate_1.makeOptional)(discountSchema),
cartDiscounts: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesArray)(discountSchema)),
customer: (0, type_validate_1.makeOptional)(customerSchema),
grandTotal: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
lineItems: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesArray)(lineItemSchema)),
noteAttributes: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesArray)(noteSchema)),
subTotal: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
taxTotal: (0, type_validate_1.makeOptional)((0, type_validate_1.matchesString)()),
});
var propertiesSchema = (0, type_validate_1.composeSchemas)((0, type_validate_1.matchesObject)({}), function (value) {
var validator = (0, type_validate_1.matchesString)();
var schema = Object.keys(value).reduce(function (acc, key) {
acc[key] = validator;
return acc;
}, {});
return (0, type_validate_1.validate)(value, (0, type_validate_1.matchesObject)(schema));
});
var matchesStringArray = (0, type_validate_1.matchesArray)((0, type_validate_1.matchesString)());
function createDataValidator(data) {
return (0, utils_1.createActionValidator)(Cart_1.Action, data ? (0, type_validate_1.matchesObject)({ data: data }) : undefined, true, true);
}
function createDataValidatorWithIndex(data) {
var indexSchema = (0, type_validate_1.matchesObject)({ index: (0, type_validate_1.matchesPositiveInteger)() });
if (data) {
var dataSchema = (0, type_validate_1.matchesObject)({ data: data });
return (0, utils_1.createActionValidator)(Cart_1.Action, (0, type_validate_1.composeSchemas)(indexSchema, dataSchema), true, true);
}
return (0, utils_1.createActionValidator)(Cart_1.Action, indexSchema, true, true);
}
function getDiscountSchema(data) {
if (data.amount) {
return discountAmount;
}
return discountCode;
}
function validateAction(action) {
switch (action.type) {
case Cart_1.Action.UPDATE:
return (0, type_validate_1.validate)(action, createDataValidator(cartSchema));
case Cart_1.Action.SET_CUSTOMER:
return (0, type_validate_1.validate)(action, createDataValidator(customerSchema));
case Cart_1.Action.ADD_CUSTOMER_ADDRESS:
return (0, type_validate_1.validate)(action, createDataValidator(addressSchema));
case Cart_1.Action.UPDATE_CUSTOMER_ADDRESS:
return (0, type_validate_1.validate)(action, createDataValidatorWithIndex(addressSchema));
case Cart_1.Action.SET_DISCOUNT:
return (0, type_validate_1.validate)(action, createDataValidator(getDiscountSchema(action.payload.data)));
case Cart_1.Action.SET_CODE_DISCOUNT:
return (0, type_validate_1.validate)(action, createDataValidator(discountCode));
case Cart_1.Action.SET_PROPERTIES:
return (0, type_validate_1.validate)(action, createDataValidator(propertiesSchema));
case Cart_1.Action.REMOVE_PROPERTIES:
return (0, type_validate_1.validate)(action, createDataValidator(matchesStringArray));
case Cart_1.Action.ADD_LINE_ITEM:
return (0, type_validate_1.validate)(action, createDataValidator(lineItemSchema));
case Cart_1.Action.UPDATE_LINE_ITEM:
return (0, type_validate_1.validate)(action, createDataValidatorWithIndex(updateLineItemSchema));
case Cart_1.Action.REMOVE_LINE_ITEM:
return (0, type_validate_1.validate)(action, createDataValidatorWithIndex());
case Cart_1.Action.SET_LINE_ITEM_DISCOUNT:
return (0, type_validate_1.validate)(action, createDataValidatorWithIndex(discountAmount));
case Cart_1.Action.REMOVE_LINE_ITEM_DISCOUNT:
return (0, type_validate_1.validate)(action, createDataValidatorWithIndex());
case Cart_1.Action.SET_LINE_ITEM_PROPERTIES:
return (0, type_validate_1.validate)(action, createDataValidatorWithIndex(propertiesSchema));
case Cart_1.Action.REMOVE_LINE_ITEM_PROPERTIES:
return (0, type_validate_1.validate)(action, createDataValidatorWithIndex(matchesStringArray));
case Cart_1.Action.FETCH:
case Cart_1.Action.REMOVE_CUSTOMER:
case Cart_1.Action.REMOVE_DISCOUNT:
case Cart_1.Action.CLEAR:
default:
return (0, type_validate_1.validate)(action, (0, utils_1.createActionValidator)(Cart_1.Action, undefined, false, true));
}
}
exports.validateAction = validateAction;