|
| 1 | +import cloneDeep from 'lodash/cloneDeep' |
| 2 | + |
| 3 | +const moduleHeadersOwn = Symbol.for('contentstack.management.moduleHeadersOwn') |
| 4 | + |
| 5 | +/** |
| 6 | + * Attaches `addHeader`, `addHeaderDict`, and `removeHeader` to a stack resource instance. |
| 7 | + * Per-module headers merge into request stack headers and override parent keys for that |
| 8 | + * instance only (copy-on-write). Pass `ownsHeadersInline: true` on Stack so the canonical |
| 9 | + * stack header map stays shared across child modules until a child calls addHeader. |
| 10 | + * |
| 11 | + * @param {Object} instance - Resource instance that uses `stackHeaders` for CMA headers. |
| 12 | + * @param {Object} [options] |
| 13 | + * @param {boolean} [options.ownsHeadersInline=false] - Mutate `stackHeaders` in place (stack root). |
| 14 | + * @returns {Object} The same instance for chaining. |
| 15 | + */ |
| 16 | +export function bindModuleHeaders (instance, { ownsHeadersInline = false } = {}) { |
| 17 | + instance.addHeader = function (key, value) { |
| 18 | + if (key === undefined || key === null) { |
| 19 | + return this |
| 20 | + } |
| 21 | + prepareStackHeaders(this, ownsHeadersInline) |
| 22 | + this.stackHeaders[key] = value |
| 23 | + return this |
| 24 | + } |
| 25 | + |
| 26 | + instance.addHeaderDict = function (headerDict) { |
| 27 | + if (!headerDict || typeof headerDict !== 'object') { |
| 28 | + return this |
| 29 | + } |
| 30 | + prepareStackHeaders(this, ownsHeadersInline) |
| 31 | + Object.assign(this.stackHeaders, headerDict) |
| 32 | + return this |
| 33 | + } |
| 34 | + |
| 35 | + instance.removeHeader = function (key) { |
| 36 | + if (key === undefined || key === null) { |
| 37 | + return this |
| 38 | + } |
| 39 | + if (ownsHeadersInline) { |
| 40 | + if (this.stackHeaders && Object.prototype.hasOwnProperty.call(this.stackHeaders, key)) { |
| 41 | + delete this.stackHeaders[key] |
| 42 | + } |
| 43 | + return this |
| 44 | + } |
| 45 | + if (!this[moduleHeadersOwn]) { |
| 46 | + if (!this.stackHeaders || !Object.prototype.hasOwnProperty.call(this.stackHeaders, key)) { |
| 47 | + return this |
| 48 | + } |
| 49 | + prepareStackHeaders(this, ownsHeadersInline) |
| 50 | + delete this.stackHeaders[key] |
| 51 | + return this |
| 52 | + } |
| 53 | + if (this.stackHeaders && Object.prototype.hasOwnProperty.call(this.stackHeaders, key)) { |
| 54 | + delete this.stackHeaders[key] |
| 55 | + } |
| 56 | + return this |
| 57 | + } |
| 58 | + |
| 59 | + return instance |
| 60 | +} |
| 61 | + |
| 62 | +function prepareStackHeaders (instance, ownsHeadersInline) { |
| 63 | + if (ownsHeadersInline) { |
| 64 | + if (!instance.stackHeaders) { |
| 65 | + instance.stackHeaders = {} |
| 66 | + } |
| 67 | + return |
| 68 | + } |
| 69 | + if (!instance[moduleHeadersOwn]) { |
| 70 | + instance.stackHeaders = cloneDeep(instance.stackHeaders || {}) |
| 71 | + instance[moduleHeadersOwn] = true |
| 72 | + } else if (!instance.stackHeaders) { |
| 73 | + instance.stackHeaders = {} |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Attaches `addHeader`, `addHeaderDict`, and `removeHeader` for a mutable header map (e.g. query builder). |
| 79 | + * |
| 80 | + * @param {Object} target - Object to receive methods. |
| 81 | + * @param {function(): Object} getHeaderMap - Returns the header key/value object used for requests. |
| 82 | + * @returns {Object} The same target for chaining. |
| 83 | + */ |
| 84 | +export function bindHeaderTarget (target, getHeaderMap) { |
| 85 | + target.addHeader = function (key, value) { |
| 86 | + if (key === undefined || key === null) { |
| 87 | + return this |
| 88 | + } |
| 89 | + const headers = getHeaderMap() |
| 90 | + if (!headers) { |
| 91 | + return this |
| 92 | + } |
| 93 | + headers[key] = value |
| 94 | + return this |
| 95 | + } |
| 96 | + |
| 97 | + target.addHeaderDict = function (headerDict) { |
| 98 | + if (!headerDict || typeof headerDict !== 'object') { |
| 99 | + return this |
| 100 | + } |
| 101 | + const headers = getHeaderMap() |
| 102 | + if (!headers) { |
| 103 | + return this |
| 104 | + } |
| 105 | + Object.assign(headers, headerDict) |
| 106 | + return this |
| 107 | + } |
| 108 | + |
| 109 | + target.removeHeader = function (key) { |
| 110 | + if (key === undefined || key === null) { |
| 111 | + return this |
| 112 | + } |
| 113 | + const headers = getHeaderMap() |
| 114 | + if (!headers) { |
| 115 | + return this |
| 116 | + } |
| 117 | + if (Object.prototype.hasOwnProperty.call(headers, key)) { |
| 118 | + delete headers[key] |
| 119 | + } |
| 120 | + return this |
| 121 | + } |
| 122 | + |
| 123 | + return target |
| 124 | +} |
0 commit comments