1919 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020 */
2121
22- /**
23- * @internal
24- * This method is a temporary fix: as per the MDN spec (https://developer.mozilla.org/en-US/docs/Web/API/Headers),
25- * the Headers object should have a `.keys()` method. It turns out that some implementations
26- * do not implement this method, so this check is necessary to verify how to access
27- * the header's content.
28- * @param headers A header potentially of the type provided by LDflex-Comunica
29- */
30- function hasKeys (
31- headers : Headers | string [ ] [ ] | Record < string , string > | undefined | unknown
32- ) : headers is Headers | string [ ] [ ] {
33- return (
34- // eslint-disable-next-line
35- // @ts -ignore
36- headers !== undefined && headers . keys !== undefined
37- ) ;
38- }
39-
40- /**
41- * @internal
42- * This method is a temporary fix: we receive from LDflex-Comunica an object
43- * that is not conform to the Headers interface. This mitigates the issue
44- * until a cleaner fix is found. This method may be seen as a type guard for
45- * a LDflex-Comunica header.
46- * @param headers A header potentially of the type provided by LDflex-Comunica
47- */
48- function hasRawGetter (
49- // The 'any' type here is needed because no type definition covers properly
50- // the headers object we want to check against here.
51- // eslint-disable-next-line
52- headers : any
53- ) : boolean {
54- return (
55- // eslint-disable-next-line
56- // @ts -ignore
57- headers !== undefined && headers . raw !== undefined
58- ) ;
59- }
60-
6122/**
6223 * @internal
6324 * This function feels unnecessarily complicated, but is required in order to
@@ -67,34 +28,23 @@ function hasRawGetter(
6728 * @param headersToFlatten A structure containing headers potentially in several formats
6829 */
6930export function flattenHeaders (
70- headersToFlatten : Headers | string [ ] [ ] | Record < string , string > | undefined
31+ headersToFlatten : Headers | Record < string , string > | string [ ] [ ] | undefined
7132) : Record < string , string > {
72- if ( headersToFlatten === undefined ) {
33+ if ( typeof headersToFlatten === " undefined" ) {
7334 return { } ;
7435 }
7536 const flatHeaders : Record < string , string > = { } ;
76- if ( ! hasKeys ( headersToFlatten ) ) {
77- if ( hasRawGetter ( headersToFlatten ) ) {
78- // This is needed because the headers object passed by Comunica do not
79- // align with either `node-fetch::Headers` or `lib.dom.d.ts::Headers`,
80- // and gets mangled if passed as is to cross-fetch.
81- // eslint-disable-next-line
82- // @ts -ignore
83- return headersToFlatten . raw ( ) ;
84- } else {
85- return headersToFlatten as Record < string , string > ;
86- }
87- } else {
88- // headersToFlatten.keys() SHOULD be valid as per the Headers spec. This seems to be an issue
89- // in lib.dom.d.ts that will need some investigation and potentially a contrib upstream.
90- // eslint-disable-next-line
37+
38+ // If the headers are already a Record<string, string>,
39+ // they can directly be returned.
40+ if ( typeof headersToFlatten . forEach !== "function" ) {
9141 // @ts -ignore
92- for ( const key of headersToFlatten . keys ( ) ) {
93- // Similar as the previous @ts -ignore
94- // eslint-disable-next-line
95- // @ts -ignore
96- flatHeaders [ key ] = headersToFlatten . get ( key ) ;
97- }
42+ return headersToFlatten ;
9843 }
44+
45+ // @ts -ignore
46+ headersToFlatten . forEach ( ( value : string , key : string ) => {
47+ flatHeaders [ key ] = value ;
48+ } ) ;
9949 return flatHeaders ;
10050}
0 commit comments