Skip to content

Commit 491dda8

Browse files
committed
refactor!: standardize tokenInfo response
BREAKING CHANGE: change `tokenInfo` API response to standard format.
1 parent c918b9d commit 491dda8

File tree

5 files changed

+42
-36
lines changed

5 files changed

+42
-36
lines changed

docs/README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,19 @@ You can validate a token or get guest or resource server information related to
154154

155155
```javascript
156156
const tokenInfo = await mtLinkSdk.tokenInfo(token, options);
157-
tokenInfo.guestUid // guest's uid
158-
tokenInfo.country // guest's country
159-
tokenInfo.currency // guest's currency
160-
tokenInfo.language // guest's language
161-
tokenInfo.resourceServer // resource server domain
162-
tokenInfo.clientId // app client id
163-
tokenInfo.clientName // app name
164-
tokenInfo.expTimestamp // token expiration timestamp
165-
tokenInfo.scopes // token access scopes
166-
tokenInfo.isMtClient // for internal use
157+
tokenInfo.iat // token creation timestamp,
158+
tokenInfo.exp // token expiration timestamp,
159+
tokenInfo.sub // guest uid or null,
160+
tokenInfo.scope // string with space separated scopes
161+
tokenInfo.client_id // application client id or null
162+
tokenInfo.app // application related information or null
163+
tokenInfo.app.name // application name
164+
tokenInfo.app.is_mt // is moneytree client (internal usage)
165+
tokenInfo.guest // guest related information or null
166+
tokenInfo.guest.email // guest email if available
167+
tokenInfo.guest.country // guest country
168+
tokenInfo.guest.currency // guest currency,
169+
tokenInfo.guest.lang // guest language
167170
```
168171

169172
| Parameter | Type | Required | Default Value | Description |

src/api/__tests__/token-info.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('api', () => {
2020
name: 'aud-name',
2121
},
2222
exp: 'exp',
23-
scopes: 'scopes',
23+
scopes: ['scopes'],
2424
};
2525

2626
const mtLinkSdk = new MtLinkSdk();

src/api/token-info.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { stringify } from 'qs';
2+
import { generateConfigs } from '../helper';
13
import { MY_ACCOUNT_DOMAINS } from '../server-paths';
24
import { StoredOptions, TokenInfo } from '../typings';
35

@@ -11,12 +13,17 @@ export default async function tokenInfo(
1113
throw new Error('[mt-link-sdk] Missing parameter `token` in `tokenInfo`.');
1214
}
1315

16+
const queryString = stringify({
17+
client_id: clientId,
18+
configs: generateConfigs()
19+
})
20+
1421
try {
15-
const response = await fetch(`${MY_ACCOUNT_DOMAINS[mode]}/oauth/token/info.json`, {
22+
const response = await fetch(`${MY_ACCOUNT_DOMAINS[mode]}/oauth/token/info.json?${queryString}`, {
1623
method: 'GET',
1724
headers: {
1825
Authorization: `Bearer ${token}`,
19-
'x-api-key': clientId as string,
26+
'API-Version': '1604911588',
2027
},
2128
});
2229

@@ -26,18 +33,7 @@ export default async function tokenInfo(
2633
throw new Error(result.error_description);
2734
}
2835

29-
return {
30-
guestUid: result.uid,
31-
resourceServer: result.resource_server,
32-
country: result.country,
33-
currency: result.currency,
34-
language: result.locale,
35-
clientId: result.aud.uid,
36-
clientName: result.aud.name,
37-
expTimestamp: result.exp,
38-
scopes: result.scopes,
39-
isMtClient: result.is_mt_client,
40-
};
36+
return result;
4137
} catch (error) {
4238
throw new Error(`[mt-link-sdk] \`tokenInfo\` execution failed. ${error}`);
4339
}

src/helper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function generateConfigs(configs: ConfigsOptions = {}): string {
7272
];
7373

7474
for (const key in configs) {
75-
if (configKeys.includes(key)) {
75+
if (configKeys.indexOf(key) !== -1) {
7676
snakeCaseConfigs[snakeCase(key)] = configs[key as keyof ConfigsOptions];
7777
}
7878
}

src/typings.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,21 @@ export interface RequestMagicLinkOptions extends ConfigsOptions {
111111
}
112112

113113
export interface TokenInfo {
114-
guestUid: string;
115-
resourceServer: string;
116-
country: string;
117-
currency: string;
118-
language: string;
119-
clientName: string;
120-
clientId: string;
121-
expTimestamp: number;
122-
scopes: Scopes;
123-
isMtClient: boolean;
114+
iss: string;
115+
iat: number;
116+
exp: number;
117+
aud: string[];
118+
sub: null | string;
119+
scope: string;
120+
client_id: null | string;
121+
app: null | {
122+
name: string;
123+
is_mt: boolean;
124+
};
125+
guest: null | {
126+
email: string;
127+
country: string;
128+
currency: string;
129+
lang: string;
130+
};
124131
}

0 commit comments

Comments
 (0)