Skip to content

Commit

Permalink
refactor: minor modification in atlas base class.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmoehabb authored and neuodev committed Dec 19, 2024
1 parent a65cd9a commit a724796
Show file tree
Hide file tree
Showing 17 changed files with 166 additions and 100 deletions.
35 changes: 28 additions & 7 deletions packages/atlas/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,58 @@ export class Auth extends Base {
async password(
credentials: IUser.Credentials
): Promise<IUser.LoginApiResponse> {
return this.post("/api/v1/auth/password", credentials);
return this.post({
route: "/api/v1/auth/password",
payload: credentials,
});
}

async google(
token: string,
role?: typeof IUser.Role.Student | typeof IUser.Role.Tutor
): Promise<IUser.LoginApiResponse> {
return this.post("/api/v1/auth/google", { token, role });
return this.post({
route: "/api/v1/auth/google",
payload: { token, role },
});
}

async forgotPassword(
payload: IUser.ForegetPasswordApiPayload
): Promise<void> {
await this.post("/api/v1/auth/password/forgot", payload);
await this.post({
route: "/api/v1/auth/password/forgot",
payload,
});
}

async resetPassword(
payload: IUser.ResetPasswordApiPayload
): Promise<IUser.ResetPasswordApiResponse> {
return await this.put("/api/v1/auth/password/reset", payload);
return await this.put({
route: "/api/v1/auth/password/reset",
payload,
});
}

async token(token: string) {
await this.post("/api/v1/auth/token", { token });
await this.post({
route: "/api/v1/auth/token",
payload: { token },
});
}

async verifyEmail(token: string): Promise<void> {
await this.put("/api/v1/auth/verify-email", { token });
await this.put({
route: "/api/v1/auth/verify-email",
payload: { token },
});
}

async sendVerifyEmail(callbackUrl: string): Promise<void> {
await this.put("/api/v1/auth/send-verify-email", { callbackUrl });
await this.put({
route: "/api/v1/auth/send-verify-email",
payload: { callbackUrl },
});
}
}
36 changes: 20 additions & 16 deletions packages/atlas/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,46 @@ import { Backend } from "@litespace/types";
import { AxiosInstance } from "axios";
import { createClient, AuthToken } from "@/client";

type HTTPMethodAttr<T, P = {}> = {
route: string;
payload?: T;
params?: P;
}

export class Base {
public readonly client: AxiosInstance;

constructor(backend: Backend, token: AuthToken | null) {
this.client = createClient(backend, token);
}

async post<T, R = void>(route: string, payload?: T): Promise<R> {
async post<T, R = void, P = {}>(attr: HTTPMethodAttr<T, P>): Promise<R> {
return this.client
.post(route, payload ? JSON.stringify(payload) : undefined)
.post(attr.route, attr.payload ? JSON.stringify(attr.payload) : undefined)
.then((response) => response.data);
}

async put<T, R = void>(route: string, payload: T): Promise<R> {
async put<T, R = void, P = {}>(attr: HTTPMethodAttr<T, P>): Promise<R> {
return this.client
.put(route, JSON.stringify(payload))
.put(attr.route, JSON.stringify(attr.payload))
.then((response) => response.data);
}

async del<T, R = void, P = {}>(
route: string,
payload?: T,
params?: P
): Promise<R> {
async del<T, R = void, P = {}>(attr: HTTPMethodAttr<T, P>): Promise<R> {
return this.client
.delete(route, { data: JSON.stringify(payload), params })
.delete(attr.route, {
data: JSON.stringify(attr.payload),
params: attr.params,
})
.then((response) => response.data);
}

async get<T, R = void, P = {}>(
route: string,
payload?: T,
params?: P
): Promise<R> {
async get<T, R = void, P = {}>(attr: HTTPMethodAttr<T, P>): Promise<R> {
return this.client
.get<R>(route, { data: JSON.stringify(payload), params })
.get<R>(attr.route, {
data: JSON.stringify(attr.payload),
params: attr.params,
})
.then((response) => response.data);
}
}
2 changes: 1 addition & 1 deletion packages/atlas/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { Base } from "@/base";

export class Cache extends Base {
public async flush(): Promise<void> {
return await this.del(`/api/v1/cache/flush`);
return await this.del({ route: `/api/v1/cache/flush` });
}
}
6 changes: 3 additions & 3 deletions packages/atlas/src/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import { ICall } from "@litespace/types";

export class Call extends Base {
async findUserCalls(id: number): Promise<ICall.FindUserCallsApiResponse> {
return await this.get(`/api/v1/call/list/user/${id}`);
return await this.get({ route: `/api/v1/call/list/user/${id}` });
}

async findById(id: number): Promise<ICall.FindCallByIdApiResponse> {
return await this.get(`/api/v1/call/${id}`);
return await this.get({ route: `/api/v1/call/${id}` });
}

async findCallMembers(
id: number,
type: ICall.Type
): Promise<ICall.FindCallMembersApiResponse> {
return await this.get(`/api/v1/call/${id}/${type}/members`);
return await this.get({ route: `/api/v1/call/${id}/${type}/members` });
}
}
26 changes: 19 additions & 7 deletions packages/atlas/src/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,55 @@ import { IFilter, IMessage, IRoom } from "@litespace/types";

export class Chat extends Base {
async createRoom(userId: number): Promise<IRoom.CreateRoomApiResponse> {
return await this.post(`/api/v1/chat/${userId}`);
return await this.post({ route: `/api/v1/chat/${userId}` });
}

async findRoomMessages(
id: number,
pagination?: IFilter.Pagination
): Promise<IMessage.FindRoomMessagesApiResponse> {
return await this.get(`/api/v1/chat/list/${id}/messages`, {}, pagination);
return await this.get({
route: `/api/v1/chat/list/${id}/messages`,
params: pagination,
});
}

async findRooms(
userId: number,
query?: IRoom.FindUserRoomsApiQuery
): Promise<IRoom.FindUserRoomsApiResponse> {
return await this.get(`/api/v1/chat/list/rooms/${userId}/`, {}, query);
return await this.get({
route: `/api/v1/chat/list/rooms/${userId}/`,
params: query,
});
}

async findRoomByMembers(
members: number[]
): Promise<IRoom.FindRoomByMembersApiResponse> {
return await this.get("/api/v1/chat/room/by/members", {}, { members });
return await this.get({
route:"/api/v1/chat/room/by/members",
params: { members },
});
}

async findRoomMembers(
room: number
): Promise<IRoom.FindRoomMembersApiResponse> {
return await this.get(`/api/v1/chat/room/members/${room}`);
return await this.get({ route: `/api/v1/chat/room/members/${room}` });
}

async findCallRoom(call: number): Promise<IRoom.FindCallRoomApiResponse> {
return await this.get(`/api/v1/chat/room/call/${call}`);
return await this.get({ route: `/api/v1/chat/room/call/${call}` });
}

async updateRoom(
room: number,
payload: IRoom.UpdateRoomApiPayload
): Promise<IRoom.Member> {
return await this.put(`/api/v1/chat/room/${room}`, payload);
return await this.put({
route: `/api/v1/chat/room/${room}`,
payload,
});
}
}
18 changes: 12 additions & 6 deletions packages/atlas/src/coupon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@ import { ICoupon } from "@litespace/types";

export class Coupon extends Base {
async create(payload: ICoupon.CreateApiPayload): Promise<ICoupon.Self> {
return this.post(`/api/v1/coupon`, payload);
return this.post({
route: `/api/v1/coupon`,
payload,
});
}

async update(
id: number,
payload: ICoupon.UpdateApiPayload
): Promise<ICoupon.Self> {
return this.put(`/api/v1/coupon/${id}`, payload);
return this.put({
route: `/api/v1/coupon/${id}`,
payload,
});
}

async delete(id: number): Promise<void> {
await this.del(`/api/v1/coupon/${id}`);
await this.del({ route: `/api/v1/coupon/${id} `});
}

async findById(id: number): Promise<ICoupon.Self> {
return this.get(`/api/v1/coupon/${id}`);
return this.get({ route: `/api/v1/coupon/${id}` });
}

async findByCode(code: string): Promise<ICoupon.Self> {
return this.get(`/api/v1/coupon/code/${code}`);
return this.get({ route: `/api/v1/coupon/code/${code}` });
}

async find(): Promise<ICoupon.FindCouponsApiResponse> {
return this.get(`/api/v1/coupon/list`);
return this.get({ route: `/api/v1/coupon/list` });
}
}
19 changes: 14 additions & 5 deletions packages/atlas/src/interview.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
import { Base } from "@/base";
import { IFilter, IInterview } from "@litespace/types";
import { IInterview } from "@litespace/types";

export class Interview extends Base {
public async create(
payload: IInterview.CreateApiPayload
): Promise<IInterview.Self> {
return await this.post(`/api/v1/interview`, payload);
return await this.post({
route: `/api/v1/interview`,
payload,
});
}

public async update(
id: number,
payload: IInterview.UpdateApiPayload
): Promise<IInterview.Self> {
return await this.put(`/api/v1/interview/${id}`, payload);
return await this.put({
route: `/api/v1/interview/${id}`,
payload,
});
}

public async findInterviews(
query: IInterview.FindInterviewsApiQuery
): Promise<IInterview.FindInterviewsApiResponse> {
return this.get("/api/v1/interview/list/", null, query);
return this.get({
route: "/api/v1/interview/list/",
params: query
});
}

public async findInterviewById(id: number): Promise<IInterview.Self> {
return this.get(`/api/v1/interview/${id}`);
return this.get({ route: `/api/v1/interview/${id}` });
}
}
19 changes: 14 additions & 5 deletions packages/atlas/src/invoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import { safeFormData } from "@/lib/form";

export class Invoice extends Base {
async create(payload: IInvoice.CreateApiPayload): Promise<IInvoice.Self> {
return await this.post("/api/v1/invoice", payload);
return await this.post({
route: "/api/v1/invoice",
payload
});
}

async stats(userId: number): Promise<IInvoice.StatsApiResponse> {
return await this.get(`/api/v1/invoice/stats/${userId}`);
return await this.get({ route: `/api/v1/invoice/stats/${userId}` });
}

async updateByReceiver(
invoiceId: number,
payload: IInvoice.UpdateByReceiverApiPayload
): Promise<void> {
return await this.put(`/api/v1/invoice/receiver/${invoiceId}`, payload);
return await this.put({
route: `/api/v1/invoice/receiver/${invoiceId}`,
payload,
});
}

async updateByAdmin(
Expand Down Expand Up @@ -44,10 +50,13 @@ export class Invoice extends Base {
async find(
params: IInvoice.FindInvoicesQuery
): Promise<Paginated<IInvoice.Self>> {
return this.get("/api/v1/invoice/list", null, params);
return this.get({
route: "/api/v1/invoice/list",
params,
});
}

async cancel(invoiceId: number): Promise<void> {
return this.del(`/api/v1/invoice/${invoiceId}`);
return this.del({ route: `/api/v1/invoice/${invoiceId}` });
}
}
12 changes: 9 additions & 3 deletions packages/atlas/src/lesson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ export class Lesson extends Base {
async create(
payload: ILesson.CreateApiPayload
): Promise<ILesson.CreateLessonApiResponse> {
return this.post(`/api/v1/lesson/`, payload);
return this.post({
route: `/api/v1/lesson/`,
payload,
});
}

async findLessons(
query: ILesson.FindLessonsApiQuery
): Promise<ILesson.FindUserLessonsApiResponse> {
return this.get(`/api/v1/lesson/list/`, {}, query);
return this.get({
route: `/api/v1/lesson/list/`,
params: query,
});
}

async cancel(id: number): Promise<void> {
return this.del(`/api/v1/lesson/${id}`);
return this.del({ route: `/api/v1/lesson/${id}` });
}
}
6 changes: 3 additions & 3 deletions packages/atlas/src/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ export class Peer extends Base {
* @deprecated we will use web sockets to register peer ids
*/
async register(payload: IPeer.RegisterPeerIdApiPayload) {
await this.post("/api/v1/peer", payload);
await this.post({ route: "/api/v1/peer", payload });
}

/**
* @deprecated we will use web sockets to de-register peer ids
*/
async delete(payload: IPeer.DeletePeerIdApiQurey) {
await this.del("/api/v1/peer", payload);
await this.del({ route: "/api/v1/peer", payload });
}

async findPeerId(
payload: IPeer.FindPeerIdApiQuery
): Promise<IPeer.FindPeerIdApiResponse> {
return this.get("/api/v1/peer", null, payload);
return this.get({ route: "/api/v1/peer", payload });
}
}
Loading

0 comments on commit a724796

Please sign in to comment.