forked from djyde/cusdis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.server.ts
57 lines (51 loc) · 1.46 KB
/
utils.server.ts
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
import jwt from 'next-auth/jwt'
import { PrismaClient } from "@prisma/client";
import { UserSession } from './service';
import { getSession as nextAuthGetSession } from 'next-auth/client'
export const singleton = async <T>(id: string, fn: () => Promise<T>) => {
if (process.env.NODE_ENV === "production") {
return await fn();
} else {
if (!global[id]) {
global[id] = await fn();
}
return global[id] as T;
}
};
export const singletonSync = <T>(id: string, fn: () => T) => {
if (process.env.NODE_ENV === "production") {
return fn();
} else {
if (!global[id]) {
global[id] = fn();
}
return global[id] as T;
}
};
export const prisma = singletonSync("prisma", () => {
return new PrismaClient();
});
export function initMiddleware(middleware) {
return (req, res) =>
new Promise((resolve, reject) => {
middleware(req, res, (result) => {
if (result instanceof Error) {
return reject(result);
}
return resolve(result);
});
});
}
export const resolvedConfig = {
useLocalAuth: process.env.USERNAME && process.env.PASSWORD,
useGithub: process.env.GITHUB_ID && process.env.GITHUB_SECRET,
jwtSecret: process.env.JWT_SECRET,
umami: {
id: process.env.UMAMI_ID as string | undefined,
src: process.env.UMAMI_SRC as string | undefined,
},
};
export const getSession = async (req) => {
// @ts-expect-error
return (await nextAuthGetSession({ req })) as UserSession;
};