-
Notifications
You must be signed in to change notification settings - Fork 1
/
prismic-configuration.ts
58 lines (47 loc) · 1.3 KB
/
prismic-configuration.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
58
import { LinkedItem } from '@prismic-types';
import Prismic from '@prismicio/client';
import { NextApiRequest } from 'next';
export const apiEndpoint = 'https://vasseneca.cdn.prismic.io/api/v2';
export const accessToken = process.env.PRISMIC_VAS_ACCESS_TOKEN;
const createClientOptions = (
req: NextApiRequest | null = null,
prismicAccessToken: string | null = null
) => {
const reqOption = req ? { req } : {};
const accessTokenOption = prismicAccessToken
? { accessToken: prismicAccessToken }
: {};
return {
...reqOption,
...accessTokenOption,
};
};
export const Client = (req: NextApiRequest) =>
Prismic.client(apiEndpoint, createClientOptions(req, accessToken));
export const PMclient = Prismic.client(
apiEndpoint,
createClientOptions(null, accessToken)
);
export const linkResolver = (doc: LinkedItem) => {
switch (doc.type) {
case 'page':
return `/${doc.lang}/${doc.uid}`;
case 'post':
return `/${doc.lang}/posts/${doc.uid}`;
case 'category':
return `/${doc.lang}/categories/${doc.uid}`;
default:
return `/${doc.lang}`;
}
};
// Additional helper function for Next/Link component
export const hrefResolver = (doc: LinkedItem) => {
switch (doc.type) {
case 'post':
return `/posts/[uid]`;
case 'category':
return `/categories/[uid]`;
default:
return '/';
}
};