原理
利用 Node 服务发送 Http 请求,对接 Github API 获取 Github 用户相关的信息
实现
创建 Node 项目 运行以下命令,选择 自定义预选
> node-simple
npm create mine
安装依赖
axios
: Http 接口请求dayjs
: 时间处理工具
npm i axios dayjs
const getGraphqlParams = ( to: string , from: string , name: string ) => ( {
query: `
query userInfo($name: String!, $from: DateTime!, $to: DateTime!) {
user(login: $name) {
name
contributionsCollection(from: $from, to: $to) {
contributionCalendar {
weeks {
contributionDays {
contributionCount
date
}
}
}
}
}
}
` ,
variables: { name, from, to }
} )
const res = await graphqlApi. post < GraphqlResponse> ( getGraphqlParams ( to, from, username as string ) )
console . log ( res)
演示
https://github-statistics-biaov.vercel.app/api/github-statistics?username= biaov
https://github-statistics-biaov.vercel.app/api/generate-text-image?text= 案例
完整代码
总结
就是对接 Github API,获取用户数据,然后根据数据生成 SVG 图片,如果你没有服务器的话,可以使用 vercel 进行部署,免费的,但是有可能需要会魔法才能访问到