Skip to content

Commit

Permalink
feat: add sdk session id (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
dorsha authored Oct 10, 2024
1 parent 2519d6d commit 9f8f5a6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
let sessionId: string;

export const getClientSessionId = (): string => {
if (sessionId) {
return sessionId;
}
const currentDate = new Date();
const utcString = `${currentDate.getUTCFullYear().toString()}-${(
currentDate.getUTCMonth() + 1
)
.toString()
.padStart(2, '0')}-${currentDate
.getUTCDate()
.toString()
.padStart(2, '0')}-${currentDate
.getUTCHours()
.toString()
.padStart(2, '0')}:${currentDate
.getUTCMinutes()
.toString()
.padStart(2, '0')}:${currentDate
.getUTCSeconds()
.toString()
.padStart(2, '0')}:${currentDate.getUTCMilliseconds().toString()}`;
const randomSuffix = Math.floor(1000 + Math.random() * 9000);
sessionId = `${utcString}-${randomSuffix}`;
return sessionId;
};
1 change: 1 addition & 0 deletions packages/sdks/core-js-sdk/src/httpClient/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as createFetchLogger } from './createFetchLogger';
export { getClientSessionId } from './getClientSessionId';

export function transformSetCookie(setCookieHeader: string) {
// Split the header by semicolons to separate different attributes
Expand Down
3 changes: 2 additions & 1 deletion packages/sdks/core-js-sdk/src/httpClient/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { transformSetCookie } from './helpers';
import { getClientSessionId, transformSetCookie } from './helpers';
import createFetchLogger from './helpers/createFetchLogger';
import {
CreateHttpClientConfig,
Expand Down Expand Up @@ -35,6 +35,7 @@ declare const BUILD_VERSION: string;
*/
const createDescopeHeaders = () => {
return {
'x-descope-sdk-session-id': getClientSessionId(),
'x-descope-sdk-name': 'core-js',
'x-descope-sdk-version': BUILD_VERSION,
};
Expand Down
23 changes: 22 additions & 1 deletion packages/sdks/core-js-sdk/test/httpClient.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DEFAULT_BASE_API_URL } from '../src/constants';
import createHttpClient from '../src/httpClient';
import { getClientSessionId } from '../src/httpClient/helpers';
import createFetchLogger from '../src/httpClient/helpers/createFetchLogger';
import { ExtendedResponse } from '../src/httpClient/types';

Expand All @@ -11,6 +12,7 @@ const afterRequestHook = jest.fn();
const descopeHeaders = {
'x-descope-sdk-name': 'core-js',
'x-descope-sdk-version': globalThis.BUILD_VERSION,
'x-descope-sdk-session-id': getClientSessionId(),
};

const httpClient = createHttpClient({
Expand Down Expand Up @@ -160,8 +162,8 @@ describe('httpClient', () => {
test2: '123',
test: '123',
Authorization: 'Bearer 456',
...descopeHeaders,
'x-descope-sdk-name': 'lulu',
'x-descope-sdk-version': globalThis.BUILD_VERSION,
}),
method: 'GET',
},
Expand Down Expand Up @@ -205,6 +207,25 @@ describe('httpClient', () => {
cookiePolicy: null,
});

httpClient.get('1/2/3/4', {
headers: { test2: '123' },
queryParams: { test2: '123' },
});

expect(mockFetch).toHaveBeenCalledWith(
'http://descope.com/1/2/3/4?test2=123',
{
body: undefined,
headers: new Headers({
test2: '123',
test: '123',
Authorization: 'Bearer 456',
...descopeHeaders,
}),
method: 'GET',
},
);

httpClient.get('1/2/3', {
headers: { test2: '123' },
queryParams: { test2: '123' },
Expand Down
7 changes: 7 additions & 0 deletions packages/sdks/web-js-sdk/test/persistTokens.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,16 @@ import createSdk from '../src/index';
import { authInfo } from './mocks';
import { createMockReturnValue } from './testUtils';

globalThis.Headers = class Headers {
constructor(obj: object) {
return Object.assign({}, obj);
}
} as any;

const descopeHeaders = {
'x-descope-sdk-name': 'web-js',
'x-descope-sdk-version': global.BUILD_VERSION,
'x-descope-sdk-session-id': expect.any(String),
};

const mockFetch = jest.fn().mockReturnValueOnce(new Promise(() => {}));
Expand Down

0 comments on commit 9f8f5a6

Please sign in to comment.