Skip to content

Commit ad94e0e

Browse files
authored
Merge pull request #79 from contentstack/fix/ECO-3097-SRE-ts-fixes
fix:SRE issue fix removed JS with TS files
2 parents 1af4b83 + 241f883 commit ad94e0e

File tree

12 files changed

+407
-320
lines changed

12 files changed

+407
-320
lines changed

.eslintrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
"rules": {
2222
"react/prop-types":0,
2323
"no-undef":"off",
24-
"no-unused-vars": "off"
24+
"no-unused-vars": "off",
25+
"react-hooks/exhaustive-deps":"off",
26+
"@next/next/no-img-element":"off"
2527
}
2628
}

contentstack-sdk/index.js

Lines changed: 0 additions & 118 deletions
This file was deleted.

contentstack-sdk/index.ts

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import * as Utils from "@contentstack/utils";
2+
import ContentstackLivePreview from "@contentstack/live-preview-utils";
3+
import getConfig from "next/config";
4+
import {
5+
customHostUrl,
6+
initializeContentStackSdk,
7+
isValidCustomHostUrl,
8+
} from "./utils";
9+
10+
type GetEntry = {
11+
contentTypeUid: string;
12+
referenceFieldPath: string[] | undefined;
13+
jsonRtePath: string[] | undefined;
14+
};
15+
16+
type GetEntryByUrl = {
17+
entryUrl: string | undefined;
18+
contentTypeUid: string;
19+
referenceFieldPath: string[] | undefined;
20+
jsonRtePath: string[] | undefined;
21+
};
22+
23+
const { publicRuntimeConfig } = getConfig();
24+
const envConfig = process.env.CONTENTSTACK_API_KEY
25+
? process.env
26+
: publicRuntimeConfig;
27+
28+
let customHostBaseUrl = envConfig.CONTENTSTACK_API_HOST as string;
29+
customHostBaseUrl = customHostUrl(customHostBaseUrl);
30+
31+
// SDK initialization
32+
const Stack = initializeContentStackSdk();
33+
34+
// set host url only for custom host or non prod base url's
35+
if (isValidCustomHostUrl(customHostBaseUrl)) {
36+
Stack.setHost(customHostBaseUrl);
37+
}
38+
39+
// Setting LP if enabled
40+
ContentstackLivePreview.init({
41+
//@ts-ignore
42+
stackSdk: Stack,
43+
clientUrlParams: {
44+
host: envConfig.CONTENTSTACK_APP_HOST,
45+
},
46+
stackDetails: {
47+
apiKey: envConfig.CONTENTSTACK_API_KEY,
48+
environment: envConfig.CONTENTSTACK_ENVIRONMENT,
49+
},
50+
enable: envConfig.CONTENTSTACK_LIVE_PREVIEW === "true",
51+
ssr: false,
52+
})?.catch((err) => console.error(err));
53+
54+
export const { onEntryChange } = ContentstackLivePreview;
55+
56+
const renderOption = {
57+
span: (node: any, next: any) => next(node.children),
58+
};
59+
60+
/**
61+
*
62+
* fetches all the entries from specific content-type
63+
* @param {* content-type uid} contentTypeUid
64+
* @param {* reference field name} referenceFieldPath
65+
* @param {* Json RTE path} jsonRtePath
66+
*
67+
*/
68+
export const getEntry = ({
69+
contentTypeUid,
70+
referenceFieldPath,
71+
jsonRtePath,
72+
}: GetEntry) => {
73+
return new Promise((resolve, reject) => {
74+
const query = Stack.ContentType(contentTypeUid).Query();
75+
if (referenceFieldPath) query.includeReference(referenceFieldPath);
76+
query
77+
.toJSON()
78+
.find()
79+
.then(
80+
(result) => {
81+
jsonRtePath &&
82+
Utils.jsonToHTML({
83+
entry: result,
84+
paths: jsonRtePath,
85+
renderOption,
86+
});
87+
resolve(result);
88+
},
89+
(error) => {
90+
reject(error);
91+
}
92+
);
93+
});
94+
};
95+
96+
/**
97+
*fetches specific entry from a content-type
98+
*
99+
* @param {* content-type uid} contentTypeUid
100+
* @param {* url for entry to be fetched} entryUrl
101+
* @param {* reference field name} referenceFieldPath
102+
* @param {* Json RTE path} jsonRtePath
103+
* @returns
104+
*/
105+
export const getEntryByUrl = ({
106+
contentTypeUid,
107+
entryUrl,
108+
referenceFieldPath,
109+
jsonRtePath,
110+
}: GetEntryByUrl) => {
111+
return new Promise((resolve, reject) => {
112+
const blogQuery = Stack.ContentType(contentTypeUid).Query();
113+
if (referenceFieldPath) blogQuery.includeReference(referenceFieldPath);
114+
blogQuery.toJSON();
115+
const data = blogQuery.where("url", `${entryUrl}`).find();
116+
data.then(
117+
(result) => {
118+
jsonRtePath &&
119+
Utils.jsonToHTML({
120+
entry: result,
121+
paths: jsonRtePath,
122+
renderOption,
123+
});
124+
resolve(result[0]);
125+
},
126+
(error) => {
127+
console.error(error);
128+
reject(error);
129+
}
130+
);
131+
});
132+
};

contentstack-sdk/utils.ts

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,80 @@
1-
import { Region } from "contentstack";
2-
export const customHostUrl = (baseUrl: string):string => {
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 => {
375
return baseUrl.replace("api", "cdn");
476
};
5-
77+
// generate prod api urls
678
export const generateUrlBasedOnRegion = (): string[] => {
779
return Object.keys(Region).map((region) => {
880
if (region === "US") {
@@ -11,7 +83,7 @@ export const generateUrlBasedOnRegion = (): string[] => {
1183
return `${region}-cdn.contentstack.com`;
1284
});
1385
};
14-
86+
// prod url validation for custom host
1587
export const isValidCustomHostUrl = (url: string): boolean => {
16-
return url? !generateUrlBasedOnRegion().includes(url):false;
88+
return url ? !generateUrlBasedOnRegion().includes(url) : false;
1789
};

0 commit comments

Comments
 (0)