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,
|
|
|
|
User,
|
|
|
|
ProfileInput
|
2022-11-18 02:23:04 +00:00
|
|
|
} from '../graphql/types.gen'
|
2022-09-09 11:53:35 +00:00
|
|
|
import { publicGraphQLClient } from '../graphql/publicGraphQLClient'
|
2022-11-14 01:17:12 +00:00
|
|
|
import { getToken, privateGraphQLClient } from '../graphql/privateGraphQLClient'
|
2022-09-09 11:53:35 +00:00
|
|
|
import topicsAll from '../graphql/query/topics-all'
|
|
|
|
import mySession from '../graphql/mutation/my-session'
|
2022-09-17 18:23:30 +00:00
|
|
|
import authLogoutQuery from '../graphql/mutation/auth-logout'
|
|
|
|
import authLoginQuery from '../graphql/query/auth-login'
|
|
|
|
import authRegisterMutation from '../graphql/mutation/auth-register'
|
|
|
|
import authCheckEmailQuery from '../graphql/query/auth-check-email'
|
2022-10-21 18:17:04 +00:00
|
|
|
import authConfirmEmailMutation from '../graphql/mutation/auth-confirm-email'
|
2022-09-17 18:23:30 +00:00
|
|
|
import authSendLinkMutation from '../graphql/mutation/auth-send-link'
|
2022-09-09 11:53:35 +00:00
|
|
|
import followMutation from '../graphql/mutation/follow'
|
|
|
|
import unfollowMutation from '../graphql/mutation/unfollow'
|
|
|
|
import topicsRandomQuery from '../graphql/query/topics-random'
|
|
|
|
import authorsAll from '../graphql/query/authors-all'
|
|
|
|
import reactionCreate from '../graphql/mutation/reaction-create'
|
|
|
|
import reactionDestroy from '../graphql/mutation/reaction-destroy'
|
|
|
|
import reactionUpdate from '../graphql/mutation/reaction-update'
|
2022-11-09 17:57:35 +00:00
|
|
|
import createArticle from '../graphql/mutation/article-create'
|
2022-11-15 12:05:56 +00:00
|
|
|
import myChats from '../graphql/query/chats-load'
|
2022-11-15 15:27:02 +00:00
|
|
|
import chatMessagesLoadBy from '../graphql/query/chat-messages-load-by'
|
2022-11-14 17:41:05 +00:00
|
|
|
import authorBySlug from '../graphql/query/author-by-slug'
|
2022-11-13 19:35:57 +00:00
|
|
|
import topicBySlug from '../graphql/query/topic-by-slug'
|
2022-11-23 04:12:11 +00:00
|
|
|
import createChat from '../graphql/mutation/create-chat'
|
2022-11-15 15:27:02 +00:00
|
|
|
import reactionsLoadBy from '../graphql/query/reactions-load-by'
|
2022-11-15 14:24:50 +00:00
|
|
|
import { REACTIONS_AMOUNT_PER_PAGE } from '../stores/zine/reactions'
|
2022-11-15 15:27:02 +00:00
|
|
|
import authorsLoadBy from '../graphql/query/authors-load-by'
|
|
|
|
import shoutsLoadBy from '../graphql/query/articles-load-by'
|
2022-11-27 11:00:44 +00:00
|
|
|
import shoutLoad from '../graphql/query/article-load'
|
2022-11-24 09:06:48 +00:00
|
|
|
import loadRecipients from '../graphql/query/chat-recipients'
|
2022-11-25 07:36:45 +00:00
|
|
|
import createMessage from '../graphql/mutation/create-chat-message'
|
2022-12-01 17:16:14 +00:00
|
|
|
import updateProfile from '../graphql/mutation/update-profile'
|
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'
|
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 = {
|
2022-11-13 19:35:57 +00:00
|
|
|
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') {
|
|
|
|
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
|
|
|
|
}: {
|
|
|
|
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()
|
2022-11-13 19:35:57 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-13 19:35:57 +00:00
|
|
|
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 }) => {
|
2022-12-01 18:45:35 +00:00
|
|
|
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 }) => {
|
2022-12-01 18:45:35 +00:00
|
|
|
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
|
|
|
|
},
|
2022-11-13 19:35:57 +00:00
|
|
|
getAuthor: async ({ slug }: { slug: string }): Promise<Author> => {
|
|
|
|
const response = await publicGraphQLClient.query(authorBySlug, { slug }).toPromise()
|
|
|
|
return response.data.getAuthor
|
|
|
|
},
|
2022-12-01 18:07:27 +00:00
|
|
|
updateProfile: async (input: ProfileInput) => {
|
|
|
|
const response = await privateGraphQLClient.mutation(updateProfile, { profile: input }).toPromise()
|
2022-12-07 08:37:40 +00:00
|
|
|
console.log('!!! response.data.getAuthor:', response.data.updateProfile)
|
|
|
|
return response.data.updateProfile
|
2022-12-01 17:16:14 +00:00
|
|
|
},
|
2022-11-13 19:35:57 +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
|
|
|
},
|
2022-11-09 17:57:35 +00:00
|
|
|
createArticle: async ({ article }: { article: ShoutInput }) => {
|
|
|
|
const response = await privateGraphQLClient.mutation(createArticle, { shout: article }).toPromise()
|
|
|
|
console.debug('createArticle response:', response)
|
|
|
|
return response.data.createShout
|
|
|
|
},
|
2022-09-09 11:53:35 +00:00
|
|
|
createReaction: async ({ reaction }) => {
|
2022-09-09 14:08:17 +00:00
|
|
|
const response = await privateGraphQLClient.mutation(reactionCreate, { reaction }).toPromise()
|
2022-10-08 16:40:58 +00:00
|
|
|
console.debug('[api-client] [api] create reaction mutation called')
|
2022-09-09 11:53:35 +00:00
|
|
|
return response.data.createReaction
|
|
|
|
},
|
2022-11-16 06:33:58 +00:00
|
|
|
|
|
|
|
// CUDL
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
updateReaction: async ({ reaction }) => {
|
2022-09-09 14:08:17 +00:00
|
|
|
const response = await privateGraphQLClient.mutation(reactionUpdate, { reaction }).toPromise()
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
return response.data.createReaction
|
|
|
|
},
|
|
|
|
destroyReaction: async ({ id }) => {
|
2022-09-09 14:08:17 +00:00
|
|
|
const response = await privateGraphQLClient.mutation(reactionDestroy, { id }).toPromise()
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
return response.data.deleteReaction
|
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
|
|
|
},
|
2022-11-18 02:23:04 +00:00
|
|
|
getShout: async (slug: string) => {
|
|
|
|
const resp = await publicGraphQLClient
|
|
|
|
.query(shoutLoad, {
|
|
|
|
slug
|
|
|
|
})
|
|
|
|
.toPromise()
|
2022-11-23 11:34:43 +00:00
|
|
|
if (resp.error) console.debug(resp)
|
2022-11-18 02:23:04 +00:00
|
|
|
return resp.data.loadShout
|
2022-11-13 09:25:31 +00:00
|
|
|
},
|
2022-11-18 02:23:04 +00:00
|
|
|
getShouts: async (options: LoadShoutsOptions) => {
|
|
|
|
const resp = await publicGraphQLClient
|
|
|
|
.query(shoutsLoadBy, {
|
|
|
|
options
|
|
|
|
})
|
|
|
|
.toPromise()
|
2022-11-23 11:34:43 +00:00
|
|
|
if (resp.error) console.debug(resp)
|
2022-11-18 02:23:04 +00:00
|
|
|
return resp.data.loadShouts
|
|
|
|
},
|
|
|
|
getReactionsBy: async ({ by, limit = REACTIONS_AMOUNT_PER_PAGE, offset = 0 }) => {
|
2022-11-15 16:50:15 +00:00
|
|
|
const resp = await publicGraphQLClient.query(reactionsLoadBy, { by, limit, offset }).toPromise()
|
2022-11-29 11:01:23 +00:00
|
|
|
console.debug(resp)
|
2022-11-15 14:24:50 +00:00
|
|
|
return resp.data.loadReactionsBy
|
2022-11-14 09:48:26 +00:00
|
|
|
},
|
2022-11-16 06:33:58 +00:00
|
|
|
|
|
|
|
// inbox
|
2022-11-24 06:52:31 +00:00
|
|
|
getChats: async (options: QueryLoadChatsArgs) => {
|
|
|
|
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
|
|
|
|
},
|
|
|
|
|
2022-11-24 06:52:31 +00:00
|
|
|
getChatMessages: async (options: QueryLoadMessagesByArgs) => {
|
|
|
|
const resp = await privateGraphQLClient.query(chatMessagesLoadBy, options).toPromise()
|
2022-11-02 13:44:00 +00:00
|
|
|
return resp.data.loadChat
|
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
|
|
|
}
|
|
|
|
}
|