@@ -91,9 +91,7 @@ export const getUserInfo = async (request: Request) => {
91
91
const { data } = await axios . get < any > (
92
92
`${ API_URL } /userinfo`
93
93
, {
94
- headers : {
95
- cookie : request . headers . get ( "Cookie" ) ?. toString ( )
96
- } as any
94
+ headers : getHeaders ( request )
97
95
} ) ;
98
96
if ( Object . keys ( data ) . length === 0 ) {
99
97
return null ;
@@ -105,9 +103,7 @@ export async function searchProduct(request: Request, query: string, price: numb
105
103
const response = await axios . get < any > (
106
104
`${ PRODUCT_SEARCH_URL } /${ price } /${ page } /${ pageSize } `
107
105
, {
108
- headers : {
109
- cookie : request . headers . get ( "Cookie" ) ?. toString ( )
110
- } as any
106
+ headers : getHeaders ( request )
111
107
} ) ;
112
108
113
109
const { data } = response ;
@@ -127,9 +123,7 @@ export async function getProductById(request: Request, id: string) {
127
123
const { data } = await axios . get < any > (
128
124
`${ PRODUCT_URL } /${ id } `
129
125
, {
130
- headers : {
131
- cookie : request . headers . get ( "Cookie" ) ?. toString ( )
132
- } as any
126
+ headers : getHeaders ( request )
133
127
} ) ;
134
128
return data as ProductDetailModel ;
135
129
}
@@ -145,26 +139,21 @@ export async function createUserSession(userId: string, redirectTo: string) {
145
139
}
146
140
147
141
export async function getCartForCurrentUser ( request : Request ) {
148
- const cookie = request . headers . get ( "Cookie" ) ?. toString ( ) ! ;
149
142
const response = await axios . get < any > (
150
143
CART_URL
151
144
, {
152
- headers : {
153
- cookie : cookie
154
- } as any
145
+ headers : getHeaders ( request )
155
146
} ) ;
156
147
157
148
const { data } = response ;
158
- const xsrfToken = convertCookie ( cookie ) [ 'XSRF-TOKEN' ] ;
159
149
160
150
console . log ( data as CartModel ) ;
161
- return { cartData : data as CartModel , csrf : xsrfToken } ;
151
+ return { cartData : data as CartModel } ;
162
152
}
163
153
164
154
export async function updateCartForCurrentUser ( request : Request , productId : string ) {
165
155
const userData = await getUserInfo ( request ) ;
166
- const { cartData, csrf } = await getCartForCurrentUser ( request ) ;
167
- const cookie = request . headers . get ( "Cookie" ) ?. toString ( ) ;
156
+ const { cartData } = await getCartForCurrentUser ( request ) ;
168
157
169
158
if ( cartData . id === null ) {
170
159
// create new cart
@@ -176,10 +165,7 @@ export async function updateCartForCurrentUser(request: Request, productId: stri
176
165
quantity : 1 ,
177
166
} ,
178
167
{
179
- headers : {
180
- cookie : cookie ,
181
- "X-XSRF-TOKEN" : csrf ,
182
- } as any
168
+ headers : getHeaders ( request , true )
183
169
} ) ;
184
170
return data as CartModel ;
185
171
} else {
@@ -191,15 +177,27 @@ export async function updateCartForCurrentUser(request: Request, productId: stri
191
177
quantity : 1 ,
192
178
} ,
193
179
{
194
- headers : {
195
- cookie : cookie ,
196
- "X-XSRF-TOKEN" : csrf ,
197
- } as any
180
+ headers : getHeaders ( request , true )
198
181
} ) ;
199
182
return data as CartModel ;
200
183
}
201
184
}
202
185
186
+ function getHeaders ( request : Request , isCsrf = false ) {
187
+ const cookie = request . headers . get ( "Cookie" ) ?. toString ( ) ! ;
188
+ if ( isCsrf ) {
189
+ const csrf = convertCookie ( cookie ) [ 'XSRF-TOKEN' ] ;
190
+ return {
191
+ cookie : cookie ,
192
+ "X-XSRF-TOKEN" : csrf ,
193
+ } as any ;
194
+ } else {
195
+ return {
196
+ cookie : cookie
197
+ } as any ;
198
+ }
199
+ }
200
+
203
201
function convertCookie ( cookie : string ) {
204
202
const str : string [ ] | any = cookie ?. toString ( ) . split ( '; ' ) ;
205
203
const result : any = { } ;
0 commit comments