Skip to content

Commit

Permalink
fix: prevent custom headers if the request is not going to tolgee pla…
Browse files Browse the repository at this point in the history
…tform (#3347)
  • Loading branch information
stepan662 authored Jun 26, 2024
1 parent 9c7466a commit 1219aa5
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/core/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,25 @@ export function getErrorMessage(error: any): string | undefined {

const defaultFetchFunction: FetchFn = (input, options) => fetch(input, options);

function headersInitToRecord(headersInit?: HeadersInit | undefined) {
return Object.fromEntries(new Headers(headersInit).entries());
}

export const createFetchFunction = (
fetchFn: FetchFn = defaultFetchFunction
): FetchFn => {
return (input, init) =>
fetchFn(input, {
return (input, init) => {
let headers = headersInitToRecord(init?.headers);
if (headers['x-api-key']) {
headers = {
'x-tolgee-sdk-type': 'JS',
'x-tolgee-sdk-version': process.env.TOLGEE_UI_VERSION || 'prerelease',
...headers,
};
}
return fetchFn(input, {
...init,
headers: {
'X-Tolgee-SDK-Type': 'JS',
'X-Tolgee-SDK-Version': process.env.TOLGEE_UI_VERSION || 'prerelease',
...init?.headers,
},
headers,
});
};
};

0 comments on commit 1219aa5

Please sign in to comment.