-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor api helpers for readability/scalability (#348)
- Loading branch information
1 parent
7ef090f
commit 8b3d187
Showing
10 changed files
with
242 additions
and
216 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./types" | ||
export * from "./auth" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./types" | ||
export * from "./incidents" |
Oops, something went wrong.