revert: replace axios with fetch

This reverts commit 82f3f6ae0c via PR #41944.
pull/42526/head
Mrugesh Mohapatra 2021-06-16 16:44:32 +05:30
parent e402464304
commit 418287f1e8
No known key found for this signature in database
GPG Key ID: 68BDF41E23F50DD8
9 changed files with 36 additions and 72 deletions

View File

@ -7392,15 +7392,6 @@
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz",
"integrity": "sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ==" "integrity": "sha512-1Yj8h9Q+QDF5FzhMs/c9+6UntbD5MkRfRwac8DoEm9ZfUBZ7tZ55YcGVAzEe4bXsdQHEk+s9S5wsOKVdZrw0tQ=="
}, },
"bindings": {
"version": "1.5.0",
"resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz",
"integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==",
"optional": true,
"requires": {
"file-uri-to-path": "1.0.0"
}
},
"bl": { "bl": {
"version": "4.1.0", "version": "4.1.0",
"resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz",
@ -11337,12 +11328,6 @@
"token-types": "^2.0.0" "token-types": "^2.0.0"
} }
}, },
"file-uri-to-path": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz",
"integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==",
"optional": true
},
"filesize": { "filesize": {
"version": "6.1.0", "version": "6.1.0",
"resolved": "https://registry.npmjs.org/filesize/-/filesize-6.1.0.tgz", "resolved": "https://registry.npmjs.org/filesize/-/filesize-6.1.0.tgz",
@ -16713,12 +16698,6 @@
"resolved": "https://registry.npmjs.org/name-all-modules-plugin/-/name-all-modules-plugin-1.0.1.tgz", "resolved": "https://registry.npmjs.org/name-all-modules-plugin/-/name-all-modules-plugin-1.0.1.tgz",
"integrity": "sha1-Cr+2rYNXGLn7Te8GdOBmV6lUN1w=" "integrity": "sha1-Cr+2rYNXGLn7Te8GdOBmV6lUN1w="
}, },
"nan": {
"version": "2.14.2",
"resolved": "https://registry.npmjs.org/nan/-/nan-2.14.2.tgz",
"integrity": "sha512-M2ufzIiINKCuDfBSAUr1vWQ+vuVcA9kqx8JJUsbQi6yf1uGRyb7HfpdfUr5qLXf3B/t8dPvcjhKMmlfnP47EzQ==",
"optional": true
},
"nanoid": { "nanoid": {
"version": "3.1.23", "version": "3.1.23",
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.23.tgz", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.1.23.tgz",
@ -22746,11 +22725,7 @@
"version": "1.2.13", "version": "1.2.13",
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-1.2.13.tgz",
"integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==", "integrity": "sha512-oWb1Z6mkHIskLzEJ/XWX0srkpkTQ7vaopMQkyaEIoq0fmtFVxOthb8cCxeT+p3ynTdkk/RZwbgG4brR5BeWECw==",
"optional": true, "optional": true
"requires": {
"bindings": "^1.5.0",
"nan": "^2.12.1"
}
}, },
"glob-parent": { "glob-parent": {
"version": "3.1.0", "version": "3.1.0",

View File

@ -55,6 +55,7 @@
"@reach/router": "1.3.4", "@reach/router": "1.3.4",
"algoliasearch": "4.9.3", "algoliasearch": "4.9.3",
"assert": "2.0.0", "assert": "2.0.0",
"axios": "0.21.1",
"babel-plugin-preval": "5.0.0", "babel-plugin-preval": "5.0.0",
"babel-plugin-prismjs": "2.0.1", "babel-plugin-prismjs": "2.0.1",
"bezier-easing": "2.1.0", "bezier-easing": "2.1.0",

View File

@ -8,7 +8,7 @@ import { putUserAcceptsTerms } from '../utils/ajax';
function* acceptTermsSaga({ payload: quincyEmails }) { function* acceptTermsSaga({ payload: quincyEmails }) {
try { try {
const response = yield call(putUserAcceptsTerms, quincyEmails); const { data: response } = yield call(putUserAcceptsTerms, quincyEmails);
yield put(acceptTermsComplete(quincyEmails)); yield put(acceptTermsComplete(quincyEmails));
yield put(createFlashMessage(response)); yield put(createFlashMessage(response));

View File

@ -16,9 +16,7 @@ function* fetchSessionUser() {
} }
try { try {
const { const {
user = {}, data: { user = {}, result = '', sessionMeta = {} }
result = '',
sessionMeta = {}
} = yield call(getSessionUser); } = yield call(getSessionUser);
const appUser = user[result] || {}; const appUser = user[result] || {};
yield put( yield put(
@ -32,11 +30,9 @@ function* fetchSessionUser() {
function* fetchOtherUser({ payload: maybeUser = '' }) { function* fetchOtherUser({ payload: maybeUser = '' }) {
try { try {
const maybeUserLC = maybeUser.toLowerCase(); const maybeUserLC = maybeUser.toLowerCase();
const { data } = yield call(getUserProfile, maybeUserLC);
const { entities: { user = {} } = {}, result = '' } = yield call( const { entities: { user = {} } = {}, result = '' } = data;
getUserProfile,
maybeUserLC
);
const otherUser = user[result] || {}; const otherUser = user[result] || {};
yield put( yield put(
fetchProfileForUserComplete({ user: otherUser, username: result }) fetchProfileForUserComplete({ user: otherUser, username: result })

View File

@ -8,7 +8,7 @@ import { postReportUser } from '../utils/ajax';
function* reportUserSaga({ payload }) { function* reportUserSaga({ payload }) {
try { try {
const response = yield call(postReportUser, payload); const { data: response } = yield call(postReportUser, payload);
yield put(reportUserComplete()); yield put(reportUserComplete());
yield put(createFlashMessage(response)); yield put(createFlashMessage(response));

View File

@ -26,7 +26,7 @@ import { createFlashMessage } from '../../components/Flash/redux';
function* submitNewAboutSaga({ payload }) { function* submitNewAboutSaga({ payload }) {
try { try {
const response = yield call(putUpdateMyAbout, payload); const { data: response } = yield call(putUpdateMyAbout, payload);
yield put(submitNewAboutComplete({ ...response, payload })); yield put(submitNewAboutComplete({ ...response, payload }));
yield put(createFlashMessage(response)); yield put(createFlashMessage(response));
} catch (e) { } catch (e) {
@ -36,7 +36,7 @@ function* submitNewAboutSaga({ payload }) {
function* submitNewUsernameSaga({ payload: username }) { function* submitNewUsernameSaga({ payload: username }) {
try { try {
const response = yield call(putUpdateMyUsername, username); const { data: response } = yield call(putUpdateMyUsername, username);
yield put(submitNewUsernameComplete({ ...response, username })); yield put(submitNewUsernameComplete({ ...response, username }));
yield put(createFlashMessage(response)); yield put(createFlashMessage(response));
} catch (e) { } catch (e) {
@ -46,7 +46,7 @@ function* submitNewUsernameSaga({ payload: username }) {
function* submitProfileUISaga({ payload }) { function* submitProfileUISaga({ payload }) {
try { try {
const response = yield call(putUpdateMyProfileUI, payload); const { data: response } = yield call(putUpdateMyProfileUI, payload);
yield put(submitProfileUIComplete({ ...response, payload })); yield put(submitProfileUIComplete({ ...response, payload }));
yield put(createFlashMessage(response)); yield put(createFlashMessage(response));
} catch (e) { } catch (e) {
@ -56,7 +56,7 @@ function* submitProfileUISaga({ payload }) {
function* updateUserFlagSaga({ payload: update }) { function* updateUserFlagSaga({ payload: update }) {
try { try {
const response = yield call(putUpdateUserFlag, update); const { data: response } = yield call(putUpdateUserFlag, update);
yield put(updateUserFlagComplete({ ...response, payload: update })); yield put(updateUserFlagComplete({ ...response, payload: update }));
yield put(createFlashMessage(response)); yield put(createFlashMessage(response));
} catch (e) { } catch (e) {
@ -67,7 +67,9 @@ function* updateUserFlagSaga({ payload: update }) {
function* validateUsernameSaga({ payload }) { function* validateUsernameSaga({ payload }) {
try { try {
yield delay(500); yield delay(500);
const { exists } = yield call(getUsernameExists, payload); const {
data: { exists }
} = yield call(getUsernameExists, payload);
yield put(validateUsernameComplete(exists)); yield put(validateUsernameComplete(exists));
} catch (e) { } catch (e) {
yield put(validateUsernameError(e)); yield put(validateUsernameError(e));
@ -76,10 +78,9 @@ function* validateUsernameSaga({ payload }) {
function* verifyCertificationSaga({ payload }) { function* verifyCertificationSaga({ payload }) {
try { try {
const { response, isCertMap, completedChallenges } = yield call( const {
putVerifyCert, data: { response, isCertMap, completedChallenges }
payload } = yield call(putVerifyCert, payload);
);
yield put( yield put(
verifyCertComplete({ verifyCertComplete({
...response, ...response,

View File

@ -13,7 +13,7 @@ function* updateMyEmailSaga({ payload: email = '' }) {
return; return;
} }
try { try {
const response = yield call(putUserUpdateEmail, email); const { data: response } = yield call(putUserUpdateEmail, email);
yield put( yield put(
updateMyEmailComplete({ updateMyEmailComplete({
...response, ...response,

View File

@ -7,7 +7,7 @@ import { showCertComplete, showCertError } from '.';
function* getShowCertSaga({ payload: { username, certSlug } }) { function* getShowCertSaga({ payload: { username, certSlug } }) {
try { try {
const response = yield call(getShowCert, username, certSlug); const { data: response } = yield call(getShowCert, username, certSlug);
const { messages } = response; const { messages } = response;
if (messages && messages.length) { if (messages && messages.length) {
for (let i = 0; i < messages.length; i++) { for (let i = 0; i < messages.length; i++) {

View File

@ -1,52 +1,41 @@
import envData from '../../../config/env.json'; import envData from '../../../config/env.json';
import axios from 'axios';
import Tokens from 'csrf'; import Tokens from 'csrf';
import cookies from 'browser-cookies'; import cookies from 'browser-cookies';
const { apiLocation, environment } = envData; const { apiLocation } = envData;
const base = apiLocation; const base = apiLocation;
const tokens = new Tokens(); const tokens = new Tokens();
// TODO: test on staging. Do we need 'include' everywhere? axios.defaults.withCredentials = true;
const defaultOptions = {
credentials: environment === 'development' ? 'include' : 'same-site'
};
// _csrf is passed to the client as a cookie. Tokens are sent back to the server // _csrf is passed to the client as a cookie. Tokens are sent back to the server
// via headers: // via headers:
function getCSRFToken() { function setCSRFTokens() {
const _csrf = typeof window !== 'undefined' && cookies.get('_csrf'); const _csrf = typeof window !== 'undefined' && cookies.get('_csrf');
if (!_csrf) { if (!_csrf) return;
return ''; axios.defaults.headers.post['CSRF-Token'] = tokens.create(_csrf);
} else { axios.defaults.headers.put['CSRF-Token'] = tokens.create(_csrf);
return tokens.create(_csrf);
}
} }
function get(path) { function get(path) {
return fetch(`${base}${path}`, defaultOptions).then(res => res.json()); return axios.get(`${base}${path}`);
} }
export function post(path, body) { export function post(path, body) {
return request('POST', path, body); setCSRFTokens();
return axios.post(`${base}${path}`, body);
} }
function put(path, body) { function put(path, body) {
return request('PUT', path, body); setCSRFTokens();
return axios.put(`${base}${path}`, body);
} }
function request(method, path, body) { // function del(path) {
const options = { // return axios.delete(`${base}${path}`);
...defaultOptions, // }
method,
headers: {
'CSRF-Token': getCSRFToken(),
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
};
return fetch(`${base}${path}`, options).then(res => res.json());
}
/** GET **/ /** GET **/
@ -117,3 +106,5 @@ export function putUserUpdateEmail(email) {
export function putVerifyCert(certSlug) { export function putVerifyCert(certSlug) {
return put('/certificate/verify', { certSlug }); return put('/certificate/verify', { certSlug });
} }
/** DELETE **/