11import { Config , Region , LivePreview , Stack } from "contentstack" ;
22
3- const {
4- CONTENTSTACK_API_KEY ,
5- CONTENTSTACK_DELIVERY_TOKEN ,
6- CONTENTSTACK_ENVIRONMENT ,
7- CONTENTSTACK_BRANCH ,
8- CONTENTSTACK_REGION ,
9- CONTENTSTACK_PREVIEW_TOKEN ,
10- CONTENTSTACK_PREVIEW_HOST ,
11- CONTENTSTACK_APP_HOST ,
12- CONTENTSTACK_LIVE_PREVIEW ,
13- } = process . env ;
3+ /** All Contentstack config uses NEXT_PUBLIC_* (see .env.local.sample and next.config.js). */
4+ const apiKey = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_API_KEY ;
5+ const deliveryToken = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN ;
6+ const environment = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT ;
7+ const branch = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_BRANCH ;
8+ const regionEnv = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_REGION ;
9+ const previewToken = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN ;
10+ const previewHost = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST ;
11+ const appHost = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_APP_HOST ;
12+ const livePreviewFlag = ( ) => process . env . NEXT_PUBLIC_CONTENTSTACK_LIVE_PREVIEW ;
13+
14+ const livePreviewEnabled = ( ) => livePreviewFlag ( ) === "true" ;
1415
1516// basic env validation
1617export const isBasicConfigValid = ( ) => {
17- return (
18- ! ! CONTENTSTACK_API_KEY &&
19- ! ! CONTENTSTACK_DELIVERY_TOKEN &&
20- ! ! CONTENTSTACK_ENVIRONMENT
21- ) ;
18+ return ! ! ( apiKey ( ) && deliveryToken ( ) && environment ( ) ) ;
2219} ;
20+
2321// Live preview config validation
2422export const isLpConfigValid = ( ) => {
2523 return (
26- ! ! CONTENTSTACK_LIVE_PREVIEW &&
27- ! ! CONTENTSTACK_PREVIEW_TOKEN &&
28- ! ! CONTENTSTACK_PREVIEW_HOST &&
29- ! ! CONTENTSTACK_APP_HOST
24+ livePreviewEnabled ( ) &&
25+ ! ! previewToken ( ) &&
26+ ! ! previewHost ( ) &&
27+ ! ! appHost ( )
3028 ) ;
3129} ;
30+
3231// set region
3332const setRegion = ( ) : Region => {
3433 let region = "US" as keyof typeof Region ;
35- if ( ! ! CONTENTSTACK_REGION && CONTENTSTACK_REGION !== "us" ) {
36- region = CONTENTSTACK_REGION . toLocaleUpperCase ( ) . replace (
37- "-" ,
38- "_"
39- ) as keyof typeof Region ;
34+ const r = regionEnv ( ) ;
35+ if ( ! ! r && r . toLowerCase ( ) !== "us" ) {
36+ region = r . toUpperCase ( ) . replace ( / - / g, "_" ) as keyof typeof Region ;
4037 }
4138 return Region [ region ] ;
4239} ;
40+
4341// set LivePreview config
4442const setLivePreviewConfig = ( ) : LivePreview => {
4543 if ( ! isLpConfigValid ( ) )
46- throw new Error ( "Your LP config is set to true. Please make you have set all required LP config in .env" ) ;
44+ throw new Error (
45+ "Live preview is enabled but required variables are missing. Set NEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN, PREVIEW_HOST, and APP_HOST in .env."
46+ ) ;
4747 return {
48- preview_token : CONTENTSTACK_PREVIEW_TOKEN as string ,
49- enable : CONTENTSTACK_LIVE_PREVIEW === " true" ,
50- host : CONTENTSTACK_PREVIEW_HOST as string ,
48+ preview_token : previewToken ( ) as string ,
49+ enable : true ,
50+ host : previewHost ( ) as string ,
5151 } as LivePreview ;
5252} ;
53+
5354// contentstack sdk initialization
5455export const initializeContentStackSdk = ( ) : Stack => {
5556 if ( ! isBasicConfigValid ( ) )
56- throw new Error ( "Please set you .env file before running starter app" ) ;
57+ throw new Error (
58+ "Set NEXT_PUBLIC_CONTENTSTACK_API_KEY, DELIVERY_TOKEN, and ENVIRONMENT in .env"
59+ ) ;
5760 const stackConfig : Config = {
58- api_key : CONTENTSTACK_API_KEY as string ,
59- delivery_token : CONTENTSTACK_DELIVERY_TOKEN as string ,
60- environment : CONTENTSTACK_ENVIRONMENT as string ,
61+ api_key : apiKey ( ) as string ,
62+ delivery_token : deliveryToken ( ) as string ,
63+ environment : environment ( ) as string ,
6164 region : setRegion ( ) ,
62- branch : CONTENTSTACK_BRANCH || "main" ,
65+ branch : ( branch ( ) as string ) || "main"
6366 } ;
64- if ( CONTENTSTACK_LIVE_PREVIEW === "true" ) {
67+ if ( livePreviewEnabled ( ) ) {
6568 stackConfig . live_preview = setLivePreviewConfig ( ) ;
6669 }
6770 return Stack ( stackConfig ) ;
6871} ;
72+
6973// api host url
7074export const customHostUrl = ( baseUrl : string ) : string => {
7175 return baseUrl . replace ( "api" , "cdn" ) ;
7276} ;
77+
7378// generate prod api urls
7479export const generateUrlBasedOnRegion = ( ) : string [ ] => {
7580 return Object . keys ( Region ) . map ( ( region ) => {
@@ -79,7 +84,8 @@ export const generateUrlBasedOnRegion = (): string[] => {
7984 return `${ region } -cdn.contentstack.com` ;
8085 } ) ;
8186} ;
87+
8288// prod url validation for custom host
83- export const isValidCustomHostUrl = ( url = '' ) : boolean => {
89+ export const isValidCustomHostUrl = ( url = "" ) : boolean => {
8490 return url ? ! generateUrlBasedOnRegion ( ) . includes ( url ) : false ;
8591} ;
0 commit comments