Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,10 @@
},
"dependencies": {
"@babel/runtime": "^7.18.3",
"@types/react": "^19.0.8",
"set-cookie-parser": "^2.5.1"
"@types/react": "^19.0.8"
},
"devDependencies": {
"@keystatic/core": "workspace:^",
"@types/set-cookie-parser": "^2.4.2",
"astro": "^5.2.5",
"react": "^19.0.0",
"react-dom": "^19.0.0"
Expand Down
58 changes: 1 addition & 57 deletions packages/astro/src/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
makeGenericAPIRouteHandler,
} from '@keystatic/core/api/generic';
import type { APIContext } from 'astro';
import { parseString } from 'set-cookie-parser';

export function makeHandler(_config: APIRouteConfig) {
return async function keystaticAPIRoute(context: APIContext) {
Expand Down Expand Up @@ -35,62 +34,7 @@ export function makeHandler(_config: APIRouteConfig) {
}
);
const { body, headers, status } = await handler(context.request);
// all this stuff should be able to go away when astro is using a version of undici with getSetCookie
let headersInADifferentStructure = new Map<string, string[]>();
if (headers) {
if (Array.isArray(headers)) {
for (const [key, value] of headers) {
if (!headersInADifferentStructure.has(key.toLowerCase())) {
headersInADifferentStructure.set(key.toLowerCase(), []);
}
headersInADifferentStructure.get(key.toLowerCase())!.push(value);
}
} else if (typeof headers.entries === 'function') {
for (const [key, value] of headers.entries()) {
headersInADifferentStructure.set(key.toLowerCase(), [value]);
}
if (
'getSetCookie' in headers &&
typeof headers.getSetCookie === 'function'
) {
const setCookieHeaders = (headers as any).getSetCookie();
if (setCookieHeaders?.length) {
headersInADifferentStructure.set('set-cookie', setCookieHeaders);
}
}
} else {
for (const [key, value] of Object.entries(headers)) {
headersInADifferentStructure.set(key.toLowerCase(), [value]);
}
}
}

const setCookieHeaders = headersInADifferentStructure.get('set-cookie');
headersInADifferentStructure.delete('set-cookie');
if (setCookieHeaders) {
for (const setCookieValue of setCookieHeaders) {
const { name, value, ...options } = parseString(setCookieValue);
const sameSite = options.sameSite?.toLowerCase();
context.cookies.set(name, value, {
domain: options.domain,
expires: options.expires,
httpOnly: options.httpOnly,
maxAge: options.maxAge,
path: options.path,
sameSite:
sameSite === 'lax' || sameSite === 'strict' || sameSite === 'none'
? sameSite
: undefined,
});
}
}

return new Response(body, {
status,
headers: [...headersInADifferentStructure.entries()].flatMap(
([key, val]) => val.map((x): [string, string] => [key, x])
),
});
return new Response(body, { status, headers });
};
}

Expand Down
13 changes: 0 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.