1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var network_1 = require("@shopify/network");
|
5 | var context_1 = require("./context");
|
6 | exports.NetworkContext = context_1.NetworkContext;
|
7 | exports.EFFECT_ID = Symbol('network');
|
8 | var NetworkManager = (function () {
|
9 | function NetworkManager(_a) {
|
10 | var headers = (_a === void 0 ? {} : _a).headers;
|
11 | var _this = this;
|
12 | this.effect = {
|
13 | id: exports.EFFECT_ID,
|
14 | afterEachPass: function () {
|
15 | return _this.redirectUrl == null;
|
16 | },
|
17 | betweenEachPass: function () {
|
18 | _this.reset();
|
19 | },
|
20 | };
|
21 | this.statusCodes = [];
|
22 | this.csp = new Map();
|
23 | this.headers = new Map();
|
24 | this.requestHeaders = normalizeHeaders(headers);
|
25 | }
|
26 | NetworkManager.prototype.reset = function () {
|
27 | this.statusCodes = [];
|
28 | this.csp.clear();
|
29 | this.headers.clear();
|
30 | this.redirectUrl = undefined;
|
31 | };
|
32 | NetworkManager.prototype.getHeader = function (header) {
|
33 | return this.requestHeaders[header.toLowerCase()];
|
34 | };
|
35 | NetworkManager.prototype.setHeader = function (header, value) {
|
36 | this.headers.set(header, value);
|
37 | };
|
38 | NetworkManager.prototype.redirectTo = function (url, status) {
|
39 | if (status === void 0) { status = network_1.StatusCode.Found; }
|
40 | this.addStatusCode(status);
|
41 | this.redirectUrl = url;
|
42 | };
|
43 | NetworkManager.prototype.addStatusCode = function (statusCode) {
|
44 | this.statusCodes.push(statusCode);
|
45 | };
|
46 | NetworkManager.prototype.addCspDirective = function (directive, value) {
|
47 | var normalizedValue = typeof value === 'string' ? [value] : value;
|
48 | var currentValue = this.csp.get(directive) || [];
|
49 | var normalizedCurrentValue = Array.isArray(currentValue)
|
50 | ? currentValue
|
51 | : [String(currentValue)];
|
52 | var newValue = Array.isArray(normalizedValue)
|
53 | ? tslib_1.__spread(normalizedCurrentValue, normalizedValue) : normalizedValue;
|
54 | this.csp.set(directive, newValue);
|
55 | };
|
56 | NetworkManager.prototype.extract = function () {
|
57 | var csp = this.csp.size === 0
|
58 | ? undefined
|
59 | : tslib_1.__spread(this.csp).map(function (_a) {
|
60 | var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1];
|
61 | var printedValue;
|
62 | if (typeof value === 'boolean') {
|
63 | printedValue = '';
|
64 | }
|
65 | else if (typeof value === 'string') {
|
66 | printedValue = value;
|
67 | }
|
68 | else {
|
69 | printedValue = value.join(' ');
|
70 | }
|
71 | return "" + key + (printedValue ? ' ' : '') + printedValue;
|
72 | })
|
73 | .join('; ');
|
74 | var headers = new Map(this.headers);
|
75 | if (csp) {
|
76 | headers.set(network_1.Header.ContentSecurityPolicy, csp);
|
77 | }
|
78 | return {
|
79 | status: this.statusCodes.length > 0
|
80 | ? this.statusCodes.reduce(function (large, code) { return Math.max(large, code); }, 0)
|
81 | : undefined,
|
82 | headers: headers,
|
83 | redirectUrl: this.redirectUrl,
|
84 | };
|
85 | };
|
86 | return NetworkManager;
|
87 | }());
|
88 | exports.NetworkManager = NetworkManager;
|
89 | function normalizeHeaders(headers) {
|
90 | if (!headers) {
|
91 | return {};
|
92 | }
|
93 | return Object.entries(headers).reduce(function (accumulator, _a) {
|
94 | var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1];
|
95 | accumulator[key.toLowerCase()] = value;
|
96 | return accumulator;
|
97 | }, {});
|
98 | }
|