Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add stale flag in request return object #843

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

MerryCello
Copy link

@MerryCello MerryCello commented May 29, 2024

This PR is an attempt to add the request response "stale" flag that is present in the axios-cache-adapter. This flag says whether or not the response is from staled cache. This would be returned along with the "cached" flag. Please let me know if there's anything that I'm missing.

*
* @see https://axios-cache-interceptor.js.org/config/response-object#stale
*/
stale: boolean;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this type might be undefined in some areas, probably a stale?: boolean is a better type

@@ -348,6 +348,7 @@ export function defaultResponseInterceptor(axios: AxiosCacheInstance): ResponseI

return {
cached: true,
stale: true,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can't you use .cached here? since this boolean will always be true when cached is true too?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point. In other cases stale may be undefined/false when cached is true.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there other cases where cache is true and the state is stale?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was:

why response.cached cannot be used in your case? Since both these values will always have the same value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They will be the same when staleIfError is set, but is other cases response.stale may be falsy when response.cached is true. For example: when a request is made, and the cache is still valid (i.e. now() < ttl), then the cache is returned with {...repsonse, cached: true, stale: false}. Does that make sense?

Copy link
Owner

@arthurfiorette arthurfiorette Jun 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MerryCello, all tests you added, stale was the same booleanish value as cached. I still don't see how this would make any difference.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arthurfiorette, I added more tests in cases where cached: true and stale: false to show where stale won't always have the same value as cached. This might make it a little more clear. Please review and lmk what you think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @arthurfiorette, just following up on this since it's been a while. Let me know if you have further questions.

@arthurfiorette
Copy link
Owner

please also add tests

@MerryCello MerryCello changed the title Feature: added stale flag in request return object Feature: add stale flag in request return object May 30, 2024
@arthurfiorette
Copy link
Owner

Ci is failing

Copy link

codecov bot commented Jun 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.27%. Comparing base (a30f8d2) to head (7e432d0).
Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #843      +/-   ##
==========================================
+ Coverage   99.26%   99.27%   +0.01%     
==========================================
  Files          19       19              
  Lines        2442     2479      +37     
  Branches      212      214       +2     
==========================================
+ Hits         2424     2461      +37     
  Misses         17       17              
  Partials        1        1              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MerryCello
Copy link
Author

@arthurfiorette, I added some tests and fixed the failing ones. I also added some comments as replies above that may have been missed. If you could review those, that would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants