Readme

👤 valTownUser

The valTownUser object provides a convenient interface to interact with Val Town's API and access user-specific information and functionalities.

User Information

getUserInfo()

Fetches and caches the user's information.

  • Use case: Retrieving all user details at once.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getUserInfo = () => valTownUser.getUserInfo(); // Example usage: const userInfo = await getUserInfo(); console.log('User Information:', userInfo); // You can also destructure specific properties you need: const { username, id, email, tier } = await getUserInfo(); console.log(`Username: ${username}`); console.log(`User ID: ${id}`); console.log(`Email: ${email}`); console.log(`Account Tier: ${tier}`);

getUsername()

Retrieves the user's username.

  • Use case: Displaying the current user's name in a val.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getUsername = () => valTownUser.getUsername(); // Example usage: const username = await getUsername(); console.log(`Current user: ${username}`);

getId()

Retrieves the user's ID.

  • Use case: Using the user ID for database queries or API calls.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getId = () => valTownUser.getId(); // Example usage: const userId = await getId(); console.log(`User ID: ${userId}`);

getBio()

Retrieves the user's biography.

  • Use case: Displaying the user's bio on a profile page.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getBio = () => valTownUser.getBio(); // Example usage: const bio = await getBio(); console.log(`User bio: ${bio || 'Not set'}`);

getProfileImageUrl()

Retrieves the URL of the user's profile image.

  • Use case: Showing the user's avatar in a val.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getProfileImageUrl = () => valTownUser.getProfileImageUrl(); // Example usage: const profileImageUrl = await getProfileImageUrl(); console.log(`Profile image URL: ${profileImageUrl || 'Not set'}`);

getEmail()

Retrieves the user's email address.

  • Use case: Sending notifications or verifications.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getEmail = () => valTownUser.getEmail(); // Example usage: const email = await getEmail(); console.log(`User email: ${email}`);

getTier()

Retrieves the user's account tier.

  • Use case: Implementing tier-specific features.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getTier = () => valTownUser.getTier(); // Example usage: const tier = await getTier(); console.log(`User tier: ${tier}`);

URL Generation

getEndpointUrl(valName)

Generates the endpoint URL for a specified val.

  • Use case: Creating links to val endpoints in your application.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getEndpointUrl = (valName: string) => valTownUser.getEndpointUrl(valName); // Example usage: const endpointUrl = await getEndpointUrl('mySitesServer'); console.log(`Endpoint URL: ${endpointUrl}`);

getValUrl(valName)

Generates the Val Town URL for a specified val.

  • Use case: Creating links to val pages in Val Town.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getValUrl = (valName: string) => valTownUser.getValUrl(valName); // Example usage: const valUrl = await getValUrl('myVal'); console.log(`Val URL: ${valUrl}`);

User Activity

getLikedVals(options)

Retrieves the vals liked by the user.

  • Use case: Displaying a list of the user's favorite vals.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getLikedVals = (options = {}) => valTownUser.getLikedVals(options); // Example usage: const likedVals = await getLikedVals({ limit: 5 }); console.log('Liked vals:', likedVals.data);

getComments(options)

Retrieves comments related to the user.

  • Use case: Showing a user's comment history.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getComments = (options = {}) => valTownUser.getComments(options); // Example usage: const comments = await getComments({ limit: 10, relationship: 'given' }); console.log('User comments:', comments.data);

getReferences(options)

Retrieves references to the user's vals.

  • Use case: Tracking how often a user's vals are used by others.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getReferences = (options = {}) => valTownUser.getReferences(options); // Example usage: const references = await getReferences({ limit: 5 }); console.log('Val references:', references.data);

Blob Management

listBlobs(prefix)

Lists the user's blobs.

  • Use case: Displaying a file manager for the user's stored data.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const listBlobs = (prefix?: string) => valTownUser.listBlobs(prefix); // Example usage: const blobs = await listBlobs(); console.log('User blobs:', blobs);

storeBlob(key, data)

Stores a new blob or updates an existing one.

  • Use case: Uploading user files or storing large datasets.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const storeBlob = (key: string, data: any) => valTownUser.storeBlob(key, data); // Example usage: await storeBlob('myFile.txt', 'Hello, World!'); console.log('Blob stored successfully');

getBlob(key)

Retrieves a specific blob.

  • Use case: Downloading a user's stored file.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const getBlob = async (key: string) => { const blobData = await valTownUser.getBlob(key); return new TextDecoder().decode(new Uint8Array(blobData)); }; // Example usage: const blobContent = await getBlob('myFile.txt'); console.log('Blob content:', blobContent);

deleteBlob(key)

Deletes a specific blob.

  • Use case: Removing old or unnecessary files.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const deleteBlob = (key: string) => valTownUser.deleteBlob(key); // Example usage: await deleteBlob('myFile.txt'); console.log('Blob deleted successfully');

Val Management

listVals(options)

Lists the user's vals.

  • Use case: Displaying a dashboard of the user's created vals.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const listVals = (options = {}) => valTownUser.listVals(options); // Example usage: const userVals = await listVals({ limit: 5 }); console.log('User vals:', userVals.data);

createVal(valInfo)

Creates a new val.

  • Use case: Programmatically creating vals based on user input.
import { valTownUser } from "https://esm.town/v/iamseeley/valTownUser"; export const createVal = (valInfo: any) => valTownUser.createVal(valInfo); // Example usage: const newVal = await createVal({ name: 'myNewVal', code: 'console.log("Hello, World!");' }); console.log('Created val:', newVal);

updateVal(valId, updates)

Updates an existing val.

  • Use case: Implementing an "edit val" feature in your application.

deleteVal(valId)

Deletes a specific val.

  • Use case: Allowing users to remove their vals through your interface.

SQLite Database Operations

executeSqlite(statement)

Executes a SQLite statement.

  • Use case: Running custom queries on the user's database.

listTables()

Lists all tables in the user's SQLite database.

  • Use case: Displaying the structure of a user's database.

getTableSchema(tableName)

Retrieves the schema of a specific table.

  • Use case: Showing the structure of a particular table.

getTableContent(tableName, limit)

Retrieves the content of a specific table.

  • Use case: Displaying the data stored in a user's table.
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', {
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!
August 26, 2024