webapp/src/utils/apiClient.ts

437 lines
15 KiB
TypeScript
Raw Normal View History

2022-11-18 02:23:04 +00:00
import type {
FollowingEntity,
AuthResult,
ShoutInput,
Topic,
Author,
2022-11-24 06:52:31 +00:00
LoadShoutsOptions,
QueryLoadChatsArgs,
QueryLoadAuthorsByArgs,
2022-11-24 07:11:24 +00:00
QueryLoadMessagesByArgs,
MutationCreateChatArgs,
2022-11-24 12:58:07 +00:00
MutationCreateMessageArgs,
2022-12-01 17:16:14 +00:00
QueryLoadRecipientsArgs,
ProfileInput,
ReactionInput,
Chat,
2023-03-27 14:45:07 +00:00
ReactionBy,
Shout,
NotificationsQueryParams,
NotificationsQueryResult,
MySubscriptionsQueryResult,
LoadRandomTopShoutsParams,
2022-11-18 02:23:04 +00:00
} from '../graphql/types.gen'
import createArticle from '../graphql/mutation/article-create'
import deleteShout from '../graphql/mutation/article-delete'
import updateArticle from '../graphql/mutation/article-update'
import authConfirmEmailMutation from '../graphql/mutation/auth-confirm-email'
2022-09-17 18:23:30 +00:00
import authLogoutQuery from '../graphql/mutation/auth-logout'
import authRegisterMutation from '../graphql/mutation/auth-register'
import authSendLinkMutation from '../graphql/mutation/auth-send-link'
import createChat from '../graphql/mutation/create-chat'
import createMessage from '../graphql/mutation/create-chat-message'
2022-09-09 11:53:35 +00:00
import followMutation from '../graphql/mutation/follow'
import markAllNotificationsAsRead from '../graphql/mutation/mark-all-notifications-as-read'
import markNotificationAsRead from '../graphql/mutation/mark-notification-as-read'
import mySession from '../graphql/mutation/my-session'
2022-09-09 11:53:35 +00:00
import reactionCreate from '../graphql/mutation/reaction-create'
import reactionDestroy from '../graphql/mutation/reaction-destroy'
import reactionUpdate from '../graphql/mutation/reaction-update'
import unfollowMutation from '../graphql/mutation/unfollow'
import updateProfile from '../graphql/mutation/update-profile'
import { getToken, privateGraphQLClient } from '../graphql/privateGraphQLClient'
import { publicGraphQLClient } from '../graphql/publicGraphQLClient'
import shoutLoad from '../graphql/query/article-load'
import shoutsLoadBy from '../graphql/query/articles-load-by'
import articlesLoadRandomTop from '../graphql/query/articles-load-random-top'
import articlesLoadUnrated from '../graphql/query/articles-load-unrated'
import authCheckEmailQuery from '../graphql/query/auth-check-email'
import authLoginQuery from '../graphql/query/auth-login'
2022-11-14 17:41:05 +00:00
import authorBySlug from '../graphql/query/author-by-slug'
2023-02-13 13:48:05 +00:00
import userSubscribers from '../graphql/query/author-followers'
2023-04-22 14:36:38 +00:00
import userFollowedTopics from '../graphql/query/author-following-topics'
import userFollowedAuthors from '../graphql/query/author-following-users'
import authorsAll from '../graphql/query/authors-all'
2022-11-15 15:27:02 +00:00
import authorsLoadBy from '../graphql/query/authors-load-by'
import chatMessagesLoadBy from '../graphql/query/chat-messages-load-by'
import loadRecipients from '../graphql/query/chat-recipients'
import myChats from '../graphql/query/chats-load'
2023-05-05 20:05:50 +00:00
import draftsLoad from '../graphql/query/drafts-load'
import myFeed from '../graphql/query/my-feed'
import mySubscriptions from '../graphql/query/my-subscriptions'
import notifications from '../graphql/query/notifications'
import reactionsLoadBy from '../graphql/query/reactions-load-by'
import topicBySlug from '../graphql/query/topic-by-slug'
import topicsAll from '../graphql/query/topics-all'
import topicsRandomQuery from '../graphql/query/topics-random'
2022-09-09 11:53:35 +00:00
2022-11-14 01:17:12 +00:00
type ApiErrorCode =
| 'unknown'
| 'email_not_confirmed'
| 'user_not_found'
| 'user_already_exists'
| 'token_expired'
| 'token_invalid'
| 'duplicate_slug'
2022-09-30 14:22:33 +00:00
export class ApiError extends Error {
code: ApiErrorCode
constructor(code: ApiErrorCode, message?: string) {
super(message)
this.code = code
}
}
2022-09-17 18:23:30 +00:00
2022-09-30 14:22:33 +00:00
export const apiClient = {
authLogin: async ({ email, password }: { email: string; password: string }): Promise<AuthResult> => {
2022-09-17 18:23:30 +00:00
const response = await publicGraphQLClient.query(authLoginQuery, { email, password }).toPromise()
2022-10-08 16:40:58 +00:00
// console.debug('[api-client] authLogin', { response })
2022-09-30 14:22:33 +00:00
if (response.error) {
if (
response.error.message === '[GraphQL] User not found' ||
response.error.message === "[GraphQL] 'dict' object has no attribute 'id'"
) {
2022-09-30 14:22:33 +00:00
throw new ApiError('user_not_found')
}
throw new ApiError('unknown', response.error.message)
}
if (response.data.signIn.error) {
if (response.data.signIn.error === 'please, confirm email') {
throw new ApiError('email_not_confirmed')
}
throw new ApiError('unknown', response.data.signIn.error)
}
2022-09-17 18:23:30 +00:00
return response.data.signIn
},
2022-10-21 18:17:04 +00:00
authRegister: async ({
email,
password,
name,
2022-10-21 18:17:04 +00:00
}: {
email: string
password: string
name: string
}): Promise<void> => {
2022-09-30 14:22:33 +00:00
const response = await publicGraphQLClient
2022-10-22 09:18:12 +00:00
.mutation(authRegisterMutation, { email, password, name })
2022-09-30 14:22:33 +00:00
.toPromise()
2022-10-21 18:17:04 +00:00
if (response.error) {
if (response.error.message === '[GraphQL] User already exist') {
throw new ApiError('user_already_exists', response.error.message)
}
throw new ApiError('unknown', response.error.message)
}
2022-09-17 18:23:30 +00:00
},
authSignOut: async () => {
const response = await publicGraphQLClient.query(authLogoutQuery, {}).toPromise()
return response.data.signOut
},
authCheckEmail: async ({ email }) => {
// check if email is used
const response = await publicGraphQLClient.query(authCheckEmailQuery, { email }).toPromise()
return response.data.isEmailUsed
},
2022-11-27 19:19:06 +00:00
authSendLink: async ({ email, lang, template }) => {
2022-09-17 18:23:30 +00:00
// send link with code on email
2022-11-27 19:19:06 +00:00
const response = await publicGraphQLClient
.mutation(authSendLinkMutation, { email, lang, template })
.toPromise()
if (response.error) {
2022-11-14 01:17:12 +00:00
if (response.error.message === '[GraphQL] User not found') {
throw new ApiError('user_not_found', response.error.message)
}
throw new ApiError('unknown', response.error.message)
}
2022-11-14 01:17:12 +00:00
if (response.data.sendLink.error) {
throw new ApiError('unknown', response.data.sendLink.message)
}
2022-11-02 06:56:01 +00:00
return response.data.sendLink
2022-09-17 18:23:30 +00:00
},
2022-10-21 18:17:04 +00:00
confirmEmail: async ({ token }: { token: string }) => {
2022-09-17 18:23:30 +00:00
// confirm email with code from link
2022-10-25 16:25:42 +00:00
const response = await publicGraphQLClient.mutation(authConfirmEmailMutation, { token }).toPromise()
2022-10-21 18:17:04 +00:00
if (response.error) {
2022-11-14 01:17:12 +00:00
// TODO: better error communication
if (response.error.message === '[GraphQL] check token lifetime') {
throw new ApiError('token_expired', response.error.message)
}
if (response.error.message === '[GraphQL] token is not valid') {
throw new ApiError('token_invalid', response.error.message)
}
2022-10-21 18:17:04 +00:00
throw new ApiError('unknown', response.error.message)
}
if (response.data?.confirmEmail?.error) {
throw new ApiError('unknown', response.data?.confirmEmail?.error)
}
return response.data.confirmEmail
2022-09-17 18:23:30 +00:00
},
2022-09-22 09:37:49 +00:00
getRandomTopics: async ({ amount }: { amount: number }) => {
const response = await publicGraphQLClient.query(topicsRandomQuery, { amount }).toPromise()
2022-09-09 11:53:35 +00:00
2022-09-30 14:22:33 +00:00
if (!response.data) {
2022-10-08 16:40:58 +00:00
console.error('[api-client] getRandomTopics', response.error)
2022-09-30 14:22:33 +00:00
}
2022-09-09 11:53:35 +00:00
return response.data.topicsRandom
},
// subscribe
follow: async ({ what, slug }: { what: FollowingEntity; slug: string }) => {
const response = await privateGraphQLClient.mutation(followMutation, { what, slug }).toPromise()
2022-09-09 11:53:35 +00:00
return response.data.follow
},
unfollow: async ({ what, slug }: { what: FollowingEntity; slug: string }) => {
const response = await privateGraphQLClient.mutation(unfollowMutation, { what, slug }).toPromise()
2022-09-09 11:53:35 +00:00
return response.data.unfollow
},
2022-09-30 14:22:33 +00:00
getSession: async (): Promise<AuthResult> => {
2022-11-14 01:17:12 +00:00
if (!getToken()) {
return null
}
2022-09-09 11:53:35 +00:00
// renew session with auth token in header (!)
const response = await privateGraphQLClient.mutation(mySession, {}).toPromise()
2022-10-21 18:17:04 +00:00
if (response.error) {
throw new ApiError('unknown', response.error.message)
}
2022-11-24 14:59:37 +00:00
if (response.data?.getSession?.error) {
throw new ApiError('unknown', response.data.getSession.error)
2022-10-21 18:17:04 +00:00
}
2022-11-24 14:59:37 +00:00
return response.data.getSession
2022-09-09 11:53:35 +00:00
},
getAllTopics: async () => {
const response = await publicGraphQLClient.query(topicsAll, {}).toPromise()
2022-10-04 12:42:11 +00:00
if (response.error) {
2022-10-08 16:40:58 +00:00
console.debug('[api-client] getAllTopics', response.error)
2022-10-04 12:42:11 +00:00
}
2022-09-09 11:53:35 +00:00
return response.data.topicsAll
},
getAllAuthors: async () => {
2022-10-04 09:26:47 +00:00
const response = await publicGraphQLClient.query(authorsAll, {}).toPromise()
2022-10-04 12:42:11 +00:00
if (response.error) {
2022-10-08 16:40:58 +00:00
console.debug('[api-client] getAllAuthors', response.error)
2022-10-04 12:42:11 +00:00
}
2022-09-09 11:53:35 +00:00
return response.data.authorsAll
},
getAuthor: async ({ slug }: { slug: string }): Promise<Author> => {
const response = await publicGraphQLClient.query(authorBySlug, { slug }).toPromise()
return response.data.getAuthor
},
2023-02-13 13:48:05 +00:00
getAuthorFollowers: async ({ slug }: { slug: string }): Promise<Author[]> => {
const response = await publicGraphQLClient.query(userSubscribers, { slug }).toPromise()
return response.data.userFollowers
},
2023-04-22 14:36:38 +00:00
getAuthorFollowingUsers: async ({ slug }: { slug: string }): Promise<Author[]> => {
const response = await publicGraphQLClient.query(userFollowedAuthors, { slug }).toPromise()
return response.data.userFollowedAuthors
},
getAuthorFollowingTopics: async ({ slug }: { slug: string }): Promise<Topic[]> => {
const response = await publicGraphQLClient.query(userFollowedTopics, { slug }).toPromise()
return response.data.userFollowedTopics
2023-02-23 14:07:25 +00:00
},
updateProfile: async (input: ProfileInput) => {
const response = await privateGraphQLClient.mutation(updateProfile, { profile: input }).toPromise()
if (response.error) {
if (
response.error.message.includes('duplicate key value violates unique constraint "user_slug_key"')
) {
throw new ApiError('duplicate_slug', response.error.message)
}
throw new ApiError('unknown', response.error.message)
}
2022-12-07 08:37:40 +00:00
return response.data.updateProfile
2022-12-01 17:16:14 +00:00
},
getTopic: async ({ slug }: { slug: string }): Promise<Topic> => {
const response = await publicGraphQLClient.query(topicBySlug, { slug }).toPromise()
return response.data.getTopic
2022-10-04 12:42:11 +00:00
},
2023-03-27 14:45:07 +00:00
createArticle: async ({ article }: { article: ShoutInput }): Promise<Shout> => {
2022-11-09 17:57:35 +00:00
const response = await privateGraphQLClient.mutation(createArticle, { shout: article }).toPromise()
2023-03-27 14:45:07 +00:00
return response.data.createShout.shout
2022-11-09 17:57:35 +00:00
},
updateArticle: async ({
shoutId,
2023-05-08 17:21:06 +00:00
shoutInput,
publish,
}: {
shoutId: number
2023-05-08 17:21:06 +00:00
shoutInput?: ShoutInput
publish: boolean
}): Promise<Shout> => {
2023-05-08 17:21:06 +00:00
const response = await privateGraphQLClient
.mutation(updateArticle, { shoutId, shoutInput, publish })
.toPromise()
2023-04-20 13:58:56 +00:00
console.debug('[updateArticle]:', response.data)
2023-05-05 20:05:50 +00:00
return response.data.updateShout.shout
2023-04-20 13:58:56 +00:00
},
2023-05-08 17:21:06 +00:00
deleteShout: async ({ shoutId }: { shoutId: number }): Promise<void> => {
const response = await privateGraphQLClient.mutation(deleteShout, { shoutId }).toPromise()
console.debug('[deleteShout]:', response)
2023-05-05 20:05:50 +00:00
},
getDrafts: async (): Promise<Shout[]> => {
const response = await privateGraphQLClient.query(draftsLoad, {}).toPromise()
console.debug('[getDrafts]:', response)
return response.data.loadDrafts
2023-05-01 18:32:32 +00:00
},
createReaction: async (input: ReactionInput) => {
const response = await privateGraphQLClient.mutation(reactionCreate, { reaction: input }).toPromise()
console.debug('[createReaction]:', response)
return response.data.createReaction.reaction
2022-09-09 11:53:35 +00:00
},
destroyReaction: async (id: number) => {
2023-02-07 12:48:45 +00:00
const response = await privateGraphQLClient.mutation(reactionDestroy, { id: id }).toPromise()
console.debug('[destroyReaction]:', response)
2023-02-17 09:21:02 +00:00
return response.data.deleteReaction.reaction
2022-09-09 11:53:35 +00:00
},
2023-02-07 12:48:45 +00:00
updateReaction: async (id: number, input: ReactionInput) => {
const response = await privateGraphQLClient
.mutation(reactionUpdate, { id: id, reaction: input })
.toPromise()
console.debug('[updateReaction]:', response)
return response.data.updateReaction.reaction
2022-09-14 11:28:43 +00:00
},
2022-11-24 06:52:31 +00:00
getAuthorsBy: async (options: QueryLoadAuthorsByArgs) => {
const resp = await publicGraphQLClient.query(authorsLoadBy, options).toPromise()
2022-11-18 02:23:04 +00:00
return resp.data.loadAuthorsBy
2022-11-13 09:25:31 +00:00
},
2023-05-08 17:21:06 +00:00
getShoutBySlug: async (slug: string) => {
2022-11-18 02:23:04 +00:00
const resp = await publicGraphQLClient
.query(shoutLoad, {
slug,
2022-11-18 02:23:04 +00:00
})
.toPromise()
2023-05-08 17:21:06 +00:00
2023-11-04 13:24:14 +00:00
// if (resp.error) {
// console.error(resp)
// }
2023-05-08 17:21:06 +00:00
2022-11-18 02:23:04 +00:00
return resp.data.loadShout
2022-11-13 09:25:31 +00:00
},
2023-05-08 17:21:06 +00:00
getShoutById: async (shoutId: number) => {
const resp = await publicGraphQLClient
.query(shoutLoad, {
shoutId,
2023-05-08 17:21:06 +00:00
})
.toPromise()
if (resp.error) {
console.error(resp)
}
return resp.data.loadShout
},
2022-11-18 02:23:04 +00:00
getShouts: async (options: LoadShoutsOptions) => {
2023-02-17 09:21:02 +00:00
const resp = await publicGraphQLClient.query(shoutsLoadBy, { options }).toPromise()
if (resp.error) {
console.error(resp)
}
2022-11-18 02:23:04 +00:00
return resp.data.loadShouts
},
getRandomTopShouts: async (params: LoadRandomTopShoutsParams): Promise<Shout[]> => {
const resp = await publicGraphQLClient.query(articlesLoadRandomTop, { params }).toPromise()
if (resp.error) {
console.error(resp)
}
return resp.data.loadRandomTopShouts
},
getUnratedShouts: async (limit: number): Promise<Shout[]> => {
const resp = await publicGraphQLClient.query(articlesLoadUnrated, { limit }).toPromise()
if (resp.error) {
console.error(resp)
}
return resp.data.loadUnratedShouts
},
getMyFeed: async (options: LoadShoutsOptions) => {
const resp = await privateGraphQLClient.query(myFeed, { options }).toPromise()
if (resp.error) {
console.error(resp)
}
return resp.data.myFeed
},
2023-02-17 09:21:02 +00:00
getReactionsBy: async ({ by, limit }: { by: ReactionBy; limit?: number }) => {
const resp = await publicGraphQLClient
.query(reactionsLoadBy, { by, limit: limit ?? 1000, offset: 0 })
.toPromise()
2022-11-15 14:24:50 +00:00
return resp.data.loadReactionsBy
},
getNotifications: async (params: NotificationsQueryParams): Promise<NotificationsQueryResult> => {
const resp = await privateGraphQLClient.query(notifications, { params }).toPromise()
return resp.data.loadNotifications
},
markNotificationAsRead: async (notificationId: number): Promise<void> => {
await privateGraphQLClient
.mutation(markNotificationAsRead, {
notificationId,
})
.toPromise()
},
2022-11-16 06:33:58 +00:00
markAllNotificationsAsRead: async (): Promise<void> => {
await privateGraphQLClient.mutation(markAllNotificationsAsRead, {}).toPromise()
},
getMySubscriptions: async (): Promise<MySubscriptionsQueryResult> => {
const resp = await privateGraphQLClient.query(mySubscriptions, {}).toPromise()
// console.debug(resp.data)
return resp.data.loadMySubscriptions
},
2022-11-16 06:33:58 +00:00
// inbox
2022-12-17 03:27:00 +00:00
getChats: async (options: QueryLoadChatsArgs): Promise<Chat[]> => {
2022-11-24 06:52:31 +00:00
const resp = await privateGraphQLClient.query(myChats, options).toPromise()
2022-11-27 07:10:04 +00:00
return resp.data.loadChats.chats
2022-11-16 06:33:58 +00:00
},
2022-11-25 07:36:45 +00:00
createChat: async (options: MutationCreateChatArgs) => {
const resp = await privateGraphQLClient.mutation(createChat, options).toPromise()
return resp.data.createChat
},
createMessage: async (options: MutationCreateMessageArgs) => {
const resp = await privateGraphQLClient.mutation(createMessage, options).toPromise()
return resp.data.createMessage.message
2022-11-25 07:36:45 +00:00
},
2022-11-24 06:52:31 +00:00
getChatMessages: async (options: QueryLoadMessagesByArgs) => {
const resp = await privateGraphQLClient.query(chatMessagesLoadBy, options).toPromise()
return resp.data.loadMessagesBy.messages
2022-11-24 09:06:48 +00:00
},
2022-11-24 12:58:07 +00:00
getRecipients: async (options: QueryLoadRecipientsArgs) => {
const resp = await privateGraphQLClient.query(loadRecipients, options).toPromise()
return resp.data.loadRecipients.members
},
2022-09-09 11:53:35 +00:00
}