2022-11-15 15:27:02 +00:00
|
|
|
import type { FollowingEntity, AuthResult, ShoutInput, Topic, Author } 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-15 12:15:07 +00:00
|
|
|
import createChatQuery 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-09-09 11:53:35 +00:00
|
|
|
|
2022-09-14 11:05:48 +00:00
|
|
|
const FEED_SIZE = 50
|
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-01 22:25:18 +00:00
|
|
|
authSendLink: async ({ email, lang }) => {
|
2022-09-17 18:23:30 +00:00
|
|
|
// send link with code on email
|
2022-11-01 22:25:18 +00:00
|
|
|
const response = await publicGraphQLClient.mutation(authSendLinkMutation, { email, lang }).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 }) => {
|
|
|
|
const response = await privateGraphQLClient.query(followMutation, { what, slug }).toPromise()
|
|
|
|
return response.data.follow
|
|
|
|
},
|
|
|
|
unfollow: async ({ what, slug }: { what: FollowingEntity; slug: string }) => {
|
|
|
|
const response = await privateGraphQLClient.query(unfollowMutation, { what, slug }).toPromise()
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.data?.refreshSession?.error) {
|
|
|
|
throw new ApiError('unknown', response.data.refreshSession.error)
|
|
|
|
}
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
return response.data.refreshSession
|
|
|
|
},
|
|
|
|
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
|
|
|
|
},
|
|
|
|
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
|
|
|
|
},
|
|
|
|
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-15 09:45:55 +00:00
|
|
|
createChat: async ({ title, members }) => {
|
2022-11-15 12:15:07 +00:00
|
|
|
return await privateGraphQLClient
|
|
|
|
.mutation(createChatQuery, { title: title, members: members })
|
|
|
|
.toPromise()
|
2022-11-15 09:45:55 +00:00
|
|
|
},
|
|
|
|
|
2022-11-02 13:44:00 +00:00
|
|
|
getChats: async (payload = {}) => {
|
2022-10-09 05:08:38 +00:00
|
|
|
const resp = await privateGraphQLClient.query(myChats, payload).toPromise()
|
|
|
|
return resp.data.myChats
|
2022-11-12 18:14:20 +00:00
|
|
|
},
|
2022-11-15 14:24:50 +00:00
|
|
|
loadAuthorsBy: async ({ by, amount = 50, offset = 0 }) => {
|
|
|
|
const resp = await publicGraphQLClient.query(authorsLoadBy, { by, amount, offset }).toPromise()
|
|
|
|
return resp.data.loadShoutsBy
|
2022-11-13 09:25:31 +00:00
|
|
|
},
|
2022-11-15 14:24:50 +00:00
|
|
|
loadShoutsBy: async ({ by, amount = 50, offset = 0 }) => {
|
2022-11-15 12:05:56 +00:00
|
|
|
const resp = await publicGraphQLClient.query(shoutsLoadBy, { by, amount, offset }).toPromise()
|
2022-11-15 14:24:50 +00:00
|
|
|
return resp.data.loadShoutsBy
|
2022-11-13 09:25:31 +00:00
|
|
|
},
|
2022-11-15 14:24:50 +00:00
|
|
|
loadReactionsBy: async ({ by, amount = REACTIONS_AMOUNT_PER_PAGE, offset = 0 }) => {
|
|
|
|
const resp = await publicGraphQLClient.query(reactionsLoadBy, { by, amount, offset }).toPromise()
|
|
|
|
return resp.data.loadReactionsBy
|
2022-11-14 09:48:26 +00:00
|
|
|
},
|
2022-11-02 13:44:00 +00:00
|
|
|
getChatMessages: async ({
|
2022-11-15 12:05:56 +00:00
|
|
|
chat,
|
|
|
|
amount = 50,
|
|
|
|
offset = 0
|
2022-11-02 13:44:00 +00:00
|
|
|
}: {
|
2022-11-15 12:05:56 +00:00
|
|
|
chat: string
|
2022-11-02 13:44:00 +00:00
|
|
|
amount?: number
|
2022-11-15 12:05:56 +00:00
|
|
|
offset?: number
|
2022-11-02 13:44:00 +00:00
|
|
|
}) => {
|
2022-11-15 12:05:56 +00:00
|
|
|
const by = {
|
|
|
|
chat
|
|
|
|
}
|
2022-11-15 15:27:02 +00:00
|
|
|
const resp = await privateGraphQLClient.query(chatMessagesLoadBy, { by, offset, amount }).toPromise()
|
2022-11-02 13:44:00 +00:00
|
|
|
return resp.data.loadChat
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
}
|