nbbaier/dbToAPI

References

Referenced 3 times

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

Readme
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;

Basic Auth for dbToAPI

This val demonstrates adding Basic auth to a dbToAPI API via Hono's [https://hono.dev/middleware/builtin/basic-auth](Basic Auth middleware). Just pass createServer an object of type {username: string; password: string}. The authentication is added to all routes.

Readme
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { createServer } from "https://esm.town/v/nbbaier/dbToAPI";
import { faker } from "npm:@faker-js/faker";
const db = {
data: {
posts: [
{
id: 1,
title: "Magna pars studiorum elit, prodita quaerimus.",
},
{
id: 2,
title: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
},
],
user: {
id: 1,
name: "Leanne Graham",
},
},
};
const app = await createServer(db, { auth: { username: "nb", password: "123" } });
export default app.fetch;

This val demonstrates the creation of an API for a Val Town lowdb instance.

Readme
1
2
3
4
5
6
import { createServer } from "https://esm.town/v/nbbaier/dbToAPI";
import { db } from "https://esm.town/v/nbbaier/lowdbTest";
const app = await createServer(db);
export default app.fetch;
1
Next