Skip to content

Commit

Permalink
Better types for api requests
Browse files Browse the repository at this point in the history
  • Loading branch information
usserwoutV2 committed Mar 3, 2024
1 parent 3a9591b commit 6494cc5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,7 +28,7 @@ public FilterRegistrationBean<JwtAuthenticationFilter> filterRegistrationBean()

@Bean
public SecurityFilterChain web(HttpSecurity http) throws Exception {
http
http.csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests((authorize) -> authorize
.anyRequest().permitAll()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions frontend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PUBLIC_URL=/
26 changes: 26 additions & 0 deletions frontend/src/@types/requests.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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
}
}
6 changes: 3 additions & 3 deletions frontend/src/util/apiFetch.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -63,8 +63,8 @@ async function apiFetch<T extends ApiRoutes>(method: "GET" | "POST" | "PUT" | "D
}

const apiCall = {
get: async <T extends ApiRoutes>(route: T) => apiFetch("GET", route),
post: async <T extends keyof POST_Requests>(route: T, body: POST_Requests[T]) => apiFetch("POST", route, body),
get: async <T extends keyof GET_Responses>(route: T) => apiFetch("GET", route) as Promise<AxiosResponse<GET_Responses[T]>>,
post: async <T extends keyof POST_Requests>(route: T, body: POST_Requests[T]) => apiFetch("POST", route, body) as Promise<AxiosResponse<POST_Responses[T]>>,
put: async <T extends keyof PUT_Requests>(route: T, body: PUT_Requests[T]) => apiFetch("PUT", route, body),
delete: async <T extends ApiRoutes>(route: T) => apiFetch("DELETE", route)
}
Expand Down

0 comments on commit 6494cc5

Please sign in to comment.