Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'zod' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
app.post("/v1/conduit/register", async (req, res) => {
// Check that they provided a public key.
if (!z.object({
pubkey: z.string()
}).check(req.body)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
// Generate a new unique code.
const code = await db.generateCode(req.body.pubkey);
console.log("[+] New Conduit registered. Code: " + code);
// Sign a JWT for the machine and return it.
res.json({
ok: true,
token: tokens.createConnectionToken(code)
});
app.post("/v1/notifications/register", async (req, res) => {
if (!z.object({
uuid: z.string(),
platform: z.string(),
token: z.string().nullable()
}).check(req.body) || !NOTIFICATION_PLATFORMS.includes(req.body.platform)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
console.log(`[+] Updated notification token for ${req.body.uuid} to '${req.body.token}'`);
await db.registerOrUpdateDeviceNotifications(req.body.uuid, req.body.platform as NotificationPlatform, req.body.token);
return res.json({
ok: true
});
});
app.post("/v1/notifications/respond", async (req, res) => {
if (!z.object({
token: z.string(),
response: z.string()
}).check(req.query)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
const token = await tokens.verifyInstantResponseToken(req.query.token);
if (!token) {
return res.status(400).json({
ok: false,
error: "Invalid token."
});
}
console.log("[+] Got response " + req.query.response + " for computer " + token.code);
app.post("/v1/notifications/register", async (req, res) => {
if (!z.object({
uuid: z.string(),
platform: z.string(),
token: z.string().nullable()
}).check(req.body) || !NOTIFICATION_PLATFORMS.includes(req.body.platform)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
console.log(`[+] Updated notification token for ${req.body.uuid} to '${req.body.token}'`);
await db.registerOrUpdateDeviceNotifications(req.body.uuid, req.body.platform as NotificationPlatform, req.body.token);
return res.json({
ok: true
});
});
app.post("/v1/notifications/subscribe", async (req, res) => {
if (!z.object({
uuid: z.string(),
token: z.string(),
type: z.string()
}).check(req.body) || !NOTIFICATION_TYPES.includes(req.body.type)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
const code = await tokens.verifyPushNotificationToken(req.body.token);
if (!code) return res.status(403).json({
ok: false,
error: "Invalid token."
});
console.log(`[+] Device ${req.body.uuid} subscribed to ${req.body.type} notifications.`);
await db.subscribeDeviceNotifications(req.body.uuid, code, req.body.type as NotificationType);
app.post("/v1/notifications/register", async (req, res) => {
if (!z.object({
uuid: z.string(),
platform: z.string(),
token: z.string().nullable()
}).check(req.body) || !NOTIFICATION_PLATFORMS.includes(req.body.platform)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
console.log(`[+] Updated notification token for ${req.body.uuid} to '${req.body.token}'`);
await db.registerOrUpdateDeviceNotifications(req.body.uuid, req.body.platform as NotificationPlatform, req.body.token);
return res.json({
ok: true
});
});
app.post("/v1/notifications/respond", async (req, res) => {
if (!z.object({
token: z.string(),
response: z.string()
}).check(req.query)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
const token = await tokens.verifyInstantResponseToken(req.query.token);
if (!token) {
return res.status(400).json({
ok: false,
error: "Invalid token."
});
}
app.post("/v1/notifications/subscribe", async (req, res) => {
if (!z.object({
uuid: z.string(),
token: z.string(),
type: z.string()
}).check(req.body) || !NOTIFICATION_TYPES.includes(req.body.type)) {
return res.status(400).json({
ok: false,
error: "Invalid request."
});
}
const code = await tokens.verifyPushNotificationToken(req.body.token);
if (!code) return res.status(403).json({
ok: false,
error: "Invalid token."
});