Skip to content

Commit

Permalink
feat(headers): set sdk info as additional headers parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mt-kenny committed Dec 9, 2020
1 parent e211ac4 commit 1a55579
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/api/__tests__/exchange-token.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare const __VERSION__: string;

import fetch from 'jest-fetch-mock';

import { MY_ACCOUNT_DOMAINS } from '../../server-paths';
Expand Down Expand Up @@ -58,6 +60,8 @@ describe('api', () => {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'mt-sdk-platform': 'js',
'mt-sdk-version': __VERSION__,
},
body: JSON.stringify({
code,
Expand Down
8 changes: 8 additions & 0 deletions src/api/__tests__/request-magic-link.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare const __VERSION__: string;

import fetch from 'jest-fetch-mock';
import qs from 'qs';

Expand Down Expand Up @@ -44,6 +46,8 @@ describe('api', () => {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'mt-sdk-platform': 'js',
'mt-sdk-version': __VERSION__,
},
body: JSON.stringify({
email,
Expand Down Expand Up @@ -71,6 +75,8 @@ describe('api', () => {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'mt-sdk-platform': 'js',
'mt-sdk-version': __VERSION__,
},
body: JSON.stringify({
email,
Expand Down Expand Up @@ -119,6 +125,8 @@ describe('api', () => {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'mt-sdk-platform': 'js',
'mt-sdk-version': __VERSION__,
},
body: JSON.stringify({
email,
Expand Down
4 changes: 4 additions & 0 deletions src/api/__tests__/token-info.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare const __VERSION__: string;

import fetch from 'jest-fetch-mock';
import qs from 'qs';

Expand Down Expand Up @@ -54,6 +56,8 @@ describe('api', () => {
headers: {
Authorization: `Bearer ${token}`,
'API-Version': '1604911588',
'mt-sdk-platform': 'js',
'mt-sdk-version': __VERSION__,
},
});
});
Expand Down
2 changes: 2 additions & 0 deletions src/api/exchange-token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import qs from 'qs';

import { generateSdkHeaderInfo } from '../helper';
import { MY_ACCOUNT_DOMAINS } from '../server-paths';
import { StoredOptions, ExchangeTokenOptions } from '../typings';
import storage from '../storage';
Expand Down Expand Up @@ -46,6 +47,7 @@ export default async function exchangeToken(
method: 'POST',
headers: {
'Content-Type': 'application/json',
...generateSdkHeaderInfo(),
},
body: JSON.stringify({
code,
Expand Down
3 changes: 2 additions & 1 deletion src/api/request-magic-link.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { stringify } from 'qs';

import { generateConfigs, mergeConfigs } from '../helper';
import { generateConfigs, mergeConfigs, generateSdkHeaderInfo } from '../helper';
import { MY_ACCOUNT_DOMAINS } from '../server-paths';
import { StoredOptions, RequestMagicLinkOptions } from '../typings';

Expand Down Expand Up @@ -38,6 +38,7 @@ export default async function requestMagicLink(
method: 'POST',
headers: {
'Content-Type': 'application/json',
...generateSdkHeaderInfo(),
},
body: JSON.stringify({
email,
Expand Down
3 changes: 2 additions & 1 deletion src/api/token-info.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { stringify } from 'qs';
import { generateConfigs } from '../helper';
import { generateConfigs, generateSdkHeaderInfo } from '../helper';
import { MY_ACCOUNT_DOMAINS } from '../server-paths';
import { StoredOptions, TokenInfo } from '../typings';

Expand All @@ -26,6 +26,7 @@ export default async function tokenInfo(
headers: {
Authorization: `Bearer ${token}`,
'API-Version': '1604911588',
...generateSdkHeaderInfo(),
},
}
);
Expand Down
10 changes: 10 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,13 @@ export function generateCodeChallenge(): string {

return encode(createHash('sha256').update(codeVerifier).digest('base64').split('=')[0]);
}

export function generateSdkHeaderInfo(): {
'mt-sdk-platform': string;
'mt-sdk-version': string;
} {
return {
'mt-sdk-platform': 'js',
'mt-sdk-version': __VERSION__,
};
}

0 comments on commit 1a55579

Please sign in to comment.