Back to packages list

Vals using uuid

Description from the NPM package:
RFC4122 (v1, v4, and v5) UUIDs
1
2
3
4
5
6
7
8
9
10
11
import { v1 as uuidv1, v4 as uuidv4 } from "npm:uuid";
export default async function(req: Request): Promise<Response> {
return Response.json({
success: true,
data: {
v1: uuidv1(),
v4: uuidv4(),
},
});
}

uuid

The uuid module provides builders for different types of universally unique identifiers. These are kinds of identifiers that you can use instead of auto-incrementing integers. They're intended to contain so much randomness that the chances of a collision between two uuids is very low.

There are different versions of uuids available: typically people use v4 UUIDs, which are fully random.

1
2
3
4
export const uuidExample = (async () => {
const uuid = await import("npm:uuid");
return uuid.v4();
})();
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
import { saveFile } from "https://esm.town/v/omgwtfbrblolttyl/saveFile";
import { signToken } from "https://esm.town/v/omgwtfbrblolttyl/signToken";
type Poll = {
id?: string;
owner: {
email: string;
};
participants: {
email: string;
}[];
optionsList: {
id?: string;
}[];
};
export const createPollEndpoint = async (
req: express.Request,
res: express.Response,
) => {
const { v4: uuid } = await import("npm:uuid");
if (req.method !== "POST") {
return res.status(404).json({ msg: "Not found" });
}
const poll = req.body;
if (typeof poll !== "object") {
return res.status(400).json({ msg: "Bad request" });
}
// Create Id for poll
poll.id = uuid();
// Create ids for options
poll.optionsList = poll.optionsList
.map((opt) => ({
...opt,
id: uuid(),
}));
// Create tokens for participants
const mkToken = (user, extra: {
read?: string[];
write?: string[];
list?: string[];
meta?: Record<string, any>;
} = {}) =>
signToken({
read: [
`/polls/${poll.id}/poll.json`,
`/polls/${poll.id}/participants/*.json`,
`/polls/${poll.id}/rankings/*.json`,
...(extra.read || []),
],
write: [
`/polls/${poll.id}/rankings/${user.email}.json`,
`/polls/${poll.id}/logs/${user.email}/*.json`,
...(extra.write || []),
],
list: [],
meta: {
email: user.email,
...(extra.meta || {}),
},
});
// TODO: make this actually private
const privatePollPath = `/polls/${poll.id}/owner/poll.json`;
const ownerToken = await mkToken(poll.owner, {
read: [privatePollPath],
});
const participantTokens = await Promise.all(
poll.participants.map((p) => mkToken(p)),
);
const withTokens = {
...poll,
owner: {
...poll.owner,
token: ownerToken,
},
participants: poll.participants.map((p, idx) => ({
...p,
token: participantTokens[idx],
})),
};
const pollFile = JSON.stringify(poll);
const privatePollFile = JSON.stringify(withTokens);
await Promise.all([
saveFile(
`/polls/${poll.id}/poll.json`,
pollFile,
),
saveFile(privatePollPath, privatePollFile),
]);
return res.status(201).json(withTokens);
};
1
Next