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 });
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v1
October 23, 2023