1
1
import { cookies } from "next/headers" ;
2
- import {
3
- Question ,
4
- StatusBody ,
5
- QuestionFullBody ,
6
- LoginResponse ,
7
- SigninResponse ,
8
- } from "./structs" ;
2
+ import { Question , StatusBody , LoginResponse , SigninResponse } from "./structs" ;
9
3
10
- function generateJSONHeaders ( ) {
4
+ export function generateAuthHeaders ( ) {
11
5
return {
6
+ Authorization : `Bearer ${ cookies ( ) . get ( "session" ) } ` ,
7
+ } ;
8
+ }
9
+
10
+ export function generateJSONHeaders ( ) {
11
+ return {
12
+ ...generateAuthHeaders ( ) ,
12
13
"Content-type" : "application/json; charset=UTF-8" ,
13
- "Authorization" : `Bearer ${ cookies ( ) . get ( "session" ) } `
14
- }
14
+ } ;
15
15
}
16
16
17
17
export async function fetchQuestion (
18
18
questionId : string
19
19
) : Promise < Question | StatusBody > {
20
20
try {
21
21
const response = await fetch (
22
- `${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions/solve/${ questionId } `
22
+ `${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions/solve/${ questionId } ` ,
23
+ {
24
+ method : "GET" ,
25
+ headers : generateAuthHeaders ( ) ,
26
+ }
23
27
) ;
24
28
if ( ! response . ok ) {
25
29
return {
@@ -33,91 +37,21 @@ export async function fetchQuestion(
33
37
}
34
38
}
35
39
36
- export async function addQuestion ( body : QuestionFullBody ) : Promise < StatusBody > {
40
+ export async function getSessionLogin ( validatedFields : {
41
+ email : string ;
42
+ password : string ;
43
+ } ) : Promise < LoginResponse | StatusBody > {
37
44
try {
38
- const response = await fetch (
39
- `${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions ` ,
45
+ const res = await fetch (
46
+ `${ process . env . NEXT_PUBLIC_USER_SERVICE } /auth/login ` ,
40
47
{
41
48
method : "POST" ,
42
- body : JSON . stringify ( body ) . replace (
43
- / ( \" d i f f i c u l t y \" : ) \" ( [ 1 - 3 ] ) \" / ,
44
- `$1$2`
45
- ) ,
46
- headers : {
47
- "Content-type" : "application/json; charset=UTF-8" ,
48
- } ,
49
- }
50
- ) ;
51
- if ( response . ok ) {
52
- return {
53
- status : response . status ,
54
- } ;
55
- }
56
- return {
57
- error : ( await response . json ( ) ) [ "Error adding question: " ] ,
58
- status : response . status ,
59
- } ;
60
- } catch ( err : any ) {
61
- return { error : err . message , status : 0 } ;
62
- }
63
- }
64
-
65
- export async function deleteQuestion ( question : Question ) : Promise < StatusBody > {
66
- try {
67
- const response = await fetch (
68
- `${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions/delete/${ question . id } ` ,
69
- {
70
- method : "DELETE" ,
49
+ body : JSON . stringify ( validatedFields ) ,
71
50
headers : {
72
51
"Content-type" : "application/json; charset=UTF-8" ,
73
52
} ,
74
53
}
75
54
) ;
76
- if ( response . ok ) {
77
- return {
78
- status : response . status ,
79
- } ;
80
- }
81
- return {
82
- error : ( await response . json ( ) ) [ "Error deleting question: " ] ,
83
- status : response . status ,
84
- } ;
85
- } catch ( err : any ) {
86
- return { error : err . message , status : 0 } ;
87
- }
88
- }
89
-
90
- export async function getAllQuestions ( ) : Promise < Question [ ] | StatusBody > {
91
- try {
92
- const response = await fetch (
93
- `${ process . env . NEXT_PUBLIC_QUESTION_SERVICE } /questions`
94
- ) ;
95
- if ( ! response . ok ) {
96
- return {
97
- error : await response . text ( ) ,
98
- status : response . status ,
99
- } ;
100
- }
101
- return ( await response . json ( ) ) as Question [ ] ;
102
- } catch ( err : any ) {
103
- return { error : err . message , status : 400 } ;
104
- }
105
- }
106
-
107
- export async function getSessionLogin (
108
- validatedFields : {
109
- email : string ;
110
- password : string ;
111
- }
112
- ) : Promise < LoginResponse | StatusBody > {
113
- try {
114
- const res = await fetch ( `${ process . env . NEXT_PUBLIC_USER_SERVICE } /auth/login` , {
115
- method : "POST" ,
116
- body : JSON . stringify ( validatedFields ) ,
117
- headers : {
118
- "Content-type" : "application/json; charset=UTF-8" ,
119
- }
120
- } ) ;
121
55
const json = await res . json ( ) ;
122
56
123
57
if ( ! res . ok ) {
@@ -131,21 +65,19 @@ export async function getSessionLogin(
131
65
}
132
66
}
133
67
134
- export async function postSignupUser (
135
- validatedFields : {
136
- username : string ;
137
- email : string ;
138
- password : string ;
139
- }
140
- ) : Promise < SigninResponse | StatusBody > {
68
+ export async function postSignupUser ( validatedFields : {
69
+ username : string ;
70
+ email : string ;
71
+ password : string ;
72
+ } ) : Promise < SigninResponse | StatusBody > {
141
73
try {
142
74
console . log ( JSON . stringify ( validatedFields ) ) ;
143
75
const res = await fetch ( `${ process . env . NEXT_PUBLIC_USER_SERVICE } /users` , {
144
76
method : "POST" ,
145
77
body : JSON . stringify ( validatedFields ) ,
146
78
headers : {
147
79
"Content-type" : "application/json; charset=UTF-8" ,
148
- }
80
+ } ,
149
81
} ) ;
150
82
const json = await res . json ( ) ;
151
83
0 commit comments