UNPKG

@shopify/app-bridge-host

Version:

App Bridge Host contains components and middleware to be consumed by the app's host, as well as the host itself. The middleware and `Frame` component are responsible for facilitating communication between the client and host, and used to act on actions se

67 lines (64 loc) 3.23 kB
import { __assign } from 'tslib'; import { isAdminSection, ResourceType, normalizeUrl, getRelativePath } from '@shopify/app-bridge-core/actions/Navigation/Redirect'; import { resetStateReducer } from '../utilities.js'; import { navigationActionCreatorsMap } from './actionCreators.js'; import navigationReducer, { defaultNavigationStore } from './reducer.js'; export { COMPLETE_ROUTE_UPDATE, completeRouteUpdate } from './actions.js'; /** * An object containing the key, actions, initial state and reducer of the Navigation feature * Can be used with the `withFeature` decorator to add the reducer * and then make its actions and store available to the wrapped component * @public * */ var feature = { actions: navigationActionCreatorsMap, key: 'navigation', initialState: defaultNavigationStore, reducer: resetStateReducer(navigationReducer), requiresRouter: true, getApi: function (_a) { var config = _a.config, actions = _a.actions; function handleRedirect(to, options) { var url = normalizeUrl(to); var localOrigin = new URL(config.url).origin; var isAppUrl = url.startsWith(localOrigin); var isHostUrl = url.startsWith("https://".concat(config.hostName)); var isRelative = url.startsWith('/'); if (isAppUrl || isHostUrl || isRelative) { var path = getRelativePath(url); if (isHostUrl || (options === null || options === void 0 ? void 0 : options.target) === 'new' || (options === null || options === void 0 ? void 0 : options.target) === 'host') { actions.handleRedirectAdmin({ path: path.replace(/^\/admin/, ''), newContext: (options === null || options === void 0 ? void 0 : options.target) === 'new', }); return; } if (((options === null || options === void 0 ? void 0 : options.target) === 'self' || !(options === null || options === void 0 ? void 0 : options.target)) && (options === null || options === void 0 ? void 0 : options.replace)) { actions.handleReplaceHistory({ path: path }); return; } actions.handleRedirectApp({ path: path }); return; } actions.handleRedirectRemote({ url: url, newContext: (options === null || options === void 0 ? void 0 : options.target) === 'new', }); } var navigate = function (to, options) { if (isAdminSection(to)) { var convertedSection = __assign(__assign({}, to), { name: ResourceType[to.name] }); actions.handleRedirectAdminSection({ section: convertedSection, newContext: (options === null || options === void 0 ? void 0 : options.target) === 'new', }); return; } handleRedirect(to, options); }; return { navigate: navigate, }; }, }; export { navigationReducer as default, defaultNavigationStore, feature, navigationActionCreatorsMap };