Skip to content

Commit

Permalink
Merge pull request #106 from BearToCode/main
Browse files Browse the repository at this point in the history
fix: `setHeaders` and `cookies` in procedures
  • Loading branch information
icflorescu committed Jan 28, 2024
2 parents 494b93c + b0fd738 commit f17a6d4
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion package/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export function createTRPCHandle<Router extends AnyRouter, URL extends string>({
}) => void;
}): Handle {
return async ({ event, resolve }) => {
if (event.url.pathname.startsWith(url+'/')) {
if (event.url.pathname.startsWith(url + '/')) {
const request = event.request as Request & {
headers: Dict<string | string[]>;
};
Expand All @@ -81,6 +81,34 @@ export function createTRPCHandle<Router extends AnyRouter, URL extends string>({
body: await request.text()
};

// Using the default `event.setHeaders` and `event.cookies` will not work
// as the event in not resolved by SvelteKit. Instead, we "proxy" the access
// to the headers and cookies, so that we can set them later.
const headersProxy: Record<string, string> = {};
const originalCookiesSet = event.cookies.set;
const originalCookiesDelete = event.cookies.delete;
const newCookiesNames: string[] = [];
const deleteCookiesNames: string[] = [];
event.setHeaders = (headers) => {
for (const [key, value] of Object.entries(headers)) {
headersProxy[key] = value;
}
};
event.cookies.set = (name, value, opts) => {
newCookiesNames.push(name);
if (deleteCookiesNames.includes(name)) {
deleteCookiesNames.splice(deleteCookiesNames.indexOf(name), 1);
}
originalCookiesSet(name, value, opts);
};
event.cookies.delete = (name) => {
deleteCookiesNames.push(name);
if (newCookiesNames.includes(name)) {
newCookiesNames.splice(newCookiesNames.indexOf(name), 1);
}
originalCookiesDelete(name);
};

const httpResponse = await resolveHTTPResponse({
router,
req,
Expand All @@ -97,6 +125,23 @@ export function createTRPCHandle<Router extends AnyRouter, URL extends string>({
body: string;
};

// Set headers and cookies that were set using SvelteKit's `event.setHeaders` and `event.cookies.set`.
for (const [key, value] of Object.entries(headersProxy)) {
headers[key] = value;
}
const cookies = event.cookies.getAll().filter((cookie) => {
// Only pick new cookies
if (!newCookiesNames.includes(cookie.name)) return false;
// Don't pick cookies that were deleted
if (deleteCookiesNames.includes(cookie.name)) return false;
return true;
});
if (cookies.length > 0) {
headers['Set-Cookie'] = cookies
.map((cookie) => `${cookie.name}=${cookie.value}`)
.join('; ');
}

return new Response(body, { status, headers });
}

Expand Down

0 comments on commit f17a6d4

Please sign in to comment.