For StarsKeeper development, you need to get the total number of projects that a user has started on GitHub, as shown below.
The first thing I thought about was the user-related api, with Authorization Token on the headers, which is actually the API https://api.github.com/user. But this is the case.There are followers and following numbers, but no starred repo.
SO After searching, there was a similar problem. The method is very simple, a little hack meaning. Take user hanzichi as an example, request https://api.github.com/users/hanzichi/starred? PerIf you add Authorization Token, then it’s https://api.github.com/user/starred?Per_page=1. We can type it on the command line likeLower code:
curl -I "https://api.github.com/users/hanzichi/starred?per_page=1"
The following are returned:
HTTP/1.1 200 OK
Date: Sat, 01 Sep 2018 08:41:02 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 5900
Server: GitHub.com
Status: 200 OK
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
X-RateLimit-Reset: 1535794862
Cache-Control: public, max-age=60, s-maxage=60
Vary: Accept
ETag: "c19c7d6569715474d9f0d49b10717584"
X-GitHub-Media-Type: github.v3; format=json
Link: <https://api.github.com/user/10890665/starred?per_page=1&page=2>; rel="next", <https://api.github.com/user/10890665/starred?per_page=1&page=692>; rel="last"
Access-Control-Expose-Headers: ETag, Link, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
Access-Control-Allow-Origin: *
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Frame-Options: deny
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
Content-Security-Policy: default-src 'none'
X-Runtime-rack: 0.049747
Vary: Accept-Encoding
X-GitHub-Request-Id: 9E01:4809:1EEAAE:291ED5:5B8A509C
Link is actually useful.
Link: <https://api.github.com/user/10890665/starred?per_page=1&page=2>; rel="next",
<https://api.github.com/user/10890665/starred?per_page=1&page=692>; rel="last"
Paying attention to the next 692 figure is actually the result.
There is also a supplementary answer to this question. If the number of repos returned is 0 or 1, there is no Link header returned. If not, repo. length is available.
In the GitHub API, this Link header is related to the page number and can be seen at https://developer.github.com/v3/# pagination for details.
This header value is a bit odd, but it’s actually RFC 5988 HTTP header, and we can parse it with the parse-link-header package to get the object.
The backend is implemented by node-fetch:
const fetch = require('node-fetch')
const parse = require('parse-link-header')
module.exports.getStarredRepoCount = token => {
return new Promise(resolve => {
fetch('https://api.github.com/user/starred?per_page=1', {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': "token " + token,
}
})
.then(res => {
let linkHeader = res.headers.get('Link')
if (linkHeader) {
resolve(parse(linkHeader).last.page)
} else {
return res.json().then(res => {resolve(res.length)})
}
})
.catch(e => {
console.log(e)
})
})
}
If there is no link header, you must return res. JSON () to resolve (res. length) in the next step, not directly resolve (res. JSON (). length)Step by step.Because res.json () returns a promise.。fetch Not many, many pits.
It should also be noted that res. headers can not directly resolve to the client, the client will get empty objects, if you need to process or traverse the headers, it is best to do in the server side.