From 07cc098e10b3f6c6f29b96dabb7b3f7afafb5b12 Mon Sep 17 00:00:00 2001 From: Jan Kaiser Date: Wed, 30 Aug 2023 19:49:43 +0200 Subject: [PATCH] feat: add accept headers interceptor --- .../src/app/accept.interceptor.ts | 24 ++++++++ angular-ngrx-scss/src/app/app.module.ts | 6 ++ .../services/organization.service.spec.ts | 6 -- .../services/organization.service.ts | 3 - .../services/repository.service.spec.ts | 37 +----------- .../repository/services/repository.service.ts | 57 +++---------------- .../app/user/services/user.service.spec.ts | 23 -------- .../src/app/user/services/user.service.ts | 33 ++--------- 8 files changed, 46 insertions(+), 143 deletions(-) create mode 100644 angular-ngrx-scss/src/app/accept.interceptor.ts diff --git a/angular-ngrx-scss/src/app/accept.interceptor.ts b/angular-ngrx-scss/src/app/accept.interceptor.ts new file mode 100644 index 000000000..fbf32bb96 --- /dev/null +++ b/angular-ngrx-scss/src/app/accept.interceptor.ts @@ -0,0 +1,24 @@ +import { Injectable } from '@angular/core'; +import { + HttpEvent, + HttpHandler, + HttpInterceptor, + HttpRequest, +} from '@angular/common/http'; +import { Observable } from 'rxjs'; + +@Injectable() +export class AcceptInterceptor implements HttpInterceptor { + intercept( + request: HttpRequest, + next: HttpHandler, + ): Observable> { + request = request.clone({ + setHeaders: { + Accept: 'application/vnd.github.v3+json', + }, + }); + + return next.handle(request); + } +} diff --git a/angular-ngrx-scss/src/app/app.module.ts b/angular-ngrx-scss/src/app/app.module.ts index 59ecb11be..4ef87d20c 100644 --- a/angular-ngrx-scss/src/app/app.module.ts +++ b/angular-ngrx-scss/src/app/app.module.ts @@ -15,6 +15,7 @@ import { AuthEffects } from './state/auth'; import { ProfileEffects } from './state/profile/profile.effects'; import { RepositoryEffects } from './state/repository/repository.effects'; import { UserEffects } from './state/user'; +import { AcceptInterceptor } from './accept.interceptor'; @NgModule({ imports: [ @@ -42,6 +43,11 @@ import { UserEffects } from './state/user'; useClass: TokenInterceptor, multi: true, }, + { + provide: HTTP_INTERCEPTORS, + useClass: AcceptInterceptor, + multi: true, + }, ], bootstrap: [AppComponent], }) diff --git a/angular-ngrx-scss/src/app/organization/services/organization.service.spec.ts b/angular-ngrx-scss/src/app/organization/services/organization.service.spec.ts index a10fd9c83..8618c03f2 100644 --- a/angular-ngrx-scss/src/app/organization/services/organization.service.spec.ts +++ b/angular-ngrx-scss/src/app/organization/services/organization.service.spec.ts @@ -91,9 +91,6 @@ describe('OrganizationService', () => { expect(httpSpy.get).toHaveBeenCalledWith( 'https://api.github.com/orgs/FakeCo/repos', jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, params: new HttpParams({ fromObject: { type: 'all', @@ -120,9 +117,6 @@ describe('OrganizationService', () => { expect(httpSpy.get).toHaveBeenCalledWith( 'https://api.github.com/orgs/OtherFakeCo/repos', jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, params: new HttpParams({ fromObject: { type: 'public', diff --git a/angular-ngrx-scss/src/app/organization/services/organization.service.ts b/angular-ngrx-scss/src/app/organization/services/organization.service.ts index 04149a2fb..61d6101a3 100644 --- a/angular-ngrx-scss/src/app/organization/services/organization.service.ts +++ b/angular-ngrx-scss/src/app/organization/services/organization.service.ts @@ -30,9 +30,6 @@ export class OrganizationService { )}/repos`; return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, params: new HttpParams({ fromObject: { ...Object.assign(defaultParams, params) }, }), diff --git a/angular-ngrx-scss/src/app/repository/services/repository.service.spec.ts b/angular-ngrx-scss/src/app/repository/services/repository.service.spec.ts index 79495bdc8..e927ebba0 100644 --- a/angular-ngrx-scss/src/app/repository/services/repository.service.spec.ts +++ b/angular-ngrx-scss/src/app/repository/services/repository.service.spec.ts @@ -290,11 +290,6 @@ describe('RepositoryService', () => { expect(httpClientSpy.get).toHaveBeenCalledOnceWith( `https://api.github.com/repos/thisdot/starter.dev-github-showcases`, - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); done(); }, @@ -371,11 +366,6 @@ describe('RepositoryService', () => { expect(httpClientSpy.get).toHaveBeenCalledOnceWith( 'https://api.github.com/repos/thisdot/starter.dev-github-showcases/contents/README.md', - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); }); }); @@ -391,11 +381,6 @@ describe('RepositoryService', () => { expect(httpClientSpy.get).toHaveBeenCalledWith( `https://api.github.com/repos/FakeCo/fake-repo/pulls/${MOCK_PULL_REQUEST_NUMBER}`, - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); }, complete: done, @@ -414,9 +399,7 @@ describe('RepositoryService', () => { expect(httpClientSpy.get).toHaveBeenCalledWith( `https://api.github.com/search/issues?q=repo:FakeCo/fake-repo+type:pr+state:all`, jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, + observe: 'response', }), ); }, @@ -439,11 +422,6 @@ describe('RepositoryService', () => { expect(httpClientSpy.get).toHaveBeenCalledWith( `https://api.github.com/repos/FakeCo/fake-repo/issues/${MOCK_PULL_REQUEST_NUMBER}/comments`, - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); }, complete: done, @@ -460,9 +438,7 @@ describe('RepositoryService', () => { expect(httpClientSpy.get).toHaveBeenCalledWith( 'https://api.github.com/search/issues?q=repo:FakeCo/fake-repo+type:issue+state:all', jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, + observe: 'response', }), ); }, @@ -482,9 +458,7 @@ describe('RepositoryService', () => { expect(httpClientSpy.get).toHaveBeenCalledWith( 'https://api.github.com/search/issues?q=repo:FakeCo/fake-repo+type:issue+state:closed', jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, + observe: 'response', }), ); }, @@ -510,11 +484,6 @@ describe('RepositoryService', () => { .toBe(1); expect(httpClientSpy.get).toHaveBeenCalledOnceWith( 'https://api.github.com/repos/thisdot/starter.dev-github-showcases/pulls/1', - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); }); }); diff --git a/angular-ngrx-scss/src/app/repository/services/repository.service.ts b/angular-ngrx-scss/src/app/repository/services/repository.service.ts index 8121e021d..5abf57ba8 100644 --- a/angular-ngrx-scss/src/app/repository/services/repository.service.ts +++ b/angular-ngrx-scss/src/app/repository/services/repository.service.ts @@ -43,11 +43,7 @@ export class RepositoryService { const name = encodeURIComponent(repoName); const url = `${environment.githubUrl}/repos/${owner}/${name}`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } getRepositoryPullRequestsCount( @@ -61,9 +57,6 @@ export class RepositoryService { return this.http .get(url, { observe: 'response', - headers: { - Accept: 'application/vnd.github.v3+json', - }, params: new HttpParams({ fromObject: { state: 'open', @@ -99,9 +92,6 @@ export class RepositoryService { return this.http .get(url, { observe: 'response', - headers: { - Accept: 'application/vnd.github.v3+json', - }, }) .pipe( map((response) => { @@ -137,11 +127,7 @@ export class RepositoryService { const name = encodeURIComponent(repoName); const url = `${environment.githubUrl}/repos/${owner}/${name}/milestones`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -158,11 +144,7 @@ export class RepositoryService { const name = encodeURIComponent(repoName); const url = `${environment.githubUrl}/repos/${owner}/${name}/labels`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -182,11 +164,7 @@ export class RepositoryService { const pullId = encodeURIComponent(pullNumber); const url = `${environment.githubUrl}/repos/${owner}/${name}/pulls/${pullId}`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -206,11 +184,7 @@ export class RepositoryService { const pullId = encodeURIComponent(pullNumber); const url = `${environment.githubUrl}/repos/${owner}/${name}/issues/${pullId}/comments`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -244,9 +218,6 @@ export class RepositoryService { return this.http .get(url, { observe: 'response', - headers: { - Accept: 'application/vnd.github.v3+json', - }, }) .pipe( map((response) => { @@ -295,11 +266,7 @@ export class RepositoryService { url += `?ref=${refPath}`; } - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -326,11 +293,7 @@ export class RepositoryService { url += `?ref=${refPath}`; } - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -347,11 +310,7 @@ export class RepositoryService { const name = encodeURIComponent(repoName); const url = `${environment.githubUrl}/repos/${owner}/${name}/readme`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } private extractTotalFromLinkHeader(linkHeader: string | null): number { diff --git a/angular-ngrx-scss/src/app/user/services/user.service.spec.ts b/angular-ngrx-scss/src/app/user/services/user.service.spec.ts index 42872e5fd..6bfa8b008 100644 --- a/angular-ngrx-scss/src/app/user/services/user.service.spec.ts +++ b/angular-ngrx-scss/src/app/user/services/user.service.spec.ts @@ -45,11 +45,6 @@ describe('UserService', () => { expect(httpClientSpy.get).toHaveBeenCalledOnceWith( `https://api.github.com/user`, - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); }, complete: done, @@ -84,11 +79,6 @@ describe('UserService', () => { next: () => { expect(httpClientSpy.get).toHaveBeenCalledWith( `https://api.github.com/users/thisdot/orgs`, - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); }, complete: done, @@ -211,11 +201,6 @@ describe('UserService', () => { next: () => { expect(httpClientSpy.get).toHaveBeenCalledWith( `https://api.github.com/users/thisdot/repos`, - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); }, complete: done, @@ -339,9 +324,6 @@ describe('UserService', () => { expect(httpClientSpy.get).toHaveBeenCalledWith( `https://api.github.com/users/thisdot/repos`, jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, params: new HttpParams({ fromObject: { sort: 'updated', @@ -369,11 +351,6 @@ describe('UserService', () => { next: () => { expect(httpClientSpy.get).toHaveBeenCalledWith( `https://api.github.com/users/thisdot/gists`, - jasmine.objectContaining({ - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }), ); }, complete: done, diff --git a/angular-ngrx-scss/src/app/user/services/user.service.ts b/angular-ngrx-scss/src/app/user/services/user.service.ts index bdb839e85..04dbd8ad9 100644 --- a/angular-ngrx-scss/src/app/user/services/user.service.ts +++ b/angular-ngrx-scss/src/app/user/services/user.service.ts @@ -23,11 +23,7 @@ export class UserService { getAuthenticatedUserInfo(): Observable { const url = `${environment.githubUrl}/user`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -38,11 +34,7 @@ export class UserService { getUserInfo(username: string): Observable { const url = `${environment.githubUrl}/users/${username}`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -57,11 +49,7 @@ export class UserService { getUserOrganizations(username: string): Observable { const url = `${environment.githubUrl}/users/${username}/orgs`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -72,11 +60,7 @@ export class UserService { getUserRepos(username: string): Observable { const url = `${environment.githubUrl}/users/${username}/repos`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } /** @@ -100,9 +84,6 @@ export class UserService { params: new HttpParams({ fromObject: { ...Object.assign(defaultParams) }, }), - headers: { - Accept: 'application/vnd.github.v3+json', - }, }); } @@ -114,10 +95,6 @@ export class UserService { getUserGists(username: string): Observable { const url = `${environment.githubUrl}/users/${username}/gists`; - return this.http.get(url, { - headers: { - Accept: 'application/vnd.github.v3+json', - }, - }); + return this.http.get(url); } }