|
1 | | -import axios from 'axios' |
| 1 | +import { ajax } from 'rxjs/ajax' |
2 | 2 | import parseLink, { Links } from 'parse-link-header' |
| 3 | +import { map, pluck } from 'rxjs/operators' |
| 4 | +import { Observable } from 'rxjs' |
3 | 5 |
|
4 | 6 | export interface Label { |
5 | 7 | id: number |
@@ -64,47 +66,40 @@ const getPageCount = (pageLinks: Links) => { |
64 | 66 | } |
65 | 67 | } |
66 | 68 |
|
67 | | -export async function getIssues( |
| 69 | +export function getIssues( |
68 | 70 | org: string, |
69 | 71 | repo: string, |
70 | 72 | page = 1 |
71 | | -): Promise<IssuesResult> { |
| 73 | +): Observable<IssuesResult> { |
72 | 74 | const url = `https://api.github.com/repos/${org}/${repo}/issues?per_page=25&page=${page}` |
73 | | - |
74 | | - try { |
75 | | - const issuesResponse = await axios.get<Issue[]>(url) |
76 | | - let pageCount = 0 |
77 | | - const pageLinks = parseLink(issuesResponse.headers.link) |
78 | | - |
79 | | - if (pageLinks !== null) { |
80 | | - pageCount = getPageCount(pageLinks) |
81 | | - } |
82 | | - |
83 | | - return { |
84 | | - pageLinks, |
85 | | - pageCount, |
86 | | - issues: issuesResponse.data, |
87 | | - } |
88 | | - } catch (err) { |
89 | | - throw err |
90 | | - } |
| 75 | + return ajax.get(url).pipe( |
| 76 | + map((r) => { |
| 77 | + let pageCount = 0 |
| 78 | + const pageLinks = parseLink(r.xhr.getResponseHeader('link') as string) |
| 79 | + |
| 80 | + if (pageLinks !== null) { |
| 81 | + pageCount = getPageCount(pageLinks) |
| 82 | + } |
| 83 | + |
| 84 | + return { |
| 85 | + pageLinks, |
| 86 | + pageCount, |
| 87 | + issues: r.response as Issue[], |
| 88 | + } |
| 89 | + }) |
| 90 | + ) |
91 | 91 | } |
92 | 92 |
|
93 | | -export async function getRepoDetails(org: string, repo: string) { |
| 93 | +export function getRepoOpenIssuesCount(org: string, repo: string) { |
94 | 94 | const url = `https://api.github.com/repos/${org}/${repo}` |
95 | | - |
96 | | - const { data } = await axios.get<RepoDetails>(url) |
97 | | - return data |
| 95 | + return ajax.getJSON<RepoDetails>(url).pipe(pluck('open_issues_count')) |
98 | 96 | } |
99 | 97 |
|
100 | | -export async function getIssue(org: string, repo: string, number: number) { |
| 98 | +export function getIssue(org: string, repo: string, number: number) { |
101 | 99 | const url = `https://api.github.com/repos/${org}/${repo}/issues/${number}` |
102 | | - |
103 | | - const { data } = await axios.get<Issue>(url) |
104 | | - return data |
| 100 | + return ajax.getJSON<Issue>(url) |
105 | 101 | } |
106 | 102 |
|
107 | | -export async function getComments(url: string) { |
108 | | - const { data } = await axios.get<Comment[]>(url) |
109 | | - return data |
| 103 | +export function getComments(url: string) { |
| 104 | + return ajax.getJSON<Comment[]>(url) |
110 | 105 | } |
0 commit comments