From 3a7eafed7bb99cade90a051dbb7f6cfffc7d1dac Mon Sep 17 00:00:00 2001 From: Mateus Souza Date: Sat, 10 Feb 2024 09:34:13 -0300 Subject: [PATCH] Refactor: use alternative headers declaration to increase compatibility (closes #37) --- src/scripts/api.ts | 177 +++++++++++++++++++++++++-------------------- 1 file changed, 100 insertions(+), 77 deletions(-) diff --git a/src/scripts/api.ts b/src/scripts/api.ts index 866c011..342fd0c 100644 --- a/src/scripts/api.ts +++ b/src/scripts/api.ts @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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) @@ -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)