Skip to content

Commit 07cc098

Browse files
committed
feat: add accept headers interceptor
1 parent 7fdc0d6 commit 07cc098

File tree

8 files changed

+46
-143
lines changed

8 files changed

+46
-143
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Injectable } from '@angular/core';
2+
import {
3+
HttpEvent,
4+
HttpHandler,
5+
HttpInterceptor,
6+
HttpRequest,
7+
} from '@angular/common/http';
8+
import { Observable } from 'rxjs';
9+
10+
@Injectable()
11+
export class AcceptInterceptor implements HttpInterceptor {
12+
intercept(
13+
request: HttpRequest<unknown>,
14+
next: HttpHandler,
15+
): Observable<HttpEvent<unknown>> {
16+
request = request.clone({
17+
setHeaders: {
18+
Accept: 'application/vnd.github.v3+json',
19+
},
20+
});
21+
22+
return next.handle(request);
23+
}
24+
}

angular-ngrx-scss/src/app/app.module.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { AuthEffects } from './state/auth';
1515
import { ProfileEffects } from './state/profile/profile.effects';
1616
import { RepositoryEffects } from './state/repository/repository.effects';
1717
import { UserEffects } from './state/user';
18+
import { AcceptInterceptor } from './accept.interceptor';
1819

1920
@NgModule({
2021
imports: [
@@ -42,6 +43,11 @@ import { UserEffects } from './state/user';
4243
useClass: TokenInterceptor,
4344
multi: true,
4445
},
46+
{
47+
provide: HTTP_INTERCEPTORS,
48+
useClass: AcceptInterceptor,
49+
multi: true,
50+
},
4551
],
4652
bootstrap: [AppComponent],
4753
})

angular-ngrx-scss/src/app/organization/services/organization.service.spec.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ describe('OrganizationService', () => {
9191
expect(httpSpy.get).toHaveBeenCalledWith(
9292
'https://api.github.com/orgs/FakeCo/repos',
9393
jasmine.objectContaining({
94-
headers: {
95-
Accept: 'application/vnd.github.v3+json',
96-
},
9794
params: new HttpParams({
9895
fromObject: {
9996
type: 'all',
@@ -120,9 +117,6 @@ describe('OrganizationService', () => {
120117
expect(httpSpy.get).toHaveBeenCalledWith(
121118
'https://api.github.com/orgs/OtherFakeCo/repos',
122119
jasmine.objectContaining({
123-
headers: {
124-
Accept: 'application/vnd.github.v3+json',
125-
},
126120
params: new HttpParams({
127121
fromObject: {
128122
type: 'public',

angular-ngrx-scss/src/app/organization/services/organization.service.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ export class OrganizationService {
3030
)}/repos`;
3131

3232
return this.http.get<OrganizationReposApiResponse>(url, {
33-
headers: {
34-
Accept: 'application/vnd.github.v3+json',
35-
},
3633
params: new HttpParams({
3734
fromObject: { ...Object.assign(defaultParams, params) },
3835
}),

angular-ngrx-scss/src/app/repository/services/repository.service.spec.ts

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,6 @@ describe('RepositoryService', () => {
290290

291291
expect(httpClientSpy.get).toHaveBeenCalledOnceWith(
292292
`https://api.github.com/repos/thisdot/starter.dev-github-showcases`,
293-
jasmine.objectContaining({
294-
headers: {
295-
Accept: 'application/vnd.github.v3+json',
296-
},
297-
}),
298293
);
299294
done();
300295
},
@@ -371,11 +366,6 @@ describe('RepositoryService', () => {
371366

372367
expect(httpClientSpy.get).toHaveBeenCalledOnceWith(
373368
'https://api.github.com/repos/thisdot/starter.dev-github-showcases/contents/README.md',
374-
jasmine.objectContaining({
375-
headers: {
376-
Accept: 'application/vnd.github.v3+json',
377-
},
378-
}),
379369
);
380370
});
381371
});
@@ -391,11 +381,6 @@ describe('RepositoryService', () => {
391381

392382
expect(httpClientSpy.get).toHaveBeenCalledWith(
393383
`https://api.github.com/repos/FakeCo/fake-repo/pulls/${MOCK_PULL_REQUEST_NUMBER}`,
394-
jasmine.objectContaining({
395-
headers: {
396-
Accept: 'application/vnd.github.v3+json',
397-
},
398-
}),
399384
);
400385
},
401386
complete: done,
@@ -414,9 +399,7 @@ describe('RepositoryService', () => {
414399
expect(httpClientSpy.get).toHaveBeenCalledWith(
415400
`https://api.github.com/search/issues?q=repo:FakeCo/fake-repo+type:pr+state:all`,
416401
jasmine.objectContaining({
417-
headers: {
418-
Accept: 'application/vnd.github.v3+json',
419-
},
402+
observe: 'response',
420403
}),
421404
);
422405
},
@@ -439,11 +422,6 @@ describe('RepositoryService', () => {
439422

440423
expect(httpClientSpy.get).toHaveBeenCalledWith(
441424
`https://api.github.com/repos/FakeCo/fake-repo/issues/${MOCK_PULL_REQUEST_NUMBER}/comments`,
442-
jasmine.objectContaining({
443-
headers: {
444-
Accept: 'application/vnd.github.v3+json',
445-
},
446-
}),
447425
);
448426
},
449427
complete: done,
@@ -460,9 +438,7 @@ describe('RepositoryService', () => {
460438
expect(httpClientSpy.get).toHaveBeenCalledWith(
461439
'https://api.github.com/search/issues?q=repo:FakeCo/fake-repo+type:issue+state:all',
462440
jasmine.objectContaining({
463-
headers: {
464-
Accept: 'application/vnd.github.v3+json',
465-
},
441+
observe: 'response',
466442
}),
467443
);
468444
},
@@ -482,9 +458,7 @@ describe('RepositoryService', () => {
482458
expect(httpClientSpy.get).toHaveBeenCalledWith(
483459
'https://api.github.com/search/issues?q=repo:FakeCo/fake-repo+type:issue+state:closed',
484460
jasmine.objectContaining({
485-
headers: {
486-
Accept: 'application/vnd.github.v3+json',
487-
},
461+
observe: 'response',
488462
}),
489463
);
490464
},
@@ -510,11 +484,6 @@ describe('RepositoryService', () => {
510484
.toBe(1);
511485
expect(httpClientSpy.get).toHaveBeenCalledOnceWith(
512486
'https://api.github.com/repos/thisdot/starter.dev-github-showcases/pulls/1',
513-
jasmine.objectContaining({
514-
headers: {
515-
Accept: 'application/vnd.github.v3+json',
516-
},
517-
}),
518487
);
519488
});
520489
});

angular-ngrx-scss/src/app/repository/services/repository.service.ts

Lines changed: 8 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ export class RepositoryService {
4343
const name = encodeURIComponent(repoName);
4444
const url = `${environment.githubUrl}/repos/${owner}/${name}`;
4545

46-
return this.http.get<RepoApiResponse>(url, {
47-
headers: {
48-
Accept: 'application/vnd.github.v3+json',
49-
},
50-
});
46+
return this.http.get<RepoApiResponse>(url);
5147
}
5248

5349
getRepositoryPullRequestsCount(
@@ -61,9 +57,6 @@ export class RepositoryService {
6157
return this.http
6258
.get<PullRequests>(url, {
6359
observe: 'response',
64-
headers: {
65-
Accept: 'application/vnd.github.v3+json',
66-
},
6760
params: new HttpParams({
6861
fromObject: {
6962
state: 'open',
@@ -99,9 +92,6 @@ export class RepositoryService {
9992
return this.http
10093
.get(url, {
10194
observe: 'response',
102-
headers: {
103-
Accept: 'application/vnd.github.v3+json',
104-
},
10595
})
10696
.pipe(
10797
map((response) => {
@@ -137,11 +127,7 @@ export class RepositoryService {
137127
const name = encodeURIComponent(repoName);
138128
const url = `${environment.githubUrl}/repos/${owner}/${name}/milestones`;
139129

140-
return this.http.get<Milestone[]>(url, {
141-
headers: {
142-
Accept: 'application/vnd.github.v3+json',
143-
},
144-
});
130+
return this.http.get<Milestone[]>(url);
145131
}
146132

147133
/**
@@ -158,11 +144,7 @@ export class RepositoryService {
158144
const name = encodeURIComponent(repoName);
159145
const url = `${environment.githubUrl}/repos/${owner}/${name}/labels`;
160146

161-
return this.http.get<IssueLabel[]>(url, {
162-
headers: {
163-
Accept: 'application/vnd.github.v3+json',
164-
},
165-
});
147+
return this.http.get<IssueLabel[]>(url);
166148
}
167149

168150
/**
@@ -182,11 +164,7 @@ export class RepositoryService {
182164
const pullId = encodeURIComponent(pullNumber);
183165
const url = `${environment.githubUrl}/repos/${owner}/${name}/pulls/${pullId}`;
184166

185-
return this.http.get<PullRequest>(url, {
186-
headers: {
187-
Accept: 'application/vnd.github.v3+json',
188-
},
189-
});
167+
return this.http.get<PullRequest>(url);
190168
}
191169

192170
/**
@@ -206,11 +184,7 @@ export class RepositoryService {
206184
const pullId = encodeURIComponent(pullNumber);
207185
const url = `${environment.githubUrl}/repos/${owner}/${name}/issues/${pullId}/comments`;
208186

209-
return this.http.get<IssueComments>(url, {
210-
headers: {
211-
Accept: 'application/vnd.github.v3+json',
212-
},
213-
});
187+
return this.http.get<IssueComments>(url);
214188
}
215189

216190
/**
@@ -244,9 +218,6 @@ export class RepositoryService {
244218
return this.http
245219
.get(url, {
246220
observe: 'response',
247-
headers: {
248-
Accept: 'application/vnd.github.v3+json',
249-
},
250221
})
251222
.pipe(
252223
map((response) => {
@@ -295,11 +266,7 @@ export class RepositoryService {
295266
url += `?ref=${refPath}`;
296267
}
297268

298-
return this.http.get<RepoContentsApiResponse[]>(url, {
299-
headers: {
300-
Accept: 'application/vnd.github.v3+json',
301-
},
302-
});
269+
return this.http.get<RepoContentsApiResponse[]>(url);
303270
}
304271

305272
/**
@@ -326,11 +293,7 @@ export class RepositoryService {
326293
url += `?ref=${refPath}`;
327294
}
328295

329-
return this.http.get<FileContentsApiResponse>(url, {
330-
headers: {
331-
Accept: 'application/vnd.github.v3+json',
332-
},
333-
});
296+
return this.http.get<FileContentsApiResponse>(url);
334297
}
335298

336299
/**
@@ -347,11 +310,7 @@ export class RepositoryService {
347310
const name = encodeURIComponent(repoName);
348311
const url = `${environment.githubUrl}/repos/${owner}/${name}/readme`;
349312

350-
return this.http.get<ReadmeApiResponse>(url, {
351-
headers: {
352-
Accept: 'application/vnd.github.v3+json',
353-
},
354-
});
313+
return this.http.get<ReadmeApiResponse>(url);
355314
}
356315

357316
private extractTotalFromLinkHeader(linkHeader: string | null): number {

angular-ngrx-scss/src/app/user/services/user.service.spec.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ describe('UserService', () => {
4545

4646
expect(httpClientSpy.get).toHaveBeenCalledOnceWith(
4747
`https://api.github.com/user`,
48-
jasmine.objectContaining({
49-
headers: {
50-
Accept: 'application/vnd.github.v3+json',
51-
},
52-
}),
5348
);
5449
},
5550
complete: done,
@@ -84,11 +79,6 @@ describe('UserService', () => {
8479
next: () => {
8580
expect(httpClientSpy.get).toHaveBeenCalledWith(
8681
`https://api.github.com/users/thisdot/orgs`,
87-
jasmine.objectContaining({
88-
headers: {
89-
Accept: 'application/vnd.github.v3+json',
90-
},
91-
}),
9282
);
9383
},
9484
complete: done,
@@ -211,11 +201,6 @@ describe('UserService', () => {
211201
next: () => {
212202
expect(httpClientSpy.get).toHaveBeenCalledWith(
213203
`https://api.github.com/users/thisdot/repos`,
214-
jasmine.objectContaining({
215-
headers: {
216-
Accept: 'application/vnd.github.v3+json',
217-
},
218-
}),
219204
);
220205
},
221206
complete: done,
@@ -339,9 +324,6 @@ describe('UserService', () => {
339324
expect(httpClientSpy.get).toHaveBeenCalledWith(
340325
`https://api.github.com/users/thisdot/repos`,
341326
jasmine.objectContaining({
342-
headers: {
343-
Accept: 'application/vnd.github.v3+json',
344-
},
345327
params: new HttpParams({
346328
fromObject: {
347329
sort: 'updated',
@@ -369,11 +351,6 @@ describe('UserService', () => {
369351
next: () => {
370352
expect(httpClientSpy.get).toHaveBeenCalledWith(
371353
`https://api.github.com/users/thisdot/gists`,
372-
jasmine.objectContaining({
373-
headers: {
374-
Accept: 'application/vnd.github.v3+json',
375-
},
376-
}),
377354
);
378355
},
379356
complete: done,

0 commit comments

Comments
 (0)