File size: 8,347 Bytes
31e8457
051bada
 
 
 
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
051bada
31e8457
051bada
31e8457
051bada
31e8457
 
 
 
051bada
 
31e8457
051bada
31e8457
 
051bada
 
 
 
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051bada
 
 
 
31e8457
 
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
051bada
31e8457
 
 
 
 
 
 
051bada
 
31e8457
051bada
31e8457
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
 
 
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
051bada
 
 
 
 
 
31e8457
051bada
31e8457
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
 * Models HTTP headers for the unstable HTTP client and server modules.
 *
 * `Headers` values are immutable maps keyed by lowercase header name. This
 * module converts common header inputs into that shape, provides helpers for
 * reading and updating header values, and redacts configured sensitive headers
 * when values are inspected.
 *
 * @since 4.0.0
 */
import * as Context from "../../Context.js";
import * as Equal from "../../Equal.js";
import * as Equ from "../../Equivalence.js";
import { dual } from "../../Function.js";
import * as Hash from "../../Hash.js";
import * as Inspectable from "../../Inspectable.js";
import * as Option from "../../Option.js";
import * as Predicate from "../../Predicate.js";
import * as Record from "../../Record.js";
import * as Redactable from "../../Redactable.js";
import * as Redacted from "../../Redacted.js";
import * as Schema from "../../Schema.js";
import * as SchemaTransformation from "../../SchemaTransformation.js";
/**
 * Runtime type identifier for `Headers` values.
 *
 * @category type IDs
 * @since 4.0.0
 */
export const TypeId = /*#__PURE__*/Symbol.for("~effect/http/Headers");
/**
 * Returns `true` if the provided value is a `Headers` value.
 *
 * @category refinements
 * @since 4.0.0
 */
export const isHeaders = u => Predicate.hasProperty(u, TypeId);
// the properties are folded into the initializer (rather than a separate
// `Object.defineProperties(Proto, ...)` statement) so the whole definition
// is pure-annotated by the build and tree-shakable.
const Proto = /*#__PURE__*/Object.defineProperties(/*#__PURE__*/Object.create(null), {
  [TypeId]: {
    value: TypeId
  },
  [Redactable.symbolRedactable]: {
    value(context) {
      return redact(this, Context.get(context, CurrentRedactedNames));
    }
  },
  toJSON: {
    value() {
      return Redactable.redact(this);
    }
  },
  [Equal.symbol]: {
    value(that) {
      return Equivalence(this, that);
    }
  },
  [Hash.symbol]: {
    value() {
      return Hash.structure(this);
    }
  },
  toString: {
    value: Inspectable.BaseProto.toString
  },
  [Inspectable.NodeInspectSymbol]: {
    value: Inspectable.BaseProto[Inspectable.NodeInspectSymbol]
  }
});
const make = input => Object.assign(Object.create(Proto), input);
/**
 * Provides an `Equivalence` instance that compares `Headers` by header names
 * and string values.
 *
 * @category instances
 * @since 4.0.0
 */
export const Equivalence = /*#__PURE__*/Record.makeEquivalence(/*#__PURE__*/Equ.strictEqual());
/**
 * Schema for `Headers` values encoded as records of string header values.
 *
 * **Details**
 *
 * Decoding normalizes header names through `fromInput`; encoding returns a plain record.
 *
 * @category schemas
 * @since 4.0.0
 */
export const HeadersSchema = /*#__PURE__*/Schema.declare(isHeaders, {
  typeConstructor: {
    _tag: "effect/http/Headers"
  },
  generation: {
    runtime: `Headers.HeadersSchema`,
    Type: `Headers.Headers`,
    Encoded: `typeof Headers.HeadersSchema["Encoded"]`,
    importDeclaration: `import * as Headers from "effect/unstable/http/Headers"`
  },
  expected: "Headers",
  toEquivalence: () => Equivalence,
  toCodec: () => Schema.link()(Schema.Record(Schema.String, Schema.String), SchemaTransformation.transform({
    decode: input => fromInput(input),
    encode: headers => ({
      ...headers
    })
  }))
});
/**
 * An empty `Headers` collection.
 *
 * @category constructors
 * @since 4.0.0
 */
export const empty = /*#__PURE__*/Object.create(Proto);
/**
 * Creates `Headers` from a record or iterable of header entries.
 *
 * **Details**
 *
 * Header names are normalized to lowercase. Array values in record input are joined with `", "`, and `undefined` values are omitted.
 *
 * @category constructors
 * @since 4.0.0
 */
export const fromInput = input => {
  if (input === undefined) {
    return empty;
  } else if (Symbol.iterator in input) {
    const out = Object.create(Proto);
    for (const [k, v] of input) {
      out[k.toLowerCase()] = v;
    }
    return out;
  }
  const out = Object.create(Proto);
  for (const [k, v] of Object.entries(input)) {
    if (Array.isArray(v)) {
      out[k.toLowerCase()] = v.join(", ");
    } else if (v !== undefined) {
      out[k.toLowerCase()] = v;
    }
  }
  return out;
};
/**
 * Treats an existing record as `Headers` unsafely.
 *
 * **Gotchas**
 *
 * This mutates the record's prototype and does not normalize header names; callers must provide the expected lowercase keys.
 *
 * @category constructors
 * @since 4.0.0
 */
export const fromRecordUnsafe = input => Object.setPrototypeOf(input, Proto);
/**
 * Returns `true` when a header with the given name is present.
 *
 * **Details**
 *
 * The lookup lowercases the provided header name.
 *
 * @category combinators
 * @since 4.0.0
 */
export const has = /*#__PURE__*/dual(2, (self, key) => key.toLowerCase() in self);
/**
 * Gets a header value by name safely.
 *
 * **Details**
 *
 * The lookup lowercases the provided header name and returns `Option.none()` when absent.
 *
 * @category combinators
 * @since 4.0.0
 */
export const get = /*#__PURE__*/dual(2, (self, key) => Option.fromUndefinedOr(self[key.toLowerCase()]));
/**
 * Returns a new `Headers` collection with the given header set.
 *
 * **Details**
 *
 * The header name is normalized to lowercase.
 *
 * @category combinators
 * @since 4.0.0
 */
export const set = /*#__PURE__*/dual(3, (self, key, value) => {
  const out = make(self);
  out[key.toLowerCase()] = value;
  return out;
});
/**
 * Returns a new `Headers` collection with all provided headers set.
 *
 * **Details**
 *
 * Input headers are normalized with `fromInput` and override existing headers with the same lowercase name.
 *
 * @category combinators
 * @since 4.0.0
 */
export const setAll = /*#__PURE__*/dual(2, (self, headers) => make({
  ...self,
  ...fromInput(headers)
}));
/**
 * Returns a new `Headers` collection containing headers from both collections.
 *
 * **Details**
 *
 * Headers from the second collection override headers from the first collection with the same name.
 *
 * @category combinators
 * @since 4.0.0
 */
export const merge = /*#__PURE__*/dual(2, (self, headers) => {
  const out = make(self);
  Object.assign(out, headers);
  return out;
});
/**
 * Returns a new `Headers` collection with the named header removed.
 *
 * **Details**
 *
 * The provided header name is normalized to lowercase before removal.
 *
 * @category combinators
 * @since 4.0.0
 */
export const remove = /*#__PURE__*/dual(2, (self, key) => {
  const out = make(self);
  delete out[key.toLowerCase()];
  return out;
});
/**
 * Returns a new `Headers` collection with each named header removed.
 *
 * **Details**
 *
 * Each provided header name is normalized to lowercase before removal.
 *
 * @category combinators
 * @since 4.0.0
 */
export const removeMany = /*#__PURE__*/dual(2, (self, keys) => {
  const out = make(self);
  for (const key of keys) {
    delete out[key.toLowerCase()];
  }
  return out;
});
/**
 * Returns a plain record with selected header values wrapped in `Redacted`.
 *
 * **Details**
 *
 * String keys are normalized to lowercase before matching; regular expressions are tested against the stored header names.
 *
 * @category combinators
 * @since 4.0.0
 */
export const redact = /*#__PURE__*/dual(2, (self, key) => {
  const out = {
    ...self
  };
  const modify = key => {
    if (typeof key === "string") {
      const k = key.toLowerCase();
      if (k in self) {
        out[k] = Redacted.make(self[k]);
      }
    } else {
      for (const name in self) {
        if (key.test(name)) {
          out[name] = Redacted.make(self[name]);
        }
      }
    }
  };
  if (Array.isArray(key)) {
    for (let i = 0; i < key.length; i++) {
      modify(key[i]);
    }
  } else {
    modify(key);
  }
  return out;
});
/**
 * Context reference listing header names or patterns that should be redacted when `Headers` are inspected or rendered.
 *
 * **Details**
 *
 * Defaults include `authorization`, `cookie`, `set-cookie`, and `x-api-key`.
 *
 * @category fiber refs
 * @since 4.0.0
 */
export const CurrentRedactedNames = /*#__PURE__*/Context.Reference("effect/Headers/CurrentRedactedNames", {
  defaultValue: () => ["authorization", "cookie", "set-cookie", "x-api-key"]
});
//# sourceMappingURL=Headers.js.map