UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

146 lines 6.42 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.requestReducer = exports.REACT_ROUTER_REDUX_LOCATION_CHANGE = exports.SET_REQUEST_INFO = void 0; var immutability_helper_1 = __importDefault(require("immutability-helper")); var stackend_1 = require("../stackend"); exports.SET_REQUEST_INFO = 'SET_REQUEST_INFO'; // Hack to access this action exports.REACT_ROUTER_REDUX_LOCATION_CHANGE = '@@router/LOCATION_CHANGE'; /** * Sets up an implementation neutral object that keeps track of the current url */ var requestReducer = function (state, action) { if (state === void 0) { state = { location: { hash: '', host: '', hostName: '', href: '', origin: '', pathname: '/', port: 80, protocol: 'http:', search: '', query: {} }, cookie: null, absoluteUrl: '', communityUrl: '', absoluteCommunityUrl: '', communityFromDomain: false, contextPath: '', anchor: null, referenceUrlId: 0 }; } var location = undefined; var communityUrl = undefined; var absoluteUrl = undefined; var absoluteCommunityUrl = undefined; switch (action.type) { /* case CALL_HISTORY_METHOD: console.log("CALL_HISTORY_METHOD", action); break; */ case exports.REACT_ROUTER_REDUX_LOCATION_CHANGE: /* In response to a navigation event. Will only update the * location and possibly the community urls */ // Show the content when the css is loaded if (document) { var p = document.getElementById('xcapPage'); if (p && p.style.display === 'none') { p.style.display = 'block'; } } // NOTE: action.payload is NOT compatible with request.location. Does not update all props action.request = !action.request && !!action.payload ? { location: action.payload } : action.request; location = action.request.location || state.location; absoluteUrl = action.request.absoluteUrl || state.absoluteUrl; communityUrl = action.request.communityUrl || state.communityUrl; absoluteUrl = state.absoluteUrl; absoluteCommunityUrl = state.absoluteCommunityUrl; if (state.communityFromDomain) { // Will update absoluteUrl if (action.request.location && action.request.location.host) { absoluteUrl = action.request.location.protocol + '//' + action.request.location.host; communityUrl = '/'; absoluteCommunityUrl = absoluteUrl; } } else { // Extract community url if (action.request.location.pathname) { var pathname = action.request.location.pathname; if (pathname !== state.location.pathname) { // Skip context path var pfx = pathname; if (pfx.startsWith(state.contextPath)) { pfx = pfx.substr(state.contextPath.length); } // Skip extra path info var i = pfx.indexOf('/', 1); if (i !== -1) { pfx = pfx.substr(0, i); } if ((0, stackend_1.isCommunityUrlBlocked)(pfx)) { communityUrl = state.contextPath; } else { communityUrl = state.contextPath + pfx; } absoluteCommunityUrl = absoluteUrl + communityUrl; } } } return (0, immutability_helper_1.default)(state, { location: { $merge: __assign(__assign({}, location), { href: state.absoluteUrl + location.pathname }) }, absoluteUrl: { $set: absoluteUrl }, communityUrl: { $set: communityUrl }, absoluteCommunityUrl: { $set: absoluteCommunityUrl } }); case exports.SET_REQUEST_INFO: { /* * Set by SSR */ location = action.request.location || state.location; absoluteUrl = action.request.absoluteUrl || state.absoluteUrl; absoluteCommunityUrl = action.request.absoluteCommunityUrl || state.absoluteCommunityUrl; var cookie = action.request.cookie || state.cookie; var communityFromDomain = action.request.communityFromDomain || state.communityFromDomain; var contextPath = action.request.contextPath || state.contextPath; communityUrl = action.request.communityUrl || state.communityUrl; var anchor = action.request.anchor || state.anchor; return (0, immutability_helper_1.default)(state, { location: { $merge: location }, cookie: { $set: cookie }, absoluteUrl: { $set: absoluteUrl }, communityUrl: { $set: communityUrl }, absoluteCommunityUrl: { $set: absoluteCommunityUrl }, communityFromDomain: { $set: communityFromDomain }, contextPath: { $set: contextPath }, referenceUrlId: { $set: action.request.referenceUrlId || state.referenceUrlId || 0 }, anchor: { $set: anchor } }); } default: return state; } }; exports.requestReducer = requestReducer; exports.default = exports.requestReducer; //# sourceMappingURL=requestReducers.js.map