|
| 1 | +import { Config, Region, LivePreview, Stack } from "contentstack"; |
| 2 | +import getConfig from "next/config"; |
| 3 | +const { publicRuntimeConfig } = getConfig(); |
| 4 | +const envConfig = process.env.CONTENTSTACK_API_KEY |
| 5 | + ? process.env |
| 6 | + : publicRuntimeConfig; |
| 7 | +const { |
| 8 | + CONTENTSTACK_API_KEY, |
| 9 | + CONTENTSTACK_DELIVERY_TOKEN, |
| 10 | + CONTENTSTACK_ENVIRONMENT, |
| 11 | + CONTENTSTACK_BRANCH, |
| 12 | + CONTENTSTACK_REGION, |
| 13 | + CONTENTSTACK_MANAGEMENT_TOKEN, |
| 14 | + CONTENTSTACK_API_HOST, |
| 15 | + CONTENTSTACK_APP_HOST, |
| 16 | + CONTENTSTACK_LIVE_PREVIEW, |
| 17 | +} = envConfig; |
| 18 | + |
| 19 | +// basic env validation |
| 20 | +export const isBasicConfigValid = () => { |
| 21 | + return ( |
| 22 | + !!CONTENTSTACK_API_KEY && |
| 23 | + !!CONTENTSTACK_DELIVERY_TOKEN && |
| 24 | + !!CONTENTSTACK_ENVIRONMENT |
| 25 | + ); |
| 26 | +}; |
| 27 | +// Live preview config validation |
| 28 | +export const isLpConfigValid = () => { |
| 29 | + return ( |
| 30 | + !!CONTENTSTACK_LIVE_PREVIEW && |
| 31 | + !!CONTENTSTACK_MANAGEMENT_TOKEN && |
| 32 | + !!CONTENTSTACK_API_HOST && |
| 33 | + !!CONTENTSTACK_APP_HOST |
| 34 | + ); |
| 35 | +}; |
| 36 | +// set region |
| 37 | +const setRegion = (): Region => { |
| 38 | + let region = "US" as keyof typeof Region; |
| 39 | + if (!!CONTENTSTACK_REGION && CONTENTSTACK_REGION !== "us") { |
| 40 | + region = CONTENTSTACK_REGION.toLocaleUpperCase().replace( |
| 41 | + "-", |
| 42 | + "_" |
| 43 | + ) as keyof typeof Region; |
| 44 | + } |
| 45 | + return Region[region]; |
| 46 | +}; |
| 47 | +// set LivePreview config |
| 48 | +const setLivePreviewConfig = (): LivePreview => { |
| 49 | + if (!isLpConfigValid()) |
| 50 | + throw new Error("Your LP config is set to true. Please make you have set all required LP config in .env"); |
| 51 | + return { |
| 52 | + management_token: CONTENTSTACK_MANAGEMENT_TOKEN as string, |
| 53 | + enable: CONTENTSTACK_LIVE_PREVIEW === "true", |
| 54 | + host: CONTENTSTACK_API_HOST as string, |
| 55 | + } as LivePreview; |
| 56 | +}; |
| 57 | +// contentstack sdk initialization |
| 58 | +export const initializeContentStackSdk = (): Stack => { |
| 59 | + if (!isBasicConfigValid()) |
| 60 | + throw new Error("Please set you .env file before running starter app"); |
| 61 | + const stackConfig: Config = { |
| 62 | + api_key: CONTENTSTACK_API_KEY as string, |
| 63 | + delivery_token: CONTENTSTACK_DELIVERY_TOKEN as string, |
| 64 | + environment: CONTENTSTACK_ENVIRONMENT as string, |
| 65 | + region: setRegion(), |
| 66 | + branch: CONTENTSTACK_BRANCH || "main", |
| 67 | + }; |
| 68 | + if (CONTENTSTACK_LIVE_PREVIEW === "true") { |
| 69 | + stackConfig.live_preview = setLivePreviewConfig(); |
| 70 | + } |
| 71 | + return Stack(stackConfig); |
| 72 | +}; |
| 73 | +// api host url |
| 74 | +export const customHostUrl = (baseUrl: string): string => { |
| 75 | + return baseUrl.replace("api", "cdn"); |
| 76 | +}; |
| 77 | +// generate prod api urls |
| 78 | +export const generateUrlBasedOnRegion = (): string[] => { |
| 79 | + return Object.keys(Region).map((region) => { |
| 80 | + if (region === "US") { |
| 81 | + return `cdn.contentstack.io`; |
| 82 | + } |
| 83 | + return `${region}-cdn.contentstack.com`; |
| 84 | + }); |
| 85 | +}; |
| 86 | +// prod url validation for custom host |
| 87 | +export const isValidCustomHostUrl = (url: string): boolean => { |
| 88 | + return url ? !generateUrlBasedOnRegion().includes(url) : false; |
| 89 | +}; |
0 commit comments