2021-09-03 13:25:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { isObject } from '@woocommerce/types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-09-07 16:01:14 +00:00
|
|
|
import { hasInnerBlocks } from './get-registered-blocks';
|
2021-09-03 13:25:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that an option is of the given type. Otherwise, throws an error.
|
|
|
|
*
|
|
|
|
* @throws Will throw an error if the type of the option doesn't match the expected type.
|
|
|
|
*/
|
|
|
|
export const assertType = (
|
|
|
|
optionName: string,
|
|
|
|
option: unknown,
|
|
|
|
expectedType: unknown
|
|
|
|
): void => {
|
|
|
|
const actualType = typeof option;
|
|
|
|
if ( actualType !== expectedType ) {
|
|
|
|
throw new Error(
|
|
|
|
`Incorrect value for the ${ optionName } argument when registering a checkout block. It was a ${ actualType }, but must be a ${ expectedType }.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-09-07 16:01:14 +00:00
|
|
|
* Validate the block name.
|
|
|
|
*
|
|
|
|
* @throws Will throw an error if the block name is invalid.
|
2021-09-03 13:25:09 +00:00
|
|
|
*/
|
2021-09-07 16:01:14 +00:00
|
|
|
export const assertBlockName = ( blockName: string ): void => {
|
|
|
|
assertType( 'blockName', blockName, 'string' );
|
|
|
|
|
|
|
|
if ( ! blockName ) {
|
2021-09-03 13:25:09 +00:00
|
|
|
throw new Error(
|
2021-09-07 16:01:14 +00:00
|
|
|
`Value for the blockName argument must not be empty.`
|
2021-09-03 13:25:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-09-07 16:01:14 +00:00
|
|
|
* Validate the block parent.
|
2021-09-03 13:25:09 +00:00
|
|
|
*
|
|
|
|
* @throws Will throw an error if the block name is invalid.
|
|
|
|
*/
|
2021-09-07 16:01:14 +00:00
|
|
|
export const assertBlockParent = ( blockParent: string | string[] ): void => {
|
|
|
|
if ( typeof blockParent !== 'string' && ! Array.isArray( blockParent ) ) {
|
|
|
|
throw new Error(
|
|
|
|
`Incorrect value for the parent argument when registering a checkout block. It was a ${ typeof blockParent }, but must be a string or array of strings.`
|
|
|
|
);
|
|
|
|
}
|
2021-09-03 13:25:09 +00:00
|
|
|
|
2021-09-07 16:01:14 +00:00
|
|
|
if ( typeof blockParent === 'string' && ! hasInnerBlocks( blockParent ) ) {
|
2021-09-03 13:25:09 +00:00
|
|
|
throw new Error(
|
2021-09-07 16:01:14 +00:00
|
|
|
`When registering a checkout block, the parent must be a valid inner block area.`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
Array.isArray( blockParent ) &&
|
|
|
|
! blockParent.some( ( parent ) => hasInnerBlocks( parent ) )
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
`When registering a checkout block, the parent must be a valid inner block area.`
|
2021-09-03 13:25:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that an option is of the given type. Otherwise, throws an error.
|
|
|
|
*
|
|
|
|
* @throws Will throw an error if the type of the option doesn't match the expected type.
|
2021-09-07 16:01:14 +00:00
|
|
|
* @param {Object} options Object containing the option to validate.
|
|
|
|
* @param {string} optionName Name of the option to validate.
|
|
|
|
* @param {string} expectedType Type expected for the option.
|
2021-09-03 13:25:09 +00:00
|
|
|
*/
|
|
|
|
export const assertOption = (
|
2021-09-07 16:01:14 +00:00
|
|
|
options: unknown,
|
2021-09-03 13:25:09 +00:00
|
|
|
optionName: string,
|
2021-09-07 16:01:14 +00:00
|
|
|
expectedType: string
|
2021-09-03 13:25:09 +00:00
|
|
|
): void => {
|
2021-09-07 16:01:14 +00:00
|
|
|
if ( ! isObject( options ) ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-03 13:25:09 +00:00
|
|
|
const actualType = typeof options[ optionName ];
|
2021-09-07 16:01:14 +00:00
|
|
|
if ( actualType !== expectedType ) {
|
2021-09-03 13:25:09 +00:00
|
|
|
throw new Error(
|
2021-09-07 16:01:14 +00:00
|
|
|
`Incorrect value for the ${ optionName } argument when registering a block component. It was a ${ actualType }, but must be a ${ expectedType }.`
|
2021-09-03 13:25:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Asserts that an option is a valid react element or lazy callback. Otherwise, throws an error.
|
|
|
|
*
|
|
|
|
* @throws Will throw an error if the type of the option doesn't match the expected type.
|
|
|
|
*/
|
|
|
|
export const assertBlockComponent = (
|
|
|
|
options: Record< string, unknown >,
|
|
|
|
optionName: string
|
|
|
|
): void => {
|
|
|
|
const optionValue = options[ optionName ];
|
|
|
|
|
|
|
|
if ( optionValue ) {
|
|
|
|
if ( typeof optionValue === 'function' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
isObject( optionValue ) &&
|
|
|
|
optionValue.$$typeof &&
|
|
|
|
optionValue.$$typeof === Symbol.for( 'react.lazy' )
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error(
|
|
|
|
`Incorrect value for the ${ optionName } argument when registering a block component. Component must be a valid React Element or Lazy callback.`
|
|
|
|
);
|
|
|
|
};
|