Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import ValTown from 'npm:@valtown/sdk';
import "jsr:@std/dotenv/load"; // needed for deno run; not req for smallweb or valtown
import { Sema } from 'npm:async-sema'; // Import async-sema
const client = new ValTown();
/**
* Retrieves a user by their username.
* @param {string} username - The username of the user to retrieve.
* @returns {Promise<Object>} The user object.
*/
export async function getUser(username) {
const user = await client.alias.username.retrieve(username);
return user;
}
/**
* Retrieves values associated with a user by their username.
* @param {string} username - The username of the user whose values are to be retrieved.
* @returns {Promise<Array>} An array of values associated with the user.
*/
export async function getVals(username) {
const user = await getUser(username);
const valsArray = []; // Initialize an array to store values
if (user && user.id) {
// Automatically fetches more pages as needed.
for await (const basicVal of client.users.vals.list(user.id, {
limit: 100,
offset: 0,
})) {
valsArray.push(basicVal); // Add each value to the array
}
} else {
console.error('User not found or invalid user ID');
}
return valsArray; // Return the array of values
}
/**
* Retrieves versions of a value by its ID or object.
* @param {string|Object} valOrId - The value ID or value object to retrieve versions for.
* @returns {Promise<Array>} An array of versions associated with the value.
*/
export async function getValVersions(valOrId) {
const versionsArray = []; // Initialize an array to store versions
const valId = typeof valOrId === 'string' ? valOrId : valOrId.id; // Determine if input is a val or valId
// Automatically fetches more pages as needed.
for await (const versionListResponse of client.vals.versions.list(valId, {
limit: 20, // Default limit
offset: 0, // Start from the first item
})) {
versionsArray.push(versionListResponse); // Add each version to the array
}
return versionsArray; // Return the array of versions
}
/**
* Retrieves values along with their versions for a user by their username.
* @param {string} username - The username of the user whose values with versions are to be retrieved.
* @returns {Promise<Array>} An array of values with their associated versions.
*/
export async function getValsWithVersions(username, concurrency = 40) {
const user = await getUser(username);
const valsArray = []; // Initialize an array to store values with versions
if (user && user.id) {
// Automatically fetches more pages as needed.
const semaphore = new Sema(concurrency); // Assuming 5 concurrent operations are allowed
const processVal = async (basicVal) => {
await semaphore.acquire(); // Acquire a semaphore before processing
const versions = await getValVersions(basicVal.id); // Get versions for each val
valsArray.push({ ...basicVal, versions }); // Add each val with its versions to the array
semaphore.release(); // Release the semaphore after processing
};
const valPromises = [];
for await (const basicVal of client.users.vals.list(user.id, {
limit: 100,
offset: 0,
})) {
valPromises.push(processVal(basicVal));
}
await Promise.all(valPromises); // Wait for all promises to resolve
} else {
console.error('User not found or invalid user ID');
}
return valsArray; // Return the array of vals with versions
}
September 9, 2024