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
import { fetchJSON } from "https://esm.town/v/mgruel/fetchJSON";
type PaginatedData<T> = {
data: T[];
links: {
prev?: string;
self: string;
next?: string;
};
};
export const paginateAPI = async <T extends any = any>(
url: string,
options?: RequestInit,
) => {
let result: T[] = [];
let nextUrl = url;
while (nextUrl) {
const { data, links } = await fetchJSON<PaginatedData<T>>(
nextUrl,
options,
);
result = result.concat(data ?? []);
nextUrl = links?.next;
}
return result;
};
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
import { fetchJSON } from "https://esm.town/v/mgruel/fetchJSON";
type PaginatedData<T> = {
data: T[];
links: {
prev?: string;
self: string;
next?: string;
};
};
export const pageApi = async <T extends any = any>(
url: string,
{ offset = 0, limit = 25 }: {
offset?: number;
limit?: number;
} = { offset: 0, limit: 25 },
options?: RequestInit,
) => {
let result: T[] = [];
let nextUrl = new URL(url);
nextUrl.searchParams.set("offset", `${offset}`);
nextUrl.searchParams.set("limit", `${limit}`);
while (nextUrl) {
const { data, links } = await fetchJSON<PaginatedData<T>>(
nextUrl,
options,
);
result = result.concat(data ?? []);
if ((data ?? []).length < limit) {
nextUrl = undefined;
}
else {
nextUrl = new URL(links.self);
const currOffset = parseInt(nextUrl.searchParams.get("offset"), 10);
nextUrl.searchParams.set("offset", `${currOffset + limit}`);
}
}
return result;
};
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
import { fetchJSON } from "https://esm.town/v/mgruel/fetchJSON";
type UserParams = {
username: string;
};
type ValParams = {
username: string;
valName: string;
token?: string;
};
type Params = UserParams | ValParams;
type UserResult = {
id: string;
username: string;
bio: string;
profileImageUrl: string;
};
type ValResult = {
id: string;
author: {
id: string;
username: string;
};
name: string;
code: string;
public: boolean;
privacy: "public" | "private";
version: number;
runEndAt: string;
runStartAt: string;
logs: any[];
output: object;
error: object | null;
readme: string | null;
likeCount: number;
referenceCount: number;
};
type Result<T> = T extends ValParams ? ValResult
: T extends UserParams ? UserResult
: never;
export function alias<P extends Params>(params: P): Promise<Result<P>> {
const username = params.username;
let url = `https://api.val.town/v1/alias/${username}`;
if ("valName" in params) {
url = url + `/${params.valName}`;
}
const headers: HeadersInit = {};
if ("valName" in params && params.token) {
headers.Authorization = `Bearer ${params.token}`;
}
return fetchJSON<Result<P>>(url, { headers });
}
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
import { searchParams } from "https://esm.town/v/mgruel/searchParams";
import { fetchJSON } from "https://esm.town/v/mgruel/fetchJSON";
type Reference = {
"reference": {
"id": string;
"username": string;
"author_id": string;
"name": string;
};
"dependsOn": {
"id": string;
"username": string;
"author_id": string;
"name": string;
};
"referencedAt": string;
};
export async function references({ token, since, until, offset, limit }: {
token: string;
since?: Date;
until?: Date;
offset?: Date;
limit?: Date;
}): Promise<Reference[]> {
const data = await fetchJSON<Reference[]>(
"https://api.val.town/v1/me/references?" +
searchParams({
since: since?.toISOString(),
until: until?.toISOString(),
offset: offset?.toString(),
limit: limit?.toString(),
}),
{
headers: {
Authorization: `Bearer ${token}`,
},
},
);
return data;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { fetchJSON } from "https://esm.town/v/mgruel/fetchJSON";
export function nominatimSearch(params: {
q?: string;
street?: string;
city?: string;
county?: string;
state?: string;
country?: string;
postalcode?: string;
} = {}) {
return fetchJSON(
"https://nominatim.openstreetmap.org/search?" +
new URLSearchParams({
format: "json",
...params,
}),
);
}
// Forked from @stevekrouse.nominatimSearch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { getExchangeRates } from "https://esm.town/v/mgruel/getExchangeRates";
/** Calls https://www.exchangerate-api.com to retrieve the currency exchange rates
* for the provided base currency or "eur" if undefined
* @param {string} desired - The desired currency convert to
* @param {number} amount - The amount of target currency
* @param {string} base - The base currency, from which the exchange should be calculated
*/
export async function exchangeCurrency(
desired: string,
amount = 1,
base = "eur",
): Promise<number | undefined> {
let { rates } = await getExchangeRates(base);
if (rates && rates[desired.toUpperCase()])
return (amount ?? 1) * (rates[desired.toUpperCase()]);
else {
return undefined;
}
}
1
Next