/
index.ts
58 lines (50 loc) · 1.37 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { ExternalHostError } from '../../types/errors/external-host-error';
import { Http, HttpResponse } from '../../util/http';
import { GetReleasesConfig, ReleaseResult } from '../common';
export const id = 'dart';
const http = new Http(id);
export async function getReleases({
lookupName,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
let result: ReleaseResult = null;
const pkgUrl = `https://pub.dartlang.org/api/packages/${lookupName}`;
interface DartResult {
versions?: {
version: string;
}[];
latest?: {
pubspec?: { homepage?: string; repository?: string };
};
}
let raw: HttpResponse<DartResult> = null;
try {
raw = await http.getJson<DartResult>(pkgUrl);
} catch (err) {
if (
err.statusCode === 429 ||
(err.statusCode >= 500 && err.statusCode < 600)
) {
throw new ExternalHostError(err);
}
throw err;
}
const body = raw && raw.body;
if (body) {
const { versions, latest } = body;
if (versions && latest) {
result = {
releases: body.versions.map(({ version }) => ({ version })),
};
const pubspec = latest.pubspec;
if (pubspec) {
if (pubspec.homepage) {
result.homepage = pubspec.homepage;
}
if (pubspec.repository) {
result.sourceUrl = pubspec.repository;
}
}
}
}
return result;
}