From 881d718f34c1ae37b6f0ecdc1795ff07009901b3 Mon Sep 17 00:00:00 2001 From: Hu Chen Date: Tue, 10 Mar 2020 22:37:57 +0800 Subject: [PATCH] feat: use axios for fetching --- package.json | 3 ++- src/index.js | 26 +++++++++++++------------- yarn.lock | 16 +++++++++++++++- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/package.json b/package.json index cc63ef7..279d82e 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/src/index.js b/src/index.js index 0254930..1776fe4 100644 --- a/src/index.js +++ b/src/index.js @@ -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'; @@ -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); } diff --git a/yarn.lock b/yarn.lock index 205f772..1dffa26 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -4089,7 +4096,7 @@ debug@2.6.9, debug@^2.1.2, debug@^2.1.3, debug@^2.2.0, debug@^2.3.3, debug@^2.6. dependencies: ms "2.0.0" -debug@3.1.0, debug@^3.1.0: +debug@3.1.0, 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== @@ -5408,6 +5415,13 @@ flush-write-stream@^1.0.0: inherits "^2.0.1" readable-stream "^2.0.4" +follow-redirects@1.5.10: + 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"