Skip to content

Commit

Permalink
feat: use axios for fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
huchenme committed Mar 10, 2020
1 parent ca77136 commit 881d718
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
"homepage": "https://github.com/huchenme/github-trending-api#readme",
"dependencies": {
"@babel/runtime-corejs3": "^7.8.4",
"axios": "^0.19.2",
"cheerio": "^1.0.0-rc.3",
"cors": "^2.8.5",
"express": "^4.17.1",
"express-cache-controller": "^1.1.0",
"lodash": "^4.17.15",
"memory-cache": "^0.2.0",
"node-fetch": "^2.6.0",
"opencollective-postinstall": "^2.0.2"
},
"devDependencies": {
Expand All @@ -49,6 +49,7 @@
"husky": "^4.2.3",
"jest": "^25.1.0",
"lint-staged": "^10.0.8",
"node-fetch": "^2.6.0",
"nodemon": "^2.0.2",
"npm-run-all": "^4.1.5",
"prettier": "^1.19.1",
Expand Down
26 changes: 13 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fetch from 'node-fetch';
import axios from 'axios';
import { sample, sampleSize, snakeCase } from 'lodash';
import languages from './languages.json';
import spokenLanguages from './spoken-languages.json';
Expand All @@ -15,36 +15,36 @@ function buildUrl(baseUrl, params = {}) {
}

export async function fetchRepositories(params) {
const res = await fetch(buildUrl(`${SERVER_URL}/repositories`, params));
if (!res.ok) {
const res = await axios(buildUrl(`${SERVER_URL}/repositories`, params));
if (res.status !== 200) {
throw new Error('Something went wrong');
}
return res.json();
return res.data;
}

export async function fetchDevelopers(params) {
const res = await fetch(buildUrl(`${SERVER_URL}/developers`, params));
if (!res.ok) {
const res = await axios(buildUrl(`${SERVER_URL}/developers`, params));
if (res.status !== 200) {
throw new Error('Something went wrong');
}
return res.json();
return res.data;
}

export async function fetchRandomRepository(params) {
const res = await fetch(buildUrl(`${SERVER_URL}/repositories`, params));
if (!res.ok) {
const res = await axios(buildUrl(`${SERVER_URL}/repositories`, params));
if (res.status !== 200) {
throw new Error('Something went wrong');
}
const json = res.json();
const json = res.data;
return sample(json);
}

export async function fetchRandomRepositories(size = 1, params) {
const res = await fetch(buildUrl(`${SERVER_URL}/repositories`, params));
if (!res.ok) {
const res = await axios(buildUrl(`${SERVER_URL}/repositories`, params));
if (res.status !== 200) {
throw new Error('Something went wrong');
}
const json = res.json();
const json = res.data;
return sampleSize(json, size);
}

Expand Down
16 changes: 15 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2769,6 +2769,13 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.8.0.tgz#f0e003d9ca9e7f59c7a508945d7b2ef9a04a542f"
integrity sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==

axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
dependencies:
follow-redirects "1.5.10"

axobject-query@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/axobject-query/-/axobject-query-2.0.2.tgz#ea187abe5b9002b377f925d8bf7d1c561adf38f9"
Expand Down Expand Up @@ -4089,7 +4096,7 @@ [email protected], debug@^2.1.2, debug@^2.1.3, debug@^2.2.0, debug@^2.3.3, debug@^2.6.
dependencies:
ms "2.0.0"

[email protected], debug@^3.1.0:
[email protected], debug@=3.1.0, debug@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
Expand Down Expand Up @@ -5408,6 +5415,13 @@ flush-write-stream@^1.0.0:
inherits "^2.0.1"
readable-stream "^2.0.4"

[email protected]:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"

for-in@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
Expand Down

0 comments on commit 881d718

Please sign in to comment.