You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sponsors.ts 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { NextApiRequest, NextApiResponse } from 'next'
  2. const AV_SIZE = 32
  3. const PADDING = 4
  4. const COLS = 16
  5. type SponsorResult = { url: string; login: string }
  6. type QueryResult = {
  7. node: { sponsorEntity: { avatarUrl: string; login: string } }
  8. }
  9. function getXY(i: number) {
  10. return [(i % COLS) * (AV_SIZE + PADDING), Math.floor(i / COLS) * (AV_SIZE + PADDING)]
  11. }
  12. export default async function GetSponsors(req: NextApiRequest, res: NextApiResponse) {
  13. const sponsorInfo = await fetch('https://api.github.com/graphql', {
  14. method: 'POST',
  15. headers: {
  16. 'Content-Type': 'application/json',
  17. Authorization: 'bearer ' + process.env.GITHUB_API_SECRET,
  18. },
  19. body: JSON.stringify({
  20. query: `{
  21. viewer {
  22. sponsors(first: 0) {
  23. totalCount
  24. }
  25. sponsorshipsAsMaintainer(first: 100, orderBy: {
  26. field:CREATED_AT,
  27. direction:DESC
  28. }) {
  29. edges {
  30. node {
  31. sponsorEntity {
  32. ...on User {
  33. avatarUrl
  34. login
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }`,
  42. }),
  43. }).then((res) => res.json())
  44. const totalCount: number = sponsorInfo.data.viewer.sponsors.totalCount
  45. const results = (
  46. sponsorInfo.data.viewer.sponsorshipsAsMaintainer.edges as QueryResult[]
  47. ).map<SponsorResult>((edge) => ({
  48. url: edge.node.sponsorEntity.avatarUrl?.replaceAll('&', '&amp;') ?? '',
  49. login: edge.node.sponsorEntity.login,
  50. }))
  51. if (results.length % COLS <= 2) {
  52. results.pop()
  53. results.pop()
  54. results.pop()
  55. }
  56. // Avatars
  57. const avatars = results
  58. .map(({ url, login }, i) => {
  59. const [x, y] = getXY(i)
  60. return `<image alt="${login}" href="${url}" x="${x}" y="${y}" width="${AV_SIZE}" height="${AV_SIZE}"/>`
  61. })
  62. .join('')
  63. // More text
  64. const [x, y] = getXY(results.length)
  65. const width = (AV_SIZE + PADDING) * 3
  66. const more = `
  67. <g transform="translate(${x},${y})"><text text-lenth="${width}" font-family="Arial" font-size="12px" font-weight="bold" text-anchor="middle" text-align="center" x="${
  68. width / 2
  69. }" y="${AV_SIZE / 2 + 3}">...and ${totalCount - 100} more!</text></g>`
  70. const svgImage = `
  71. <svg xmlns="http://www.w3.org/2000/svg"><a href="https://github.com/sponsors/steveruizok"><g>${avatars}${more}</g></a></svg>`
  72. // const html = `
  73. // <div style="display: grid; width: fit-content; grid-template-columns: repeat(25, auto); gap: 4px;">
  74. // ${images.join(`
  75. // `)}
  76. // </div>`
  77. res
  78. .status(200)
  79. .setHeader('Cache-Control', 'max-age=604800')
  80. .setHeader('Content-Type', 'image/svg+xml')
  81. .send(svgImage)
  82. }