From 1a5557919ee9844409848154ba22ec863c4d1a58 Mon Sep 17 00:00:00 2001 From: mt-kenny Date: Tue, 8 Dec 2020 15:38:01 +0900 Subject: [PATCH] feat(headers): set sdk info as additional headers parameter --- src/api/__tests__/exchange-token.test.ts | 4 ++++ src/api/__tests__/request-magic-link.test.ts | 8 ++++++++ src/api/__tests__/token-info.test.ts | 4 ++++ src/api/exchange-token.ts | 2 ++ src/api/request-magic-link.ts | 3 ++- src/api/token-info.ts | 3 ++- src/helper.ts | 10 ++++++++++ 7 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/api/__tests__/exchange-token.test.ts b/src/api/__tests__/exchange-token.test.ts index 611cf4a..80bd644 100644 --- a/src/api/__tests__/exchange-token.test.ts +++ b/src/api/__tests__/exchange-token.test.ts @@ -1,3 +1,5 @@ +declare const __VERSION__: string; + import fetch from 'jest-fetch-mock'; import { MY_ACCOUNT_DOMAINS } from '../../server-paths'; @@ -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, diff --git a/src/api/__tests__/request-magic-link.test.ts b/src/api/__tests__/request-magic-link.test.ts index bc9acfa..bd68f4e 100644 --- a/src/api/__tests__/request-magic-link.test.ts +++ b/src/api/__tests__/request-magic-link.test.ts @@ -1,3 +1,5 @@ +declare const __VERSION__: string; + import fetch from 'jest-fetch-mock'; import qs from 'qs'; @@ -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, @@ -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, @@ -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, diff --git a/src/api/__tests__/token-info.test.ts b/src/api/__tests__/token-info.test.ts index 35b53a8..5c0a46c 100644 --- a/src/api/__tests__/token-info.test.ts +++ b/src/api/__tests__/token-info.test.ts @@ -1,3 +1,5 @@ +declare const __VERSION__: string; + import fetch from 'jest-fetch-mock'; import qs from 'qs'; @@ -54,6 +56,8 @@ describe('api', () => { headers: { Authorization: `Bearer ${token}`, 'API-Version': '1604911588', + 'mt-sdk-platform': 'js', + 'mt-sdk-version': __VERSION__, }, }); }); diff --git a/src/api/exchange-token.ts b/src/api/exchange-token.ts index 218defa..b40c5cf 100644 --- a/src/api/exchange-token.ts +++ b/src/api/exchange-token.ts @@ -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'; @@ -46,6 +47,7 @@ export default async function exchangeToken( method: 'POST', headers: { 'Content-Type': 'application/json', + ...generateSdkHeaderInfo(), }, body: JSON.stringify({ code, diff --git a/src/api/request-magic-link.ts b/src/api/request-magic-link.ts index 838a495..4a0b500 100644 --- a/src/api/request-magic-link.ts +++ b/src/api/request-magic-link.ts @@ -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'; @@ -38,6 +38,7 @@ export default async function requestMagicLink( method: 'POST', headers: { 'Content-Type': 'application/json', + ...generateSdkHeaderInfo(), }, body: JSON.stringify({ email, diff --git a/src/api/token-info.ts b/src/api/token-info.ts index f0a1675..6e6d5a3 100644 --- a/src/api/token-info.ts +++ b/src/api/token-info.ts @@ -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'; @@ -26,6 +26,7 @@ export default async function tokenInfo( headers: { Authorization: `Bearer ${token}`, 'API-Version': '1604911588', + ...generateSdkHeaderInfo(), }, } ); diff --git a/src/helper.ts b/src/helper.ts index e0ddeb0..b362956 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -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__, + }; +}