Centralized fetch methods

This commit is contained in:
squidfunk
2021-02-11 19:41:34 +01:00
parent 4808b46dfb
commit 884330da3b
21 changed files with 269 additions and 75 deletions

View File

@@ -21,14 +21,10 @@
*/
import { Repo, User } from "github-types"
import { Observable, from } from "rxjs"
import {
defaultIfEmpty,
filter,
map,
switchMap
} from "rxjs/operators"
import { Observable } from "rxjs"
import { defaultIfEmpty, map } from "rxjs/operators"
import { fetchJSON } from "~/browser"
import { round } from "~/utilities"
import { SourceFacts } from "../_"
@@ -51,10 +47,8 @@ export function fetchSourceFactsFromGitHub(
const url = typeof repo !== "undefined"
? `https://api.github.com/repos/${user}/${repo}`
: `https://api.github.com/users/${user}`
return from(fetch(url))
return fetchJSON<Repo & User>(url)
.pipe(
filter(res => res.status === 200),
switchMap(res => res.json()),
map(data => {
/* GitHub repository */

View File

@@ -21,14 +21,10 @@
*/
import { ProjectSchema } from "gitlab"
import { Observable, from } from "rxjs"
import {
defaultIfEmpty,
filter,
map,
switchMap
} from "rxjs/operators"
import { Observable } from "rxjs"
import { defaultIfEmpty, map } from "rxjs/operators"
import { fetchJSON } from "~/browser"
import { round } from "~/utilities"
import { SourceFacts } from "../_"
@@ -49,11 +45,9 @@ export function fetchSourceFactsFromGitLab(
base: string, project: string
): Observable<SourceFacts> {
const url = `https://${base}/api/v4/projects/${encodeURIComponent(project)}`
return from(fetch(url))
return fetchJSON<ProjectSchema>(url)
.pipe(
filter(res => res.status === 200),
switchMap(res => res.json()),
map(({ star_count, forks_count }: ProjectSchema) => ([
map(({ star_count, forks_count }) => ([
`${round(star_count)} Stars`,
`${round(forks_count)} Forks`
])),