Skip to content

Commit

Permalink
feat: implement entities api (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan authored Mar 10, 2024
1 parent ed8689d commit 697c60b
Show file tree
Hide file tree
Showing 23 changed files with 264 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"ci:spell": "cspell --config .cspell.json \"**/*.{js,ts,tsx,md,html}\" --quiet"
},
"dependencies": {
"@tanstack/react-query": "^5.24.8",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.1"
Expand Down
9 changes: 7 additions & 2 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import RouterProvider from "./providers/RouterProvider";
import { QueryClientProvider } from "./providers/query-client.provider";
import { RouterProvider } from "./providers/router.provider";
import "./styles/normalize.css";

const App = () => {
return <RouterProvider />;
return (
<QueryClientProvider>
<RouterProvider />
</QueryClientProvider>
);
};

export default App;
15 changes: 15 additions & 0 deletions src/app/providers/query-client.provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
QueryClient,
QueryClientProvider as TanstackQueryClientProvider,
} from "@tanstack/react-query";
import { PropsWithChildren } from "react";

const queryClient = new QueryClient();

export const QueryClientProvider = ({ children }: PropsWithChildren) => {
return (
<TanstackQueryClientProvider client={queryClient}>
{children}
</TanstackQueryClientProvider>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ const router = createBrowserRouter([
},
]);

const RouterProvider = () => <ReactRouterProvider router={router} />;

export default RouterProvider;
export const RouterProvider = () => <ReactRouterProvider router={router} />;
7 changes: 7 additions & 0 deletions src/entities/link-book/api/create-link-book.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { HttpMethod, fetcher } from "@/shared/lib/fetcher";
import type { CreateLinkBookDto } from "./dto/create-link-book.dto";

export const createLinkBook = (dto: CreateLinkBookDto) =>
fetcher.fetch(HttpMethod.POST, "/link-books", {
body: dto,
});
6 changes: 6 additions & 0 deletions src/entities/link-book/api/delete-link-book.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { HttpMethod, fetcher } from "@/shared/lib/fetcher";
import type { GetLinkBooksDto } from "./dto/get-link-books.dto";

export const deleteLinkBook = (id: string) => {
return fetcher.fetch<GetLinkBooksDto>(HttpMethod.DELETE, `/link-books/${id}`);
};
6 changes: 6 additions & 0 deletions src/entities/link-book/api/dto/create-link-book.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type CreateLinkBookDto = {
backgroundColor: string;
illustration: string;
title: string;
titleColor: string;
};
15 changes: 15 additions & 0 deletions src/entities/link-book/api/dto/get-link-books.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface GetLinkBooksDto {
linkBooks: {
backgroundColor: string;
createdAt: string;
illustration: string;
isDefault: string;
lastSavedAt: string;
linkBookId: string;
linkCount: number;
title: string;
titleColor: string;
userId: string;
}[];
totalLinkCount: number;
}
6 changes: 6 additions & 0 deletions src/entities/link-book/api/dto/update-link-book.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type UpdateLinkBookDto = {
backgroundColor: string;
illustration: string;
title: string;
titleColor: string;
};
12 changes: 12 additions & 0 deletions src/entities/link-book/api/get-link-books.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { HttpMethod, fetcher } from "@/shared/lib/fetcher";
import { mapLinkBooks } from "@/entities/link-book/api/mapper/link-books.map";
import type { GetLinkBooksDto } from "./dto/get-link-books.dto";

export const getLinkBooks = (query: {
sort: "created_at" | "last_saved_at" | "title";
}) =>
fetcher
.fetch<GetLinkBooksDto>(HttpMethod.GET, "/api/link-books", {
query,
})
.then(mapLinkBooks);
18 changes: 18 additions & 0 deletions src/entities/link-book/api/mapper/link-books.map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { LinkBook } from "../../model/link-book";
import type { GetLinkBooksDto } from "../dto/get-link-books.dto";

export const mapLinkBooks = (
response: GetLinkBooksDto,
): {
linkBooks: LinkBook[];
totalLinkCount: number;
} => {
return {
linkBooks: response.linkBooks.map((linkBook) => ({
...linkBook,
createdAt: new Date(linkBook.createdAt),
lastSavedAt: new Date(linkBook.lastSavedAt),
})),
totalLinkCount: response.totalLinkCount,
};
};
9 changes: 9 additions & 0 deletions src/entities/link-book/api/update-link-book.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { HttpMethod, fetcher } from "@/shared/lib/fetcher";
import type { GetLinkBooksDto } from "./dto/get-link-books.dto";
import type { UpdateLinkBookDto } from "./dto/update-link-book.dto";

export const updateLinkBook = (id: string, dto: UpdateLinkBookDto) => {
return fetcher.fetch<GetLinkBooksDto>(HttpMethod.PUT, `/link-books/${id}`, {
body: dto,
});
};
12 changes: 12 additions & 0 deletions src/entities/link-book/model/link-book.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface LinkBook {
backgroundColor: string;
createdAt: Date;
illustration: string;
isDefault: string;
lastSavedAt: Date;
linkBookId: string;
linkCount: number;
title: string;
titleColor: string;
userId: string;
}
21 changes: 21 additions & 0 deletions src/entities/notification/api/dto/get-notifications.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface GetNotificationsDto {
notifications: [
{
body: string;
createdAt: string;
isRead: boolean;
notificationId: string;
title: string;
type: string;
userId: string;
},
];
page: {
next: number;
page: number;
perPage: number;
prev: number;
total: number;
totalPage: number;
};
}
12 changes: 12 additions & 0 deletions src/entities/notification/api/get-notifications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { HttpMethod, fetcher } from "@/shared/lib/fetcher";
import type { GetNotificationsDto } from "./dto/get-notifications.dto";
import { mapNotifications } from "./mapper/notifications.map";

export const getNotifications = (page: number) =>
fetcher
.fetch<GetNotificationsDto>(HttpMethod.GET, "/notifications", {
query: {
page,
},
})
.then(mapNotifications);
17 changes: 17 additions & 0 deletions src/entities/notification/api/mapper/notifications.map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Notification } from "../../model/notification";
import type { GetNotificationsDto } from "../dto/get-notifications.dto";

export const mapNotifications = (
response: GetNotificationsDto,
): {
notifications: Notification[];
page: GetNotificationsDto["page"];
} => {
return {
notifications: response.notifications.map((notification) => ({
...notification,
createdAt: new Date(notification.createdAt),
})),
page: response.page,
};
};
File renamed without changes.
9 changes: 9 additions & 0 deletions src/entities/notification/model/notification.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface Notification {
body: string;
createdAt: Date;
isRead: boolean;
notificationId: string;
title: string;
type: string;
userId: string;
}
2 changes: 1 addition & 1 deletion src/pages/root/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const RootPage = () => {
return <>Root Page</>;
return <></>;
};

export default RootPage;
59 changes: 59 additions & 0 deletions src/shared/lib/fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export enum HttpMethod {
GET = "GET",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
}

export type FetchQuery = Record<string, string | number>;
export type FetchBody = Record<string, unknown>;

export type FetchOptions = {
query?: FetchQuery;
body?: FetchBody;
};

type FetcherConfig = {
baseUrl?: string;
};

class Fetcher {
constructor(private config: FetcherConfig) {}

public async fetch<T>(
method: HttpMethod,
endpoint: string,
options?: FetchOptions,
): Promise<T> {
const response = await fetch(this.getUrl(endpoint, options?.query), {
method,
headers: {
"Content-Type": "application/json",
Authorization: import.meta.env.AUTHORIZATION,
},
body: options?.body ? JSON.stringify(options.body) : undefined,
});

if (!response.ok) {
//
}
return await response.json();
}

private getUrl(
endpoint: string,
query?: Record<string, string | number>,
): string {
const url = new URL(endpoint, this.config.baseUrl);
if (query) {
Object.entries(query).forEach(([key, value]) => {
url.searchParams.append(key, value.toString());
});
}
return url.toString();
}
}

export const fetcher = new Fetcher({
baseUrl: import.meta.env.BASE_URL,
});
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@

"paths": {
"@/app/*": ["./src/app/*"],
"@/pages/*": ["./src/pages/*"]
"@/pages/*": ["./src/pages/*"],
"@/shared/*": ["./src/shared/*"],
"@/entities/*": ["./src/entities/*"]
}
},
"include": ["src"],
Expand Down
8 changes: 8 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ import tsconfigPaths from "vite-tsconfig-paths";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), tsconfigPaths()],
server: {
proxy: {
"/api": {
target: "http://49.50.165.241/",
changeOrigin: true,
},
},
},
});
19 changes: 19 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1622,6 +1622,24 @@ __metadata:
languageName: node
linkType: hard

"@tanstack/query-core@npm:5.24.8":
version: 5.24.8
resolution: "@tanstack/query-core@npm:5.24.8"
checksum: 3c3d51f1c6e049ac9f6f7c36cefc640b3266d9a91307cbfc02e4ce6b4e1cf68de1b8f0243fca20c86d8cf616e334579dc123424aed9daedc32a92e5c9e97ca64
languageName: node
linkType: hard

"@tanstack/react-query@npm:^5.24.8":
version: 5.24.8
resolution: "@tanstack/react-query@npm:5.24.8"
dependencies:
"@tanstack/query-core": "npm:5.24.8"
peerDependencies:
react: ^18.0.0
checksum: f2d7d3ba93e5cd7ff5d21e91e2670bdcfd864741c13e7cca93c996f8b61738cb16796e3702d0aa5b9ba36c5c15ffdc068466662c0de40baf9c45c053559b3fdd
languageName: node
linkType: hard

"@types/babel__core@npm:^7.1.14, @types/babel__core@npm:^7.20.5":
version: 7.20.5
resolution: "@types/babel__core@npm:7.20.5"
Expand Down Expand Up @@ -4242,6 +4260,7 @@ __metadata:
dependencies:
"@html-eslint/eslint-plugin": "npm:^0.23.1"
"@html-eslint/parser": "npm:^0.23.0"
"@tanstack/react-query": "npm:^5.24.8"
"@types/jest": "npm:^29.5.12"
"@types/react": "npm:^18.2.56"
"@types/react-dom": "npm:^18.2.19"
Expand Down

0 comments on commit 697c60b

Please sign in to comment.