Skip to content

Commit 7b5e237

Browse files
committed
✨ 🏗️ services architecture impl
1 parent 19370c1 commit 7b5e237

File tree

7 files changed

+48
-0
lines changed

7 files changed

+48
-0
lines changed

src/modules/Home/services/homeBase.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { api, handlerApiAsync } from "@root/services";
2+
3+
import { homeBase } from "../utils";
4+
5+
interface HomeAsyncProps {
6+
id: number;
7+
}
8+
9+
export async function getHomeAsync({ id }: HomeAsyncProps) {
10+
const response = await handlerApiAsync({ execute: () => api.get(`${homeBase()}/${id}`) });
11+
return response;
12+
}
13+
14+
export function postHomeAsync({ id }: HomeAsyncProps) {
15+
return api.post(`${homeBase()}/${id}`);
16+
}

src/modules/Home/services/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./homeBase";

src/modules/Home/utils/homeBaseUrl.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const homeBase = () => {
2+
return "/posts";
3+
};

src/modules/Home/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./homeBaseUrl";

src/services/api.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import axios from "axios";
2+
3+
export const api = axios.create({
4+
baseURL: "https://jsonplaceholder.typicode.com/",
5+
});

src/services/handlerApi.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { AxiosResponse } from "axios";
2+
3+
interface Props {
4+
execute: () => Promise<AxiosResponse>;
5+
}
6+
7+
export const handlerApiAsync = async ({ execute }: Props) => {
8+
try {
9+
const { data } = await execute();
10+
return {
11+
success: true,
12+
data,
13+
};
14+
} catch (error) {
15+
return {
16+
success: false,
17+
error,
18+
};
19+
}
20+
};

src/services/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./api";
2+
export * from "./handlerApi";

0 commit comments

Comments
 (0)