Skip to content

Commit 62ebed6

Browse files
committed
feat: added framework settings config and expanded tests
1 parent 5ba4864 commit 62ebed6

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

lib/main.test.ts

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ describe("index exports", () => {
6363
"getUserOrganizations",
6464
"getUserProfile",
6565
"setActiveStorage",
66+
67+
// config
68+
"frameworkSettings",
6669
];
6770

6871
expect(actualExports.sort()).toEqual(expectedExports.sort());

lib/utils/exchangeAuthCode.test.ts

+59-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { exchangeAuthCode } from ".";
33
import { MemoryStorage, StorageKeys } from "../sessionManager";
44
import { setActiveStorage } from "./token";
55
import createFetchMock from "vitest-fetch-mock";
6+
import { frameworkSettings } from "./exchangeAuthCode";
67

78
const fetchMock = createFetchMock(vi);
89

@@ -89,7 +90,7 @@ describe("exhangeAuthCode", () => {
8990
});
9091
});
9192

92-
it("should encode a simple string", async () => {
93+
it("should exchange tokens, set storage and clear temp values", async () => {
9394
const store = new MemoryStorage();
9495
setActiveStorage(store);
9596

@@ -133,5 +134,62 @@ describe("exhangeAuthCode", () => {
133134
StorageKeys.codeVerifier,
134135
);
135136
expect(postCodeVerifier).toBeNull();
137+
expect(fetchMock).toHaveBeenCalledTimes(1);
138+
const [url, options] = fetchMock.mock.calls[0];
139+
expect(url).toBe("http://test.kinde.com/oauth2/token");
140+
expect(options).toMatchObject({
141+
method: "POST",
142+
headers: {
143+
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
144+
},
145+
});
146+
});
147+
148+
it("set the framework and version on header", async () => {
149+
const store = new MemoryStorage();
150+
setActiveStorage(store);
151+
152+
const state = "state";
153+
154+
await store.setItems({
155+
[StorageKeys.state]: state,
156+
});
157+
158+
frameworkSettings.framework = "Framework";
159+
frameworkSettings.frameworkVersion = "Version";
160+
161+
const input = "hello";
162+
163+
const urlParams = new URLSearchParams();
164+
urlParams.append("code", input);
165+
urlParams.append("state", state);
166+
urlParams.append("client_id", "test");
167+
168+
fetchMock.mockResponseOnce(
169+
JSON.stringify({
170+
access_token: "access_token",
171+
refresh_token: "refresh_token",
172+
id_token: "id_token",
173+
}),
174+
);
175+
176+
await exchangeAuthCode({
177+
urlParams,
178+
domain: "http://test.kinde.com",
179+
clientId: "test",
180+
redirectURL: "http://test.kinde.com",
181+
});
182+
183+
expect(fetchMock).toHaveBeenCalledTimes(1);
184+
const [url, options] = fetchMock.mock.calls[0];
185+
expect(url).toBe("http://test.kinde.com/oauth2/token");
186+
expect(options).toMatchObject({
187+
method: "POST",
188+
headers: {
189+
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
190+
"Kinde-SDK": "Framework/Version",
191+
},
192+
});
136193
});
194+
137195
});

lib/utils/exchangeAuthCode.ts

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { getActiveStorage, StorageKeys } from "../main";
22

3-
// TODO: Set the framework and version
4-
const _framework = "";
5-
const _frameworkVersion = "";
3+
export const frameworkSettings: { framework: string; frameworkVersion: string } = {
4+
framework: "",
5+
frameworkVersion: "",
6+
};
67

78
export const exchangeAuthCode = async ({
89
urlParams,
@@ -33,7 +34,7 @@ export const exchangeAuthCode = async ({
3334
activeStorage.getSessionItem(StorageKeys.state);
3435

3536
// warn if framework and version has not been set
36-
if (!_framework || !_frameworkVersion) {
37+
if (!frameworkSettings.framework || !frameworkSettings.frameworkVersion) {
3738
console.warn(
3839
"Framework and version not set. Please set the framework and version in the config object",
3940
);
@@ -52,14 +53,19 @@ export const exchangeAuthCode = async ({
5253
StorageKeys.codeVerifier,
5354
)) as string;
5455

56+
const headers: { 'Content-type': string, "Kinde-SDK"?: string } = {
57+
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
58+
}
59+
60+
if (frameworkSettings.framework) {
61+
headers["Kinde-SDK"] = `${frameworkSettings.framework}/${frameworkSettings.frameworkVersion}`
62+
}
63+
5564
const response = await fetch(`${domain}/oauth2/token`, {
5665
method: "POST",
5766
// ...(isUseCookie && {credentials: 'include'}),
5867
credentials: "include",
59-
headers: new Headers({
60-
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
61-
"Kinde-SDK": `${_framework}/${_frameworkVersion}`,
62-
}),
68+
headers,
6369
body: new URLSearchParams({
6470
client_id: clientId,
6571
code,

lib/utils/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import { extractAuthResults } from "./extractAuthResults";
55
import { sanatizeURL } from "./sanatizeUrl";
66
import { generateAuthUrl } from "./generateAuthUrl";
77
import { mapLoginMethodParamsForUrl } from "./mapLoginMethodParamsForUrl";
8-
import { exchangeAuthCode } from "./exchangeAuthCode";
8+
import { exchangeAuthCode, frameworkSettings } from "./exchangeAuthCode";
99

1010
export {
11+
// config
12+
frameworkSettings,
13+
14+
// utils
1115
base64UrlEncode,
1216
generateRandomString,
1317
extractAuthResults,

0 commit comments

Comments
 (0)