@shopify/app-bridge-core
Version:
**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**
66 lines (63 loc) • 2.44 kB
JavaScript
import { Action as Action$1 } from '../../actions/Navigation/History/index.js';
import { ResourceType, Action } from '../../actions/Navigation/Redirect/index.js';
import { matchesObject, composeSchemas, validate, matchesEnum, makeOptional, matchesBoolean, matchesString } from '../type-validate.js';
import { createActionValidator, relativePathSchema } from '../utils.js';
function matchesAbsolutePath(value) {
return value.match('^https?://')
? undefined
: [
{
value,
error: 'invalid_absolute_url',
message: 'expected string to start with `https://` or `http://`',
},
];
}
function getSectionSchema(payload) {
const isProductVariant = payload &&
payload.section &&
payload.section.resource &&
payload.section.name === ResourceType.Product;
const resourceSchema = {
create: makeOptional(matchesBoolean()),
id: makeOptional(matchesString()),
};
const productVariantSchema = {
...resourceSchema,
variant: makeOptional(matchesObject(resourceSchema)),
};
return matchesObject({
section: matchesObject({
name: matchesEnum(ResourceType),
resource: makeOptional(matchesObject(isProductVariant ? productVariantSchema : resourceSchema)),
}),
});
}
function validateAction(action) {
const newContextSchema = matchesObject({ newContext: makeOptional(matchesBoolean()) });
let actionType = Action;
let schema;
switch (action.type) {
case Action$1.PUSH:
case Action$1.REPLACE:
actionType = Action$1;
schema = relativePathSchema;
break;
case Action.APP:
schema = relativePathSchema;
break;
case Action.REMOTE:
schema = composeSchemas(matchesObject({
url: composeSchemas(matchesString(), (value) => matchesAbsolutePath(value)),
}), newContextSchema);
break;
case Action.ADMIN_PATH:
schema = composeSchemas(relativePathSchema, newContextSchema);
break;
case Action.ADMIN_SECTION:
schema = composeSchemas(getSectionSchema(action.payload), newContextSchema);
break;
}
return validate(action, createActionValidator(actionType, schema));
}
export { getSectionSchema, matchesAbsolutePath, validateAction };