File

projects/ng-jkmdev-github/src/lib/shared/github-api.service.ts

Constructor

constructor(httpClient: HttpClient)

Methods

getUserEvents
getUserEvents(username: string)
Returns: void

Properties

baseURL
baseURL: string
import { Injectable } from '@angular/core';
import { AppConstants } from '../ng-jkmdev-github.constants';
import { HttpClient } from '@angular/common/http';
import { format, formatDistance, formatRelative, subDays } from 'date-fns'

import { map } from "rxjs/operators";
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators'; 

import { UserActivity } from '../user-activity/user-activity.model';

@Injectable({
  providedIn: 'root'
})
export class GithubApiService {

  baseURL: string;

  constructor(private httpClient: HttpClient) { 
    this.baseURL = AppConstants.githubBaseURL;
  }

  /*
   * Gets public user events for a given Github user
   * Reformats the raw Github data and returns the data most
   * relevant to the user's PR/push activity 
   */
  getUserEvents(username: string) {
    const getUserEventsURL = `${this.baseURL}/users/${username}/events/public`;
    return this.httpClient.get(getUserEventsURL)
      .pipe(map( (data: any[]) => {

        console.log(data);

        let response = new UserActivity(
          data[0].actor.avatar_url,
          data[0].actor.url, 
          data[0].actor.login
        );

        data.forEach(entry => {

          let date = formatDistance(subDays(new Date(), 3), new Date(entry.created_at));
          date = date + ' ago';

          if (entry.type === 'PushEvent' || entry.type === 'PullRequestEvent') {
            response.data.push({
              type: entry.type,
              createdAt: date,
              payload: entry.payload,
              repoName: entry.repo.name,
              repoGETUrl: entry.repo.url
            });
          }
        });

        return response;

      }))
      .pipe(catchError( error => {
        return throwError(error);
      }));
  }
  
}

results matching ""

    No results matching ""