Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Support all id-like keys with TWT
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Aug 11, 2020
1 parent e70fa98 commit 8327891
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/_staart/helpers/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,30 @@ import { config } from "@anandchowdhary/cosmic";
export const prisma = new PrismaClient({
log: getConfig("NODE_ENV") === "production" ? ["warn"] : ["info", "warn"],
});

prisma.$use(async (params, next) => {
const result = await next(params);
console.log("Got result", result);
console.log("Secret it", config("twtSecret"));

// Use TWT IDs for objects
if (typeof result === "object" && !Array.isArray(result))
if (typeof result.id === "number")
result.id = sign(String(result.id), config("twtSecret"), 10);
Object.keys(result).forEach((key) => {
if (
(key === "id" || key.endsWith("Id")) &&
typeof result[key] === "number"
)
result[key] = sign(String(result[key]), config("twtSecret"), 10);
});

// Use TWT IDs for arrays of objects
if (Array.isArray(result))
result.map((result) => {
if (typeof result.id === "number")
result.id = sign(String(result.id), config("twtSecret"), 10);
Object.keys(result).forEach((key) => {
if (
(key === "id" || key.endsWith("Id")) &&
typeof result[key] === "number"
)
result[key] = sign(String(result[key]), config("twtSecret"), 10);
});
return result;
});
return result;
Expand Down

0 comments on commit 8327891

Please sign in to comment.