Skip to content

Commit a225d28

Browse files
committed
Use apiClient instance instead of directly calling Axios
Reduces the amount of duplication and provides a single place where we can configure base URL, crendentials and other headers
1 parent 65aefcd commit a225d28

File tree

11 files changed

+80
-93
lines changed

11 files changed

+80
-93
lines changed

client/modules/IDE/actions/assets.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import axios from 'axios';
1+
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
33
import { startLoader, stopLoader } from './loader';
44

5-
const __process = (typeof global !== 'undefined' ? global : window).process;
6-
const ROOT_URL = __process.env.API_URL;
7-
85
function setAssets(assets, totalSize) {
96
return {
107
type: ActionTypes.SET_ASSETS,
@@ -16,7 +13,7 @@ function setAssets(assets, totalSize) {
1613
export function getAssets() {
1714
return (dispatch) => {
1815
dispatch(startLoader());
19-
axios.get(`${ROOT_URL}/S3/objects`, { withCredentials: true })
16+
apiClient.get('/S3/objects')
2017
.then((response) => {
2118
dispatch(setAssets(response.data.assets, response.data.totalSize));
2219
dispatch(stopLoader());
@@ -39,7 +36,7 @@ export function deleteAsset(assetKey) {
3936

4037
export function deleteAssetRequest(assetKey) {
4138
return (dispatch) => {
42-
axios.delete(`${ROOT_URL}/S3/${assetKey}`, { withCredentials: true })
39+
apiClient.delete(`/S3/${assetKey}`)
4340
.then((response) => {
4441
dispatch(deleteAsset(assetKey));
4542
})

client/modules/IDE/actions/collections.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import axios from 'axios';
21
import { browserHistory } from 'react-router';
2+
import apiClient from '../../../utils/apiClient';
33
import * as ActionTypes from '../../../constants';
44
import { startLoader, stopLoader } from './loader';
55
import { setToastText, showToast } from './toast';
66

7-
const __process = (typeof global !== 'undefined' ? global : window).process;
8-
const ROOT_URL = __process.env.API_URL;
97

108
const TOAST_DISPLAY_TIME_MS = 1500;
119

@@ -15,11 +13,11 @@ export function getCollections(username) {
1513
dispatch(startLoader());
1614
let url;
1715
if (username) {
18-
url = `${ROOT_URL}/${username}/collections`;
16+
url = `/${username}/collections`;
1917
} else {
20-
url = `${ROOT_URL}/collections`;
18+
url = '/collections';
2119
}
22-
axios.get(url, { withCredentials: true })
20+
apiClient.get(url)
2321
.then((response) => {
2422
dispatch({
2523
type: ActionTypes.SET_COLLECTIONS,
@@ -41,8 +39,8 @@ export function getCollections(username) {
4139
export function createCollection(collection) {
4240
return (dispatch) => {
4341
dispatch(startLoader());
44-
const url = `${ROOT_URL}/collections`;
45-
return axios.post(url, collection, { withCredentials: true })
42+
const url = '/collections';
43+
return apiClient.post(url, collection)
4644
.then((response) => {
4745
dispatch({
4846
type: ActionTypes.CREATE_COLLECTION
@@ -73,8 +71,8 @@ export function createCollection(collection) {
7371
export function addToCollection(collectionId, projectId) {
7472
return (dispatch) => {
7573
dispatch(startLoader());
76-
const url = `${ROOT_URL}/collections/${collectionId}/${projectId}`;
77-
return axios.post(url, { withCredentials: true })
74+
const url = `/collections/${collectionId}/${projectId}`;
75+
return apiClient.post(url)
7876
.then((response) => {
7977
dispatch({
8078
type: ActionTypes.ADD_TO_COLLECTION,
@@ -105,8 +103,8 @@ export function addToCollection(collectionId, projectId) {
105103
export function removeFromCollection(collectionId, projectId) {
106104
return (dispatch) => {
107105
dispatch(startLoader());
108-
const url = `${ROOT_URL}/collections/${collectionId}/${projectId}`;
109-
return axios.delete(url, { withCredentials: true })
106+
const url = `/collections/${collectionId}/${projectId}`;
107+
return apiClient.delete(url)
110108
.then((response) => {
111109
dispatch({
112110
type: ActionTypes.REMOVE_FROM_COLLECTION,
@@ -136,8 +134,8 @@ export function removeFromCollection(collectionId, projectId) {
136134

137135
export function editCollection(collectionId, { name, description }) {
138136
return (dispatch) => {
139-
const url = `${ROOT_URL}/collections/${collectionId}`;
140-
return axios.patch(url, { name, description }, { withCredentials: true })
137+
const url = `/collections/${collectionId}`;
138+
return apiClient.patch(url, { name, description })
141139
.then((response) => {
142140
dispatch({
143141
type: ActionTypes.EDIT_COLLECTION,
@@ -159,8 +157,8 @@ export function editCollection(collectionId, { name, description }) {
159157

160158
export function deleteCollection(collectionId) {
161159
return (dispatch) => {
162-
const url = `${ROOT_URL}/collections/${collectionId}`;
163-
return axios.delete(url, { withCredentials: true })
160+
const url = `/collections/${collectionId}`;
161+
return apiClient.delete(url)
164162
.then((response) => {
165163
dispatch({
166164
type: ActionTypes.DELETE_COLLECTION,

client/modules/IDE/actions/files.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import axios from 'axios';
21
import objectID from 'bson-objectid';
32
import blobUtil from 'blob-util';
43
import { reset } from 'redux-form';
4+
import apiClient from '../../../utils/apiClient';
55
import * as ActionTypes from '../../../constants';
66
import { setUnsavedChanges, closeNewFolderModal, closeNewFileModal } from './ide';
77
import { setProjectSavedTime } from './project';
88

9-
const __process = (typeof global !== 'undefined' ? global : window).process;
10-
const ROOT_URL = __process.env.API_URL;
119

1210
function appendToFilename(filename, string) {
1311
const dotIndex = filename.lastIndexOf('.');
@@ -50,7 +48,7 @@ export function createFile(formProps) {
5048
parentId,
5149
children: []
5250
};
53-
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
51+
apiClient.post(`/projects/${state.project.id}/files`, postParams)
5452
.then((response) => {
5553
dispatch({
5654
type: ActionTypes.CREATE_FILE,
@@ -106,7 +104,7 @@ export function createFolder(formProps) {
106104
parentId,
107105
fileType: 'folder'
108106
};
109-
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
107+
apiClient.post(`/projects/${state.project.id}/files`, postParams)
110108
.then((response) => {
111109
dispatch({
112110
type: ActionTypes.CREATE_FILE,
@@ -161,7 +159,7 @@ export function deleteFile(id, parentId) {
161159
parentId
162160
}
163161
};
164-
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
162+
apiClient.delete(`/projects/${state.project.id}/files/${id}`, deleteConfig)
165163
.then(() => {
166164
dispatch({
167165
type: ActionTypes.DELETE_FILE,

client/modules/IDE/actions/preferences.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import axios from 'axios';
1+
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
33

4-
const __process = (typeof global !== 'undefined' ? global : window).process;
5-
const ROOT_URL = __process.env.API_URL;
6-
74
function updatePreferences(formParams, dispatch) {
8-
axios.put(`${ROOT_URL}/preferences`, formParams, { withCredentials: true })
5+
apiClient.put('/preferences', formParams)
96
.then(() => {
107
})
118
.catch((error) => {

client/modules/IDE/actions/project.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { browserHistory } from 'react-router';
2-
import axios from 'axios';
32
import objectID from 'bson-objectid';
43
import each from 'async/each';
54
import isEqual from 'lodash/isEqual';
5+
import apiClient from '../../../utils/apiClient';
6+
import getConfig from '../../../utils/getConfig';
67
import * as ActionTypes from '../../../constants';
78
import { showToast, setToastText } from './toast';
89
import {
@@ -14,8 +15,7 @@ import {
1415
} from './ide';
1516
import { clearState, saveState } from '../../../persistState';
1617

17-
const __process = (typeof global !== 'undefined' ? global : window).process;
18-
const ROOT_URL = __process.env.API_URL;
18+
const ROOT_URL = getConfig('API_URL');
1919

2020
export function setProject(project) {
2121
return {
@@ -52,7 +52,7 @@ export function setNewProject(project) {
5252
export function getProject(id, username) {
5353
return (dispatch, getState) => {
5454
dispatch(justOpenedProject());
55-
axios.get(`${ROOT_URL}/${username}/projects/${id}`, { withCredentials: true })
55+
apiClient.get(`/${username}/projects/${id}`)
5656
.then((response) => {
5757
dispatch(setProject(response.data));
5858
dispatch(setUnsavedChanges(false));
@@ -142,7 +142,7 @@ export function saveProject(selectedFile = null, autosave = false) {
142142
fileToUpdate.content = selectedFile.content;
143143
}
144144
if (state.project.id) {
145-
return axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
145+
return apiClient.put(`/projects/${state.project.id}`, formParams)
146146
.then((response) => {
147147
dispatch(endSavingProject());
148148
dispatch(setUnsavedChanges(false));
@@ -177,7 +177,7 @@ export function saveProject(selectedFile = null, autosave = false) {
177177
});
178178
}
179179

180-
return axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
180+
return apiClient.post('/projects', formParams)
181181
.then((response) => {
182182
dispatch(endSavingProject());
183183
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
@@ -260,7 +260,7 @@ export function cloneProject(id) {
260260
if (!id) {
261261
resolve(getState());
262262
} else {
263-
fetch(`${ROOT_URL}/projects/${id}`)
263+
apiClient.get(`/projects/${id}`)
264264
.then(res => res.json())
265265
.then(data => resolve({
266266
files: data.files,
@@ -287,7 +287,7 @@ export function cloneProject(id) {
287287
const formParams = {
288288
url: file.url
289289
};
290-
axios.post(`${ROOT_URL}/S3/copy`, formParams, { withCredentials: true })
290+
apiClient.post('/S3/copy', formParams)
291291
.then((response) => {
292292
file.url = response.data.url;
293293
callback(null);
@@ -298,7 +298,7 @@ export function cloneProject(id) {
298298
}, (err) => {
299299
// if not errors in duplicating the files on S3, then duplicate it
300300
const formParams = Object.assign({}, { name: `${state.project.name} copy` }, { files: newFiles });
301-
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
301+
apiClient.post('/projects', formParams)
302302
.then((response) => {
303303
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
304304
dispatch(setNewProject(response.data));
@@ -337,7 +337,7 @@ export function setProjectSavedTime(updatedAt) {
337337
export function changeProjectName(id, newName) {
338338
return (dispatch, getState) => {
339339
const state = getState();
340-
axios.put(`${ROOT_URL}/projects/${id}`, { name: newName }, { withCredentials: true })
340+
apiClient.put(`/projects/${id}`, { name: newName })
341341
.then((response) => {
342342
if (response.status === 200) {
343343
dispatch({
@@ -364,7 +364,7 @@ export function changeProjectName(id, newName) {
364364

365365
export function deleteProject(id) {
366366
return (dispatch, getState) => {
367-
axios.delete(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
367+
apiClient.delete(`/projects/${id}`)
368368
.then(() => {
369369
const state = getState();
370370
if (id === state.project.id) {

client/modules/IDE/actions/projects.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
import axios from 'axios';
1+
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
33
import { startLoader, stopLoader } from './loader';
44

5-
const __process = (typeof global !== 'undefined' ? global : window).process;
6-
const ROOT_URL = __process.env.API_URL;
7-
85
// eslint-disable-next-line
96
export function getProjects(username) {
107
return (dispatch) => {
118
dispatch(startLoader());
129
let url;
1310
if (username) {
14-
url = `${ROOT_URL}/${username}/projects`;
11+
url = `/${username}/projects`;
1512
} else {
16-
url = `${ROOT_URL}/projects`;
13+
url = '/projects';
1714
}
18-
axios.get(url, { withCredentials: true })
15+
apiClient.get(url)
1916
.then((response) => {
2017
dispatch({
2118
type: ActionTypes.SET_PROJECTS,

client/modules/IDE/actions/uploader.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import axios from 'axios';
1+
import apiClient from '../../../utils/apiClient';
22
import getConfig from '../../../utils/getConfig';
33
import { createFile } from './files';
44
import { TEXT_FILE_REGEX } from '../../../../server/utils/fileUtils';
55

6-
const __process = (typeof global !== 'undefined' ? global : window).process;
76
const s3BucketHttps = getConfig('S3_BUCKET_URL_BASE') ||
87
`https://s3-${getConfig('AWS_REGION')}.amazonaws.com/${getConfig('S3_BUCKET')}/`;
9-
const ROOT_URL = __process.env.API_URL;
108
const MAX_LOCAL_FILE_SIZE = 80000; // bytes, aka 80 KB
119

1210
function localIntercept(file, options = {}) {
@@ -47,18 +45,13 @@ export function dropzoneAcceptCallback(userId, file, done) {
4745
});
4846
} else {
4947
file.postData = []; // eslint-disable-line
50-
axios.post(
51-
`${ROOT_URL}/S3/sign`, {
52-
name: file.name,
53-
type: file.type,
54-
size: file.size,
55-
userId
48+
apiClient.post('/S3/sign', {
49+
name: file.name,
50+
type: file.type,
51+
size: file.size,
52+
userId
5653
// _csrf: document.getElementById('__createPostToken').value
57-
},
58-
{
59-
withCredentials: true
60-
}
61-
)
54+
})
6255
.then((response) => {
6356
file.custom_status = 'ready'; // eslint-disable-line
6457
file.postData = response.data; // eslint-disable-line

0 commit comments

Comments
 (0)