Skip to content

Commit

Permalink
fix: make sessionId be a string when set automatically (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswk authored Sep 4, 2024
1 parent 81903aa commit d394b41
Show file tree
Hide file tree
Showing 4 changed files with 696 additions and 871 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,6 @@
"rules": {
"@typescript-eslint/no-explicit-any": "off"
}
}
},
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
32 changes: 30 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,8 @@ test('Should read session id from localStorage', async () => {
public async get(name: string) {
if (name === 'sessionId') {
return sessionId;
} else {
return Promise.resolve([]);
}
return Promise.resolve([]);
}
}

Expand All @@ -183,6 +182,35 @@ test('Should read session id from localStorage', async () => {
expect(context.sessionId).toBe(sessionId);
});

test('Should send sessionId as string, even if it was saved a number', async () => {
const sessionId = 123;
fetchMock.mockReject();

class Store implements IStorageProvider {
public async save() {
return Promise.resolve();
}

public async get(name: string) {
if (name === 'sessionId') {
return sessionId;
}
return Promise.resolve([]);
}
}

const storageProvider = new Store();
const config: IConfig = {
url: 'http://localhost/test',
clientKey: '12',
appName: 'web',
storageProvider,
};
const client = new UnleashClient(config);
await client.start();
const context = client.getContext();
expect(context.sessionId).toBe('123');
});
test('Should read toggles from localStorage', async () => {
jest.spyOn(global.console, 'error').mockImplementation(() => jest.fn());
const toggles = [
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,9 @@ export class UnleashClient extends TinyEmitter {
let sessionId = await this.storage.get('sessionId');
if (!sessionId) {
sessionId = Math.floor(Math.random() * 1_000_000_000);
await this.storage.save('sessionId', sessionId);
await this.storage.save('sessionId', sessionId.toString(10));
}
return sessionId;
return sessionId.toString(10);
}

private getHeaders() {
Expand Down
Loading

0 comments on commit d394b41

Please sign in to comment.