File size: 1,965 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { HttpService } from "@nestjs/axios";
import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { ContributorDto } from "@reactive-resume/dto";

import { Config } from "../config/schema";

type GitHubResponse = { id: number; login: string; html_url: string; avatar_url: string }[];

type CrowdinContributorsResponse = {
  data: { data: { id: number; username: string; avatarUrl: string } }[];
};

@Injectable()
export class ContributorsService {
  constructor(
    private readonly httpService: HttpService,
    private readonly configService: ConfigService<Config>,
  ) {}

  async fetchGitHubContributors() {
    const response = await this.httpService.axiosRef.get(
      `https://api.github.com/repos/AmruthPillai/Reactive-Resume/contributors`,
    );
    const data = response.data as GitHubResponse;

    return data
      .filter((_, index) => index <= 20)
      .map((user) => {
        return {
          id: user.id,
          name: user.login,
          url: user.html_url,
          avatar: user.avatar_url,
        } satisfies ContributorDto;
      });
  }

  async fetchCrowdinContributors() {
    try {
      const projectId = this.configService.getOrThrow("CROWDIN_PROJECT_ID");
      const accessToken = this.configService.getOrThrow("CROWDIN_PERSONAL_TOKEN");

      const response = await this.httpService.axiosRef.get(
        `https://api.crowdin.com/api/v2/projects/${projectId}/members`,
        { headers: { Authorization: `Bearer ${accessToken}` } },
      );
      const { data } = response.data as CrowdinContributorsResponse;

      return data
        .filter((_, index) => index <= 20)
        .map(({ data }) => {
          return {
            id: data.id,
            name: data.username,
            url: `https://crowdin.com/profile/${data.username}`,
            avatar: data.avatarUrl,
          } satisfies ContributorDto;
        });
    } catch {
      return [];
    }
  }
}