Skip to content

Commit

Permalink
Add support for re-using duplicated code in nextjs from customframework
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30-st committed Oct 3, 2024
1 parent 69180f2 commit 88f9500
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 202 deletions.
11 changes: 10 additions & 1 deletion lib/build/customFramework.d.ts

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

95 changes: 57 additions & 38 deletions lib/build/customFramework.js

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

2 changes: 2 additions & 0 deletions lib/build/nextjs.d.ts

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

66 changes: 8 additions & 58 deletions lib/build/nextjs.js

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

104 changes: 65 additions & 39 deletions lib/ts/customFramework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,50 +255,76 @@ export async function withSession(
try {
userResponse = await handler(undefined, session);
} catch (err) {
await errorHandler()(err, baseRequest, baseResponse, (errorHandlerError: Error) => {
if (errorHandlerError) {
throw errorHandlerError;
}
});

// The headers in the userResponse are set twice from baseResponse, but the resulting response contains unique headers.
userResponse = new Response(baseResponse.body, {
status: baseResponse.statusCode,
headers: baseResponse.headers,
});
userResponse = await handleError<Response>(err, baseRequest, baseResponse);
}

let didAddCookies = false;
let didAddHeaders = false;
return addCookies(baseResponse, userResponse);
} catch (error) {
return await handler(error as Error, undefined);
}
}

for (const respCookie of baseResponse.cookies) {
didAddCookies = true;
userResponse.headers.append(
"Set-Cookie",
serialize(respCookie.key, respCookie.value, {
domain: respCookie.domain,
expires: new Date(respCookie.expires),
httpOnly: respCookie.httpOnly,
path: respCookie.path,
sameSite: respCookie.sameSite,
secure: respCookie.secure,
})
);
export function addCookies<UserResponseType extends Response = Response>(
baseResponse: CollectingResponse,
userResponse: UserResponseType
): UserResponseType {
/**
* Add cookies to the userResponse passed by copying it from the baseResponse.
*/
let didAddCookies = false;
let didAddHeaders = false;

for (const respCookie of baseResponse.cookies) {
didAddCookies = true;
userResponse.headers.append(
"Set-Cookie",
serialize(respCookie.key, respCookie.value, {
domain: respCookie.domain,
expires: new Date(respCookie.expires),
httpOnly: respCookie.httpOnly,
path: respCookie.path,
sameSite: respCookie.sameSite,
secure: respCookie.secure,
})
);
}

baseResponse.headers.forEach((value: string, key: string) => {
didAddHeaders = true;
userResponse.headers.set(key, value);
});

/**
* For some deployment services (Vercel for example) production builds can return cached results for
* APIs with older header values. In this case if the session tokens have changed (because of refreshing
* for example) the cached result would still contain the older tokens and sessions would stop working.
*
* As a result, if we add cookies or headers from base response we also set the Cache-Control header
* to make sure that the final result is not a cached version.
*/
if (didAddCookies || didAddHeaders) {
if (!userResponse.headers.has("Cache-Control")) {
// This is needed for production deployments with Vercel
userResponse.headers.set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
}
}
return userResponse;
}

baseResponse.headers.forEach((value: string, key: string) => {
didAddHeaders = true;
userResponse.headers.set(key, value);
});
if (didAddCookies || didAddHeaders) {
if (!userResponse.headers.has("Cache-Control")) {
// This is needed for production deployments with Vercel
userResponse.headers.set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate");
}
export async function handleError<UserResponseType extends Response = Response>(
err: any,
baseRequest: PreParsedRequest,
baseResponse: CollectingResponse
): Promise<UserResponseType> {
await errorHandler()(err, baseRequest, baseResponse, (errorHandlerError: Error) => {
if (errorHandlerError) {
throw errorHandlerError;
}
});

return userResponse;
} catch (error) {
return await handler(error as Error, undefined);
}
// The headers in the userResponse are set twice from baseResponse, but the resulting response contains unique headers.
return new Response(baseResponse.body, {
status: baseResponse.statusCode,
headers: baseResponse.headers,
}) as UserResponseType;
}
Loading

0 comments on commit 88f9500

Please sign in to comment.