Skip to content

Commit ab1229b

Browse files
committed
Rename localStorge => credentialStorage
While localStorage is the default backend, I just remembered that the sessionStorage API has the exact same type signature and can be used interchangably, so there's no reason to name it in a way that confuses.
1 parent b01f50b commit ab1229b

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

src/local-storage.ts renamed to src/credential-storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ try {
2525
defaultBackend = new NullStorageBackend()
2626
}
2727

28-
export class LocalStorage {
28+
export class CredentialStorage {
2929
private _jwtKey: string | false
3030
private _backend: StorageBackend
3131

src/model.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ import relationshipIdentifiersFor from "./util/relationship-identifiers"
66
import { Request, RequestVerbs, JsonapiResponse } from "./request"
77
import { WritePayload } from "./util/write-payload"
88
import {
9-
LocalStorage,
9+
CredentialStorage,
1010
NullStorageBackend,
1111
StorageBackend
12-
} from "./local-storage"
12+
} from "./credential-storage"
1313

1414
import { deserialize, deserializeInstance } from "./util/deserialize"
1515
import { Attribute } from "./attribute"
@@ -45,7 +45,7 @@ export interface ModelConfiguration {
4545
jsonapiType: string
4646
endpoint: string
4747
jwt: string
48-
jwtLocalStorage: string | false
48+
jwtStorage: string | false
4949
camelizeKeys: boolean
5050
strictAttributes: boolean
5151
logger: ILogger
@@ -138,35 +138,35 @@ export class JSORMBase {
138138
static currentClass: typeof JSORMBase = JSORMBase
139139
static beforeFetch: BeforeFilter | undefined
140140
static afterFetch: AfterFilter | undefined
141-
static jwtLocalStorage: string | false = "jwt"
141+
static jwtStorage: string | false = "jwt"
142142

143143
private static _typeRegistry: JsonapiTypeRegistry
144144
private static _middlewareStack: MiddlewareStack
145-
private static _localStorageBackend?: StorageBackend
146-
private static _localStorage?: LocalStorage
147-
148-
static get localStorage(): LocalStorage {
149-
if (!this._localStorage) {
150-
if (!this._localStorageBackend) {
151-
if (this.jwtLocalStorage && typeof localStorage !== "undefined") {
152-
this._localStorageBackend = localStorage
145+
private static _credentialStorageBackend?: StorageBackend
146+
private static _credentialStorage?: CredentialStorage
147+
148+
static get credentialStorage(): CredentialStorage {
149+
if (!this._credentialStorage) {
150+
if (!this._credentialStorageBackend) {
151+
if (this.jwtStorage && typeof localStorage !== "undefined") {
152+
this._credentialStorageBackend = localStorage
153153
} else {
154-
this._localStorageBackend = new NullStorageBackend()
154+
this._credentialStorageBackend = new NullStorageBackend()
155155
}
156156
}
157157

158-
this._localStorage = new LocalStorage(
159-
this.jwtLocalStorage,
160-
this._localStorageBackend
158+
this._credentialStorage = new CredentialStorage(
159+
this.jwtStorage,
160+
this._credentialStorageBackend
161161
)
162162
}
163163

164-
return this._localStorage
164+
return this._credentialStorage
165165
}
166166

167-
static set localStorageBackend(backend: StorageBackend | undefined) {
168-
this._localStorageBackend = backend
169-
this._localStorage = undefined
167+
static set credentialStorageBackend(backend: StorageBackend | undefined) {
168+
this._credentialStorageBackend = backend
169+
this._credentialStorage = undefined
170170
}
171171

172172
/*
@@ -214,7 +214,7 @@ export class JSORMBase {
214214
this._middlewareStack = new MiddlewareStack()
215215
}
216216

217-
const jwt = this.localStorage.getJWT()
217+
const jwt = this.credentialStorage.getJWT()
218218
this.setJWT(jwt)
219219
}
220220

@@ -665,7 +665,7 @@ export class JSORMBase {
665665
}
666666

667667
this.baseClass.jwt = token || undefined
668-
this.localStorage.setJWT(token)
668+
this.credentialStorage.setJWT(token)
669669
}
670670

671671
static getJWT(): string | undefined {

test/integration/authorization.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ describe("authorization headers", () => {
150150
})
151151

152152
describe("local storage", () => {
153-
let localStorageMock: {
153+
let credentialStorageMock: {
154154
getItem: SinonSpy
155155
setItem: SinonSpy
156156
removeItem: SinonSpy
@@ -163,31 +163,31 @@ describe("authorization headers", () => {
163163
;(<any>ApplicationRecord) = null
164164
;(<any>Author) = null
165165

166-
localStorageMock = {
166+
credentialStorageMock = {
167167
setItem: sinon.spy(),
168168
getItem: sinon.spy(),
169169
removeItem: sinon.spy()
170170
}
171-
JSORMBase.localStorageBackend = localStorageMock
171+
JSORMBase.credentialStorageBackend = credentialStorageMock
172172
})
173173

174174
afterEach(() => {
175-
JSORMBase.localStorageBackend = undefined as any
176-
JSORMBase.jwtLocalStorage = false
175+
JSORMBase.credentialStorageBackend = undefined as any
176+
JSORMBase.jwtStorage = false
177177
})
178178

179179
describe("when configured to store jwt", () => {
180-
describe("when JWT is not in localStorage", () => {
180+
describe("when JWT is not in credentialStorage", () => {
181181
beforeEach(() => {
182-
JSORMBase.jwtLocalStorage = "jwt"
182+
JSORMBase.jwtStorage = "jwt"
183183

184184
buildModels()
185185
})
186186

187-
it("updates localStorage on server response", async () => {
187+
it("updates credentialStorage on server response", async () => {
188188
await Author.all()
189189

190-
expect(localStorageMock.setItem).to.have.been.calledWith(
190+
expect(credentialStorageMock.setItem).to.have.been.calledWith(
191191
"jwt",
192192
"somet0k3n"
193193
)
@@ -213,11 +213,11 @@ describe("authorization headers", () => {
213213
})
214214
})
215215

216-
describe("when JWT is already in localStorage", () => {
216+
describe("when JWT is already in credentialStorage", () => {
217217
beforeEach(() => {
218-
JSORMBase.jwtLocalStorage = "jwt"
218+
JSORMBase.jwtStorage = "jwt"
219219
fetchMock.restore()
220-
JSORMBase.localStorage.getJWT = sinon.stub().returns("myt0k3n")
220+
JSORMBase.credentialStorage.getJWT = sinon.stub().returns("myt0k3n")
221221

222222
buildModels()
223223
})
@@ -240,15 +240,15 @@ describe("authorization headers", () => {
240240

241241
describe("when configured to NOT store jwt", () => {
242242
beforeEach(() => {
243-
JSORMBase.jwtLocalStorage = false
243+
JSORMBase.jwtStorage = false
244244

245245
buildModels()
246246
})
247247

248-
it("is does NOT update localStorage on server response", async () => {
248+
it("is does NOT update credentialStorage on server response", async () => {
249249
await Author.all()
250250

251-
expect(localStorageMock.setItem).not.to.have.been.called
251+
expect(credentialStorageMock.setItem).not.to.have.been.called
252252
})
253253
})
254254
})

test/unit/model.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { JSORMBase } from "../../src/model"
33
import { hasOne } from "../../src/associations"
44
import { attr } from "../../src/attribute"
55
import { JsonapiTypeRegistry } from "../../src/jsonapi-type-registry"
6-
import { StorageBackend } from "../../src/local-storage"
6+
import { StorageBackend } from "../../src/credential-storage"
77
import { JsonapiResource, JsonapiResponseDoc } from "../../src/index"
88

99
import {
@@ -86,13 +86,13 @@ describe("Model", () => {
8686
})
8787
})
8888

89-
describe("when localStorage is configured", () => {
89+
describe("when credentialStorage is configured", () => {
9090
let backend: StorageBackend
9191
const buildModel = () => {
9292
// need new class for this since it needs initialization to have the jwt config set
9393
@Model({
94-
jwtLocalStorage: "MyJWT",
95-
localStorageBackend: backend // Cast to any since we don't want this to be part of the standard model config interface. Just for test stubbing purposes
94+
jwtStorage: "MyJWT",
95+
credentialStorageBackend: backend // Cast to any since we don't want this to be part of the standard model config interface. Just for test stubbing purposes
9696
} as any)
9797
class Base extends JSORMBase {}
9898
BaseClass = Base
@@ -108,7 +108,7 @@ describe("Model", () => {
108108
buildModel()
109109
})
110110

111-
it("adds to localStorage", () => {
111+
it("adds to credentialStorage", () => {
112112
BaseClass.setJWT("n3wt0k3n")
113113
expect(backend.setItem).to.have.been.calledWith(
114114
"MyJWT",

0 commit comments

Comments
 (0)