survey-webapp/SurveyFrontend/src/api/QuestionApi.ts
2025-05-10 15:56:50 +05:00

29 lines
801 B
TypeScript

import {BASE_URL, createRequestConfig, handleResponse} from "./BaseApi.ts";
export interface INewQuestion{
title: string;
questionType: string;
answerVariants: string[];
}
//
// export interface IErrorQuestionResponse {
//
// }
export const addNewQuestion = async (surveyId: number, question: INewQuestion) => {
const token = localStorage.getItem("token");
if (!token) {
throw new Error("Токен отсутствует");
}
try{
const response = await fetch(`${BASE_URL}/surveys/${surveyId}/questions`, {
...createRequestConfig('POST'),
body: JSON.stringify(question),
})
return await handleResponse(response)
} catch (error){
throw new Error(`Error when adding a new question: ${error}`);
}
}