1- import { HttpClient , HttpParams } from '@angular/common/http' ;
1+ import { HttpClient , HttpHeaders , HttpParams } from '@angular/common/http' ;
22import { Injectable } from '@angular/core' ;
33import { Observable , map } from 'rxjs' ;
44import {
55 FileContentsApiResponse ,
66 IssueAPIResponse ,
77 IssueLabel ,
88 Milestone ,
9+ PaginationParams ,
910 PullRequestAPIResponse ,
1011 ReadmeApiResponse ,
1112 RepoApiResponse ,
@@ -20,7 +21,6 @@ import {
2021 PullRequest ,
2122 PullRequests ,
2223 RepositoryIssuesApiParams ,
23- RepositoryPullsApiParams ,
2424} from './repository.interfaces' ;
2525
2626@Injectable ( {
@@ -87,24 +87,14 @@ export class RepositoryService {
8787 getRepositoryPullRequests (
8888 repoOwner : string ,
8989 repoName : string ,
90- params : RepositoryPullsApiParams ,
90+ params : RepositoryIssuesApiParams ,
9191 ) : Observable < RepoPullRequests > {
9292 const owner = encodeURIComponent ( repoOwner ) ;
9393 const name = encodeURIComponent ( repoName ) ;
9494 const state = encodeURIComponent ( params . state ) ;
9595 let url = `${ environment . githubUrl } /search/issues?q=repo:${ owner } /${ name } +type:pr+state:${ state } ` ;
9696
97- if ( params ?. labels ) {
98- url += `+label:"${ params . labels } "` ;
99- }
100-
101- if ( params ?. sort ) {
102- url += `+sort:${ params . sort } ` ;
103- }
104-
105- if ( params ?. page ) {
106- url += `&page=${ params . page } ` ;
107- }
97+ url = this . appendUrlParams ( url , params ) ;
10898
10999 return this . http
110100 . get ( url , {
@@ -115,22 +105,16 @@ export class RepositoryService {
115105 } )
116106 . pipe (
117107 map ( ( response ) => {
118- const linkHeader = response . headers . get ( 'Link' ) ;
119-
120- const canNext = ! ! ( linkHeader && linkHeader . includes ( 'rel="next"' ) ) ;
121- const canPrev = ! ! ( linkHeader && linkHeader . includes ( 'rel="prev"' ) ) ;
122-
123108 const data = response . body as PullRequestAPIResponse ;
124109
125110 const total = data . total_count ;
126111
127112 const page = params ?. page || 1 ;
128113
129- const paginationParams = {
130- canNext,
131- canPrev,
114+ const paginationParams = this . getPaginationParams (
115+ response . headers ,
132116 page ,
133- } ;
117+ ) ;
134118
135119 const pullRequests : PullRequest [ ] = data . items ;
136120
@@ -255,21 +239,7 @@ export class RepositoryService {
255239 const state = encodeURIComponent ( params ?. state ?? defaultParams . state ) ;
256240 let url = `${ environment . githubUrl } /search/issues?q=repo:${ owner } /${ name } +type:issue+state:${ state } ` ;
257241
258- if ( params ?. labels ) {
259- url += `+label:"${ params . labels } "` ;
260- }
261-
262- if ( params ?. milestone ) {
263- url += `+milestone:"${ params . milestone } "` ;
264- }
265-
266- if ( params ?. sort ) {
267- url += `+sort:${ params . sort } ` ;
268- }
269-
270- if ( params ?. page ) {
271- url += `&page=${ params . page } ` ;
272- }
242+ url = this . appendUrlParams ( url , params ) ;
273243
274244 return this . http
275245 . get ( url , {
@@ -280,22 +250,16 @@ export class RepositoryService {
280250 } )
281251 . pipe (
282252 map ( ( response ) => {
283- const linkHeader = response . headers . get ( 'Link' ) ;
284-
285- const canNext = ! ! ( linkHeader && linkHeader . includes ( 'rel="next"' ) ) ;
286- const canPrev = ! ! ( linkHeader && linkHeader . includes ( 'rel="prev"' ) ) ;
287-
288253 const data = response . body as IssueAPIResponse ;
289254
290255 const total = data . total_count ;
291256
292257 const page = params ?. page || 1 ;
293258
294- const paginationParams = {
295- canNext,
296- canPrev,
259+ const paginationParams = this . getPaginationParams (
260+ response . headers ,
297261 page ,
298- } ;
262+ ) ;
299263
300264 const issues : Issue [ ] = data . items ;
301265
@@ -420,4 +384,42 @@ export class RepositoryService {
420384
421385 return page ;
422386 }
387+
388+ private appendUrlParams (
389+ url : string ,
390+ params ?: RepositoryIssuesApiParams ,
391+ ) : string {
392+ if ( params ?. labels ) {
393+ url += `+label:"${ params . labels } "` ;
394+ }
395+
396+ if ( params ?. milestone ) {
397+ url += `+milestone:"${ params . milestone } "` ;
398+ }
399+
400+ if ( params ?. sort ) {
401+ url += `+sort:${ params . sort } ` ;
402+ }
403+
404+ if ( params ?. page ) {
405+ url += `&page=${ params . page } ` ;
406+ }
407+ return url ;
408+ }
409+
410+ private getPaginationParams (
411+ headers : HttpHeaders ,
412+ page : number ,
413+ ) : PaginationParams {
414+ const linkHeader = headers . get ( 'Link' ) ;
415+
416+ const canNext = ! ! ( linkHeader && linkHeader . includes ( 'rel="next"' ) ) ;
417+ const canPrev = ! ! ( linkHeader && linkHeader . includes ( 'rel="prev"' ) ) ;
418+
419+ return {
420+ canNext,
421+ canPrev,
422+ page,
423+ } ;
424+ }
423425}
0 commit comments