aboutsummaryrefslogtreecommitdiff
path: root/examples/ssr/src/pages/api/cart.ts
diff options
context:
space:
mode:
authorGravatar Daniel <daniel.freese@gmail.com> 2022-10-18 15:52:49 +0200
committerGravatar GitHub <noreply@github.com> 2022-10-18 09:52:49 -0400
commit5923dd77c17c80747f9fe746ff8270ad4c820003 (patch)
tree23611ba030bb4d940660821efdd8a2448fc88733 /examples/ssr/src/pages/api/cart.ts
parent04ce7f4e5c49c16302baacbfbdec38da1bb8d4c3 (diff)
downloadastro-5923dd77c17c80747f9fe746ff8270ad4c820003.tar.gz
astro-5923dd77c17c80747f9fe746ff8270ad4c820003.tar.zst
astro-5923dd77c17c80747f9fe746ff8270ad4c820003.zip
adding cookies to api route response [simple result] (#5060)
* adding cookies to the an api route response, also when returning a simple result * in dev server, convert a simple endpoint result into a response object Co-authored-by: AirBorne04 <unknown> Co-authored-by: AirBorne04 <> Co-authored-by: Matthew Phillips <matthew@skypack.dev>
Diffstat (limited to 'examples/ssr/src/pages/api/cart.ts')
-rw-r--r--examples/ssr/src/pages/api/cart.ts12
1 files changed, 5 insertions, 7 deletions
diff --git a/examples/ssr/src/pages/api/cart.ts b/examples/ssr/src/pages/api/cart.ts
index d8bfa31a0..80db01f16 100644
--- a/examples/ssr/src/pages/api/cart.ts
+++ b/examples/ssr/src/pages/api/cart.ts
@@ -1,10 +1,9 @@
import { APIContext } from 'astro';
-import lightcookie from 'lightcookie';
import { userCartItems } from '../../models/session';
-export function get({ request }: APIContext) {
- let cookie = request.headers.get('cookie');
- let userId = cookie ? lightcookie.parse(cookie)['user-id'] : '1'; // default for testing
+export function get({ cookies }: APIContext) {
+ let userId = cookies.get('user-id').value;
+
if (!userId || !userCartItems.has(userId)) {
return {
body: JSON.stringify({ items: [] }),
@@ -23,11 +22,10 @@ interface AddToCartItem {
name: string;
}
-export async function post({ request }: APIContext) {
+export async function post({ cookies, request }: APIContext) {
const item: AddToCartItem = await request.json();
- let cookie = request.headers.get('cookie');
- let userId = lightcookie.parse(cookie)['user-id'];
+ let userId = cookies.get('user-id').value;
if (!userCartItems.has(userId)) {
userCartItems.set(userId, new Map());