Replace occurrences of fetch() with axios

environments/review-polyfills-wo6d05/deployments/2423
Alex Gleason 2 years ago
parent f244bd3ce1
commit ce01b93a70
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -1,4 +1,5 @@
import { useQuery, useQueryClient } from '@tanstack/react-query'; import { useQuery, useQueryClient } from '@tanstack/react-query';
import axios from 'axios';
import React, { useState, useEffect, useRef } from 'react'; import React, { useState, useEffect, useRef } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
@ -24,9 +25,9 @@ const Ad: React.FC<IAd> = ({ ad }) => {
// Fetch the impression URL (if any) upon displaying the ad. // Fetch the impression URL (if any) upon displaying the ad.
// Don't fetch it more than once. // Don't fetch it more than once.
useQuery(['ads', 'impression', ad.impression], () => { useQuery(['ads', 'impression', ad.impression], async () => {
if (ad.impression) { if (ad.impression) {
return fetch(ad.impression); return await axios.get(ad.impression);
} }
}, { cacheTime: Infinity, staleTime: Infinity }); }, { cacheTime: Infinity, staleTime: Infinity });

@ -1,3 +1,5 @@
import axios from 'axios';
import { getSettings } from 'soapbox/actions/settings'; import { getSettings } from 'soapbox/actions/settings';
import { getSoapboxConfig } from 'soapbox/actions/soapbox'; import { getSoapboxConfig } from 'soapbox/actions/soapbox';
import { normalizeAd, normalizeCard } from 'soapbox/normalizers'; import { normalizeAd, normalizeCard } from 'soapbox/normalizers';
@ -28,14 +30,13 @@ const RumbleAdProvider: AdProvider = {
const endpoint = soapboxConfig.extensions.getIn(['ads', 'endpoint']) as string | undefined; const endpoint = soapboxConfig.extensions.getIn(['ads', 'endpoint']) as string | undefined;
if (endpoint) { if (endpoint) {
const response = await fetch(endpoint, { try {
const { data } = await axios.get<RumbleApiResponse>(endpoint, {
headers: { headers: {
'Accept-Language': settings.get('locale', '*') as string, 'Accept-Language': settings.get('locale', '*') as string,
}, },
}); });
if (response.ok) {
const data = await response.json() as RumbleApiResponse;
return data.ads.map(item => normalizeAd({ return data.ads.map(item => normalizeAd({
impression: item.impression, impression: item.impression,
card: normalizeCard({ card: normalizeCard({
@ -45,6 +46,8 @@ const RumbleAdProvider: AdProvider = {
}), }),
expires_at: new Date(item.expires * 1000), expires_at: new Date(item.expires * 1000),
})); }));
} catch (e) {
// do nothing
} }
} }

@ -1,3 +1,5 @@
import axios from 'axios';
import { getSettings } from 'soapbox/actions/settings'; import { getSettings } from 'soapbox/actions/settings';
import { normalizeCard } from 'soapbox/normalizers'; import { normalizeCard } from 'soapbox/normalizers';
@ -18,18 +20,19 @@ const TruthAdProvider: AdProvider = {
const state = getState(); const state = getState();
const settings = getSettings(state); const settings = getSettings(state);
const response = await fetch('/api/v2/truth/ads?device=desktop', { try {
const { data } = await axios.get<TruthAd[]>('/api/v2/truth/ads?device=desktop', {
headers: { headers: {
'Accept-Language': settings.get('locale', '*') as string, 'Accept-Language': settings.get('locale', '*') as string,
}, },
}); });
if (response.ok) {
const data = await response.json() as TruthAd[];
return data.map(item => ({ return data.map(item => ({
...item, ...item,
card: normalizeCard(item.card), card: normalizeCard(item.card),
})); }));
} catch (e) {
// do nothing
} }
return []; return [];

@ -1,3 +1,4 @@
/* eslint-disable compat/compat */
import IntlMessageFormat from 'intl-messageformat'; import IntlMessageFormat from 'intl-messageformat';
import 'intl-pluralrules'; import 'intl-pluralrules';
import unescape from 'lodash/unescape'; import unescape from 'lodash/unescape';

Loading…
Cancel
Save