Files
tvone/lib/services.ts
T

101 lines
3.8 KiB
TypeScript
Raw Normal View History

2026-04-23 10:09:44 +01:00
import { ApiClient } from "./api-client";
import type {
AttachImageInputDto,
ArticleOutputDto,
CategoryOutputDto,
CategoryTreeOutputDto,
CommentOutputDto,
CommentTreeOutputDto,
CreateArticleInputDto,
CreateCategoryInputDto,
CreateCommentInputDto,
CreateTagInputDto,
DeleteResponseDto,
ListArticlesInputDto,
ManageArticlesInputDto,
PaginatedArticlesOutputDto,
PaginatedBookmarksOutputDto,
ProfileOutputDto,
TagOutputDto,
UpdateArticleInputDto,
UpdateCategoryInputDto,
UpdateMeInputDto,
UpdateTagInputDto,
UploadedImageOutputDto,
UserOutputDto,
BookmarkOutputDto,
} from "./dtos";
type ApiQuery = Record<string, string | number | boolean | undefined>;
export function createTvOneApiServices(api: ApiClient) {
return {
users: {
me: () => api.get<UserOutputDto>("/users/me"),
updateMe: (dto: UpdateMeInputDto) => api.patch<UserOutputDto, UpdateMeInputDto>("/users/me", dto),
},
profile: {
get: () => api.get<ProfileOutputDto>("/profile"),
},
categories: {
tree: () => api.get<CategoryTreeOutputDto[]>("/categories"),
flat: () => api.get<CategoryOutputDto[]>("/categories/flat"),
create: (dto: CreateCategoryInputDto) =>
api.post<CategoryOutputDto, CreateCategoryInputDto>("/categories", dto),
update: (id: string, dto: UpdateCategoryInputDto) =>
api.patch<CategoryOutputDto, UpdateCategoryInputDto>(`/categories/${id}`, dto),
remove: (id: string) => api.delete<DeleteResponseDto>(`/categories/${id}`),
},
tags: {
list: () => api.get<TagOutputDto[]>("/tags"),
create: (dto: CreateTagInputDto) => api.post<TagOutputDto, CreateTagInputDto>("/tags", dto),
update: (id: string, dto: UpdateTagInputDto) =>
api.patch<TagOutputDto, UpdateTagInputDto>(`/tags/${id}`, dto),
remove: (id: string) => api.delete<DeleteResponseDto>(`/tags/${id}`),
},
articles: {
listPublished: (query?: ListArticlesInputDto) =>
api.get<PaginatedArticlesOutputDto>("/articles", query as ApiQuery | undefined),
findPublishedBySlug: (slug: string) => api.get<ArticleOutputDto>(`/articles/by-slug/${slug}`),
listManage: (query?: ManageArticlesInputDto) =>
api.get<PaginatedArticlesOutputDto>("/articles/manage", query as ApiQuery | undefined),
findById: (id: string) => api.get<ArticleOutputDto>(`/articles/by-id/${id}`),
create: (dto: CreateArticleInputDto) =>
api.post<ArticleOutputDto, CreateArticleInputDto>("/articles", dto),
update: (id: string, dto: UpdateArticleInputDto) =>
api.patch<ArticleOutputDto, UpdateArticleInputDto>(`/articles/${id}`, dto),
remove: (id: string) => api.delete<DeleteResponseDto>(`/articles/${id}`),
attachImage: (id: string, dto: AttachImageInputDto) =>
api.post<ArticleOutputDto, AttachImageInputDto>(`/articles/${id}/images`, dto),
},
comments: {
listForArticle: (articleId: string) =>
api.get<CommentTreeOutputDto[]>(`/comments/article/${articleId}`),
create: (articleId: string, dto: CreateCommentInputDto) =>
api.post<CommentOutputDto, CreateCommentInputDto>(`/comments/article/${articleId}`, dto),
remove: (id: string) => api.delete<DeleteResponseDto>(`/comments/${id}`),
},
bookmarks: {
listMine: (query?: { page?: number; limit?: number }) =>
api.get<PaginatedBookmarksOutputDto>("/bookmarks/me", query),
add: (articleId: string) => api.post<BookmarkOutputDto>(`/bookmarks/${articleId}`),
remove: (articleId: string) => api.delete<DeleteResponseDto>(`/bookmarks/${articleId}`),
},
images: {
upload: async (file: File) => {
const form = new FormData();
form.append("file", file);
return api.post<UploadedImageOutputDto, FormData>("/images/upload", form, {
isFormData: true,
});
},
},
};
}