UNPKG

@acoustic-content-sdk/redux-feature-url-config

Version:

> TODO: description

272 lines (258 loc) 8.68 kB
import { isURL, isString, isFunction, urlToString, urlFromProvider, parseURL, isNotNil, UNDEFINED_TYPE, wchGetBaseURL, mergeHubInfo, wchGetHubInfoFromBaseURL, wchGetHubInfoFromLinks, wchGetResourceUrlFromApiURL, wchIsPreviewMode, isAbsoluteURL, pluckProperty } from '@acoustic-content-sdk/utils'; import { createAction, handleActions } from 'redux-actions'; import { selectPayload, selectFeature, createReduxFeatureModule } from '@acoustic-content-sdk/redux-store'; /** * @fileoverview added by tsickle * Generated from: utils/predicates.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * Tests if a value is a provider * * @param {?} aValue - the value * @return {?} true if the value is a provider, else false */ function isStaticHubInfoUrlProvider(aValue) { return isURL(aValue) || isString(aValue) || isFunction(aValue); } /** * @fileoverview added by tsickle * Generated from: state/url-config/url.config.actions.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var ACTION_SET_URL_CONFIG = 'ACTION_SET_URL_CONFIG'; /** @type {?} */ var ACTION_CLEAR_URL_CONFIG = 'ACTION_CLEAR_URL_CONFIG'; /** @type {?} */ var clearUrlConfigAction = createAction(ACTION_CLEAR_URL_CONFIG); /** @type {?} */ var _setUrlConfigAction = createAction(ACTION_SET_URL_CONFIG); /** @type {?} */ var setUrlConfigAction = (/** * @param {?} provider * @return {?} */ function (provider) { return _setUrlConfigAction(isStaticHubInfoUrlProvider(provider) ? urlToString(urlFromProvider(provider)) : provider); }); /** * @fileoverview added by tsickle * Generated from: state/url-config/url.config.id.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var URL_CONFIG_FEATURE = 'urlConfig'; var _a; // default /** @type {?} */ var DEFAULT_CONFIG = { apiUrl: null, resourceUrl: null, isPreviewMode: false, baseUrl: null }; /* * Makes sure our path ends with a proper trailing slash */ /** * @param {?} aUrl * @return {?} */ function _guaranteeTrailingSlash(aUrl) { return aUrl.endsWith('/') ? aUrl : aUrl + '/'; } /** * @param {?} aUrl * @return {?} */ function createBaseURL(aUrl) { /** @type {?} */ var url = parseURL(aUrl); return isNotNil(url) ? _guaranteeTrailingSlash("" + url.origin + url.pathname) : undefined; } /** @type {?} */ var HAS_DOCUMENT = typeof document !== UNDEFINED_TYPE; /** * Returns the global document if it exists * @return {?} */ function getDocument() { return HAS_DOCUMENT ? document : undefined; } /** * Decodes the URL configuration from a URL provider * * @param {?} aApiUrl - the API URL * @param {?=} aDocument - optionally the document * * @return {?} the configuration */ function wchGetUrlConfig(aApiUrl, aDocument) { /** * Decode the resource base */ // const resourceBaseUrl = getBaseUrlFromDocument(aDocument); // const resourceBaseUrl = getBaseUrlFromDocument(aDocument); /** * Decode the base URL from the configuration or fall back to the global window * @type {?} */ var baseUrl = parseURL(createBaseURL(wchGetBaseURL(aDocument))); // decode the fallback /** @type {?} */ var cfgFallback = mergeHubInfo(wchGetHubInfoFromBaseURL(baseUrl), wchGetHubInfoFromLinks(aDocument)); /** @type {?} */ var infoApiUrl = urlFromProvider(aApiUrl) || parseURL(cfgFallback.apiUrl); /** @type {?} */ var apiRoot = createBaseURL(infoApiUrl); /** @type {?} */ var resourceRoot = createBaseURL(wchGetResourceUrlFromApiURL(infoApiUrl)); // convert /** @type {?} */ var apiUrl = parseURL(apiRoot); /** @type {?} */ var resourceUrl = parseURL(resourceRoot); /** @type {?} */ var isPreviewMode = wchIsPreviewMode(apiUrl); // ok return { apiUrl: apiUrl, resourceUrl: resourceUrl, baseUrl: baseUrl, isPreviewMode: isPreviewMode }; } /** * reducers for config settings * @type {?} */ var urlConfigReducer = handleActions((_a = {}, _a[ACTION_SET_URL_CONFIG] = (/** * @param {?} state * @param {?} action * @return {?} */ function (state, action) { // extract the payload /** @type {?} */ var payload = selectPayload(action); return isString(payload) ? wchGetUrlConfig(payload, getDocument()) : payload; }), _a[ACTION_CLEAR_URL_CONFIG] = (/** * @param {?} state * @param {?} action * @return {?} */ function (state, action) { return DEFAULT_CONFIG; }), _a), DEFAULT_CONFIG); var _a$1; /** * Represents the {\@link UrlConfigState | configuration} of the URLs to be used * when working with {\@link https://developer.ibm.com/api/view/dx-prod:ibm-watson-content-hub:title-IBM_Watson_Content_Hub | WCH REST} services. * @record */ function UrlConfigFeatureState() { } if (false) { /* Skipping unnamed member: [URL_CONFIG_FEATURE]: UrlConfigState;*/ } /** * * @type {?} */ var urlConfigFeatureReducer = (_a$1 = {}, _a$1[URL_CONFIG_FEATURE] = urlConfigReducer, _a$1); /** * Select the URL config feature * * \@param aState - the feature state * \@return the URL configuration object * @type {?} */ var selectUrlConfigFeature = selectFeature(URL_CONFIG_FEATURE); /** * @fileoverview added by tsickle * Generated from: state/url-config/url.config.module.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * Exposes the feature module selector * @type {?} */ var urlConfigFeature = createReduxFeatureModule(URL_CONFIG_FEATURE, urlConfigReducer); /** * @fileoverview added by tsickle * Generated from: utils/general.selectors.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * Tests if a value is a valid URL * * @param {?} aValue - the value to test * @return {?} true if the value is a valid URL */ function isValidUrl(aValue) { return (isNotNil(aValue) && isNotNil(aValue.href) && isAbsoluteURL(aValue.href)); } /** * @fileoverview added by tsickle * Generated from: state/url-config/url.config.selectors.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var selectIsPreviewMode = pluckProperty('isPreviewMode'); /** @type {?} */ var selectApiUrl = pluckProperty('apiUrl'); /** @type {?} */ var selectResourceUrl = pluckProperty('resourceUrl'); /** * Tests if an object is a valid {\@link UrlConfigState} object * * @param {?} aState - the object to test * @return {?} true if the state is valid else false */ function isValidUrlConfig(aState) { return (isNotNil(aState) && isValidUrl(selectApiUrl(aState)) && isValidUrl(selectResourceUrl(aState))); } /** * @fileoverview added by tsickle * Generated from: state/url-config/index.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * Generated from: version.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * Version and build number of the package * @type {?} */ var VERSION = { version: { major: '9', minor: '0', patch: '10076', branch: 'master' }, build: new Date(1585229427693) }; /** * Module name * @type {?} */ var MODULE = '@acoustic-content-sdk/redux-feature-url-config'; /** * @fileoverview added by tsickle * Generated from: public_api.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * Generated from: acoustic-content-sdk-redux-feature-url-config.ts * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { URL_CONFIG_FEATURE, VERSION, isValidUrl, isValidUrlConfig, selectApiUrl, selectIsPreviewMode, selectResourceUrl, selectUrlConfigFeature, setUrlConfigAction, urlConfigFeature, urlConfigFeatureReducer, urlConfigReducer }; //# sourceMappingURL=acoustic-content-sdk-redux-feature-url-config.js.map