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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { api } from "https://esm.town/v/iamseeley/api";
let cachedUser = null;
const valTownUser = {
async getUserInfo() {
if (!cachedUser) {
try {
cachedUser = await api('/v1/me', { authenticated: true });
} catch (error) {
console.error('Error fetching user info:', error);
return null;
}
}
return cachedUser;
},
async getUsername() {
const userInfo = await this.getUserInfo();
return userInfo?.username;
},
async getId() {
const userInfo = await this.getUserInfo();
return userInfo?.id;
},
async getEndpointUrl(valName) {
const username = await this.getUsername();
return `https://${username}-${valName}.web.val.run`;
},
async getValUrl(valName) {
const username = await this.getUsername();
return `https://val.town/v/${username}/${valName}`;
},
async getBio() {
const userInfo = await this.getUserInfo();
return userInfo?.bio;
},
async getProfileImageUrl() {
const userInfo = await this.getUserInfo();
return userInfo?.profileImageUrl;
},
async getEmail() {
const userInfo = await this.getUserInfo();
return userInfo?.email;
},
async getTier() {
const userInfo = await this.getUserInfo();
return userInfo?.tier;
},
async getLikedVals(options = {}) {
const { offset = 0, limit = 20 } = options;
return api(`/v1/me/likes?offset=${offset}&limit=${limit}`, { authenticated: true });
},
async getComments(options = {}) {
const { offset = 0, limit = 20, since, until, relationship = 'any' } = options;
let url = `/v1/me/comments?offset=${offset}&limit=${limit}&relationship=${relationship}`;
if (since) url += `&since=${since}`;
if (until) url += `&until=${until}`;
return api(url, { authenticated: true });
},
async getReferences(options = {}) {
const { offset = 0, limit = 20, since, until } = options;
let url = `/v1/me/references?offset=${offset}&limit=${limit}`;
if (since) url += `&since=${since}`;
if (until) url += `&until=${until}`;
return api(url, { authenticated: true });
},
async listBlobs(prefix) {
let url = '/v1/blob';
if (prefix) url += `?prefix=${encodeURIComponent(prefix)}`;
return api(url, { authenticated: true });
},
async getBlob(key) {
return api(`/v1/blob/${encodeURIComponent(key)}`, {
authenticated: true,
headers: {
'Accept': 'application/octet-stream'
},
parseResponse: false
});
},
async storeBlob(key, data) {
return api(`/v1/blob/${encodeURIComponent(key)}`, {
method: 'POST',
body: data,
authenticated: true
});
},
async deleteBlob(key) {
return api(`/v1/blob/${encodeURIComponent(key)}`, {
method: 'DELETE',
authenticated: true
});
},
async listVals(options = {}) {
const { offset = 0, limit = 20 } = options;
const userInfo = await this.getUserInfo();
return api(`/v1/users/${userInfo.id}/vals?offset=${offset}&limit=${limit}`, { authenticated: true });
},
async createVal(valInfo) {
return api('/v1/vals', {