Skip to content

Commit

Permalink
Replace deprecated /users/follows endpoint with /channels/followed (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
alsolovyev authored May 25, 2024
2 parents 3dd3f2e + 41d51ba commit 6cc4257
Show file tree
Hide file tree
Showing 5 changed files with 373 additions and 379 deletions.
12 changes: 5 additions & 7 deletions src/constants/fake.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ITwitchLiveStream,
ITwitchSearchedChannel,
ITwitchUser,
ITwitchUserFollowsFromTo,
ITwitchUserChannelFollow,
ITwitchVideo
} from '../services/twitch.service'

Expand Down Expand Up @@ -44,12 +44,10 @@ export const fakeTwitchLiveStream: ITwitchLiveStream = {
viewer_count: 78365
}

export const fakeTwitchUserFollowsFromTo: ITwitchUserFollowsFromTo = {
from_id: '171003792',
from_login: 'iiisutha067iii',
from_name: 'IIIsutha067III',
to_id: '23161357',
to_name: 'LIRIK',
export const fakeTwitchUserFollowsFromTo: ITwitchUserChannelFollow = {
broadcaster_id: '23161357',
broadcaster_login: 'LIRIK',
broadcaster_name: 'LIRIK',
followed_at: '2017-08-22T22:55:24Z'
}

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFollowedStreams.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('Hook: useLiveFollowedStreams', () => {
expect(getUserFollowsSpy).toBeCalledTimes(1)
expect(getUserFollowsSpy).toBeCalledWith(fakeTwitchUser.id)
expect(getUsersSpy).toBeCalledTimes(1)
expect(getUsersSpy).toBeCalledWith([fakeTwitchUserFollowsFromTo.to_id])
expect(getUsersSpy).toBeCalledWith([fakeTwitchUserFollowsFromTo.broadcaster_id])
})

it('should return an error', async () => {
Expand Down
99 changes: 49 additions & 50 deletions src/hooks/useFollowedStreams.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
import { IApiServiceError } from '../services/api.service'
import { useEffect, useState } from 'react'
import twitchService, { ITwitchUserFollowsFromTo } from '../services/twitch.service'
import type { ITwitchError, ITwitchLiveStream, ITwitchUser } from '../services/twitch.service'


/**
* A React hook to get information about streams belonging to channels that the authenticated user follows.
*
* @param isOfflineHidden - Specifies whether to show channels that are currently offline.
* @param minViewCount - The minimum number of views to display a channel.
* @returns an array of three elements: error, isLoading and an array of live stream.
*/
export default (
isOfflineHidden: boolean = false,
minViewCount: number = 1e4
): [ITwitchError | IApiServiceError | undefined, boolean, Array<ITwitchLiveStream>, Array<ITwitchUser>] => {
const [error, setError] = useState<ITwitchError | IApiServiceError>()
const [isLoading, setIsLoading] = useState<boolean>(true)
const [liveStreams, setLiveStreams] = useState<Array<ITwitchLiveStream>>([])
const [offlineStreams, setOfflineStreams] = useState<Array<ITwitchUser>>([])

useEffect(() => {
const getStreams = async () => {
const authUser: ITwitchUser = await twitchService.getAuthUser()
const liveStreams: Array<ITwitchLiveStream> = await twitchService.getLiveFollowedStreams(authUser.id)

setLiveStreams(liveStreams)

if (isOfflineHidden) return

const followedUsers: Array<ITwitchUserFollowsFromTo> = await twitchService.getUserFollows(authUser.id)
const offlineFollowedUsers: Array<ITwitchUserFollowsFromTo> = followedUsers.filter(
({ to_id }) => !liveStreams.some(({ user_id }) => user_id === to_id)
)
const offlineStreams: Array<ITwitchUser> = await twitchService.getUsers(
offlineFollowedUsers.map(({ to_id }) => to_id)
)
const filteredOfflineStreams: Array<ITwitchUser> = offlineStreams.filter(
({ view_count }) => Number(view_count) > minViewCount
)

setOfflineStreams(filteredOfflineStreams)
}

getStreams().catch(setError).finally(() => setIsLoading(false))
}, [])

return [error, isLoading, liveStreams, offlineStreams]
}
import { IApiServiceError } from '../services/api.service'
import { useEffect, useState } from 'react'
import twitchService, { ITwitchUserChannelFollow } from '../services/twitch.service'
import type { ITwitchError, ITwitchLiveStream, ITwitchUser } from '../services/twitch.service'


/**
* A React hook to get information about streams belonging to channels that the authenticated user follows.
*
* @param isOfflineHidden - Specifies whether to show channels that are currently offline.
* @param minViewCount - The minimum number of views to display a channel.
* @returns an array of three elements: error, isLoading and an array of live stream.
*/
export default (
isOfflineHidden: boolean = false
): [ITwitchError | IApiServiceError | undefined, boolean, Array<ITwitchLiveStream>, Array<ITwitchUser>] => {
const [error, setError] = useState<ITwitchError | IApiServiceError>()
const [isLoading, setIsLoading] = useState<boolean>(true)
const [liveStreams, setLiveStreams] = useState<Array<ITwitchLiveStream>>([])
const [offlineStreams, setOfflineStreams] = useState<Array<ITwitchUser>>([])

useEffect(() => {
const getStreams = async () => {
const authUser: ITwitchUser = await twitchService.getAuthUser()
const liveStreams: Array<ITwitchLiveStream> = await twitchService.getLiveFollowedStreams(authUser.id)

setLiveStreams(liveStreams)

if (isOfflineHidden) return

const followedUsers: Array<ITwitchUserChannelFollow> = await twitchService.getUserFollows(authUser.id)
const offlineFollowedUsers: Array<ITwitchUserChannelFollow> = followedUsers.filter(
({ broadcaster_id }) => !liveStreams.some(({ user_id }) => user_id === broadcaster_id)
)

const offlineStreams: Array<ITwitchUser> = await twitchService.getUsers(
offlineFollowedUsers.map(({ broadcaster_id }) => broadcaster_id)
)

setOfflineStreams(offlineStreams)
}

getStreams()
.catch(setError)
.finally(() => setIsLoading(false))
}, [])

return [error, isLoading, liveStreams, offlineStreams]
}
6 changes: 3 additions & 3 deletions src/services/twitch.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ describe('Twitch Service', () => {
describe('getUserFollows', () => {
it('should return a list of users', async () => {
nock(TwitchResources.host)
.get(`${TwitchResources.follows}?from_id=${fakeTwitchUser.id}`)
.get(`${TwitchResources.follows}?user_id=${fakeTwitchUser.id}`)
.reply(200, { data: [fakeTwitchUserFollowsFromTo, fakeTwitchUserFollowsFromTo] })
await expect(twitchService.getUserFollows(fakeTwitchUser.id as string)).resolves.toStrictEqual([
fakeTwitchUserFollowsFromTo,
Expand All @@ -131,10 +131,10 @@ describe('Twitch Service', () => {
})

it('should return an error if the request fails', async () => {
nock(TwitchResources.host).get(`${TwitchResources.follows}?from_id=${fakeTwitchUser.id}`).reply(401, fakeTwitchError)
nock(TwitchResources.host).get(`${TwitchResources.follows}?user_id=${fakeTwitchUser.id}`).reply(401, fakeTwitchError)
await expect(twitchService.getUserFollows(fakeTwitchUser.id as string)).rejects.toStrictEqual(fakeTwitchError)

nock(TwitchResources.host).get(`${TwitchResources.follows}?from_id=${fakeTwitchUser.id}`).reply(200, '')
nock(TwitchResources.host).get(`${TwitchResources.follows}?user_id=${fakeTwitchUser.id}`).reply(200, '')
await expect(twitchService.getUserFollows(fakeTwitchUser.id as string)).rejects.toStrictEqual(
API_SERVICE_PARSE_ERROR
)
Expand Down
Loading

0 comments on commit 6cc4257

Please sign in to comment.