From 6494cc5edc72b62585fb4342132efc54a9bd9610 Mon Sep 17 00:00:00 2001 From: usserwoutV2 Date: Sun, 3 Mar 2024 20:54:10 +0100 Subject: [PATCH] Better types for api requests --- .../com/ugent/pidgeon/PidgeonApplication.java | 5 ---- .../com/ugent/pidgeon/config/AuthConfig.java | 3 ++- .../controllers/AuthTestController.java | 5 ++++ frontend/.env | 1 + frontend/src/@types/requests.d.ts | 26 +++++++++++++++++++ frontend/src/util/apiFetch.ts | 6 ++--- 6 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 frontend/.env diff --git a/backend/app/src/main/java/com/ugent/pidgeon/PidgeonApplication.java b/backend/app/src/main/java/com/ugent/pidgeon/PidgeonApplication.java index 5ca00414..c600e2c2 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/PidgeonApplication.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/PidgeonApplication.java @@ -10,11 +10,6 @@ @SpringBootApplication public class PidgeonApplication { - @RequestMapping("/") - public String home(){ - return "hello world form spring!"; - } - public static void main(String[] args) { SpringApplication.run(PidgeonApplication.class, args); } diff --git a/backend/app/src/main/java/com/ugent/pidgeon/config/AuthConfig.java b/backend/app/src/main/java/com/ugent/pidgeon/config/AuthConfig.java index f0cdbef8..2ba50ca7 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/config/AuthConfig.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/config/AuthConfig.java @@ -6,6 +6,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; import org.springframework.security.web.SecurityFilterChain; @Configuration @@ -27,7 +28,7 @@ public FilterRegistrationBean filterRegistrationBean() @Bean public SecurityFilterChain web(HttpSecurity http) throws Exception { - http + http.csrf(AbstractHttpConfigurer::disable) .authorizeHttpRequests((authorize) -> authorize .anyRequest().permitAll() ); diff --git a/backend/app/src/main/java/com/ugent/pidgeon/controllers/AuthTestController.java b/backend/app/src/main/java/com/ugent/pidgeon/controllers/AuthTestController.java index 84c8082c..de342a07 100644 --- a/backend/app/src/main/java/com/ugent/pidgeon/controllers/AuthTestController.java +++ b/backend/app/src/main/java/com/ugent/pidgeon/controllers/AuthTestController.java @@ -3,6 +3,7 @@ import com.ugent.pidgeon.model.User; import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController @@ -13,6 +14,10 @@ public User testApi(HttpServletRequest request, Auth auth) { return auth.getUser(); } + @PostMapping("/api/test2") + public String postTest(){ + return "Post test succeeded!"; + } @GetMapping("/ping") public String ping() { diff --git a/frontend/.env b/frontend/.env new file mode 100644 index 00000000..6a54b3e9 --- /dev/null +++ b/frontend/.env @@ -0,0 +1 @@ +PUBLIC_URL=/ \ No newline at end of file diff --git a/frontend/src/@types/requests.d.ts b/frontend/src/@types/requests.d.ts index ac24a756..6981ada0 100644 --- a/frontend/src/@types/requests.d.ts +++ b/frontend/src/@types/requests.d.ts @@ -20,6 +20,16 @@ export type POST_Requests = { } } + +/** + * The response you get from the POST request + */ +export type POST_Responses = { + [ApiRoutes.COURSES]: { + id: string + } +} + /** * the body of the PUT requests */ @@ -29,3 +39,19 @@ export type PUT_Requests = { } } +/** + * The response you get from the GET request + */ +export type GET_Responses = { + [ApiRoutes.COURSES]: { + id: string + name: string + }, + [ApiRoutes.TEST]: { + name: string + firstName: string + lastName: string + email: string + oid: string + } +} \ No newline at end of file diff --git a/frontend/src/util/apiFetch.ts b/frontend/src/util/apiFetch.ts index 4c7c98c5..d1808623 100644 --- a/frontend/src/util/apiFetch.ts +++ b/frontend/src/util/apiFetch.ts @@ -1,4 +1,4 @@ -import { ApiRoutes, POST_Requests, PUT_Requests } from "../@types/requests"; +import { ApiRoutes, GET_Responses, POST_Requests, POST_Responses, PUT_Requests } from "../@types/requests"; import axios, { AxiosResponse } from "axios"; import {msalInstance} from "../index"; import { AxiosRequestConfig } from "axios"; @@ -63,8 +63,8 @@ async function apiFetch(method: "GET" | "POST" | "PUT" | "D } const apiCall = { - get: async (route: T) => apiFetch("GET", route), - post: async (route: T, body: POST_Requests[T]) => apiFetch("POST", route, body), + get: async (route: T) => apiFetch("GET", route) as Promise>, + post: async (route: T, body: POST_Requests[T]) => apiFetch("POST", route, body) as Promise>, put: async (route: T, body: PUT_Requests[T]) => apiFetch("PUT", route, body), delete: async (route: T) => apiFetch("DELETE", route) }