Skip to content

Commit cf1622c

Browse files
authored
Clean the LDflex workaround up (#177)
* Clean the LDflex workaround up The previous workaround was unnecessarily convoluted, and a simpler, more standard approach is supported. However, there is still a small conflict between the `lib.dom.d.ts` Headers type and the reference https://fetch.spec.whatwg.org/#headers-class. An issue will be opened to track this.
1 parent 574ca72 commit cf1622c

File tree

3 files changed

+19
-73
lines changed

3 files changed

+19
-73
lines changed

__tests__/authenticatedFetch/headers/HeadersUtils.spec.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ describe("Headers interoperability function", () => {
4040
]);
4141
});
4242

43-
it("supports Comunica-style headers structures", () => {
44-
// This enables testing that an object similar to the prototypes passed by
45-
// Comunica is supported.
43+
it("supports non-iterable headers if they provide a reasonably standard way of browsing them", () => {
4644
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4745
const myHeaders: any = {};
48-
myHeaders["accept"] = "application/json";
49-
myHeaders["content-type"] = "text/turtle";
50-
myHeaders["raw"] = (): Record<string, string> => {
51-
return {
52-
accept: "application/json",
53-
"content-type": "text/turtle"
54-
};
46+
myHeaders["forEach"] = (
47+
callback: (value: string, key: string) => void
48+
): void => {
49+
callback("application/json", "accept");
50+
callback("text/turtle", "content-type");
5551
};
5652
const flatHeaders = flattenHeaders(myHeaders);
5753
expect(Object.entries(flatHeaders)).toEqual([

src/authenticatedFetch/dpop/DpopAuthenticatedFetcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export default class DpopAuthenticatedFetcher implements IAuthenticatedFetcher {
8484
return this.fetcher.fetch(url, {
8585
...requestInit,
8686
headers: {
87-
...flattenHeaders(requestInitiWithDefaults.headers),
87+
...flattenHeaders(requestInitiWithDefaults.headers as Headers),
8888
authorization: `DPOP ${authToken}`,
8989
dpop: await this.dpopHeaderCreator.createHeaderToken(
9090
this.urlRepresentationConverter.requestInfoToUrl(url),

src/authenticatedFetch/headers/HeadersUtils.ts

Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,45 +19,6 @@
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
*/
6930
export 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

Comments
 (0)