@@ -49,7 +49,8 @@ class Auth {
49
49
drop : 'drop' ,
50
50
get : 'get' ,
51
51
set : 'set'
52
- }
52
+ } ,
53
+ status : 'cached'
53
54
} ;
54
55
55
56
/** @type CoreError */
@@ -145,7 +146,7 @@ class Auth {
145
146
* @param {Object } [opts.cache=false]
146
147
* @param {String } opts.cache.key
147
148
* @param {String } opts.cache.action
148
- * @param {Boolean } opts.cache.onError
149
+ * @param {Boolean } opts.cache.onNotFound
149
150
* @param {Number } retry Allow retries in case of expired token
150
151
*
151
152
* @returns {Promise<{} | Array<{}>> }
@@ -174,25 +175,23 @@ class Auth {
174
175
175
176
let status ;
176
177
let body ;
177
-
178
178
if ( this . isCacheEnabled && opts . cache ) {
179
179
if ( opts . cache . action === this . cache . actions . drop ) {
180
180
await this . executeCacheOp ( opts . cache ) ;
181
181
}
182
182
if ( opts . cache . action === this . cache . actions . get ) {
183
183
body = await this . executeCacheOp ( opts . cache ) ;
184
- status = body || body === false ? 'cached' : null ;
184
+ status = body || body === false ? this . cache . status : null ;
185
185
}
186
186
}
187
187
188
- if ( ! opts . isPublic ) {
189
- const clientToken = await this . getApplicationToken ( ) ;
190
- opts . headers . authorization = `Bearer ${ clientToken } ` ;
191
- }
192
-
193
- begin = new Date ( ) . getTime ( ) ;
194
-
195
188
if ( ! body && body !== false ) {
189
+ if ( ! opts . isPublic ) {
190
+ const clientToken = await this . getApplicationToken ( ) ;
191
+ opts . headers . authorization = `Bearer ${ clientToken } ` ;
192
+ }
193
+
194
+ begin = new Date ( ) . getTime ( ) ;
196
195
( { status, body } = await superagent ( method , url )
197
196
. set ( opts . headers )
198
197
. query ( opts . query )
@@ -211,27 +210,31 @@ class Auth {
211
210
}
212
211
}
213
212
213
+ if ( this . isCacheEnabled && body === false ) {
214
+ throw CoreError . CachedNotFound ( ) ;
215
+ }
216
+
214
217
this . log (
215
218
`${ service } ${ method } , code: ${ status } , t: ${ this . timeDelta (
216
219
begin
217
220
) } ms -> url: ${ url } , query: ${ JSON . stringify ( opts . query || { } ) } `
218
221
) ;
219
222
220
- // cached as false, throw an error
221
- if ( this . isCacheEnabled && body === false ) {
222
- throw new CoreError ( new Error ( 'Not found' ) , { statusCode : 404 } ) ;
223
- }
224
-
225
223
return body ;
226
224
} catch ( error ) {
227
225
if (
228
226
this . isCacheEnabled &&
229
227
opts . cache &&
230
228
opts . cache . action === this . cache . actions . get &&
231
- opts . cache . onError &&
232
- error . status === 404
229
+ opts . cache . onNotFound &&
230
+ error . status === 404 &&
231
+ error . typeof !== CoreError . CachedNotFound
233
232
) {
234
- await this . executeCacheOp ( { value : false , ...opts . cache } ) ;
233
+ await this . executeCacheOp ( {
234
+ value : false ,
235
+ ...opts . cache ,
236
+ ...{ action : this . cache . actions . set }
237
+ } ) ;
235
238
}
236
239
237
240
if (
0 commit comments