Skip to content

Commit

Permalink
remark js client updates + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akellbl4 committed Nov 12, 2023
1 parent 636c85f commit a41ec55
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
11 changes: 6 additions & 5 deletions frontend/packages/api/src/clients/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ export function createAdminClient({ site, baseUrl }: ClientParams) {
}

async function toggleCommenting(url: string, ro: 0 | 1): Promise<void> {
return fetcher.put('/readonly', { query: { url, ro } })
await fetcher.put('/readonly', { query: { url, ro } })
}

async function toggleUserBlock(id: string, ttl?: BlockTTL): Promise<BlockUserResponse> {
const query = ttl ? { block: 1, ttl: ttl === 'permanently' ? 0 : ttl } : { block: 0 }
const data = await fetcher.put<BlockUserResponse>(`/user/${id}`, { query })

return fetcher.put<BlockUserResponse>(`/user/${id}`, { query })
return data
}

/**
* Request list of blocked users
*/
async function getBlockedUsers(): Promise<User[]> {
return fetcher.get<User[]>('/blocked')
const users = await fetcher.get<User[]>('/blocked')
return users
}

/**
Expand Down Expand Up @@ -110,8 +112,7 @@ export function createAdminClient({ site, baseUrl }: ClientParams) {
* @param url page URL
*/
async function disableCommenting(url: string): Promise<void> {
const x = toggleCommenting(url, 0)
return x
await toggleCommenting(url, 0)
}

return {
Expand Down
18 changes: 18 additions & 0 deletions frontend/packages/api/src/clients/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ import { createFetcher } from '../lib/fetcher'
export function createAuthClient({ site, baseUrl }: ClientParams) {
const fetcher = createFetcher(site, `${baseUrl}/auth`)

/**
* Authenticate user as anonymous
* @param username
* @returns authorized user
*/
async function anonymous(username: string): Promise<User> {
const user = await fetcher.get<User>('/anonymous/login', { user: username, aud: site })

return user
}

/**
* Authenticate user by email
* @param email
* @param username
* @returns authorized user
*/
async function email(email: string, username: string): Promise<(token: string) => Promise<User>> {
const EMAIL_SIGNIN_ENDPOINT = '/email/login'

Expand All @@ -22,6 +33,10 @@ export function createAuthClient({ site, baseUrl }: ClientParams) {
}
}

/**
* Authenticate user by telegram
* @returns telegram auth data
*/
async function telegram() {
const TELEGRAM_SIGNIN_ENDPOINT = '/telegram/login'

Expand All @@ -38,6 +53,9 @@ export function createAuthClient({ site, baseUrl }: ClientParams) {
}
}

/**
* Logout user
*/
async function logout(): Promise<void> {
await fetcher.get('/logout')
}
Expand Down
41 changes: 35 additions & 6 deletions frontend/packages/api/src/clients/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,31 @@ export type Vote = -1 | 1
export function createPublicClient({ site, baseUrl }: ClientParams) {
const fetcher = createFetcher(site, `${baseUrl}${API_BASE}`)

/** Get server config */
/**
* Get server config
* @returns server config
*/
async function getConfig(): Promise<Config> {
const config = await fetcher.get<Config>('/config')

return config
}

/** Get current authorized user */
/**
* Get current authorized user
* @returns authorized user or null if not authorized
*/
async function getUser(): Promise<User | null> {
const user = await fetcher.get<User | null>('/user').catch(() => null)

return user
}

/** Get comments */
/**
* Get comments
* @param params url or params
* @returns comments
*/
async function getComments<T extends string | GetUserCommentsParams>(
params: T,
): Promise<T extends string ? CommentsTree : Comment[]> {
Expand All @@ -111,6 +121,9 @@ export function createPublicClient({ site, baseUrl }: ClientParams) {

/**
* Add new comment
* @param url page url
* @param payload comment payload
* @returns added comment
*/
async function addComment(url: string, payload: CommentPayload): Promise<Comment> {
const comment = await fetcher.post<Comment>('/comment', {
Expand All @@ -119,18 +132,34 @@ export function createPublicClient({ site, baseUrl }: ClientParams) {
return comment
}

/** Update comment */
/**
* Update comment
* @param url page url
* @param id comment id
* @param text comment text
* @returns updated comment
*/
async function updateComment(url: string, id: string, text: string): Promise<Comment> {
return fetcher.put(`/comment/${id}`, { query: { url }, payload: { text } })
}

/** Remove comment on a page */
/**
* Remove comment on a page
* @param url page url
* @param id comment id
*/
async function removeComment(url: string, id: string): Promise<void> {
await fetcher.put(`/comment/${id}`, { query: { url }, payload: { delete: true } })
}

type VotePayload = { url: string; vote: Vote }
/** Vote for a comment */
/**
* Vote for a comment
* @param url page url
* @param id comment id
* @param vote vote value
* @returns vote payload
*/
async function vote(url: string, id: string, vote: Vote): Promise<VotePayload> {
const result = await fetcher.put<VotePayload>(`/vote/${id}`, { query: { url, vote } })
return result
Expand Down

0 comments on commit a41ec55

Please sign in to comment.