arrow functions
This commit is contained in:
+4
-4
@@ -1,12 +1,12 @@
|
||||
import { deleteTokenCookie, getTokenCookie } from "@utils/auth";
|
||||
import { APIPath, postBackendAPI } from "./backend";
|
||||
|
||||
export async function generateToken(username: string, password: string): Promise<string> {
|
||||
export const generateToken = async (username: string, password: string): Promise<string> => {
|
||||
const { token } = await postBackendAPI<{ username: string, password: string }, { token: string }>({ path: APIPath.AUTH_TOKEN }, { username, password });
|
||||
return token;
|
||||
}
|
||||
};
|
||||
|
||||
export async function isAuthenticated(): Promise<boolean> {
|
||||
export const authenticate = async (): Promise<boolean> => {
|
||||
try {
|
||||
const token = getTokenCookie();
|
||||
await postBackendAPI({ path: APIPath.AUTH_TOKEN_VERIFY }, { token });
|
||||
@@ -16,4 +16,4 @@ export async function isAuthenticated(): Promise<boolean> {
|
||||
deleteTokenCookie();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
+15
-15
@@ -71,14 +71,14 @@ const fillUrlParams = (apiPath: APIPath, params: API["urlParams"] = {}): string
|
||||
return `/${path}`;
|
||||
};
|
||||
|
||||
async function callBackendAPI<RequestType, ResponseType>(
|
||||
const callBackendAPI = async <RequestType, ResponseType>(
|
||||
path: APIPath,
|
||||
urlParams: API["urlParams"] = {},
|
||||
queryParams: API["queryParams"] = {},
|
||||
method: AxiosRequestConfig["method"],
|
||||
headers: Headers,
|
||||
requestBody: RequestType,
|
||||
): Promise<ResponseType> {
|
||||
): Promise<ResponseType> => {
|
||||
const url = fillUrlParams(path, urlParams);
|
||||
const request: AxiosRequestConfig = {
|
||||
url,
|
||||
@@ -95,32 +95,32 @@ async function callBackendAPI<RequestType, ResponseType>(
|
||||
return arrayResp.results;
|
||||
}
|
||||
return response.data;
|
||||
}
|
||||
};
|
||||
|
||||
export async function getBackendAPI<ResponseType>({
|
||||
export const getBackendAPI = async <ResponseType>({
|
||||
path, urlParams, queryParams, authenticated,
|
||||
}: API): Promise<ResponseType> {
|
||||
}: API): Promise<ResponseType> => {
|
||||
const headers = getHeaders(authenticated);
|
||||
return callBackendAPI<undefined, ResponseType>(path, urlParams, queryParams, "GET", headers, undefined);
|
||||
}
|
||||
};
|
||||
|
||||
export async function postBackendAPI<RequestType, ResponseType>({
|
||||
export const postBackendAPI = async <RequestType, ResponseType>({
|
||||
path, urlParams, queryParams, authenticated,
|
||||
}: API, body: RequestType): Promise<ResponseType> {
|
||||
}: API, body: RequestType): Promise<ResponseType> => {
|
||||
const headers = getHeaders(authenticated);
|
||||
return callBackendAPI<RequestType, ResponseType>(path, urlParams, queryParams, "POST", headers, body);
|
||||
}
|
||||
};
|
||||
|
||||
export async function putBackendAPI<RequestType, ResponseType>({
|
||||
export const putBackendAPI = async <RequestType, ResponseType>({
|
||||
path, urlParams, queryParams, authenticated,
|
||||
}: API, body: RequestType): Promise<ResponseType> {
|
||||
}: API, body: RequestType): Promise<ResponseType> => {
|
||||
const headers = getHeaders(authenticated);
|
||||
return callBackendAPI<RequestType, ResponseType>(path, urlParams, queryParams, "PUT", headers, body);
|
||||
}
|
||||
};
|
||||
|
||||
export async function deleteBackendAPI<ResponseType>({
|
||||
export const deleteBackendAPI = async <ResponseType>({
|
||||
path, urlParams, queryParams, authenticated,
|
||||
}: API): Promise<ResponseType> {
|
||||
}: API): Promise<ResponseType> => {
|
||||
const headers = getHeaders(authenticated);
|
||||
return callBackendAPI<undefined, ResponseType>(path, urlParams, queryParams, "DELETE", headers, undefined);
|
||||
}
|
||||
};
|
||||
|
||||
+11
-11
@@ -12,7 +12,7 @@ interface Options {
|
||||
}
|
||||
|
||||
class EventApi {
|
||||
static async getEvent(id: number, auth = false): Promise<Event> {
|
||||
static getEvent = async (id: number, auth = false): Promise<Event> => {
|
||||
try {
|
||||
return await getBackendAPI<Event>({
|
||||
path: APIPath.EVENTS, urlParams: { id }, authenticated: auth,
|
||||
@@ -21,11 +21,11 @@ class EventApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async getEvents({
|
||||
static getEvents = async ({
|
||||
since, limit, offset, auth,
|
||||
}: Options = {}): Promise<Event[]> {
|
||||
}: Options = {}): Promise<Event[]> => {
|
||||
try {
|
||||
return await getBackendAPI<Event[]>({
|
||||
path: APIPath.EVENTS,
|
||||
@@ -40,9 +40,9 @@ class EventApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async createEvent(data: Event): Promise<Event> {
|
||||
static createEvent = async (data: Event): Promise<Event> => {
|
||||
try {
|
||||
return await postBackendAPI<Event, Event>({
|
||||
path: APIPath.EVENTS, authenticated: true,
|
||||
@@ -51,9 +51,9 @@ class EventApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async updateEvent(data: Event): Promise<Event> {
|
||||
static updateEvent = async (data: Event): Promise<Event> => {
|
||||
try {
|
||||
return await putBackendAPI<Event, Event>({
|
||||
path: APIPath.EVENTS, urlParams: { id: data.id }, authenticated: true,
|
||||
@@ -62,16 +62,16 @@ class EventApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async deleteEvent(id: number): Promise<void> {
|
||||
static deleteEvent = async (id: number): Promise<void> => {
|
||||
try {
|
||||
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default EventApi;
|
||||
|
||||
+10
-10
@@ -11,7 +11,7 @@ interface Options {
|
||||
}
|
||||
|
||||
class FeedApi {
|
||||
static async getPost(id: number, auth?: boolean): Promise<Post> {
|
||||
static getPost = async (id: number, auth?: boolean): Promise<Post> => {
|
||||
try {
|
||||
return await getBackendAPI<Post>({
|
||||
path: APIPath.FEED, urlParams: { id }, authenticated: auth,
|
||||
@@ -20,9 +20,9 @@ class FeedApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async getFeed({ limit, offset, auth }: Options = {}): Promise<Post[]> {
|
||||
static getFeed = async ({ limit, offset, auth }: Options = {}): Promise<Post[]> => {
|
||||
try {
|
||||
return await getBackendAPI<Post[]>({
|
||||
path: APIPath.FEED,
|
||||
@@ -36,18 +36,18 @@ class FeedApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async createPost(data: Post): Promise<Post> {
|
||||
static createPost = async (data: Post): Promise<Post> => {
|
||||
try {
|
||||
return await postBackendAPI<Post, Post>({ path: APIPath.FEED, authenticated: true }, data);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async updatePost(data: Post): Promise<Post> {
|
||||
static updatePost = async (data: Post): Promise<Post> => {
|
||||
try {
|
||||
return await putBackendAPI<Post, Post>({
|
||||
path: APIPath.FEED, urlParams: { id: data.id }, authenticated: true,
|
||||
@@ -56,16 +56,16 @@ class FeedApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async deletePost(id: number): Promise<void> {
|
||||
static deletePost = async (id: number): Promise<void> => {
|
||||
try {
|
||||
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default FeedApi;
|
||||
|
||||
+11
-11
@@ -12,7 +12,7 @@ interface Options {
|
||||
}
|
||||
|
||||
class JobAdApi {
|
||||
static async getJobAd(id: number, auth = false): Promise<JobAd> {
|
||||
static getJobAd = async (id: number, auth = false): Promise<JobAd> => {
|
||||
try {
|
||||
return await getBackendAPI({
|
||||
path: APIPath.JOBADS, urlParams: { id }, authenticated: auth,
|
||||
@@ -21,11 +21,11 @@ class JobAdApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async getJobAds({
|
||||
static getJobAds = async ({
|
||||
since, limit, offset, auth,
|
||||
}: Options = {}): Promise<JobAd[]> {
|
||||
}: Options = {}): Promise<JobAd[]> => {
|
||||
try {
|
||||
return await getBackendAPI<JobAd[]>({
|
||||
path: APIPath.JOBADS,
|
||||
@@ -40,9 +40,9 @@ class JobAdApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async createJobAd(data: JobAd): Promise<JobAd> {
|
||||
static createJobAd = async (data: JobAd): Promise<JobAd> => {
|
||||
try {
|
||||
return await postBackendAPI<JobAd, JobAd>({
|
||||
path: APIPath.JOBADS, authenticated: true,
|
||||
@@ -51,9 +51,9 @@ class JobAdApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async updateJobAd(data: JobAd): Promise<JobAd> {
|
||||
static updateJobAd = async (data: JobAd): Promise<JobAd> => {
|
||||
try {
|
||||
return await putBackendAPI<JobAd, JobAd>({
|
||||
path: APIPath.JOBADS, urlParams: { id: data.id }, authenticated: true,
|
||||
@@ -62,16 +62,16 @@ class JobAdApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async deleteJobAd(id: number): Promise<void> {
|
||||
static deleteJobAd = async (id: number): Promise<void> => {
|
||||
try {
|
||||
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.JOBADS, urlParams: { id }, authenticated: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default JobAdApi;
|
||||
|
||||
+24
-24
@@ -11,7 +11,7 @@ export type EmailRequest = {
|
||||
};
|
||||
|
||||
class SignupApi {
|
||||
static async getSignup(id: number): Promise<Signup> {
|
||||
static getSignup = async (id: number): Promise<Signup> => {
|
||||
try {
|
||||
return await getBackendAPI<Signup>({
|
||||
path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true,
|
||||
@@ -20,9 +20,9 @@ class SignupApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async createSignup(data: Signup): Promise<Signup> {
|
||||
static createSignup = async (data: Signup): Promise<Signup> => {
|
||||
try {
|
||||
return await postBackendAPI<Signup, Signup>({
|
||||
path: APIPath.SIGNUPS,
|
||||
@@ -31,9 +31,9 @@ class SignupApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async updateSignup(data: Signup, uuid: string): Promise<Signup> {
|
||||
static updateSignup = async (data: Signup, uuid: string): Promise<Signup> => {
|
||||
try {
|
||||
const { id } = data;
|
||||
if (!id) throw new Error("SignupId required!");
|
||||
@@ -48,9 +48,9 @@ class SignupApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async getSignupUUID(id: number, uuid: string): Promise<Signup> {
|
||||
static getSignupUUID = async (id: number, uuid: string): Promise<Signup> => {
|
||||
try {
|
||||
return await getBackendAPI<Signup>({
|
||||
path: APIPath.SIGNUPS_EDIT,
|
||||
@@ -63,18 +63,18 @@ class SignupApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async deleteSignup(id: number): Promise<void> {
|
||||
static deleteSignup = async (id: number): Promise<void> => {
|
||||
try {
|
||||
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async getForm(id: number, auth = false): Promise<SignupForm> {
|
||||
static getForm = async (id: number, auth = false): Promise<SignupForm> => {
|
||||
try {
|
||||
return await getBackendAPI<SignupForm>({
|
||||
path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: auth,
|
||||
@@ -83,9 +83,9 @@ class SignupApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async getForms(auth = false): Promise<SignupForm[]> {
|
||||
static getForms = async (auth = false): Promise<SignupForm[]> => {
|
||||
try {
|
||||
return await getBackendAPI<SignupForm[]>({
|
||||
path: APIPath.SIGNUP_FORMS, authenticated: auth,
|
||||
@@ -94,9 +94,9 @@ class SignupApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async createForm(data: SignupForm): Promise<SignupForm> {
|
||||
static createForm = async (data: SignupForm): Promise<SignupForm> => {
|
||||
try {
|
||||
return await postBackendAPI<SignupForm, SignupForm>({
|
||||
path: APIPath.SIGNUP_FORMS, authenticated: true,
|
||||
@@ -105,9 +105,9 @@ class SignupApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async updateForm(data: SignupForm): Promise<SignupForm> {
|
||||
static updateForm = async (data: SignupForm): Promise<SignupForm> => {
|
||||
try {
|
||||
return await putBackendAPI<SignupForm, SignupForm>({
|
||||
path: APIPath.SIGNUP_FORMS, urlParams: { id: data.id }, authenticated: true,
|
||||
@@ -116,34 +116,34 @@ class SignupApi {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async deleteForm(id: number): Promise<void> {
|
||||
static deleteForm = async (id: number): Promise<void> => {
|
||||
try {
|
||||
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async signupFormSendEmail(data: EmailRequest, id: number): Promise<void> {
|
||||
static signupFormSendEmail = async (data: EmailRequest, id: number): Promise<void> => {
|
||||
try {
|
||||
await postBackendAPI<EmailRequest, { message: "Email sent" }>({ path: APIPath.SIGNUP_FORMS_EMAIL, urlParams: { id }, authenticated: true }, data);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static async getSignups(id: number): Promise<Signup[]> {
|
||||
static getSignups = async (id: number): Promise<Signup[]> => {
|
||||
try {
|
||||
return await getBackendAPI<Signup[]>({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id }, authenticated: true });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default SignupApi;
|
||||
|
||||
+2
-2
@@ -3,14 +3,14 @@ import Tag from "@models/Tag";
|
||||
import { APIPath, getBackendAPI } from "./backend";
|
||||
|
||||
class TagApi {
|
||||
static async getTags(): Promise<Tag[]> {
|
||||
static getTags = async (): Promise<Tag[]> => {
|
||||
try {
|
||||
return await getBackendAPI<Tag[]>({ path: APIPath.TAGS });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default TagApi;
|
||||
|
||||
Reference in New Issue
Block a user