Repository: withastro/astro
File: packages/astro/src/core/cookies/cookies.ts (TypeScript)

Code before:
	 * Astro.cookies.get(key) is used to get a cookie value. The cookie value is read from the
	 * request. If you have set a cookie via Astro.cookies.set(key, value), the value will be taken
	 * from that set call, overriding any values already part of the request.
	 * @param key The cookie to get.
	 * @returns An object containing the cookie value as well as convenience methods for converting its value.
	 */
	get(
		key: string,
		options: AstroCookieGetOptions | undefined = undefined,
	): AstroCookie | undefined {
		// Check for outgoing Set-Cookie values first
		if (this.#outgoing?.has(key)) {
			let [serializedValue, , isSetValue] = this.#outgoing.get(key)!;
			if (isSetValue) {
				return new AstroCookie(serializedValue);
			} else {
				return undefined;
			}
		}

		const values = this.#ensureParsed(options);
		if (key in values) {
			const value = values[key];
			return new AstroCookie(value);
		}
	}

	/**
	 * Astro.cookies.has(key) returns a boolean indicating whether this cookie is either
	 * part of the initial request or set via Astro.cookies.set(key)
	 * @param key The cookie to check for.
	 * @returns
	 */
	has(key: string, options: AstroCookieGetOptions | undefined = undefined): boolean {
		if (this.#outgoing?.has(key)) {
			let [, , isSetValue] = this.#outgoing.get(key)!;
			return isSetValue;
		}
		const values = this.#ensureParsed(options);
		return !!values[key];
	}

	/**
	 * Astro.cookies.set(key, value) is used to set a cookie's value. If provided
	 * an object it will be stringified via JSON.stringify(value). Additionally you
	 * can provide options customizing how this cookie will be set, such as setting httpOnly
	 * in order to prevent the cookie from being read in client-side JavaScript.
	 * @param key The name of the cookie to set.
	 * @param value A value, either a string or other primitive or an object.
	 * @param options Options for the cookie, such as the path and security settings.
	 */

Review comment (suggestion):
The new type returns `Record<string, string | undefined>`, so to avoid possible breakages in `AstroCookie`, I create the type only when the value isn't `undefined`

Code after:
	 * Astro.cookies.get(key) is used to get a cookie value. The cookie value is read from the
	 * request. If you have set a cookie via Astro.cookies.set(key, value), the value will be taken
	 * from that set call, overriding any values already part of the request.
	 * @param key The cookie to get.
	 * @returns An object containing the cookie value as well as convenience methods for converting its value.
	 */
	get(
		key: string,
		options: AstroCookieGetOptions | undefined = undefined,
	): AstroCookie | undefined {
		// Check for outgoing Set-Cookie values first
		if (this.#outgoing?.has(key)) {
			let [serializedValue, , isSetValue] = this.#outgoing.get(key)!;
			if (isSetValue) {
				return new AstroCookie(serializedValue);
			} else {
				return undefined;
			}
		}

		const values = this.#ensureParsed(options);
		if (key in values) {
			const value = values[key];
			if (value) {
				return new AstroCookie(value);
			}
		}
	}

	/**
	 * Astro.cookies.has(key) returns a boolean indicating whether this cookie is either
	 * part of the initial request or set via Astro.cookies.set(key)
	 * @param key The cookie to check for.
	 * @returns
	 */
	has(key: string, options: AstroCookieGetOptions | undefined = undefined): boolean {
		if (this.#outgoing?.has(key)) {
			let [, , isSetValue] = this.#outgoing.get(key)!;
			return isSetValue;
		}
		const values = this.#ensureParsed(options);
		return !!values[key];
	}

	/**
	 * Astro.cookies.set(key, value) is used to set a cookie's value. If provided
	 * an object it will be stringified via JSON.stringify(value). Additionally you
	 * can provide options customizing how this cookie will be set, such as setting httpOnly
	 * in order to prevent the cookie from being read in client-side JavaScript.
	 * @param key The name of the cookie to set.
	 * @param value A value, either a string or other primitive or an object.