-
Notifications
You must be signed in to change notification settings - Fork 26.1k
/
index.ts
53 lines (48 loc) · 1.81 KB
/
index.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
import { GetServerSidePropsContext, GetStaticPropsContext } from "next";
import { SitecorePageProps } from "lib/page-props";
import * as plugins from "temp/page-props-factory-plugins";
/**
* Determines whether context is GetServerSidePropsContext (SSR) or GetStaticPropsContext (SSG)
* @param {GetServerSidePropsContext | GetStaticPropsContext} context
*/
export const isServerSidePropsContext = function (
context: GetServerSidePropsContext | GetStaticPropsContext,
): context is GetServerSidePropsContext {
return (context as GetServerSidePropsContext).req !== undefined;
};
export interface Plugin {
/**
* Detect order when the plugin should be called, e.g. 0 - will be called first (can be a plugin which data is required for other plugins)
*/
order: number;
/**
* A function which will be called during page props generation
*/
exec(
props: SitecorePageProps,
context: GetServerSidePropsContext | GetStaticPropsContext,
): Promise<SitecorePageProps>;
}
export class SitecorePagePropsFactory {
/**
* Create SitecorePageProps for given context (SSR / GetServerSidePropsContext or SSG / GetStaticPropsContext)
* @param {GetServerSidePropsContext | GetStaticPropsContext} context
* @see SitecorePageProps
*/
public async create(
context: GetServerSidePropsContext | GetStaticPropsContext,
): Promise<SitecorePageProps> {
const extendedProps = await (Object.values(plugins) as Plugin[])
.sort((p1, p2) => p1.order - p2.order)
.reduce(
async (result, plugin) => {
const props = await result;
const newProps = await plugin.exec(props, context);
return newProps;
},
Promise.resolve({} as SitecorePageProps),
);
return extendedProps;
}
}
export const sitecorePagePropsFactory = new SitecorePagePropsFactory();