Skip to content

Commit 9687be8

Browse files
authored
Merge pull request #1763 from SundeepChand/refactor/update-to-axios
Refactored all HTTP requests to use axios
2 parents 104a072 + 3a2b933 commit 9687be8

7 files changed

+157
-160
lines changed

Diff for: package-lock.json

+9-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

-2
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@
216216
"redux-devtools-dock-monitor": "^1.1.3",
217217
"redux-devtools-log-monitor": "^1.4.0",
218218
"redux-thunk": "^2.3.0",
219-
"request": "^2.88.2",
220-
"request-promise": "^4.2.5",
221219
"reselect": "^4.0.0",
222220
"s3-policy-v4": "0.0.3",
223221
"sass-extract": "^2.1.0",

Diff for: server/controllers/project.controller.js

+39-39
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import format from 'date-fns/format';
33
import isUrl from 'is-url';
44
import jsdom, { serializeDocument } from 'jsdom';
55
import isAfter from 'date-fns/isAfter';
6-
import request from 'request';
6+
import axios from 'axios';
77
import slugify from 'slugify';
88
import Project from '../models/project';
99
import User from '../models/user';
@@ -125,7 +125,7 @@ export function getProjectsForUserId(userId) {
125125
export function getProjectAsset(req, res) {
126126
Project.findById(req.params.project_id)
127127
.populate('user', 'username')
128-
.exec((err, project) => { // eslint-disable-line
128+
.exec(async (err, project) => { // eslint-disable-line
129129
if (err) {
130130
return res
131131
.status(404)
@@ -145,15 +145,15 @@ export function getProjectAsset(req, res) {
145145
if (!resolvedFile.url) {
146146
return res.send(resolvedFile.content);
147147
}
148-
request(
149-
{ method: 'GET', url: resolvedFile.url, encoding: null },
150-
(innerErr, response, body) => {
151-
if (innerErr) {
152-
return res.status(404).send({ message: 'Asset does not exist' });
153-
}
154-
return res.send(body);
155-
}
156-
);
148+
149+
try {
150+
const { data } = await axios.get(resolvedFile.url, {
151+
responseType: 'arraybuffer'
152+
});
153+
res.send(data);
154+
} catch (error) {
155+
res.status(404).send({ message: 'Asset does not exist' });
156+
}
157157
});
158158
}
159159

@@ -198,7 +198,7 @@ function bundleExternalLibs(project, zip, callback) {
198198
let numScriptsResolved = 0;
199199
let numScriptTags = 0;
200200

201-
function resolveScriptTagSrc(scriptTag, document) {
201+
async function resolveScriptTagSrc(scriptTag, document) {
202202
const path = scriptTag.src.split('/');
203203
const filename = path[path.length - 1];
204204
const { src } = scriptTag;
@@ -212,23 +212,21 @@ function bundleExternalLibs(project, zip, callback) {
212212
return;
213213
}
214214

215-
request(
216-
{ method: 'GET', url: src, encoding: null },
217-
(err, response, body) => {
218-
if (err) {
219-
console.log(err);
220-
} else {
221-
zip.append(body, { name: filename });
222-
scriptTag.src = filename;
223-
}
215+
try {
216+
const { data } = await axios.get(src, {
217+
responseType: 'arraybuffer'
218+
});
219+
zip.append(data, { name: filename });
220+
scriptTag.src = filename;
221+
} catch (err) {
222+
console.log(err);
223+
}
224224

225-
numScriptsResolved += 1;
226-
if (numScriptsResolved === numScriptTags) {
227-
indexHtml.content = serializeDocument(document);
228-
callback();
229-
}
230-
}
231-
);
225+
numScriptsResolved += 1;
226+
if (numScriptsResolved === numScriptTags) {
227+
indexHtml.content = serializeDocument(document);
228+
callback();
229+
}
232230
}
233231

234232
jsdom.env(indexHtml.content, (innerErr, window) => {
@@ -264,7 +262,7 @@ function buildZip(project, req, res) {
264262
);
265263
zip.pipe(res);
266264

267-
function addFileToZip(file, path) {
265+
async function addFileToZip(file, path) {
268266
if (file.fileType === 'folder') {
269267
const newPath = file.name === 'root' ? path : `${path}${file.name}/`;
270268
file.children.forEach((fileId) => {
@@ -274,16 +272,18 @@ function buildZip(project, req, res) {
274272
})();
275273
});
276274
} else if (file.url) {
277-
request(
278-
{ method: 'GET', url: file.url, encoding: null },
279-
(err, response, body) => {
280-
zip.append(body, { name: `${path}${file.name}` });
281-
numCompletedFiles += 1;
282-
if (numCompletedFiles === numFiles) {
283-
zip.finalize();
284-
}
285-
}
286-
);
275+
try {
276+
const { data } = await axios.get(file.url, {
277+
responseType: 'arraybuffer'
278+
});
279+
zip.append(data, { name: `${path}${file.name}` });
280+
} catch (err) {
281+
console.log(err);
282+
}
283+
numCompletedFiles += 1;
284+
if (numCompletedFiles === numFiles) {
285+
zip.finalize();
286+
}
287287
} else {
288288
zip.append(file.content, { name: `${path}${file.name}` });
289289
numCompletedFiles += 1;

Diff for: server/scripts/examples-gg-latest.js

+46-50
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import rp from 'request-promise';
1+
import axios from 'axios';
22
import Q from 'q';
33
import mongoose from 'mongoose';
44
import objectID from 'bson-objectid';
@@ -106,7 +106,7 @@ const insert = function insert(_mainString, _insString, _pos) {
106106
/* --- data processing --- */
107107
// 1. first get the top level directories P and M
108108
// https://api.github.com/repos/generative-design/Code-Package-p5.js/contents?ref=pre-release
109-
function getCodePackage() {
109+
async function getCodePackage() {
110110
const sketchRootList = [];
111111
const options = {
112112
// url: 'https://api.github.com/repos/generative-design/Code-Package-p5.js/contents',
@@ -118,34 +118,31 @@ function getCodePackage() {
118118
Authorization: `Basic ${Buffer.from(
119119
`${clientId}:${clientSecret}`
120120
).toString('base64')}`
121-
},
122-
json: true
121+
}
123122
};
124123

125-
return rp(options)
126-
.then((res) => {
127-
res.forEach((metadata) => {
128-
if (
129-
metadata.name.endsWith('P') === true ||
130-
metadata.name.endsWith('M') === true
131-
) {
132-
sketchRootList.push(metadata);
133-
}
134-
});
135-
136-
return sketchRootList;
137-
})
138-
.catch((err) => {
139-
throw err;
124+
try {
125+
const { data } = await axios.request(options);
126+
data.forEach((metadata) => {
127+
if (
128+
metadata.name.endsWith('P') === true ||
129+
metadata.name.endsWith('M') === true
130+
) {
131+
sketchRootList.push(metadata);
132+
}
140133
});
134+
return sketchRootList;
135+
} catch (err) {
136+
throw err;
137+
}
141138
}
142139

143140
// 2. get the list of all the top-level sketch directories in P and M
144141
function getSketchDirectories(sketchRootList) {
145142
// console.log(sketchRootList);
146143

147144
return Q.all(
148-
sketchRootList.map((sketches) => {
145+
sketchRootList.map(async (sketches) => {
149146
// console.log(sketches)
150147
const options = {
151148
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${sketches.path}${branchRef}`,
@@ -155,19 +152,16 @@ function getSketchDirectories(sketchRootList) {
155152
Authorization: `Basic ${Buffer.from(
156153
`${clientId}:${clientSecret}`
157154
).toString('base64')}`
158-
},
159-
json: true
155+
}
160156
};
161157

162-
return rp(options)
163-
.then((res) => {
164-
const sketchDirs = flatten(res);
165-
166-
return sketchDirs;
167-
})
168-
.catch((err) => {
169-
throw err;
170-
});
158+
try {
159+
const { data } = await axios.request(options);
160+
const sketchDirs = flatten(data);
161+
return sketchDirs;
162+
} catch (err) {
163+
throw err;
164+
}
171165
})
172166
).then((output) => {
173167
const sketchList = [];
@@ -186,7 +180,7 @@ function getSketchDirectories(sketchRootList) {
186180
// 3. For each sketch item in the sketchList, append the tree contents to each item
187181
function appendSketchItemLinks(sketchList) {
188182
return Q.all(
189-
sketchList.map((sketches) => {
183+
sketchList.map(async (sketches) => {
190184
const options = {
191185
// url: `${sketches.url}?client_id=${clientId}&client_secret=${clientSecret}`,
192186
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${sketches.path}${branchRef}`,
@@ -196,15 +190,16 @@ function appendSketchItemLinks(sketchList) {
196190
Authorization: `Basic ${Buffer.from(
197191
`${clientId}:${clientSecret}`
198192
).toString('base64')}`
199-
},
200-
json: true
193+
}
201194
};
202195

203-
return rp(options).then((res) => {
204-
sketches.tree = res;
205-
196+
try {
197+
const { data } = await axios.request(options);
198+
sketches.tree = data;
206199
return sketchList;
207-
});
200+
} catch (err) {
201+
throw err;
202+
}
208203
})
209204
);
210205
}
@@ -214,24 +209,24 @@ function getSketchItems(sketchList) {
214209
// const completeSketchPkg = [];
215210

216211
/* eslint-disable */
217-
return Q.all(sketchList[0].map(sketch => Q.all(sketch.tree.map((item) => {
212+
return Q.all(sketchList[0].map(async sketch => Q.all(sketch.tree.map((item) => {
218213
if (item.name === 'data') {
219214
const options = {
220215
url: `https://api.github.com/repos/generative-design/Code-Package-p5.js/contents/${item.path}${branchRef}`,
221216
method: 'GET',
222217
headers: {
223218
...headers,
224219
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`
225-
},
226-
json: true
220+
}
227221
};
228222

229-
return rp(options).then((res) => {
230-
sketch.data = res;
223+
try {
224+
const { data } = axios.request(options);
225+
sketch.data = data;
231226
return sketch;
232-
}).catch((err) => {
227+
} catch (err) {
233228
throw err;
234-
});
229+
}
235230
}
236231
// pass
237232
})))).then(() => sketchList[0]);
@@ -399,7 +394,7 @@ function formatAllSketches(sketchList) {
399394
// get all the sketch data content and download to the newProjects array
400395
function getAllSketchContent(newProjectList) {
401396
/* eslint-disable */
402-
return Q.all(newProjectList.map(newProject => Q.all(newProject.files.map((sketchFile, i) => {
397+
return Q.all(newProjectList.map(newProject => Q.all(newProject.files.map(async (sketchFile, i) => {
403398
/*
404399
sketchFile.name.endsWith(".mp4") !== true &&
405400
sketchFile.name.endsWith(".ogg") !== true &&
@@ -427,12 +422,13 @@ function getAllSketchContent(newProjectList) {
427422
};
428423

429424
// console.log("CONVERT ME!")
430-
return rp(options).then((res) => {
431-
newProject.files[i].content = res;
425+
try {
426+
const { data } = await axios.request(options);
427+
newProject.files[i].content = data;
432428
return newProject;
433-
}).catch((err) => {
429+
} catch (err) {
434430
throw err;
435-
});
431+
}
436432
}
437433
if (newProject.files[i].url) {
438434
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)