2022-09-09 11:53:35 +00:00
|
|
|
import type { Reaction, Shout, FollowingEntity } from '../graphql/types.gen'
|
|
|
|
|
|
|
|
import { publicGraphQLClient } from '../graphql/publicGraphQLClient'
|
|
|
|
import articleBySlug from '../graphql/query/article-by-slug'
|
|
|
|
import articleReactions from '../graphql/query/article-reactions'
|
|
|
|
import articlesRecentAll from '../graphql/query/articles-recent-all'
|
|
|
|
import articlesRecentPublished from '../graphql/query/articles-recent-published'
|
|
|
|
import topicsAll from '../graphql/query/topics-all'
|
|
|
|
import { getLogger } from './logger'
|
|
|
|
import reactionsForShouts from '../graphql/query/reactions-for-shouts'
|
|
|
|
import mySession from '../graphql/mutation/my-session'
|
|
|
|
import { privateGraphQLClient } from '../graphql/privateGraphQLClient'
|
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'
|
|
|
|
import authConfirmCodeMutation from '../graphql/mutation/auth-confirm-email'
|
|
|
|
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 articlesForAuthors from '../graphql/query/articles-for-authors'
|
|
|
|
import articlesForTopics from '../graphql/query/articles-for-topics'
|
|
|
|
import searchResults from '../graphql/query/search-results'
|
|
|
|
import topicsRandomQuery from '../graphql/query/topics-random'
|
|
|
|
import articlesTopMonth from '../graphql/query/articles-top-month'
|
|
|
|
import articlesTopRated from '../graphql/query/articles-top-rated'
|
|
|
|
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-09-13 09:59:04 +00:00
|
|
|
import authorsBySlugs from '../graphql/query/authors-by-slugs'
|
2022-09-14 11:28:43 +00:00
|
|
|
import incrementView from '../graphql/mutation/increment-view'
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
const log = getLogger('api-client')
|
2022-09-13 09:59:04 +00:00
|
|
|
|
2022-09-14 11:05:48 +00:00
|
|
|
const FEED_SIZE = 50
|
|
|
|
const REACTIONS_PAGE_SIZE = 100
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
export const apiClient = {
|
2022-09-17 18:23:30 +00:00
|
|
|
// auth
|
|
|
|
|
|
|
|
authLogin: async ({ email, password }) => {
|
|
|
|
const response = await publicGraphQLClient.query(authLoginQuery, { email, password }).toPromise()
|
|
|
|
return response.data.signIn
|
|
|
|
},
|
|
|
|
authRegiser: async ({ email, password }) => {
|
|
|
|
const response = await publicGraphQLClient.query(authRegisterMutation, { email, password }).toPromise()
|
|
|
|
return response.data.registerUser
|
|
|
|
},
|
|
|
|
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
|
|
|
|
},
|
|
|
|
authSendLink: async ({ email }) => {
|
|
|
|
// send link with code on email
|
|
|
|
const response = await publicGraphQLClient.query(authSendLinkMutation, { email }).toPromise()
|
|
|
|
return response.data.reset
|
|
|
|
},
|
|
|
|
authConfirmCode: async ({ code }) => {
|
|
|
|
// confirm email with code from link
|
|
|
|
const response = await publicGraphQLClient.query(authConfirmCodeMutation, { code }).toPromise()
|
|
|
|
return response.data.reset
|
|
|
|
},
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
getTopArticles: async () => {
|
2022-09-14 11:05:48 +00:00
|
|
|
const response = await publicGraphQLClient.query(articlesTopRated, { limit: 10, offset: 0 }).toPromise()
|
2022-09-09 11:53:35 +00:00
|
|
|
return response.data.topOverall
|
|
|
|
},
|
|
|
|
getTopMonthArticles: async () => {
|
2022-09-14 11:05:48 +00:00
|
|
|
const response = await publicGraphQLClient.query(articlesTopMonth, { limit: 10, offset: 0 }).toPromise()
|
2022-09-13 08:05:11 +00:00
|
|
|
return response.data.topMonth
|
2022-09-09 11:53:35 +00:00
|
|
|
},
|
2022-09-13 09:59:04 +00:00
|
|
|
getRecentPublishedArticles: async ({
|
2022-09-14 11:05:48 +00:00
|
|
|
limit = FEED_SIZE,
|
|
|
|
offset = 0
|
2022-09-13 09:59:04 +00:00
|
|
|
}: {
|
2022-09-14 11:05:48 +00:00
|
|
|
limit?: number
|
|
|
|
offset?: number
|
2022-09-13 09:59:04 +00:00
|
|
|
}) => {
|
2022-09-14 11:05:48 +00:00
|
|
|
const response = await publicGraphQLClient.query(articlesRecentPublished, { limit, offset }).toPromise()
|
2022-09-13 09:59:04 +00:00
|
|
|
|
|
|
|
return response.data.recentPublished
|
|
|
|
},
|
2022-09-22 09:37:49 +00:00
|
|
|
getRandomTopics: async ({ amount }: { amount: number }) => {
|
|
|
|
log.debug('getRandomTopics')
|
|
|
|
const response = await publicGraphQLClient.query(topicsRandomQuery, { amount }).toPromise()
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
return response.data.topicsRandom
|
|
|
|
},
|
|
|
|
getSearchResults: async ({
|
|
|
|
query,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit = FEED_SIZE,
|
|
|
|
offset = 0
|
2022-09-09 11:53:35 +00:00
|
|
|
}: {
|
|
|
|
query: string
|
2022-09-14 11:05:48 +00:00
|
|
|
limit: number
|
2022-09-22 09:37:49 +00:00
|
|
|
offset?: number
|
2022-09-09 11:53:35 +00:00
|
|
|
}): Promise<Shout[]> => {
|
|
|
|
const response = await publicGraphQLClient
|
|
|
|
.query(searchResults, {
|
|
|
|
q: query,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit,
|
|
|
|
offset
|
2022-09-09 11:53:35 +00:00
|
|
|
})
|
|
|
|
.toPromise()
|
|
|
|
|
2022-09-14 11:28:43 +00:00
|
|
|
return response.data?.searchQuery || []
|
2022-09-09 11:53:35 +00:00
|
|
|
},
|
2022-09-13 09:59:04 +00:00
|
|
|
getRecentArticles: async ({
|
2022-09-14 11:05:48 +00:00
|
|
|
limit = FEED_SIZE,
|
|
|
|
offset = 0
|
2022-09-09 12:18:09 +00:00
|
|
|
}: {
|
2022-09-14 11:05:48 +00:00
|
|
|
limit: number
|
2022-09-22 09:37:49 +00:00
|
|
|
offset?: number
|
2022-09-09 12:18:09 +00:00
|
|
|
}): Promise<Shout[]> => {
|
|
|
|
const response = await publicGraphQLClient
|
2022-09-13 09:59:04 +00:00
|
|
|
.query(articlesRecentAll, {
|
2022-09-14 11:05:48 +00:00
|
|
|
limit,
|
|
|
|
offset
|
2022-09-09 12:18:09 +00:00
|
|
|
})
|
|
|
|
.toPromise()
|
|
|
|
|
2022-09-13 09:59:04 +00:00
|
|
|
return response.data.recentAll
|
2022-09-09 12:18:09 +00:00
|
|
|
},
|
2022-09-09 11:53:35 +00:00
|
|
|
getArticlesForTopics: async ({
|
|
|
|
topicSlugs,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit = FEED_SIZE,
|
|
|
|
offset = 0
|
2022-09-09 11:53:35 +00:00
|
|
|
}: {
|
|
|
|
topicSlugs: string[]
|
2022-09-14 11:05:48 +00:00
|
|
|
limit: number
|
2022-09-22 09:37:49 +00:00
|
|
|
offset?: number
|
2022-09-09 11:53:35 +00:00
|
|
|
}): Promise<Shout[]> => {
|
|
|
|
const response = await publicGraphQLClient
|
|
|
|
.query(articlesForTopics, {
|
|
|
|
slugs: topicSlugs,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit,
|
|
|
|
offset
|
2022-09-09 11:53:35 +00:00
|
|
|
})
|
|
|
|
.toPromise()
|
|
|
|
|
|
|
|
return response.data.shoutsByTopics
|
|
|
|
},
|
|
|
|
getArticlesForAuthors: async ({
|
|
|
|
authorSlugs,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit = FEED_SIZE,
|
|
|
|
offset = 0
|
2022-09-09 11:53:35 +00:00
|
|
|
}: {
|
|
|
|
authorSlugs: string[]
|
2022-09-14 11:05:48 +00:00
|
|
|
limit: number
|
2022-09-22 09:37:49 +00:00
|
|
|
offset?: number
|
2022-09-09 11:53:35 +00:00
|
|
|
}): Promise<Shout[]> => {
|
|
|
|
const response = await publicGraphQLClient
|
|
|
|
.query(articlesForAuthors, {
|
|
|
|
slugs: authorSlugs,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit,
|
|
|
|
offset
|
2022-09-09 11:53:35 +00:00
|
|
|
})
|
|
|
|
.toPromise()
|
|
|
|
|
|
|
|
return response.data.shoutsByAuthors
|
|
|
|
},
|
|
|
|
|
|
|
|
// 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
|
|
|
|
},
|
|
|
|
|
|
|
|
getSession: async () => {
|
|
|
|
// renew session with auth token in header (!)
|
|
|
|
const response = await privateGraphQLClient.mutation(mySession, {}).toPromise()
|
|
|
|
return response.data.refreshSession
|
|
|
|
},
|
2022-09-14 11:05:48 +00:00
|
|
|
getPublishedArticles: async ({ limit = FEED_SIZE, offset }: { limit?: number; offset?: number }) => {
|
|
|
|
const response = await publicGraphQLClient.query(articlesRecentPublished, { limit, offset }).toPromise()
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2022-09-13 09:59:04 +00:00
|
|
|
return response.data.recentPublished
|
|
|
|
},
|
2022-09-09 11:53:35 +00:00
|
|
|
getAllTopics: async () => {
|
|
|
|
const response = await publicGraphQLClient.query(topicsAll, {}).toPromise()
|
|
|
|
return response.data.topicsAll
|
|
|
|
},
|
|
|
|
|
|
|
|
getAllAuthors: async () => {
|
2022-09-14 11:05:48 +00:00
|
|
|
const response = await publicGraphQLClient.query(authorsAll, { limit: 9999, offset: 9999 }).toPromise()
|
2022-09-09 11:53:35 +00:00
|
|
|
return response.data.authorsAll
|
|
|
|
},
|
|
|
|
getArticle: async ({ slug }: { slug: string }): Promise<Shout> => {
|
|
|
|
const response = await publicGraphQLClient.query(articleBySlug, { slug }).toPromise()
|
|
|
|
return response.data?.getShoutBySlug
|
|
|
|
},
|
2022-09-09 12:18:09 +00:00
|
|
|
|
|
|
|
// reactions
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
getReactionsForShouts: async ({
|
|
|
|
shoutSlugs,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit = FEED_SIZE,
|
|
|
|
offset = 0
|
2022-09-09 11:53:35 +00:00
|
|
|
}: {
|
|
|
|
shoutSlugs: string[]
|
2022-09-14 11:05:48 +00:00
|
|
|
limit?: number
|
|
|
|
offset?: number
|
2022-09-09 11:53:35 +00:00
|
|
|
}): Promise<Reaction[]> => {
|
|
|
|
const response = await publicGraphQLClient
|
|
|
|
.query(reactionsForShouts, {
|
|
|
|
shouts: shoutSlugs,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit,
|
|
|
|
offset
|
2022-09-09 11:53:35 +00:00
|
|
|
})
|
|
|
|
.toPromise()
|
|
|
|
|
2022-09-20 08:26:12 +00:00
|
|
|
return response.data.reactionsForShouts
|
2022-09-09 11:53:35 +00:00
|
|
|
},
|
|
|
|
getArticleReactions: async ({
|
|
|
|
articleSlug,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit = REACTIONS_PAGE_SIZE,
|
|
|
|
offset = 0
|
2022-09-09 11:53:35 +00:00
|
|
|
}: {
|
|
|
|
articleSlug: string
|
2022-09-14 11:05:48 +00:00
|
|
|
limit: number
|
|
|
|
offset: number
|
2022-09-09 11:53:35 +00:00
|
|
|
}): Promise<Reaction[]> => {
|
|
|
|
const response = await publicGraphQLClient
|
|
|
|
.query(articleReactions, {
|
|
|
|
slug: articleSlug,
|
2022-09-14 11:05:48 +00:00
|
|
|
limit,
|
|
|
|
offset
|
2022-09-09 11:53:35 +00:00
|
|
|
})
|
|
|
|
.toPromise()
|
|
|
|
|
2022-09-17 18:23:30 +00:00
|
|
|
return response.data?.reactionsByShout
|
2022-09-09 11:53:35 +00:00
|
|
|
},
|
2022-09-13 09:59:04 +00:00
|
|
|
getAuthorsBySlugs: async ({ slugs }) => {
|
|
|
|
const response = await publicGraphQLClient.query(authorsBySlugs, { slugs }).toPromise()
|
|
|
|
return response.data.getUsersBySlugs
|
|
|
|
},
|
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-09-09 11:53:35 +00:00
|
|
|
log.debug('[api] create reaction mutation called')
|
|
|
|
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
|
|
|
},
|
|
|
|
incrementView: async ({ articleSlug }) => {
|
|
|
|
await privateGraphQLClient.mutation(incrementView, { shout: articleSlug })
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
}
|