Skip to content
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

Split cache interceptor #47

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class AppComponent {
activePattern: Pattern;
newCommandForInput: string;
resetCommand$: Observable<number> = this.redisConnectService.execCommandTime$;
isAuth$: Observable<boolean> = this.githubDataService.isAuth;
isAuth$: Observable<boolean> = this.githubDataService.isAuth$;

constructor(
private githubDataService: GithubDataService,
Expand Down
6 changes: 6 additions & 0 deletions src/app/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CommonModule } from '@angular/common';

import { ConfigService } from './services/config.service';
import { CacheInterceptor } from './interceptors/cache-interceptor';
import { AuthInterceptor } from './interceptors/auth.interceptor';
import { HeaderComponent } from '@app/core/components/header/header.component';

@NgModule({
Expand All @@ -30,6 +31,11 @@ import { HeaderComponent } from '@app/core/components/header/header.component';
provide: HTTP_INTERCEPTORS,
useClass: CacheInterceptor,
multi: true
},
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
}
],
})
Expand Down
32 changes: 32 additions & 0 deletions src/app/core/interceptors/auth.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import {
HttpEvent,
HttpHandler,
HttpInterceptor,
HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { GithubDataService } from '@app/core/services/github-data.service';
@Injectable({
providedIn: 'root'
})
export class AuthInterceptor implements HttpInterceptor {
public constructor(private githubDataService: GithubDataService) { }
public intercept(request: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> {
if (this.isAuthenticated()) {
request = this.createAuthenticatedRequest(request);
}
return next.handle(request);
}

private createAuthenticatedRequest(request: HttpRequest<any>) {
return request.clone({ setHeaders: { Authorization: `token ${this.githubDataService.accessToken}` } });
}

private isAuthenticated(): boolean {
return this.githubDataService.isAuth;
}
}
39 changes: 24 additions & 15 deletions src/app/core/interceptors/cache-interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,43 @@ import { tap } from 'rxjs/operators';
import { environment } from '@app/../environments/environment';
import { CachingService } from '@app/core/services/caching.service';
import { GithubDataService } from '@app/core/services/github-data.service';
@Injectable({
providedIn: 'root'
})
export class CacheInterceptor implements HttpInterceptor {
constructor(private cachingService: CachingService, private githubDataService: GithubDataService) { }
public constructor(private readonly cachingService: CachingService) { }
intercept(request: HttpRequest<any>, next: HttpHandler ): Observable<HttpEvent<any>> {
if (this.githubDataService.accessToken && this.githubDataService.accessToken.length) {
request = request.clone({ setHeaders: { Authorization: `token ${this.githubDataService.accessToken}` } });
}
if (!request.headers.has(environment.cacheableHeaderKey)) {
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (!this.isCacheableRequest(request)) {
return next.handle(request);
}
/** remove cacheable headers form original request */
request = request.clone({ headers: request.headers.delete(environment.cacheableHeaderKey) });
const cachedResponse = this.cachingService.get(request.url);
return cachedResponse ? of(cachedResponse) : this.sendRequest(request, next);

const cachedResponse = this.getCachedResponse(request);
if (cachedResponse) {
return of(cachedResponse);
}

return this.nextAndCache(request, next);
}

private getCachedResponse(request: HttpRequest<any>): HttpResponse<any> {
return this.cachingService.get(request.url);
}

private isCacheableRequest(request: HttpRequest<any>): boolean {
return request.headers.has(environment.cacheableHeaderKey);
}
sendRequest(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
private nextAndCache(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
/* remove cacheable headers form original request */
request = request.clone({ headers: request.headers.delete(environment.cacheableHeaderKey) });

return next.handle(request).pipe(
tap((event) => {
if (event instanceof HttpResponse) {
this.cachingService.set(req.url, event);
this.cachingService.set(request.url, event);
}
})
);
Expand Down
10 changes: 7 additions & 3 deletions src/app/core/services/github-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,20 @@ export class GithubDataService {
const isToken = (res.data.access_token) ? true : false;
this.authorized.next(isToken);
this.accessToken = res.data && res.data.access_token;
return this.isAuth;
return this.isAuth$;
}),
catchError(() => {
this.authorized.next(false);
return this.isAuth;
return this.isAuth$;
})
);
}

get isAuth() {
get isAuth$() {
return this.authorized.asObservable();
}

get isAuth(): boolean {
return this.authorized.value;
}
}