Skip to content

Commit ad62b75

Browse files
committed
add get and getAllKeys APIs
1 parent 98bbf83 commit ad62b75

2 files changed

Lines changed: 106 additions & 26 deletions

File tree

‎src/WebStorageStateStore.js‎

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,53 @@ import Log from './Log';
22
import Window from './Window';
33

44
export default class WebStorageStateStore {
5-
constructor({prefix = "oidc.", store = Window.localStorage} = {}){
5+
constructor({prefix = "oidc.", store = Window.localStorage} = {}) {
66
this._store = store;
77
this._prefix = prefix;
88
}
9-
10-
set(key, value){
9+
10+
set(key, value) {
1111
Log.info("WebStorageStateStore.set", key);
12+
13+
key = this._prefix + key;
14+
15+
this._store.setItem(key, value);
1216

17+
return Promise.resolve();
18+
}
19+
20+
get(key) {
21+
Log.info("WebStorageStateStore.get", key);
22+
1323
key = this._prefix + key;
24+
25+
let item = this._store.getItem(key);
1426

15-
try{
16-
this._store.setItem(key, value);
17-
return Promise.resolve();
18-
}
19-
catch(e){
20-
Log.error("storage exception", e);
21-
return Promise.reject(new Error("Failed to store item", key));
22-
}
23-
}
24-
25-
remove(key){
27+
return Promise.resolve(item);
28+
}
29+
30+
remove(key) {
2631
Log.info("WebStorageStateStore.remove", key);
27-
32+
2833
key = this._prefix + key;
2934

30-
var item = this._store.getItem(key);
35+
let item = this._store.getItem(key);
3136
this._store.removeItem(key);
3237

3338
return Promise.resolve(item);
34-
}
35-
}
39+
}
40+
41+
getAllKeys() {
42+
var keys = [];
43+
44+
for (let index = 0; index < this._store.length; index++) {
45+
let key = this._store.key(index);
46+
47+
if (key.indexOf(this._prefix) === 0) {
48+
keys.push(key);
49+
}
50+
}
51+
52+
return Promise.resolve(keys);
53+
}
54+
}

‎test/unit/WebStorageStateStore.spec.js‎

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe("WebStorageStateStore", function() {
1515
beforeEach(function() {
1616
prefix = "";
1717
store = new InMemoryWebStorage();
18-
subject = new WebStorageStateStore({prefix:prefix, store:store});
18+
subject = new WebStorageStateStore({ prefix: prefix, store: store });
1919
});
2020

2121
describe("set", function() {
@@ -25,17 +25,17 @@ describe("WebStorageStateStore", function() {
2525
});
2626

2727
it("should store item", function(done) {
28-
subject.set("key", "value").then(()=>{
28+
subject.set("key", "value").then(() => {
2929
store.getItem("key").should.equal("value");
3030
done();
3131
})
3232
});
33-
34-
it("should use prefix if specified", function(){
33+
34+
it("should use prefix if specified", function() {
3535
prefix = "foo.";
36-
subject = new WebStorageStateStore({prefix:prefix, store:store});
36+
subject = new WebStorageStateStore({ prefix: prefix, store: store });
3737

38-
subject.set("key", "value").then(()=>{
38+
subject.set("key", "value").then(() => {
3939
store.getItem(prefix + "key").should.equal("value");
4040
done();
4141
})
@@ -74,9 +74,9 @@ describe("WebStorageStateStore", function() {
7474
});
7575
});
7676

77-
it("should use prefix if specified", function(done){
77+
it("should use prefix if specified", function(done) {
7878
prefix = "foo.";
79-
subject = new WebStorageStateStore({prefix:prefix, store:store});
79+
subject = new WebStorageStateStore({ prefix: prefix, store: store });
8080

8181
store.setItem("foo.key", "value");
8282

@@ -88,4 +88,65 @@ describe("WebStorageStateStore", function() {
8888

8989
});
9090

91+
describe("get", function() {
92+
93+
it("should return a promise", function() {
94+
subject.get("key").should.be.instanceof(Promise);
95+
});
96+
97+
it("should return value if exists", function(done) {
98+
store.setItem("key", "test");
99+
100+
subject.get("key").then(value => {
101+
value.should.equal('test');
102+
done();
103+
});
104+
});
105+
106+
it("should return undefined if doesn't exist", function(done) {
107+
subject.get("key").then(value => {
108+
expect(value).to.be.undefined;
109+
done();
110+
});
111+
});
112+
113+
it("should use prefix if specified", function(done) {
114+
prefix = "foo.";
115+
subject = new WebStorageStateStore({ prefix: prefix, store: store });
116+
117+
store.setItem("foo.key", "value");
118+
119+
subject.get("key").then(item => {
120+
item.should.equal("value");
121+
done();
122+
});
123+
});
124+
125+
});
126+
127+
describe("getAllKeys", function() {
128+
129+
it("should return a promise", function() {
130+
subject.getAllKeys().should.be.instanceof(Promise);
131+
});
132+
133+
it("should return keys", function(done) {
134+
store.setItem("key1", "test");
135+
store.setItem("key2", "test");
136+
137+
subject.getAllKeys().then(keys => {
138+
keys.should.deep.equal(["key1", "key2"]);
139+
done();
140+
});
141+
});
142+
143+
it("should return empty keys when empty", function(done) {
144+
subject.getAllKeys().then(keys => {
145+
keys.should.deep.equal([]);
146+
done();
147+
});
148+
});
149+
150+
});
151+
91152
});

0 commit comments

Comments
 (0)