Giter Site home page Giter Site logo

feat: consider cookiejar support about ky HOT 4 OPEN

Thinkscape avatar Thinkscape commented on August 23, 2024
feat: consider cookiejar support

from ky.

Comments (4)

sholladay avatar sholladay commented on August 23, 2024

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.

from ky.

Thinkscape avatar Thinkscape commented on August 23, 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.

from ky.

Thinkscape avatar Thinkscape commented on August 23, 2024

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" });

from ky.

sholladay avatar sholladay commented on August 23, 2024

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.

from ky.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.