Skip to content

Commit

Permalink
Fix LocalForageConfigStore storing stringified data (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
RonConfigu committed Mar 11, 2024
1 parent 68d3270 commit b6f4dd1
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions ts/packages/browser/src/stores/LocalForage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,27 @@ export class LocalForageConfigStore extends KeyValueConfigStore {
if (typeof data === 'string') {
return data ?? '';
}
return JSON.stringify(data) ?? '';
return data === null ? '' : JSON.stringify(data) ?? '';
}

// * https://localforage.github.io/localForage/#data-api-setitem
async upsert(key: string, value: string): Promise<void> {
await this.client.setItem(key, value);
let parsedValue;
try {
parsedValue = JSON.parse(value, (__, jsonValue) => {
if (typeof jsonValue === 'string') {
try {
return JSON.parse(jsonValue);
} catch (e) {
return jsonValue;
}
}
return jsonValue;
});
} catch {
parsedValue = value;
}
await this.client.setItem(key, parsedValue);
}

// * https://localforage.github.io/localForage/#data-api-removeitem
Expand Down

0 comments on commit b6f4dd1

Please sign in to comment.