1
+ import { getToken } from "./login-store" ;
2
+
3
+ const QUESTION_SERVICE_URL = process . env . NEXT_PUBLIC_QUESTION_SERVICE_URL ;
4
+
1
5
export interface Question {
2
6
id : number ;
3
7
docRefId : string ;
@@ -37,7 +41,7 @@ export const GetQuestions = async (
37
41
categories ?: string [ ] ,
38
42
title ?: string
39
43
) : Promise < QuestionListResponse > => {
40
- let query_url = `${ process . env . NEXT_PUBLIC_API_URL } questions` ;
44
+ let query_url = `${ QUESTION_SERVICE_URL } questions` ;
41
45
let query_params = "" ;
42
46
43
47
if ( currentPage ) {
@@ -73,15 +77,26 @@ export const GetQuestions = async (
73
77
}
74
78
75
79
query_url += query_params ;
76
- const response = await fetch ( query_url ) ;
80
+ const response = await fetch ( query_url , {
81
+ method : "GET" ,
82
+ headers : {
83
+ 'Authorization' : `Bearer ${ getToken ( ) } ` ,
84
+ }
85
+ } ) ;
77
86
const data = await response . json ( ) ;
78
87
return data ;
79
88
} ;
80
89
81
90
// Get single question
82
91
export const GetSingleQuestion = async ( docRef : string ) : Promise < Question > => {
83
92
const response = await fetch (
84
- `${ process . env . NEXT_PUBLIC_API_URL } questions/${ docRef } `
93
+ `${ QUESTION_SERVICE_URL } questions/${ docRef } ` ,
94
+ {
95
+ method : "GET" ,
96
+ headers : {
97
+ 'Authorization' : `Bearer ${ getToken ( ) } ` ,
98
+ }
99
+ }
85
100
) ;
86
101
const data = await response . json ( ) ;
87
102
return data ;
@@ -91,10 +106,11 @@ export const GetSingleQuestion = async (docRef: string): Promise<Question> => {
91
106
export const CreateQuestion = async (
92
107
question : NewQuestion
93
108
) : Promise < Question > => {
94
- const response = await fetch ( `${ process . env . NEXT_PUBLIC_API_URL } questions` , {
109
+ const response = await fetch ( `${ QUESTION_SERVICE_URL } questions` , {
95
110
method : "POST" ,
96
111
headers : {
97
112
"Content-Type" : "application/json" ,
113
+ 'Authorization' : `Bearer ${ getToken ( ) } ` ,
98
114
} ,
99
115
body : JSON . stringify ( question ) ,
100
116
} ) ;
@@ -113,11 +129,12 @@ export const EditQuestion = async (
113
129
docRefId : string
114
130
) : Promise < Question > => {
115
131
const response = await fetch (
116
- `${ process . env . NEXT_PUBLIC_API_URL } questions/${ docRefId } ` ,
132
+ `${ QUESTION_SERVICE_URL } questions/${ docRefId } ` ,
117
133
{
118
134
method : "PUT" ,
119
135
headers : {
120
136
"Content-Type" : "application/json" ,
137
+ 'Authorization' : `Bearer ${ getToken ( ) } ` ,
121
138
} ,
122
139
body : JSON . stringify ( question ) ,
123
140
}
@@ -135,9 +152,12 @@ export const EditQuestion = async (
135
152
// Delete single question (TODO: Ryan)
136
153
export async function DeleteQuestion ( docRef : String ) : Promise < void > {
137
154
const res = await fetch (
138
- `${ process . env . NEXT_PUBLIC_API_URL } questions/${ docRef } ` ,
155
+ `${ QUESTION_SERVICE_URL } questions/${ docRef } ` ,
139
156
{
140
157
method : "DELETE" ,
158
+ headers : {
159
+ 'Authorization' : `Bearer ${ getToken ( ) } ` ,
160
+ } ,
141
161
}
142
162
) ;
143
163
// error handling later
0 commit comments