1
- const USER_SERVICE_URL = process . env . NEXT_PUBLIC_USER_SERVICE_URL
1
+ const USER_SERVICE_URL = process . env . NEXT_PUBLIC_USER_SERVICE_URL ;
2
2
3
3
export interface User {
4
4
id : string ;
@@ -25,51 +25,65 @@ export interface VerifyTokenResponseType {
25
25
}
26
26
27
27
type UserResponseData = {
28
- "id" : string ,
29
- " username" : string ,
30
- " email" : string ,
31
- " isAdmin" : boolean ,
32
- " createdAt" : string ,
33
- }
28
+ id : string ;
29
+ username : string ;
30
+ email : string ;
31
+ isAdmin : boolean ;
32
+ createdAt : string ;
33
+ } ;
34
34
35
35
function withDefaultHeaders ( opts : RequestInit ) : RequestInit {
36
36
return {
37
37
...opts ,
38
38
headers : {
39
39
...( opts . headers ?? { } ) ,
40
- "Content-Type" : "application/json"
40
+ "Content-Type" : "application/json" ,
41
41
} ,
42
- }
42
+ } ;
43
43
}
44
44
45
- export async function createUser ( username : string , email : string , password : string ) : Promise < void > {
45
+ export async function createUser (
46
+ username : string ,
47
+ email : string ,
48
+ password : string
49
+ ) : Promise < void > {
46
50
const opts = withDefaultHeaders ( {
47
51
method : "POST" ,
48
52
body : JSON . stringify ( {
49
- username, email, password
53
+ username,
54
+ email,
55
+ password,
50
56
} ) ,
51
- } )
52
- const res = await fetch ( `${ USER_SERVICE_URL } users` , opts )
57
+ } ) ;
58
+ const res = await fetch ( `${ USER_SERVICE_URL } users` , opts ) ;
53
59
54
60
if ( ! res . ok ) {
55
- throw new Error ( `User service responded with ${ res . status } : ${ await res . text ( ) } ` )
61
+ throw new Error (
62
+ `User service responded with ${ res . status } : ${ await res . text ( ) } `
63
+ ) ;
56
64
}
57
65
}
58
66
59
- export async function loginUser ( email : string , password : string ) : Promise < string > {
67
+ export async function loginUser (
68
+ email : string ,
69
+ password : string
70
+ ) : Promise < string > {
60
71
const opts = withDefaultHeaders ( {
61
72
method : "POST" ,
62
73
body : JSON . stringify ( {
63
- email, password
64
- } )
74
+ email,
75
+ password,
76
+ } ) ,
65
77
} ) ;
66
78
const res = await fetch ( `${ USER_SERVICE_URL } auth/login` , opts ) ;
67
79
68
80
if ( ! res . ok ) {
69
- throw new Error ( `User service responded with ${ res . status } : ${ await res . text ( ) } ` ) ;
81
+ throw new Error (
82
+ `User service responded with ${ res . status } : ${ await res . text ( ) } `
83
+ ) ;
70
84
}
71
85
72
- const ret : { " data" : { " accessToken" : string } } = await res . json ( ) ;
86
+ const ret : { data : { accessToken : string } } = await res . json ( ) ;
73
87
return ret . data . accessToken ;
74
88
}
75
89
@@ -89,9 +103,11 @@ export const UpdateUser = async (
89
103
body : JSON . stringify ( user ) ,
90
104
}
91
105
) ;
92
-
93
106
if ( response . status === 200 ) {
94
107
return response . json ( ) ;
108
+ } else if ( response . status === 409 ) {
109
+ const errorResponse = await response . json ( ) ;
110
+ throw new Error ( errorResponse . message ) ;
95
111
} else {
96
112
throw new Error (
97
113
`Error updating user: ${ response . status } ${ response . statusText } `
0 commit comments