Skip to content

Commit

Permalink
refactor: update all endpoints to follow the contract to return alway…
Browse files Browse the repository at this point in the history
…s json
  • Loading branch information
EvgenyWas committed Aug 27, 2024
1 parent 2b985a7 commit f79cdf6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 11 deletions.
6 changes: 3 additions & 3 deletions composables/api/usePublicAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FetchOptions } from 'ofetch';
import type { ArticlesStats } from '~/types/responses';
import type { ArticlesStats, UpdateArticleStatsResponse } from '~/types/responses';

interface FetchArticleStatsOptions extends FetchOptions {
params: {
Expand All @@ -19,10 +19,10 @@ export default function usePublicAPI() {
$fetch<ArticlesStats>('/api/public/article/stats', { ...options, method: 'GET' });

const updateArticleRate = (options: FetchArticleStatsRateOptions) =>
$fetch<boolean>('/api/public/article/stats/rate', { ...options, method: 'PUT' });
$fetch<UpdateArticleStatsResponse>('/api/public/article/stats/rate', { ...options, method: 'PUT' });

const updateArticleViews = (options: FetchArticleStatsOptions) =>
$fetch<boolean>('/api/public/article/stats/views', { ...options, method: 'PUT' });
$fetch<UpdateArticleStatsResponse>('/api/public/article/stats/views', { ...options, method: 'PUT' });

return { fetchArticleStats, updateArticleRate, updateArticleViews };
}
14 changes: 11 additions & 3 deletions composables/api/useUserAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FetchOptions } from 'ofetch';
import type { WhoamiResponse } from '~/types/responses';
import type { DeleteUserFavouriteResponse, UpdateUserAvatarResponse, WhoamiResponse } from '~/types/responses';
import type { FavouriteArticle, Profile } from '~/types/user';

interface UpdateProfileOptions extends FetchOptions {
Expand Down Expand Up @@ -62,10 +62,18 @@ export default function useUserAPI() {
$fetch<Profile>(`/api/user/${id}/profile`, { ...defaultOptions, ...options, method: 'PUT' });

const updateProfileAvatar = (id: string, options: UpdateProfileAvatarOptions) =>
$fetch<string>(`/api/user/${id}/profile/avatar`, { ...defaultOptions, ...options, method: 'PUT' });
$fetch<UpdateUserAvatarResponse>(`/api/user/${id}/profile/avatar`, {
...defaultOptions,
...options,
method: 'PUT',
});

const removeFavouriteArticle = (id: string, options: RemoveFavouriteArticleOptions) =>
$fetch<boolean>(`/api/user/${id}/favourites`, { ...defaultOptions, ...options, method: 'DELETE' });
$fetch<DeleteUserFavouriteResponse>(`/api/user/${id}/favourites`, {
...defaultOptions,
...options,
method: 'DELETE',
});

const addFavouriteArticle = (id: string, options: AddFavouriteArticleOptions) =>
$fetch<FavouriteArticle>(`/api/user/${id}/favourites`, { ...defaultOptions, ...options, method: 'PUT' });
Expand Down
3 changes: 2 additions & 1 deletion pages/profile/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ const saveChanges = async () => {
if (avatar.value) {
const avatarBody = new FormData();
avatarBody.append('avatar', avatar.value);
uploadedAvatar = await updateProfileAvatar(user.value.profile.id as string, { body: avatarBody });
const res = await updateProfileAvatar(user.value.profile.id as string, { body: avatarBody });
uploadedAvatar = res.path;
}
const profileBody = {
Expand Down
2 changes: 1 addition & 1 deletion server/api/public/article/stats/rate.put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ export default defineEventHandler(async (event) => {

await stats.updateOne({ ratings, rate });

return true;
return { updated: true };
});
2 changes: 1 addition & 1 deletion server/api/public/article/stats/views.put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export default defineEventHandler(async (event) => {
await ArticleStats.create({ topic, title, views: 1 });
}

return true;
return { updated: true };
});
2 changes: 1 addition & 1 deletion server/api/user/[id]/favourites/index.delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default defineEventHandler(async (event) => {
throw new MongooseError('User with provided ID is not found');
}

return true;
return { deleted: true };
} catch (error) {
return sendError(
event,
Expand Down
2 changes: 1 addition & 1 deletion server/api/user/[id]/profile/avatar/index.put.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export default defineEventHandler(async (event) => {
transformation: { format: 'webp' },
});

return `/storage/${img.public_id}`;
return { path: `/storage/${img.public_id}` };
} catch (error) {
const errorInput = isCloudinarUploadApiError(error)
? { statusCode: error.http_code, statusMessage: error.message }
Expand Down
12 changes: 12 additions & 0 deletions types/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ export interface ArticlesStats {
topic?: string;
title?: string;
}

export interface UpdateArticleStatsResponse {
updated: boolean;
}

export interface DeleteUserFavouriteResponse {
deleted: boolean;
}

export interface UpdateUserAvatarResponse {
path: string;
}

0 comments on commit f79cdf6

Please sign in to comment.