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

refactor: cookie support via interceptors #216

Merged
merged 2 commits into from
Dec 12, 2022
Merged

Conversation

ricellis
Copy link
Member

@ricellis ricellis commented Dec 9, 2022

Attempts to upgrade to axios 1.x (#209/#213) have been impeded by incompatibilities with axios-cookiejar-support:

This PR:

  • Removes axios-cookiejar-support dependency.
  • Adds cookie-support.ts for providing axios interceptors to interact with tough-cookie.
  • Updates the cookiejar tests for the above.
Checklist
  • npm test passes (tip: npm run lint-fix can correct most style issues)
  • tests are included
  • documentation is changed or added - N/A current docs about cookies remain valid

Removes axios-cookiejar-support dependency.
Adds cookie-support.ts for providing interceptors to
interact with tough-cookie.
Updates cookiejar tests.

Signed-off-by: Rich Ellis <ricellis@users.noreply.github.com>
Copy link
Member

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

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

Looks great overall! Thanks for the contribution. I just have one comment about some async code. Might not need to change but wanted to check your intentions

if (cookies) {
logger.debug(`CookieInterceptor: setting cookies in jar for URL ${response.config.url}.`);
cookies.forEach(async (cookie: string) => {
await this.cookieJar.setCookie(cookie, response.config.url);
Copy link
Member

Choose a reason for hiding this comment

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

There can be some issues with calling async functions within a forEach method. Are you intending to call setCookie sequentially?

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you intending to call setCookie sequentially?

Good spot, thanks. My main intention was to ensure that the cookies were written before the interceptor returned the response, such that any requests happening after that response would be able to read the cookies.

I honestly hadn't given a lot of thought to whether writes were happening sequentially or in parallel and I don't know a lot about the safety of parallel writes to asynchronous cookie stores (of which I guess there are potentially many implementations). With that safety in mind and given that it is likely that the cookies are small and there will be only a single cookie in the majority of cases I suspect that there won't be a huge performance penalty from writing sequentially. So I've made an attempt at changing to that in 3bf097b.

@ricellis ricellis force-pushed the ricellis/cookie-support branch 3 times, most recently from 605ce50 to 994a25c Compare December 12, 2022 12:21
Signed-off-by: Rich Ellis <ricellis@users.noreply.github.com>
Copy link
Member

@dpopp07 dpopp07 left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@dpopp07 dpopp07 merged commit 1684729 into main Dec 12, 2022
@dpopp07 dpopp07 deleted the ricellis/cookie-support branch December 12, 2022 18:07
@ibm-devx-sdk
Copy link

🎉 This PR is included in version 4.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

None yet

3 participants