Skip to content

Commit

Permalink
Refactor: use alternative headers declaration to increase compatibili…
Browse files Browse the repository at this point in the history
…ty (closes #37)
  • Loading branch information
mateussouzaweb committed Feb 10, 2024
1 parent aa07953 commit 3a7eafe
Showing 1 changed file with 100 additions and 77 deletions.
177 changes: 100 additions & 77 deletions src/scripts/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const encode = (data: Data) => {
* @param headers
* @returns
*/
const request = async (method: string, endpoint: string, body: any, headers: Headers) => {
const request = async (method: string, endpoint: string, body: any, headers: HeadersInit) => {

const proxyUrl = document.body.dataset.proxyUrl
const proxyEncode = document.body.dataset.proxyEncode
Expand Down Expand Up @@ -85,10 +85,10 @@ const makeLogin = async (username: string, password: string) => {
scope: 'offline_access',
})


const headers = new Headers()
headers.append('Content-Type', 'application/x-www-form-urlencoded')
headers.append('Authorization', 'Basic ' + authToken);
const headers = {
'Authorization': 'Basic ' + authToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const result = await request('POST', '/auth/v1/token', data, headers)
return result
Expand All @@ -107,9 +107,10 @@ const refreshLogin = async (refreshToken: string) => {
scope: 'offline_access',
})

const headers = new Headers()
headers.append('Authorization', 'Basic ' + authToken);
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Basic ' + authToken,
'Content-Type': 'application/x-www-form-urlencoded',
}

const result = await request('POST', '/auth/v1/token', data, headers)
return result
Expand All @@ -122,9 +123,10 @@ const refreshLogin = async (refreshToken: string) => {
*/
const getCookies = async (accessToken: string) => {

const headers = new Headers();
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const result = await request('GET', '/index/v2', null, headers)
return result
Expand All @@ -137,9 +139,10 @@ const getCookies = async (accessToken: string) => {
*/
const getProfile = async (accessToken: string) => {

const headers = new Headers();
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const result = await request('GET', '/accounts/v1/me/profile', null, headers)
return result
Expand All @@ -153,9 +156,10 @@ const getProfile = async (accessToken: string) => {
*/
const updateProfile = async (accessToken: string, data: Data) => {

const headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/json')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
}

const body = JSON.stringify(data)
const result = await request('PATCH', '/accounts/v1/me/profile', body, headers)
Expand All @@ -177,9 +181,10 @@ const updateProfile = async (accessToken: string, data: Data) => {
*/
const homeFeed = async (accessToken: string, accountId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/discover/' + accountId + '/home_feed?' + params
Expand Down Expand Up @@ -209,9 +214,10 @@ const homeFeed = async (accessToken: string, accountId: string, filters: Data) =
*/
const browser = async (accessToken: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/discover/browse?' + params
Expand All @@ -235,9 +241,10 @@ const browser = async (accessToken: string, filters: Data) => {
*/
const search = async (accessToken: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/discover/search?' + params
Expand All @@ -261,9 +268,10 @@ const search = async (accessToken: string, filters: Data) => {
*/
const recommendations = async (accessToken: string, accountId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/discover/' + accountId + '/recommendations?' + params
Expand All @@ -289,9 +297,10 @@ const recommendations = async (accessToken: string, accountId: string, filters:
*/
const watchlist = async (accessToken: string, accountId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/discover/' + accountId + '/watchlist?' + params
Expand All @@ -315,9 +324,10 @@ const watchlist = async (accessToken: string, accountId: string, filters: Data)
*/
const inWatchlist = async (accessToken: string, accountId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/' + accountId + '/watchlist?' + params
Expand All @@ -340,9 +350,10 @@ const inWatchlist = async (accessToken: string, accountId: string, filters: Data
*/
const addToWatchlist = async (accessToken: string, accountId: string, contentId: string, filters: Data) => {

const headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/json')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
}

const body = JSON.stringify({
content_id: contentId
Expand All @@ -368,9 +379,10 @@ const addToWatchlist = async (accessToken: string, accountId: string, contentId:
*/
const removeFromWatchlist = async (accessToken: string, accountId: string, contentId: string, filters: Data) => {

const headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/json')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
}

const params = encode(filters)
const result = await request('DELETE', '/content/v2/' + accountId + '/watchlist/' + contentId + '?' + params, null, headers)
Expand All @@ -394,9 +406,10 @@ const removeFromWatchlist = async (accessToken: string, accountId: string, conte
*/
const history = async (accessToken: string, accountId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/' + accountId + '/watch-history?' + params
Expand All @@ -418,9 +431,10 @@ const history = async (accessToken: string, accountId: string, filters: Data) =>
*/
const upNext = async (accessToken: string, contentId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/discover/up_next/' + contentId + '?' + params
Expand All @@ -442,9 +456,10 @@ const upNext = async (accessToken: string, contentId: string, filters: Data) =>
*/
const previousEpisode = async (accessToken: string, contentId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/discover/previous_episode/' + contentId + '?' + params
Expand All @@ -467,9 +482,10 @@ const previousEpisode = async (accessToken: string, contentId: string, filters:
*/
const playHeads = async (accessToken: string, accountId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/' + accountId + '/playheads?' + params
Expand All @@ -492,9 +508,10 @@ const playHeads = async (accessToken: string, accountId: string, filters: Data)
*/
const setProgress = async (accessToken: string, accountId: string, filters: Data, data: Data) => {

const headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/json')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
}

const params = encode(filters)
const body = JSON.stringify(data)
Expand All @@ -515,9 +532,10 @@ const setProgress = async (accessToken: string, accountId: string, filters: Data
*/
const categories = async (accessToken: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v1/tenant_categories?' + params
Expand All @@ -539,9 +557,10 @@ const categories = async (accessToken: string, filters: Data) => {
*/
const serie = async (accessToken: string, serieId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/cms/series/' + serieId + '?' + params
Expand All @@ -567,9 +586,10 @@ const serie = async (accessToken: string, serieId: string, filters: Data) => {
*/
const seasons = async (accessToken: string, bucket: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/cms/v2' + bucket + '/seasons?' + params
Expand All @@ -595,9 +615,10 @@ const seasons = async (accessToken: string, bucket: string, filters: Data) => {
*/
const episodes = async (accessToken: string, bucket: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/cms/v2' + bucket + '/episodes?' + params
Expand All @@ -619,9 +640,10 @@ const episodes = async (accessToken: string, bucket: string, filters: Data) => {
*/
const episode = async (accessToken: string, contentId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/content/v2/cms/objects/' + contentId + '?' + params
Expand All @@ -647,9 +669,10 @@ const episode = async (accessToken: string, contentId: string, filters: Data) =>
*/
const streams = async (accessToken: string, bucket: string, contentId: string, filters: Data) => {

var headers = new Headers()
headers.append('Authorization', 'Bearer ' + accessToken)
headers.append('Content-Type', 'application/x-www-form-urlencoded')
const headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/x-www-form-urlencoded'
}

const params = encode(filters)
const endpoint = '/cms/v2' + bucket + '/videos/' + contentId + '/streams?' + params
Expand All @@ -664,7 +687,7 @@ const streams = async (accessToken: string, bucket: string, contentId: string, f
*/
const textLanguages = async () => {

const headers = new Headers()
const headers = {}
const endpoint = staticUrl + '/config/i18n/v3/timed_text_languages.json'
const result = await request('GET', endpoint, null, headers)

Expand All @@ -677,7 +700,7 @@ const textLanguages = async () => {
*/
const audioLanguages = async () => {

const headers = new Headers()
const headers = {}
const endpoint = staticUrl + '/config/i18n/v3/audio_languages.json'
const result = await request('GET', endpoint, null, headers)

Expand All @@ -690,7 +713,7 @@ const audioLanguages = async () => {
*/
const intro = async (contentId: string) => {

const headers = new Headers()
const headers = {}
const endpoint = staticUrl + '/datalab-intro-v2/' + contentId + '.json'
const result = await request('GET', endpoint, null, headers)

Expand Down

0 comments on commit 3a7eafe

Please sign in to comment.