Thanks to visit codestin.com
Credit goes to github.com

Skip to content

cookieStore #47314

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

Closed
mmkal opened this issue Jan 5, 2022 · 5 comments
Closed

cookieStore #47314

mmkal opened this issue Jan 5, 2022 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@mmkal
Copy link

mmkal commented Jan 5, 2022

lib Update Request

The cookieStore API

Configuration Check

My compilation target is esnext and my lib is ["dom", "dom.iterable", "esnext"].

Missing / Incorrect Definition

cookieStore: https://developer.mozilla.org/en-US/docs/Web/API/CookieStore

Sample Code

From https://davidwalsh.name/cookiestore

await cookieStore.set({ 
  name: "dw-test", 
  value: 1, 
  domain: 'davidwalsh.name', 
  // Very far in the future!
  expires: Date.now() + Date.now() 
});

const testCookie = await cookieStore.get('dw-test');

Documentation Link

https://developer.mozilla.org/en-US/docs/Web/API/CookieStore


Note: I'm a little confused by DOM lib generator repo. Should I be creating that issue there? It also mentions that it needs to be implemented in two major engines, and maybe Chromium counts as one? I'd have thought it'd make sense to allow including it explicitly via lib somehow though.

FireFox implementation tracking issue
Safari implementation tracking issue

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jan 11, 2022
@RyanCavanaugh
Copy link
Member

We don't include APIs that aren't in all major browsers yet

@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@dwjohnston
Copy link

Has anyone got a type definition that will work in the meantime?

@dwjohnston
Copy link

dwjohnston commented Feb 7, 2025

In an appeal to Cunningham's Law - here is my implementation:

interface Cookie {
	domain: string;
	expires: number;
	name: string;
	partitioned: boolean;
	path: string;
	sameSite: "Strict" | "Lax" | "None";
	secure: boolean;
	value: string;
}

interface CookieChangedEvent extends Event {
	changed: Readonly<Array<Cookie>>;
	deleted: Readonly<Array<Cookie>>;
}

interface CookieStore {
	get(name: string): Promise<Cookie | null>;
	get(options: { name: string; url?: string }): Promise<Cookie | null>;

	getAll(): Promise<Cookie[]>;
	getAll(name: string): Promise<Cookie[]>;
	getAll(options: { name?: string; url?: string }): Promise<Cookie[]>;

	set(name: string, value: string): Promise<void>;
	set(
		options: Partial<Cookie> & { name: string; value: string },
	): Promise<void>;

	delete(name: string): Promise<void>;
	delete(options: { name: string; url?: string }): Promise<void>;

	addEventListener(
		eventName: "change",
		handler: (event: CookieChangedEvent) => void,
	): void;
	removeEventListener(
		eventName: "change",
		handler: (event: CookieChangedEvent) => void,
	): void;
	onchange: null | ((event: CookieChangedEvent) => void);
}

declare var cookieStore: CookieStore;

@jnoordsij
Copy link

In an appeal to Cunningham's Law - here is my implementation:

interface Cookie {
	domain: string;
	expires: number;
	name: string;
	partitioned: boolean;
	path: string;
	sameSite: "Strict" | "Lax" | "None";
	secure: boolean;
	value: string;
}

interface CookieChangedEvent extends Event {
	changed: Readonly<Array<Cookie>>;
	deleted: Readonly<Array<Cookie>>;
}

interface CookieStore {
	get(name: string): Promise<Cookie | null>;
	get(options: { name: string; url?: string }): Promise<Cookie | null>;

	getAll(): Promise<Cookie[]>;
	getAll(name: string): Promise<Cookie[]>;
	getAll(options: { name?: string; url?: string }): Promise<Cookie[]>;

	set(name: string, value: string): Promise<void>;
	set(
		options: Partial<Cookie> & { name: string; value: string },
	): Promise<void>;

	delete(name: string): Promise<void>;
	delete(options: { name: string; url?: string }): Promise<void>;

	addEventListener(
		eventName: "change",
		handler: (event: CookieChangedEvent) => void,
	): void;
	removeEventListener(
		eventName: "change",
		handler: (event: CookieChangedEvent) => void,
	): void;
	onchange: null | ((event: CookieChangedEvent) => void);
}

declare var cookieStore: CookieStore;

Thanks for sharing! This seems to be mostly fine to me, I've found two things that probably should be altered:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants