Skip to content

Commit

Permalink
Use object instead of Headers (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos authored Nov 29, 2022
1 parent bf0f839 commit 508bac4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
18 changes: 10 additions & 8 deletions src/contentFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,35 +159,37 @@ export class ContentFetcher {

const {apiKey, appId} = this.configuration;

const headers = new Headers();
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};

headers.set('X-Client-Library', CLIENT_LIBRARY);
headers['X-Client-Library'] = CLIENT_LIBRARY;

if (appId !== undefined) {
headers.set('X-App-Id', appId);
headers['X-App-Id'] = appId;
}

if (apiKey !== undefined) {
headers.set('X-Api-Key', apiKey);
headers['X-Api-Key'] = apiKey;
}

const dynamic = ContentFetcher.isDynamicContent(options);

if (dynamic) {
if (options.clientId !== undefined) {
headers.set('X-Client-Id', options.clientId);
headers['X-Client-Id'] = options.clientId;
}

if (options.clientIp !== undefined) {
headers.set('X-Client-Ip', options.clientIp);
headers['X-Client-Ip'] = options.clientIp;
}

if (options.userToken !== undefined) {
headers.set('X-Token', options.userToken.toString());
headers['X-Token'] = options.userToken.toString();
}

if (options.userAgent !== undefined) {
headers.set('User-Agent', options.userAgent);
headers['User-Agent'] = options.userAgent;
}

if (options.version !== undefined) {
Expand Down
18 changes: 10 additions & 8 deletions src/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,30 +210,32 @@ export class Evaluator {
const {appId, apiKey} = this.configuration;
const {clientId, clientIp, userAgent, userToken} = options;

const headers = new Headers();
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};

headers.set('X-Client-Library', CLIENT_LIBRARY);
headers['X-Client-Library'] = CLIENT_LIBRARY;

if (apiKey !== undefined) {
headers.set('X-Api-Key', apiKey);
headers['X-Api-Key'] = apiKey;
} else if (appId !== undefined) {
headers.set('X-App-Id', appId);
headers['X-App-Id'] = appId;
}

if (clientId !== undefined) {
headers.set('X-Client-Id', clientId);
headers['X-Client-Id'] = clientId;
}

if (clientIp !== undefined) {
headers.set('X-Client-Ip', clientIp);
headers['X-Client-Ip'] = clientIp;
}

if (userToken !== undefined) {
headers.set('X-Token', userToken.toString());
headers['X-Token'] = userToken.toString();
}

if (userAgent !== undefined) {
headers.set('User-Agent', userAgent);
headers['User-Agent'] = userAgent;
}

return fetch(this.endpoint, {
Expand Down
1 change: 1 addition & 0 deletions test/contentFetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('A content fetcher', () => {
const requestMatcher: MockOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Client-Library': CLIENT_LIBRARY,
},
body: {
Expand Down
1 change: 1 addition & 0 deletions test/evaluator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('An evaluator', () => {
const requestMatcher: MockOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Client-Library': CLIENT_LIBRARY,
},
body: {
Expand Down

0 comments on commit 508bac4

Please sign in to comment.