@@ -8,7 +8,7 @@ import { WritePayload } from "./util/write-payload"
8
8
import { flipEnumerable , getNonEnumerables } from "./util/enumerables"
9
9
import {
10
10
CredentialStorage ,
11
- NullStorageBackend ,
11
+ InMemoryStorageBackend ,
12
12
StorageBackend
13
13
} from "./credential-storage"
14
14
import { IDMap } from "./id-map"
@@ -53,6 +53,7 @@ export interface ModelConfiguration {
53
53
apiNamespace : string
54
54
jsonapiType : string
55
55
endpoint : string
56
+ credentialStorageBackend : StorageBackend ,
56
57
jwt : string
57
58
jwtStorage : string | false
58
59
keyCase : KeyCase
@@ -117,6 +118,26 @@ export const applyModelConfig = <T extends typeof JSORMBase>(
117
118
) : void => {
118
119
let k : keyof ModelConfigurationOptions
119
120
121
+ config = { ...config } // clone since we're going to mutate it
122
+
123
+ // Handle all JWT configuration at once since it's run-order dependent
124
+ // We'll delete each key we encounter then pass the rest off to
125
+ // a loop for assigning other arbitrary options
126
+ if ( config . credentialStorageBackend ) {
127
+ ModelClass . credentialStorageBackend = config . credentialStorageBackend
128
+ delete config . jwtStorage
129
+ }
130
+
131
+ if ( config . jwtStorage ) {
132
+ ModelClass . jwtStorage = config . jwtStorage
133
+ delete config . jwtStorage
134
+ }
135
+
136
+ if ( config . jwt ) {
137
+ ModelClass . setJWT ( config . jwt )
138
+ delete config . jwt
139
+ }
140
+
120
141
for ( k in config ) {
121
142
if ( config . hasOwnProperty ( k ) ) {
122
143
ModelClass [ k ] = config [ k ]
@@ -136,7 +157,6 @@ export class JSORMBase {
136
157
static jsonapiType ?: string
137
158
static endpoint : string
138
159
static isBaseClass : boolean
139
- static jwt ?: string
140
160
static keyCase : KeyCase = { server : "snake" , client : "camel" }
141
161
static strictAttributes : boolean = false
142
162
static logger : ILogger = defaultLogger
@@ -148,36 +168,48 @@ export class JSORMBase {
148
168
static currentClass : typeof JSORMBase = JSORMBase
149
169
static beforeFetch : BeforeFilter | undefined
150
170
static afterFetch : AfterFilter | undefined
151
- static jwtStorage : string | false = "jwt"
152
171
153
172
private static _typeRegistry : JsonapiTypeRegistry
154
173
private static _IDMap : IDMap
155
174
private static _middlewareStack : MiddlewareStack
156
- private static _credentialStorageBackend ?: StorageBackend
157
- private static _credentialStorage ?: CredentialStorage
175
+ private static _credentialStorageBackend : StorageBackend
176
+ private static _credentialStorage : CredentialStorage
177
+ private static _jwtStorage : string | false = "jwt"
158
178
159
179
static get credentialStorage ( ) : CredentialStorage {
160
- if ( ! this . _credentialStorage ) {
161
- if ( ! this . _credentialStorageBackend ) {
162
- if ( this . jwtStorage && typeof localStorage !== "undefined" ) {
163
- this . _credentialStorageBackend = localStorage
164
- } else {
165
- this . _credentialStorageBackend = new NullStorageBackend ( )
166
- }
167
- }
180
+ return this . _credentialStorage
181
+ }
168
182
169
- this . _credentialStorage = new CredentialStorage (
170
- this . jwtStorage ,
171
- this . _credentialStorageBackend
172
- )
183
+ static set jwtStorage ( val : string | false ) {
184
+ if ( val !== this . _jwtStorage ) {
185
+ this . _jwtStorage = val
186
+ this . credentialStorageBackend = this . _credentialStorageBackend
173
187
}
188
+ }
174
189
175
- return this . _credentialStorage
190
+ static get jwtStorage ( ) : string | false {
191
+ return this . _jwtStorage
176
192
}
177
193
178
- static set credentialStorageBackend ( backend : StorageBackend | undefined ) {
194
+ static set credentialStorageBackend ( backend : StorageBackend ) {
179
195
this . _credentialStorageBackend = backend
180
- this . _credentialStorage = undefined
196
+
197
+ this . _credentialStorage = new CredentialStorage (
198
+ this . jwtStorage || 'jwt' ,
199
+ this . _credentialStorageBackend
200
+ )
201
+ }
202
+
203
+ static get credentialStorageBackend ( ) : StorageBackend {
204
+ return this . _credentialStorageBackend
205
+ }
206
+
207
+ static initializeCredentialStorage ( ) {
208
+ if ( this . jwtStorage && typeof localStorage !== "undefined" ) {
209
+ this . credentialStorageBackend = localStorage
210
+ } else {
211
+ this . credentialStorageBackend = new InMemoryStorageBackend ( )
212
+ }
181
213
}
182
214
183
215
/*
@@ -228,9 +260,6 @@ export class JSORMBase {
228
260
if ( ! this . _IDMap ) {
229
261
this . _IDMap = new IDMap ( )
230
262
}
231
-
232
- const jwt = this . credentialStorage . getJWT ( )
233
- this . setJWT ( jwt )
234
263
}
235
264
236
265
static isSubclassOf ( maybeSuper : typeof JSORMBase ) : boolean {
@@ -753,22 +782,16 @@ export class JSORMBase {
753
782
}
754
783
755
784
static setJWT ( token : string | undefined | null ) : void {
756
- if ( this . baseClass === undefined ) {
757
- throw new Error ( `Cannot set JWT on ${ this . name } : No base class present.` )
758
- }
759
-
760
- this . baseClass . jwt = token || undefined
761
785
this . credentialStorage . setJWT ( token )
762
786
}
763
787
764
788
static getJWT ( ) : string | undefined {
765
- const owner = this . baseClass
766
-
767
- if ( owner ) {
768
- return owner . jwt
769
- }
789
+ return this . credentialStorage . getJWT ( )
770
790
}
771
791
792
+ static get jwt ( ) : string | undefined { return this . getJWT ( ) }
793
+ static set jwt ( token : string | undefined ) { this . setJWT ( token ) }
794
+
772
795
static generateAuthHeader ( jwt : string ) : string {
773
796
return `Token token="${ jwt } "`
774
797
}
@@ -897,6 +920,7 @@ export class JSORMBase {
897
920
}
898
921
899
922
; ( < any > JSORMBase . prototype ) . klass = JSORMBase
923
+ JSORMBase . initializeCredentialStorage ( )
900
924
901
925
export const isModelClass = ( arg : any ) : arg is typeof JSORMBase => {
902
926
if ( ! arg ) {
0 commit comments