Skip to content

Commit

Permalink
Merge branch 'main' into aria-audit
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Jan 3, 2024
2 parents 89f3f65 + f85cb1f commit c843b04
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
8 changes: 6 additions & 2 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,17 @@ export type {
} from '../assets/types.js';
export type { RemotePattern } from '../assets/utils/remotePattern.js';
export type { SSRManifest } from '../core/app/types.js';
export type { AstroCookies, AstroCookieSetOptions, AstroCookieGetOptions } from '../core/cookies/index.js';
export type {
AstroCookies,
AstroCookieSetOptions,
AstroCookieGetOptions,
} from '../core/cookies/index.js';

export interface AstroBuiltinProps {
'client:load'?: boolean;
'client:idle'?: boolean;
'client:media'?: string;
'client:visible'?: string|boolean;
'client:visible'?: string | boolean;
'client:only'?: boolean | string;
}

Expand Down
7 changes: 5 additions & 2 deletions packages/astro/src/core/cookies/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface AstroCookieSetOptions {

export interface AstroCookieGetOptions {
decode?: (value: string) => string;
};
}

type AstroCookieDeleteOptions = Pick<AstroCookieSetOptions, 'domain' | 'path'>;

Expand Down Expand Up @@ -102,7 +102,10 @@ class AstroCookies implements AstroCookiesInterface {
* @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 {
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)!;
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/cookies/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ export {
getSetCookiesFromResponse,
responseHasCookies,
} from './response.js';
export type { AstroCookieSetOptions, AstroCookieGetOptions } from "./cookies.js";
export type { AstroCookieSetOptions, AstroCookieGetOptions } from './cookies.js';
12 changes: 6 additions & 6 deletions packages/astro/test/units/cookies/get.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ describe('astro/src/core/cookies', () => {
});

it('gets the cookie value with default decode', () => {
const url = 'http://localhost';
const url = 'http://localhost';
const req = new Request('http://example.com/', {
headers: {
cookie: `url=${encodeURIComponent(url)}`,
},
});
const cookies = new AstroCookies(req);
// by default decodeURIComponent is used on the value
// by default decodeURIComponent is used on the value
expect(cookies.get('url').value).to.equal(url);
});

it('gets the cookie value with custom decode', () => {
const url = 'http://localhost';
const url = 'http://localhost';
const req = new Request('http://example.com/', {
headers: {
cookie: `url=${encodeURIComponent(url)}`,
},
});
const cookies = new AstroCookies(req);
// set decode to the identity function to prevent decodeURIComponent on the value
expect(cookies.get('url', { decode: o => o }).value).to.equal(encodeURIComponent(url));
// set decode to the identity function to prevent decodeURIComponent on the value
expect(cookies.get('url', { decode: (o) => o }).value).to.equal(encodeURIComponent(url));
});

it("Returns undefined is the value doesn't exist", () => {
Expand Down
12 changes: 6 additions & 6 deletions packages/astro/test/units/cookies/set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ describe('astro/src/core/cookies', () => {
it('Sets a cookie value that can be serialized w/ defaultencodeURIComponent behavior', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);
const url = 'http://localhost/path';
const url = 'http://localhost/path';
cookies.set('url', url);
let headers = Array.from(cookies.headers());
expect(headers).to.have.a.lengthOf(1);
// by default cookie value is URI encoded
// by default cookie value is URI encoded
expect(headers[0]).to.equal(`url=${encodeURIComponent(url)}`);
});

it('Sets a cookie value that can be serialized w/ custom encode behavior', () => {
let req = new Request('http://example.com/');
let cookies = new AstroCookies(req);
const url = 'http://localhost/path';
// set encode option to the identity function
cookies.set('url', url, { encode: o => o });
const url = 'http://localhost/path';
// set encode option to the identity function
cookies.set('url', url, { encode: (o) => o });
let headers = Array.from(cookies.headers());
expect(headers).to.have.a.lengthOf(1);
// no longer URI encoded
// no longer URI encoded
expect(headers[0]).to.equal(`url=${url}`);
});

Expand Down

0 comments on commit c843b04

Please sign in to comment.