A Fetch Wrapper That Retain Cookies with Deno

A Fetch Wrapper That Retain Cookies with Deno

deno-another-cookiejar This library offers a fetch wrapper that retain cookies. This library also provides a simple cookiejar;

deno-another-cookiejar

This library offers a fetch wrapper that retain cookies. This library also provides a simple cookiejar;

Why the name ? because I didn't want to reserve the cookiejar name, since this library may not be good at it. (But I hope you like it)

usage

you can import Cookie, CookieJar, wrapFetch from mod.ts file.

export { Cookie, CookieJar, wrapFetch} from 'https://deno.land/x/[email protected]/mod.ts';

wrapFetch

// this simple
const fetch = wrapFetch();

Or

// you can also pass your own cookiejar to wrapFetch to save/load your cookies
const cookieJar = new CookieJar();
// Now use this fetch and any cookie that is set will be sent with your next requests automatically
const fetch = wrapFetch({ cookieJar });
//...
fetch("http://example.com");
// and you can read your cookies from the jar
cookieJar.getCookie({
  name: 'cookieName',
})?.value // your cookie value

You can play around with it too see what it has to offer!

cookies should only be sent to their corresponding domains automatically.

Secure cookies will not be sent over unsecure connections.

Cookie

you can create cookies in two ways:

// first: using Cookie constructor with CookieOptions
const cookie = new Cookie({
  name: 'foo',
  value: 'bar'
});
// second: 
const cookie = Cookie.from('foo=bar;'); // any string from Set-Cookie header value is also valid.

CookieJar

const cookieJar = new CookieJar();

also if you have cookies from before:

const cookieJar = new CookieJar(cookiesArray); // cookiesArray: Array<Cookie> | Array<CookieOptions>
Note on retrieving cookies (+v2.0.0)

You can get cookies using either CookieOptions or a Cookie itself. The difference is if you use CookieOptions, it will strictly check any prop that is passed against the cookie. But if you use a Cookie object, it will only check name, path and domain.

Each cookie object is easily serialized and deserialized. Example:

const exampleOption = { name: 'foo' , value: 'bar' };

const myCookie = new Cookie(exampleOption);

new Cookie ( 
  JSON.parse(
    JSON.stringify(myCookie)
  )
).toString() === myCookie.toString(); // true

JSON serializing CookieJar

You can even easily serialize your CookierJar. Example:

const exampleOption = { name: 'foo' , value: 'bar' };

const myCookie = new Cookie(exampleOption);

const cookieJar = new CookieJar([myCookie]);

new CookieJar (
  JSON.parse(
    JSON.stringify(cookieJar)
  )
).cookies[0].toString() === myCookie.toString(); // true

test

fetch wrapper tests require network access to emulate server.

run with deno test --allow-net

notes

This library is only tested lightly. you can contribute to this if you want to make it better, but I probably won't add much feature/test anymore.

This library does not strictly follow the specs, but does try to follow it loosely. just keep it in mind when using so you don't get surprises.

does not support handling of __Secure- and __Host- cookies.

Download Details:

Author: jd1378

Source Code: https://github.com/jd1378/deno-another-cookiejar

deno nodejs node javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings