-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from StackOverflowIsBetterThanAnyAI/16-add-bac…
…kend-for-deployment migrate api logic to external backend on vercel
- Loading branch information
Showing
12 changed files
with
138 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,3 @@ | ||
# dependencies | ||
/node_modules | ||
|
||
# production | ||
/build | ||
|
||
# api | ||
/src/clientdata |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,46 @@ | ||
import axios from 'axios' | ||
import { AuthorizationProps } from '../types/AuthorizationProps' | ||
import { getTwitchAuthorization } from './getTwitchAuthorization' | ||
|
||
import type { StreamProps } from '../types/StreamProps' | ||
|
||
export const getStreams = async ( | ||
CLIENT_ID: string, | ||
CLIENT_SECRET: string, | ||
url: string | ||
): Promise<StreamProps | undefined | { error: 'login' }> => { | ||
const authorizationObject: AuthorizationProps = | ||
await getTwitchAuthorization(CLIENT_ID, CLIENT_SECRET) | ||
await getTwitchAuthorization() | ||
let { access_token, token_type } = authorizationObject | ||
if (access_token === '' && token_type === '') return { error: 'login' } | ||
if (!access_token || !token_type) return { error: 'login' } | ||
|
||
token_type = | ||
token_type.substring(0, 1).toUpperCase() + | ||
token_type.substring(1, token_type.length) | ||
|
||
const authorization = `${token_type} ${access_token}` | ||
try { | ||
const response = await axios.post( | ||
'https://twitch-backend.vercel.app/api/streams', | ||
{ | ||
access_token, | ||
token_type, | ||
url, | ||
} | ||
) | ||
|
||
const headers = { | ||
authorization, | ||
'Client-ID': CLIENT_ID, | ||
} | ||
if (response.status !== 200 || typeof response.data !== 'object') { | ||
throw new Error(`${response.status}`) | ||
} | ||
|
||
try { | ||
const response = await fetch(url, { | ||
headers, | ||
method: 'GET', | ||
}) | ||
if (!response.ok) throw new Error(`${response.status} ${response.url}`) | ||
const data: StreamProps = await response.json() | ||
if (!data.data.length) | ||
if (!response.data.data) | ||
throw new Error( | ||
`There are no Livestreams available under this url: ${response.url}` | ||
`There are no Livestreams available under this url: ${url}` | ||
) | ||
return data | ||
|
||
return response.data | ||
} catch (error: any) { | ||
console.error( | ||
'The following error occured while fetching the current Livestreams:', | ||
error | ||
) | ||
} | ||
|
||
return undefined | ||
} |
Oops, something went wrong.