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

feat: consider cookiejar support #589

Open
Thinkscape opened this issue May 29, 2024 · 4 comments
Open

feat: consider cookiejar support #589

Thinkscape opened this issue May 29, 2024 · 4 comments

Comments

@Thinkscape
Copy link

Quite impressive feature set out of the box.

I'm just missing one, esp. useful for crawling and automation:

Cookie Jar support.
I know I could bake that with hooks, but it seems like something that should live in core, given that you've already got lifecycle features like retries and timeouts.

@sholladay
Copy link
Collaborator

Propose an API.

I would argue that retries and timeouts are universally necessary because networks are unreliable. But cookies are much closer to your application logic.

@Thinkscape
Copy link
Author

Thinkscape commented Jul 3, 2024

Well, yes and no.

The credentials: include feature in a server-side fetch is an interesting concept, which is why 'tough-cookie' exists.

API could be as simple as:

import ky from 'ky';
import { CookieJar } from 'tough-cookie';

const cookieJar = new CookieJar();
await ky.post('https://example.com/login', {
  body: "login=foo&password=bar",
  cookieJar
});
console.log(
  'session id =', 
  await cookiejar.getCookies("https://example.com").find(cookie => cookie.key === 'session_id')
);

const response = await ky('https://example.com/dashboard', {
  cookieJar
});

tough-cookie handles most of the hard bits with domains and matching.

@Thinkscape
Copy link
Author

It's easy to add with hooks 🤷

Unsure if it needs another separate package, or could live somewhere here

I.e. could be split, and used as:

import { withCookies } from "ky";

const client = ky.create(withCookies()); // registers necessary hooks, consumes tough-cookie
const apiClient = client.extend({ prefixUrl: "http://api.example.com" });

@sholladay
Copy link
Collaborator

Seems that got already has this feature.

https://github.com/sindresorhus/got/blob/main/test/cookies.ts

From what I can tell, this is mainly useful for server-side users. But it's a valid use case.

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

No branches or pull requests

2 participants