File tree Expand file tree Collapse file tree 2 files changed +38
-10
lines changed Expand file tree Collapse file tree 2 files changed +38
-10
lines changed Original file line number Diff line number Diff line change 1
1
/* tslint:disable:no-console*/
2
2
import { API_BASE_URL } from '../../config/config' ;
3
+ import { getReqHeaders , HeadReqType , headResponseWrapper } from './utils' ;
3
4
4
5
export const saveCode = ( code : string ) => {
5
- return fetch ( `${ API_BASE_URL } code/save` , {
6
- body : JSON . stringify ( {
7
- code,
8
- } ) ,
6
+ return fetch ( `${ API_BASE_URL } code` , {
7
+ body : code ,
9
8
credentials : 'include' ,
10
- headers : {
11
- Accept : 'application/json' ,
12
- 'Content-Type' : 'application/json' ,
13
- } ,
14
- method : 'POST' ,
9
+ headers : getReqHeaders ( ) ,
10
+ method : 'PUT' ,
15
11
} )
16
12
. then ( ( response ) => {
17
- return response . json ( ) ;
13
+ return headResponseWrapper ( response , HeadReqType . OTHERS ) ;
18
14
} )
19
15
. then ( ( data ) => {
20
16
return data ;
Original file line number Diff line number Diff line change @@ -7,6 +7,7 @@ export enum HeadReqType {
7
7
USERNAME = 'USERNAME' ,
8
8
PROFILE = 'PROFILE' ,
9
9
PASSWORD = 'PASSWORD' ,
10
+ OTHERS = 'OTHERS' ,
10
11
}
11
12
12
13
export function jsonResponseWrapper ( response : any ) {
@@ -54,10 +55,41 @@ export function headResponseWrapper(response: any, headReqType: HeadReqType) {
54
55
: 'Please try again with correct password' ;
55
56
type = resType . ERROR ;
56
57
break ;
58
+ case 500 :
59
+ case 403 :
60
+ error = headReqType === HeadReqType . OTHERS ? '' : '' ;
61
+ type = resType . ERROR ;
57
62
}
58
63
resolve ( {
59
64
error,
60
65
type,
61
66
} ) ;
62
67
} ) ;
63
68
}
69
+
70
+ export function getReqHeaders ( ) {
71
+ const getCookie = ( name : string ) => {
72
+ const cookies = Object . assign (
73
+ { } ,
74
+ ...document . cookie . split ( '; ' ) . map ( ( cookie ) => {
75
+ const key = cookie . split ( '=' ) [ 0 ] ;
76
+ const value = cookie . split ( '=' ) [ 1 ] ;
77
+
78
+ return { [ key ] : value } ;
79
+ } ) ,
80
+ ) ;
81
+ return cookies [ name ] ;
82
+ } ;
83
+ const headersConfig = new Headers ( ) ;
84
+ headersConfig . append ( 'Content-Type' , 'application/json' ) ;
85
+ headersConfig . append ( SET_COOKIE . XSRF , getCookie ( GET_COOKIE . XSRF ) ) ;
86
+ return headersConfig ;
87
+ }
88
+
89
+ export enum SET_COOKIE {
90
+ XSRF = 'X-XSRF-TOKEN' ,
91
+ }
92
+
93
+ export enum GET_COOKIE {
94
+ XSRF = 'XSRF-TOKEN' ,
95
+ }
You can’t perform that action at this time.
0 commit comments