nbbaier-mocktoapi.web.val.run
Readme

This val demonstrates the creation of an API with a programmatically generated set of data instead of an existing Val Town lowdb instance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { createServer } from "https://esm.town/v/nbbaier/dbToAPI";
import { faker } from "npm:@faker-js/faker";
const mock = () => {
const data = { users: [], posts: [], random: {} };
for (let i = 0; i < 25; i++) {
const user: string = faker.person.fullName(); // Rowan Nikolaus
const userSplit: string[] = user.split(" ");
const email: string = userSplit[0].endsWith(".")
? userSplit[1].toLowerCase() + "." + userSplit[2].toLowerCase() + "@"
+ faker.internet.domainName()
: userSplit[0].toLowerCase() + "." + user.split(" ")[1].toLowerCase() + "@"
+ faker.internet.domainName();
data.users.push({ id: i + 1, user, email });
data.posts.push({ id: i + 1, title: faker.lorem.sentence({ min: 3, max: 7 }) });
}
return { data };
};
const app = await createServer(mock(), { auth: { username: "nb", password: "123" } });
export default app.fetch;
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!
December 8, 2023