nextjs-cms 0.9.21 → 0.9.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -13
- package/dist/api/actions/files.d.ts +30 -0
- package/dist/api/actions/files.d.ts.map +1 -0
- package/dist/api/actions/files.js +234 -0
- package/dist/api/actions/index.d.ts +4 -0
- package/dist/api/actions/index.d.ts.map +1 -0
- package/dist/api/actions/index.js +3 -0
- package/dist/api/actions/pages.d.ts +297 -0
- package/dist/api/actions/pages.d.ts.map +1 -0
- package/dist/api/actions/pages.js +1215 -0
- package/dist/api/actions/privileges.d.ts +25 -0
- package/dist/api/actions/privileges.d.ts.map +1 -0
- package/dist/api/actions/privileges.js +98 -0
- package/dist/api/client/index.d.ts +4 -0
- package/dist/api/client/index.d.ts.map +1 -0
- package/dist/api/client/index.js +3 -0
- package/dist/api/client.d.ts +30 -0
- package/dist/api/client.d.ts.map +1 -0
- package/dist/api/client.js +82 -0
- package/dist/api/index.d.ts +1 -938
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +0 -13
- package/dist/api/lib/serverActions.d.ts +3 -3
- package/dist/api/plugin/index.d.ts +7 -0
- package/dist/api/plugin/index.d.ts.map +1 -0
- package/dist/api/plugin/index.js +5 -0
- package/dist/api/root.d.ts +18 -1844
- package/dist/api/root.d.ts.map +1 -1
- package/dist/api/root.js +18 -83
- package/dist/api/routers/navigation.d.ts +3 -3
- package/dist/api/server/index.d.ts +8 -0
- package/dist/api/server/index.d.ts.map +1 -0
- package/dist/api/server/index.js +3 -0
- package/dist/api/server.d.ts +2748 -0
- package/dist/api/server.d.ts.map +1 -0
- package/dist/api/server.js +100 -0
- package/dist/api/trpc/client.d.ts +19 -3
- package/dist/api/trpc/client.d.ts.map +1 -1
- package/dist/api/trpc/client.js +55 -1
- package/dist/api/trpc/query-client.d.ts +3 -1
- package/dist/api/trpc/query-client.d.ts.map +1 -1
- package/dist/api/trpc/query-client.js +25 -20
- package/dist/api/trpc/root.d.ts +906 -0
- package/dist/api/trpc/root.d.ts.map +1 -0
- package/dist/api/trpc/root.js +47 -0
- package/dist/api/trpc/routers/accountSettings.d.ts +66 -0
- package/dist/api/trpc/routers/accountSettings.d.ts.map +1 -0
- package/dist/api/trpc/routers/accountSettings.js +200 -0
- package/dist/api/trpc/routers/admins.d.ts +112 -0
- package/dist/api/trpc/routers/admins.d.ts.map +1 -0
- package/dist/api/trpc/routers/admins.js +331 -0
- package/dist/api/trpc/routers/auth.d.ts +54 -0
- package/dist/api/trpc/routers/auth.d.ts.map +1 -0
- package/dist/api/trpc/routers/auth.js +50 -0
- package/dist/api/trpc/routers/categorySection.d.ts +105 -0
- package/dist/api/trpc/routers/categorySection.d.ts.map +1 -0
- package/dist/api/trpc/routers/categorySection.js +49 -0
- package/dist/api/trpc/routers/config.d.ts +48 -0
- package/dist/api/trpc/routers/config.d.ts.map +1 -0
- package/dist/api/trpc/routers/config.js +18 -0
- package/dist/api/trpc/routers/cpanel.d.ts +82 -0
- package/dist/api/trpc/routers/cpanel.d.ts.map +1 -0
- package/dist/api/trpc/routers/cpanel.js +216 -0
- package/dist/api/trpc/routers/fields.d.ts +35 -0
- package/dist/api/trpc/routers/fields.d.ts.map +1 -0
- package/dist/api/trpc/routers/fields.js +81 -0
- package/dist/api/trpc/routers/files.d.ts +34 -0
- package/dist/api/trpc/routers/files.d.ts.map +1 -0
- package/dist/api/trpc/routers/files.js +14 -0
- package/dist/api/trpc/routers/gallery.d.ts +35 -0
- package/dist/api/trpc/routers/gallery.d.ts.map +1 -0
- package/dist/api/trpc/routers/gallery.js +92 -0
- package/dist/api/trpc/routers/hasItemsSection.d.ts +194 -0
- package/dist/api/trpc/routers/hasItemsSection.d.ts.map +1 -0
- package/dist/api/trpc/routers/hasItemsSection.js +86 -0
- package/dist/api/trpc/routers/logs.d.ts +59 -0
- package/dist/api/trpc/routers/logs.d.ts.map +1 -0
- package/dist/api/trpc/routers/logs.js +79 -0
- package/dist/api/trpc/routers/navigation.d.ts +65 -0
- package/dist/api/trpc/routers/navigation.d.ts.map +1 -0
- package/dist/api/trpc/routers/navigation.js +11 -0
- package/dist/api/trpc/routers/simpleSection.d.ts +93 -0
- package/dist/api/trpc/routers/simpleSection.d.ts.map +1 -0
- package/dist/api/trpc/routers/simpleSection.js +54 -0
- package/dist/api/trpc/server.d.ts +2789 -5
- package/dist/api/trpc/server.d.ts.map +1 -1
- package/dist/api/trpc/server.js +91 -52
- package/dist/api/trpc/trpc.d.ts +111 -0
- package/dist/api/trpc/trpc.d.ts.map +1 -0
- package/dist/api/trpc/trpc.js +99 -0
- package/dist/api/trpc/utils/async-caller-proxy.d.ts +2 -0
- package/dist/api/trpc/utils/async-caller-proxy.d.ts.map +1 -0
- package/dist/api/trpc/utils/async-caller-proxy.js +38 -0
- package/dist/api/trpc/utils/refresh-token-link.d.ts +6 -0
- package/dist/api/trpc/utils/refresh-token-link.d.ts.map +1 -0
- package/dist/api/trpc/utils/refresh-token-link.js +81 -0
- package/dist/api/trpc/utils/router-types.d.ts +7 -0
- package/dist/api/trpc/utils/router-types.d.ts.map +1 -0
- package/dist/api/trpc/utils/router-types.js +0 -0
- package/dist/api/use-axios-private.d.ts +6 -0
- package/dist/api/use-axios-private.d.ts.map +1 -0
- package/dist/api/use-axios-private.js +57 -0
- package/dist/api/utils/async-caller-proxy.d.ts +2 -0
- package/dist/api/utils/async-caller-proxy.d.ts.map +1 -0
- package/dist/api/utils/async-caller-proxy.js +36 -0
- package/dist/api/utils/lazy-caller-proxy.d.ts +2 -0
- package/dist/api/utils/lazy-caller-proxy.d.ts.map +1 -0
- package/dist/api/utils/lazy-caller-proxy.js +36 -0
- package/dist/api/utils/router-types.d.ts +7 -0
- package/dist/api/utils/router-types.d.ts.map +1 -0
- package/dist/api/utils/router-types.js +0 -0
- package/dist/auth/hooks/index.d.ts +1 -2
- package/dist/auth/hooks/index.d.ts.map +1 -1
- package/dist/auth/hooks/index.js +1 -2
- package/dist/auth/react.d.ts +1 -2
- package/dist/auth/react.d.ts.map +1 -1
- package/dist/auth/react.js +1 -2
- package/dist/auth/trpc.d.ts +1 -1
- package/dist/auth/trpc.d.ts.map +1 -1
- package/dist/auth/trpc.js +0 -1
- package/dist/cli/lib/fix-master-admin.d.ts.map +1 -1
- package/dist/cli/lib/fix-master-admin.js +12 -25
- package/dist/cli/lib/update-sections.d.ts.map +1 -1
- package/dist/cli/lib/update-sections.js +90 -46
- package/dist/core/config/config-loader.d.ts +23 -7
- package/dist/core/config/config-loader.d.ts.map +1 -1
- package/dist/core/config/config-loader.js +26 -9
- package/dist/core/db/table-checker/MysqlTable.d.ts.map +1 -1
- package/dist/core/db/table-checker/MysqlTable.js +3 -1
- package/dist/core/fields/date-range.d.ts +4 -4
- package/dist/core/fields/select.d.ts +1 -1
- package/dist/core/sections/category.d.ts +8 -8
- package/dist/core/sections/hasItems.d.ts +8 -8
- package/dist/core/sections/section.d.ts +5 -5
- package/dist/core/sections/simple.d.ts +4 -4
- package/dist/core/types/index.d.ts +17 -0
- package/dist/core/types/index.d.ts.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/plugins/client.d.ts +19 -0
- package/dist/plugins/client.d.ts.map +1 -0
- package/dist/plugins/client.js +24 -0
- package/dist/plugins/define.d.ts +4 -0
- package/dist/plugins/define.d.ts.map +1 -0
- package/dist/plugins/define.js +3 -0
- package/dist/plugins/derive.d.ts +32 -0
- package/dist/plugins/derive.d.ts.map +1 -0
- package/dist/plugins/derive.js +77 -0
- package/dist/plugins/loader.d.ts +51 -7
- package/dist/plugins/loader.d.ts.map +1 -1
- package/dist/plugins/loader.js +111 -51
- package/dist/plugins/manifest.d.ts +28 -0
- package/dist/plugins/manifest.d.ts.map +1 -0
- package/dist/plugins/manifest.js +83 -0
- package/dist/plugins/prefetch.d.ts +16 -0
- package/dist/plugins/prefetch.d.ts.map +1 -0
- package/dist/plugins/prefetch.js +40 -0
- package/dist/plugins/registry.d.ts +22 -0
- package/dist/plugins/registry.d.ts.map +1 -0
- package/dist/plugins/registry.js +25 -0
- package/dist/plugins/server.d.ts +2 -0
- package/dist/plugins/server.d.ts.map +1 -1
- package/dist/plugins/server.js +2 -0
- package/dist/plugins/types.d.ts +9 -0
- package/dist/plugins/types.d.ts.map +1 -0
- package/dist/plugins/types.js +0 -0
- package/dist/translations/base/en.d.ts +5 -0
- package/dist/translations/base/en.d.ts.map +1 -1
- package/dist/translations/base/en.js +5 -0
- package/dist/translations/client.d.ts +64 -4
- package/dist/translations/client.d.ts.map +1 -1
- package/dist/translations/server.d.ts +64 -4
- package/dist/translations/server.d.ts.map +1 -1
- package/dist/utils/console-log.d.ts +18 -0
- package/dist/utils/console-log.d.ts.map +1 -0
- package/dist/utils/console-log.js +28 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/log.d.ts +18 -0
- package/dist/utils/log.d.ts.map +1 -0
- package/dist/utils/log.js +28 -0
- package/dist/validators/index.d.ts +1 -0
- package/dist/validators/index.d.ts.map +1 -1
- package/dist/validators/index.js +1 -0
- package/dist/validators/tags.d.ts +4 -0
- package/dist/validators/tags.d.ts.map +1 -0
- package/dist/validators/tags.js +8 -0
- package/package.json +36 -18
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import 'server-only';
|
|
2
|
+
import type { Session } from '../../auth/index.js';
|
|
3
|
+
export declare const isAccessAllowed: ({ sectionName, role, userId, }: {
|
|
4
|
+
sectionName: string;
|
|
5
|
+
role?: "C" | "U" | "D";
|
|
6
|
+
userId: string;
|
|
7
|
+
}) => Promise<boolean>;
|
|
8
|
+
export declare const getAdminPrivileges: (adminId: string) => Promise<Set<string>>;
|
|
9
|
+
export declare const getAllPrivileges: (session: Session) => Promise<{
|
|
10
|
+
title: string;
|
|
11
|
+
order: number;
|
|
12
|
+
sectionType: string;
|
|
13
|
+
sectionName: string;
|
|
14
|
+
}[]>;
|
|
15
|
+
export declare const getAdminsList: () => Promise<{
|
|
16
|
+
id: string;
|
|
17
|
+
username: string;
|
|
18
|
+
avatar: string | null;
|
|
19
|
+
roles: {
|
|
20
|
+
operations: "C" | "U" | "D" | "CU" | "CD" | "UD" | "CUD";
|
|
21
|
+
publisher: boolean | null;
|
|
22
|
+
sectionName: string;
|
|
23
|
+
}[];
|
|
24
|
+
}[]>;
|
|
25
|
+
//# sourceMappingURL=privileges.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"privileges.d.ts","sourceRoot":"","sources":["../../../src/api/actions/privileges.ts"],"names":[],"mappings":"AAAA,OAAO,aAAa,CAAA;AAIpB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AASlD,eAAO,MAAM,eAAe,GAAU,gCAInC;IACC,WAAW,EAAE,MAAM,CAAA;IACnB,IAAI,CAAC,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,CAAA;IACtB,MAAM,EAAE,MAAM,CAAA;CACjB,qBAmBA,CAAA;AAGD,eAAO,MAAM,kBAAkB,GAAU,SAAS,MAAM,yBAOvD,CAAA;AAED,eAAO,MAAM,gBAAgB,GAAU,SAAS,OAAO;WAaxC,MAAM;WACN,MAAM;iBACA,MAAM;iBACN,MAAM;IA4C1B,CAAA;AAED,eAAO,MAAM,aAAa;QAcd,MAAM;cACA,MAAM;YACR,MAAM,GAAG,IAAI;WACd;QACH,UAAU,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,CAAA;QACxD,SAAS,EAAE,OAAO,GAAG,IAAI,CAAA;QACzB,WAAW,EAAE,MAAM,CAAA;KACtB,EAAE;IAYV,CAAA"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import 'server-only';
|
|
2
|
+
import { and, eq } from 'drizzle-orm';
|
|
3
|
+
import { getCMSConfig } from '../../core/config/index.js';
|
|
4
|
+
import { SectionFactory } from '../../core/factories/index.js';
|
|
5
|
+
import { db } from '../../db/client.js';
|
|
6
|
+
import { AdminPrivilegesTable, AdminsTable } from '../../db/schema.js';
|
|
7
|
+
import { getPluginRoutes } from '../../plugins/loader.js';
|
|
8
|
+
import getString from '../../translations/index.js';
|
|
9
|
+
import { resolveLanguage, resolveMultilingualString } from '../../translations/language-utils.js';
|
|
10
|
+
export const isAccessAllowed = async ({ sectionName, role, userId, }) => {
|
|
11
|
+
/**
|
|
12
|
+
* Check admin privileges
|
|
13
|
+
*/
|
|
14
|
+
const _res = await db
|
|
15
|
+
.select()
|
|
16
|
+
.from(AdminPrivilegesTable)
|
|
17
|
+
.where(and(eq(AdminPrivilegesTable.adminId, userId), eq(AdminPrivilegesTable.sectionName, sectionName)))
|
|
18
|
+
.limit(1);
|
|
19
|
+
const allowed = _res[0];
|
|
20
|
+
if (!allowed)
|
|
21
|
+
return false;
|
|
22
|
+
if (role) {
|
|
23
|
+
if (!allowed.operations.includes(role))
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
};
|
|
28
|
+
export const getAdminPrivileges = async (adminId) => {
|
|
29
|
+
const rows = await db
|
|
30
|
+
.select({ sectionName: AdminPrivilegesTable.sectionName })
|
|
31
|
+
.from(AdminPrivilegesTable)
|
|
32
|
+
.where(eq(AdminPrivilegesTable.adminId, adminId));
|
|
33
|
+
return new Set(rows.map((row) => row.sectionName));
|
|
34
|
+
};
|
|
35
|
+
export const getAllPrivileges = async (session) => {
|
|
36
|
+
const [sections, pluginRoutes] = await Promise.all([SectionFactory.getSections(), getPluginRoutes()]);
|
|
37
|
+
// Get the locale for resolving localized titles
|
|
38
|
+
const cmsConfig = await getCMSConfig();
|
|
39
|
+
const language = resolveLanguage(session.user.language, cmsConfig.i18n.supportedLanguages, cmsConfig.i18n.fallbackLanguage);
|
|
40
|
+
// First, let's assign the static privileges
|
|
41
|
+
const privilegesList = [];
|
|
42
|
+
// Now, let's add the rest of the privileges to the list
|
|
43
|
+
sections.forEach((section, index) => {
|
|
44
|
+
let title;
|
|
45
|
+
if (section.type === 'has_items' || section.type === 'category') {
|
|
46
|
+
title = resolveMultilingualString(section.title.section, language, cmsConfig.i18n.fallbackLanguage);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
title = resolveMultilingualString(section.title, language, cmsConfig.i18n.fallbackLanguage);
|
|
50
|
+
}
|
|
51
|
+
privilegesList.push({
|
|
52
|
+
title,
|
|
53
|
+
order: section.order || index,
|
|
54
|
+
sectionName: section.name,
|
|
55
|
+
sectionType: section.type,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
const pluginPrivileges = new Map();
|
|
59
|
+
pluginRoutes.forEach((route) => {
|
|
60
|
+
if (pluginPrivileges.has(route.pluginName))
|
|
61
|
+
return;
|
|
62
|
+
pluginPrivileges.set(route.pluginName, {
|
|
63
|
+
title: resolveMultilingualString(route.title, language, cmsConfig.i18n.fallbackLanguage),
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
Array.from(pluginPrivileges.entries()).forEach(([pluginName, meta], index) => {
|
|
67
|
+
privilegesList.push({
|
|
68
|
+
title: meta.title,
|
|
69
|
+
order: sections.length + index,
|
|
70
|
+
sectionName: pluginName,
|
|
71
|
+
sectionType: 'plugin',
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
privilegesList.push({ title: getString('admins', language), order: 0, sectionName: 'admins', sectionType: 'static' }, { title: getString('log', language), order: 2, sectionName: 'log', sectionType: 'static' });
|
|
75
|
+
return privilegesList;
|
|
76
|
+
};
|
|
77
|
+
export const getAdminsList = async () => {
|
|
78
|
+
const admins = await db
|
|
79
|
+
.select({
|
|
80
|
+
id: AdminsTable.id,
|
|
81
|
+
username: AdminsTable.user,
|
|
82
|
+
avatar: AdminsTable.coverphoto,
|
|
83
|
+
})
|
|
84
|
+
.from(AdminsTable);
|
|
85
|
+
const roles = await db.select().from(AdminPrivilegesTable);
|
|
86
|
+
/**
|
|
87
|
+
* Assign the roles to the admins
|
|
88
|
+
*/
|
|
89
|
+
const adminsWithRoles = [];
|
|
90
|
+
admins.forEach((admin) => {
|
|
91
|
+
const adminRoles = roles.filter((role) => role.adminId === admin.id);
|
|
92
|
+
adminsWithRoles.push({
|
|
93
|
+
...admin,
|
|
94
|
+
roles: adminRoles,
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
return adminsWithRoles;
|
|
98
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/api/client/index.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAA;AACzD,OAAO,EAAE,OAAO,IAAI,eAAe,EAAE,MAAM,yBAAyB,CAAA;AACpE,YAAY,EAAE,eAAe,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { QueryClientConfig } from '@tanstack/react-query';
|
|
2
|
+
import type { TRPCLink } from '@trpc/client';
|
|
3
|
+
import type { CreateTRPCReact } from '@trpc/react-query';
|
|
4
|
+
import type { AnyRouter } from '@trpc/server';
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
import type { ReactNode } from 'react';
|
|
7
|
+
import type { Session } from '../auth/index.js';
|
|
8
|
+
import type { SessionProviderProps } from '../auth/react.js';
|
|
9
|
+
type SessionOptions = Omit<SessionProviderProps, 'children' | 'session'>;
|
|
10
|
+
export interface NextjsCmsProviderTRPCOptions<TRouter extends AnyRouter> {
|
|
11
|
+
endpoint?: string;
|
|
12
|
+
devtools?: boolean;
|
|
13
|
+
queryClientOptions?: QueryClientConfig;
|
|
14
|
+
links?: {
|
|
15
|
+
beforeHttp?: TRPCLink<TRouter>[];
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export interface NextjsCmsProviderProps<TRouter extends AnyRouter> {
|
|
19
|
+
children: ReactNode;
|
|
20
|
+
session?: Session | null;
|
|
21
|
+
sessionOptions?: SessionOptions;
|
|
22
|
+
trpcOptions?: NextjsCmsProviderTRPCOptions<TRouter>;
|
|
23
|
+
}
|
|
24
|
+
export interface NextjsCmsClient<TRouter extends AnyRouter> {
|
|
25
|
+
trpc: CreateTRPCReact<TRouter, unknown>;
|
|
26
|
+
NextjsCmsProvider: (props: NextjsCmsProviderProps<TRouter>) => React.JSX.Element;
|
|
27
|
+
}
|
|
28
|
+
export declare function createNextjsCmsClient<TRouter extends AnyRouter>(): NextjsCmsClient<TRouter>;
|
|
29
|
+
export {};
|
|
30
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../src/api/client.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAA;AAG9D,OAAO,KAAK,EAAE,QAAQ,EAAwB,MAAM,cAAc,CAAA;AAElE,OAAO,KAAK,EAAE,eAAe,EAAuB,MAAM,mBAAmB,CAAA;AAC7E,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAC9B,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAItC,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAE/C,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,kBAAkB,CAAA;AAG5D,KAAK,cAAc,GAAG,IAAI,CAAC,oBAAoB,EAAE,UAAU,GAAG,SAAS,CAAC,CAAA;AAExE,MAAM,WAAW,4BAA4B,CAAC,OAAO,SAAS,SAAS;IACnE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,kBAAkB,CAAC,EAAE,iBAAiB,CAAA;IACtC,KAAK,CAAC,EAAE;QACJ,UAAU,CAAC,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAA;KACnC,CAAA;CACJ;AAED,MAAM,WAAW,sBAAsB,CAAC,OAAO,SAAS,SAAS;IAC7D,QAAQ,EAAE,SAAS,CAAA;IACnB,OAAO,CAAC,EAAE,OAAO,GAAG,IAAI,CAAA;IACxB,cAAc,CAAC,EAAE,cAAc,CAAA;IAC/B,WAAW,CAAC,EAAE,4BAA4B,CAAC,OAAO,CAAC,CAAA;CACtD;AAED,MAAM,WAAW,eAAe,CAAC,OAAO,SAAS,SAAS;IACtD,IAAI,EAAE,eAAe,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;IACvC,iBAAiB,EAAE,CAAC,KAAK,EAAE,sBAAsB,CAAC,OAAO,CAAC,KAAK,KAAK,CAAC,GAAG,CAAC,OAAO,CAAA;CACnF;AAyBD,wBAAgB,qBAAqB,CAAC,OAAO,SAAS,SAAS,KAAK,eAAe,CAAC,OAAO,CAAC,CA8D3F"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { defaultShouldDehydrateQuery, QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
|
3
|
+
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
|
|
4
|
+
import { httpBatchLink, loggerLink } from '@trpc/client';
|
|
5
|
+
import { createTRPCReact } from '@trpc/react-query';
|
|
6
|
+
import * as React from 'react';
|
|
7
|
+
import { useState } from 'react';
|
|
8
|
+
import SuperJSON from 'superjson';
|
|
9
|
+
import { getCsrfToken, refreshTokenLink, SessionProvider } from '../auth/react.js';
|
|
10
|
+
import { PluginTRPCProvider } from '../plugins/client.js';
|
|
11
|
+
function mergeQueryClientOptions(options) {
|
|
12
|
+
return {
|
|
13
|
+
...options,
|
|
14
|
+
defaultOptions: {
|
|
15
|
+
...options?.defaultOptions,
|
|
16
|
+
queries: {
|
|
17
|
+
refetchOnWindowFocus: process.env.NODE_ENV === 'development',
|
|
18
|
+
retry: 1,
|
|
19
|
+
...options?.defaultOptions?.queries,
|
|
20
|
+
},
|
|
21
|
+
dehydrate: {
|
|
22
|
+
serializeData: SuperJSON.serialize,
|
|
23
|
+
shouldDehydrateQuery: (query) => defaultShouldDehydrateQuery(query) || query.state.status === 'pending',
|
|
24
|
+
...options?.defaultOptions?.dehydrate,
|
|
25
|
+
},
|
|
26
|
+
hydrate: {
|
|
27
|
+
deserializeData: SuperJSON.deserialize,
|
|
28
|
+
...options?.defaultOptions?.hydrate,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export function createNextjsCmsClient() {
|
|
34
|
+
const trpc = createTRPCReact();
|
|
35
|
+
const trpcBase = trpc;
|
|
36
|
+
let browserQueryClient;
|
|
37
|
+
function createQueryClient(options) {
|
|
38
|
+
return new QueryClient(mergeQueryClientOptions(options));
|
|
39
|
+
}
|
|
40
|
+
function getQueryClient(options) {
|
|
41
|
+
if (typeof window === 'undefined') {
|
|
42
|
+
return createQueryClient(options);
|
|
43
|
+
}
|
|
44
|
+
if (!browserQueryClient)
|
|
45
|
+
browserQueryClient = createQueryClient(options);
|
|
46
|
+
return browserQueryClient;
|
|
47
|
+
}
|
|
48
|
+
function NextjsCmsProvider({ children, session, sessionOptions, trpcOptions }) {
|
|
49
|
+
const queryClient = getQueryClient(trpcOptions?.queryClientOptions);
|
|
50
|
+
const endpoint = trpcOptions?.endpoint ?? '/api/trpc';
|
|
51
|
+
const showDevtools = trpcOptions?.devtools ?? process.env.NODE_ENV === 'development';
|
|
52
|
+
const [trpcClient] = useState(() => trpcBase.createClient({
|
|
53
|
+
links: [
|
|
54
|
+
refreshTokenLink(),
|
|
55
|
+
loggerLink({
|
|
56
|
+
enabled: (op) => process.env.NODE_ENV === 'development' ||
|
|
57
|
+
(op.direction === 'down' && op.result instanceof Error),
|
|
58
|
+
}),
|
|
59
|
+
...(trpcOptions?.links?.beforeHttp ?? []),
|
|
60
|
+
httpBatchLink({
|
|
61
|
+
transformer: SuperJSON,
|
|
62
|
+
url: endpoint,
|
|
63
|
+
async headers(op) {
|
|
64
|
+
const headers = new Headers();
|
|
65
|
+
if (op.opList.some((operation) => operation.type === 'mutation')) {
|
|
66
|
+
const csrfToken = await getCsrfToken();
|
|
67
|
+
headers.set('x-csrf-token', csrfToken);
|
|
68
|
+
}
|
|
69
|
+
headers.set('x-trpc-source', 'nextjs-react');
|
|
70
|
+
return headers;
|
|
71
|
+
},
|
|
72
|
+
}),
|
|
73
|
+
],
|
|
74
|
+
}));
|
|
75
|
+
return (React.createElement(QueryClientProvider, { client: queryClient },
|
|
76
|
+
React.createElement(trpcBase.Provider, { client: trpcClient, queryClient: queryClient },
|
|
77
|
+
React.createElement(SessionProvider, { session: session, ...sessionOptions },
|
|
78
|
+
React.createElement(PluginTRPCProvider, { trpc: trpc }, children)),
|
|
79
|
+
showDevtools ? React.createElement(ReactQueryDevtools, { initialIsOpen: false }) : null)));
|
|
80
|
+
}
|
|
81
|
+
return { trpc, NextjsCmsProvider };
|
|
82
|
+
}
|