forked from t3-oss/create-t3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase-context-transform.ts
44 lines (37 loc) · 1.29 KB
/
base-context-transform.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
import { API, FileInfo } from "jscodeshift";
import addImports from "jscodeshift-add-imports";
import curryArrowFunctionHelpers from "../../../src/helpers/jscodeshift/curryArrowFunctionHelpers.js";
export interface BaseContextTransformOptions {
usingAuth?: boolean;
usingPrisma?: boolean;
}
export default function transformer(
file: FileInfo,
api: API,
options: BaseContextTransformOptions,
) {
const j = api.jscodeshift;
const s = j.template.statement;
const root = j(file.source);
const createContextHelpers = curryArrowFunctionHelpers({
collection: root,
name: "createContext",
});
if (options.usingAuth) {
addImports(root, [
s`import { unstable_getServerSession as getServerSession } from "next-auth";`,
s`import { authOptions as nextAuthOptions } from "../../pages/api/auth/[...nextauth]";`,
]);
createContextHelpers.ensureAsync();
createContextHelpers.insertBeforeReturnStatement(s`
const session =
req && res && (await getServerSession(req, res, nextAuthOptions));
`);
createContextHelpers.addReturnStatementProperty(s`session`);
}
if (options.usingPrisma) {
addImports(root, s`import { prisma } from "../db/client";`);
createContextHelpers.addReturnStatementProperty(s`prisma`);
}
return root.toSource();
}