Skip to content

Commit

Permalink
refactor api helpers for readability/scalability (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-rdrgz authored Mar 6, 2024
1 parent 7ef090f commit 8b3d187
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 216 deletions.
215 changes: 0 additions & 215 deletions frontend/helpers/api/api.ts

This file was deleted.

61 changes: 61 additions & 0 deletions frontend/helpers/api/auth/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { request, AccessToken } from "../base"
import {
User,
RegisterRequest,
LoginRequest,
ForgotPassword,
ResetPasswordRequest,
ResetPasswordResponse,
WhoamiRequest
} from "./types"

export function login(data: LoginRequest): Promise<AccessToken> {
return request({
url: "/auth/login",
method: "POST",
data
}).then(({ access_token }) => access_token)
}

export function register(data: RegisterRequest): Promise<AccessToken> {
return request({
url: "/auth/register",
method: "POST",
data
}).then(({ access_token }) => access_token)
}

export function forgotPassowrd(data: ForgotPassword): Promise<void> {
return request({
url: "/auth/forgotPassword",
method: "POST",
data
})
}

export function resetPassword(req: ResetPasswordRequest): Promise<ResetPasswordResponse> {
const { accessToken } = req

return request({
url: `/auth/resetPassword`,
method: "POST",
data: { password: req.password },
accessToken
})
}

export function whoami({ accessToken }: WhoamiRequest): Promise<User> {
return request({
url: "/auth/whoami",
method: "GET",
accessToken
}).then(({ active, email, email_confirmed_at, first_name, last_name, phone_number, role }) => ({
active,
email,
emailConfirmedAt: email_confirmed_at,
firstName: first_name,
lastName: last_name,
phoneNumber: phone_number,
role: role
}))
}
2 changes: 2 additions & 0 deletions frontend/helpers/api/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./types"
export * from "./auth"
41 changes: 41 additions & 0 deletions frontend/helpers/api/auth/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { AuthenticatedRequest } from "../base"

export interface User {
active: boolean
role: string
email: string
emailConfirmedAt?: string
firstName?: string
lastName?: string
phoneNumber?: string
}

export interface NewUser {
email: string
password: string
firstName?: string
lastName?: string
phoneNumber?: string
}

export interface LoginCredentials {
email: string
password: string
}

export interface ForgotPassword {
email: string
}

export interface ResetPasswordRequest extends AuthenticatedRequest {
accessToken: string
password: string
}

export interface ResetPasswordResponse {
message: string
}

export type RegisterRequest = NewUser
export type LoginRequest = LoginCredentials
export type WhoamiRequest = AuthenticatedRequest
31 changes: 31 additions & 0 deletions frontend/helpers/api/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import axiosModule, { AxiosRequestConfig } from "axios"
import { baseURL } from "./config"

export type AccessToken = string

export interface AuthenticatedRequest {
accessToken: AccessToken
}

const axios = axiosModule.create({
baseURL,
timeout: 5000
})

export function request({
accessToken,
...config
}: AxiosRequestConfig & { accessToken?: AccessToken }) {
let { headers, ...rest } = config
if (accessToken) {
headers = {
Authorization: `Bearer ${accessToken}`,
...headers
}
}

return axios({
headers,
...rest
}).then((response) => response.data)
}
1 change: 1 addition & 0 deletions frontend/helpers/api/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const baseURL = process.env.NEXT_PUBLIC_API_BASE_URL || "http://localhost:5000/api/v1"
27 changes: 27 additions & 0 deletions frontend/helpers/api/incidents/incidents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { AccessToken, request } from "../base"
import { Incident, IncidentSearchRequest, IncidentSearchResponse } from "./types"

export function searchIncidents({
accessToken,
dateStart,
dateEnd,
...rest
}: IncidentSearchRequest): Promise<IncidentSearchResponse> {
if (dateStart) dateStart = new Date(dateStart).toISOString().slice(0, -1)
if (dateEnd) dateEnd = new Date(dateEnd).toISOString().slice(0, -1)

return request({
url: "/incidents/search",
method: "POST",
accessToken,
data: { dateStart, dateEnd, ...rest }
})
}

export async function getIncidentById(id: number, accessToken: AccessToken): Promise<Incident> {
return request({
url: `/incidents/get/${id}`,
method: "GET",
accessToken
})
}
2 changes: 2 additions & 0 deletions frontend/helpers/api/incidents/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./types"
export * from "./incidents"
Loading

0 comments on commit 8b3d187

Please sign in to comment.