-
Notifications
You must be signed in to change notification settings - Fork 0
VA-9: Services for API #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidavila2
wants to merge
25
commits into
develop
Choose a base branch
from
VA-9-services
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d44d4e7
Merge pull request #5 from onehungrymind/develop
c3b366c
Merge pull request #9 from onehungrymind/develop
6dc1f69
started creating services
33986cd
incr changes
fdb252e
merged in develop
f531f31
incr chanhges
558ca05
merge master
2a73cd3
merge master
bbbfb7b
Merge Develop into Master (#21)
b4abd06
fix: Merge branch 'VA-9-services' with master
nautilistic 0b2f157
fixed issue in app-module
elideg 0a05355
Merge branch 'develop' into VA-9-services
elideg 8f46aec
Merge remote-tracking branch 'origin/VA-9-services' into VA-9-services
elideg 5c77d4b
incremental changes
elideg f02cf0c
finished node server
elideg eb5f69c
fixed organizations and user graphql service
elideg 1a8ae7e
refactored user query
elideg 38c8e55
changed client_id and client_secret for variables in node server
elideg e898ced
removed logic used for testing
elideg acbd98b
git ignored node_modules
elideg d384036
git ingored node_modules
elideg 06a82dd
feat: create express server in nrwl workspace
ce9a118
abstracted node server
elideg cefd203
Merge remote-tracking branch 'origin/VA-9-services' into VA-9-services
elideg 0d7c8ce
update: start server script
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| <gc-ui-toolbar> | ||
| </gc-ui-toolbar> | ||
| </gc-ui-toolbar> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,14 @@ | ||
| export * from './lib/core-data.module'; | ||
| export { CoreDataModule } from './lib/core-data.module'; | ||
|
|
||
| // Models | ||
| export { Repository, emptyRepository, Visibility } from './lib/repositories/repositories'; | ||
| export { User, emptyUser } from './lib/user/user.model'; | ||
|
|
||
|
|
||
| // Services | ||
| export { AuthGuard } from './lib/auth/auth-guard'; | ||
| export { AuthService } from './lib/auth/auth.service'; | ||
| export { NotifyService } from './lib/notify/notify.service'; | ||
| export { RepositoriesService } from './lib/repositories/repositories.service'; | ||
| export { TokenInterceptor } from './lib/auth/token-interceptor'; | ||
| export { UserService } from './lib/user/user.service'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,28 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router'; | ||
|
|
||
| import { Observable } from 'rxjs'; | ||
|
|
||
| import { AuthService } from './auth.service'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class AuthGuard { | ||
| export class AuthGuard implements CanActivate { | ||
| constructor( | ||
| private router: Router, | ||
| private authService: AuthService | ||
| ) {} | ||
|
|
||
| canActivate( | ||
| next: ActivatedRouteSnapshot, | ||
| state: RouterStateSnapshot | ||
| ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree { | ||
| if (!this.authService.isAuthenticated$.value) { | ||
| this.router.navigate(['/login']); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| constructor() { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,39 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { HttpClient } from '@angular/common/http'; | ||
| import { Router } from '@angular/router'; | ||
|
|
||
| import { tap } from 'rxjs/operators'; | ||
| import { BehaviorSubject } from 'rxjs'; | ||
|
|
||
| export const TOKEN_NAME = 'gh:auth:token'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class AuthService { | ||
| isAuthenticated$ = new BehaviorSubject(false); | ||
|
|
||
| constructor( | ||
| private router: Router, | ||
| private http: HttpClient | ||
| ) { | ||
| this.setToken(this.getToken()); | ||
| } | ||
|
|
||
| handleRedirectCallback(code: string) { | ||
| const loginURL = `https://gh-auth-server.herokuapp.com/authenticate/${code}`; | ||
| return this.http.get(loginURL).pipe( | ||
| tap((res: {token: string}) => this.setToken(res.token)), | ||
| tap(() => this.router.navigate([''])) | ||
| ); | ||
| } | ||
|
|
||
| setToken(token) { | ||
| localStorage.setItem(TOKEN_NAME, token); | ||
| this.isAuthenticated$.next(token !== '' || token !== null || token !== undefined); | ||
| } | ||
|
|
||
| constructor() { } | ||
| getToken() { | ||
| return localStorage.getItem(TOKEN_NAME); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,40 @@ | ||
| import { Injectable } from '@angular/core'; | ||
| import { Router } from '@angular/router'; | ||
| import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpErrorResponse } from '@angular/common/http'; | ||
|
|
||
| import { Observable } from 'rxjs'; | ||
| import { tap } from 'rxjs/operators'; | ||
|
|
||
| import { AuthService } from './auth.service'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class TokenInterceptor { | ||
| export class TokenInterceptor implements HttpInterceptor { | ||
| constructor( | ||
| private router: Router, | ||
| private authService: AuthService | ||
| ) { } | ||
|
|
||
| intercept( | ||
| request: HttpRequest<any>, | ||
| next: HttpHandler | ||
| ): Observable<HttpEvent<any>> { | ||
| request = request.clone({ | ||
| setHeaders: { | ||
| Authorization: `bearer ${this.authService.getToken()}` | ||
| } | ||
| }); | ||
|
|
||
| return next.handle(request).pipe( | ||
| tap(() => {}, (error: HttpErrorResponse) => this.handleUnAuth(error)) | ||
| ); | ||
| } | ||
|
|
||
| handleUnAuth(error: HttpErrorResponse) { | ||
| if (error.status === 401) { | ||
| this.router.navigate(['login']); | ||
| } | ||
| } | ||
|
|
||
| constructor() { } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,34 @@ | ||
| import { NgModule } from '@angular/core'; | ||
| import { CommonModule } from '@angular/common'; | ||
| import { HttpClientModule, HttpHeaders, HTTP_INTERCEPTORS } from '@angular/common/http'; | ||
|
|
||
| import { APOLLO_OPTIONS, ApolloModule } from 'apollo-angular'; | ||
| import { HttpLink, HttpLinkModule } from 'apollo-angular-link-http'; | ||
| import { InMemoryCache } from 'apollo-cache-inmemory'; | ||
| import { TokenInterceptor } from './auth/token-interceptor'; | ||
|
|
||
| const uri = 'https://api.github.com/graphql'; | ||
|
|
||
| export function createApollo(httpLink: HttpLink) { | ||
| return { | ||
| link: httpLink.create({ uri }), | ||
| cache: new InMemoryCache() | ||
| } | ||
| } | ||
|
|
||
| @NgModule({ | ||
| imports: [CommonModule] | ||
| imports: [HttpClientModule], | ||
| providers: [ | ||
| { | ||
| provide: HTTP_INTERCEPTORS, | ||
| useClass: TokenInterceptor, | ||
| multi: true, | ||
| }, | ||
| { | ||
| provide: APOLLO_OPTIONS, | ||
| useFactory: createApollo, | ||
| deps: [HttpLink] | ||
| } | ||
| ], | ||
| exports: [ApolloModule, HttpLinkModule] | ||
| }) | ||
| export class CoreDataModule {} |
37 changes: 37 additions & 0 deletions
37
libs/core-data/src/lib/repositories/repositories.graphql.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import gql from 'graphql-tag'; | ||
|
|
||
| const repositoryFragment = gql` | ||
| fragment repositoryFragment on Repository { | ||
| id | ||
| name | ||
| description | ||
| homepageUrl | ||
| url | ||
| forkCount | ||
| isPrivate | ||
| isFork | ||
| languages(first: 1) { | ||
| nodes { | ||
| name | ||
| color | ||
| id | ||
| } | ||
| } | ||
| createdAt | ||
| updatedAt | ||
| pushedAt | ||
| } | ||
| `; | ||
|
|
||
| export const repositoriesQuery = gql` | ||
| query repositoriesQuery($login: String!) { | ||
| user(login: $login) { | ||
| repositories(first: 100, orderBy: {field: CREATED_AT, direction: DESC}) { | ||
| nodes { | ||
| ...repositoryFragment | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ${repositoryFragment} | ||
| `; | ||
24 changes: 23 additions & 1 deletion
24
libs/core-data/src/lib/repositories/repositories.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,31 @@ | ||
|
|
||
| import { Injectable } from '@angular/core'; | ||
| import { Apollo } from 'apollo-angular'; | ||
| import { map } from 'rxjs/operators'; | ||
| import { ApolloQueryResult } from 'apollo-client'; | ||
| import { UserService } from '../user/user.service'; | ||
| import { repositoriesQuery } from './repositories.graphql'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class RepositoriesService { | ||
|
|
||
| constructor() { } | ||
| constructor( | ||
| private apollo: Apollo, | ||
| private userService: UserService | ||
| ) { } | ||
|
|
||
| all() { | ||
| const login = this.userService.getUsername(); | ||
| return this.apollo.mutate({ | ||
| mutation: repositoriesQuery, | ||
| variables: { | ||
| login | ||
| } | ||
| }).pipe( | ||
| map((response: ApolloQueryResult<any>) => response.data.user.repositories.nodes), | ||
| map((repositories: any[]) => repositories.map((repo) => ({ ...repo, languages: repo.languages.nodes[0] }))) | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| export interface Repository { | ||
| id: string; | ||
| name: string; | ||
| description: string; | ||
| homepageUrl: string; | ||
| url: string; | ||
| forkCount: number; | ||
| isPrivate: boolean; | ||
| isFork: boolean; | ||
| languages: Language; | ||
| createdAt: string; | ||
| updatedAt: string; | ||
| pushedAt: string; | ||
| } | ||
|
|
||
| export interface RepositoryInput { | ||
| name: string, | ||
| visibility: Visibility, | ||
| description: string, | ||
| hasIssuesEnabled: boolean | ||
| } | ||
|
|
||
| export enum Visibility { | ||
| PRIVATE = 'PRIVATE', | ||
| PUBLIC = 'PUBLIC', | ||
| INTERNAL = 'INTERNAL' | ||
| } | ||
|
|
||
| interface Language { | ||
| id: string; | ||
| name: string; | ||
| color: string; | ||
| } | ||
|
|
||
| export const emptyRepository: Repository = { | ||
| id: null, | ||
| name: '', | ||
| description: '', | ||
| homepageUrl: '', | ||
| url: '', | ||
| forkCount: 0, | ||
| isPrivate: false, | ||
| isFork: false, | ||
| languages: { | ||
| id: null, | ||
| name: '', | ||
| color: '' | ||
| }, | ||
| createdAt: '', | ||
| updatedAt: '', | ||
| pushedAt: '' | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import gql from 'graphql-tag'; | ||
|
|
||
| const userFragment = gql` | ||
| fragment userFragment on User { | ||
| id | ||
| avatarUrl | ||
| name | ||
| login | ||
| url | ||
| bio | ||
| } | ||
| `; | ||
|
|
||
| export const getUsernameQuery = gql` | ||
| query getUsernameQuery { | ||
| viewer { | ||
| login | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| export const usersQuery = gql` | ||
| query usersQuery($login: String!) { | ||
| user(login: $login) { | ||
| ...userFragment | ||
| } | ||
| } | ||
| ${userFragment} | ||
| `; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| export interface User { | ||
| id: string; | ||
| avatarUrl: string; | ||
| name: string; | ||
| login: string; | ||
| url: string; | ||
| email: string; | ||
| bio: string; | ||
| } | ||
|
|
||
| export const emptyUser: User = { | ||
| id: null, | ||
| avatarUrl: '', | ||
| name: '', | ||
| login: '', | ||
| url: '', | ||
| email: '', | ||
| bio: '' | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.