UNPKG

@shopify/react-network

Version:

A collection of components that allow you to set common HTTP headers from within your React application.

57 lines (56 loc) 2.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var network_1 = require("@shopify/network"); var context_1 = require("./context"); exports.NetworkContext = context_1.NetworkContext; exports.EFFECT_ID = Symbol('network'); var NetworkManager = /** @class */ (function () { function NetworkManager() { var _this = this; this.effect = { id: exports.EFFECT_ID, betweenEachPass: function () { return _this.reset(); }, }; this.statusCodes = []; this.csp = new Map(); } NetworkManager.prototype.reset = function () { this.statusCodes = []; this.csp.clear(); this.redirectUrl = undefined; }; NetworkManager.prototype.redirectTo = function (url, status) { if (status === void 0) { status = network_1.StatusCode.Found; } this.addStatusCode(status); this.redirectUrl = url; }; NetworkManager.prototype.addStatusCode = function (statusCode) { this.statusCodes.push(statusCode); }; NetworkManager.prototype.addCspDirective = function (directive, value) { var normalizedValue = typeof value === 'string' ? [value] : value; var currentValue = this.csp.get(directive) || []; var normalizedCurrentValue = Array.isArray(currentValue) ? currentValue : [String(currentValue)]; var newValue = Array.isArray(normalizedValue) ? tslib_1.__spread(normalizedCurrentValue, normalizedValue) : normalizedValue; this.csp.set(directive, newValue); }; NetworkManager.prototype.extract = function () { return { status: this.statusCodes.length > 0 ? this.statusCodes.reduce(function (large, code) { return Math.max(large, code); }, 0) : undefined, csp: tslib_1.__spread(this.csp.entries()).reduce(function (csp, _a) { var _b = tslib_1.__read(_a, 2), directive = _b[0], value = _b[1]; var _c; return (tslib_1.__assign({}, csp, (_c = {}, _c[directive] = value, _c))); }, {}), redirectUrl: this.redirectUrl, }; }; return NetworkManager; }()); exports.NetworkManager = NetworkManager;