Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:SRE issue fix removed JS with TS files #79

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"rules": {
"react/prop-types":0,
"no-undef":"off",
"no-unused-vars": "off"
"no-unused-vars": "off",
"react-hooks/exhaustive-deps":"off",
"@next/next/no-img-element":"off"
}
}
118 changes: 0 additions & 118 deletions contentstack-sdk/index.js

This file was deleted.

132 changes: 132 additions & 0 deletions contentstack-sdk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import * as Utils from "@contentstack/utils";
import ContentstackLivePreview from "@contentstack/live-preview-utils";
import getConfig from "next/config";
import {
customHostUrl,
initializeContentStackSdk,
isValidCustomHostUrl,
} from "./utils";

type GetEntry = {
contentTypeUid: string;
referenceFieldPath: string[] | undefined;
jsonRtePath: string[] | undefined;
};

type GetEntryByUrl = {
entryUrl: string | undefined;
contentTypeUid: string;
referenceFieldPath: string[] | undefined;
jsonRtePath: string[] | undefined;
};

const { publicRuntimeConfig } = getConfig();
const envConfig = process.env.CONTENTSTACK_API_KEY
? process.env
: publicRuntimeConfig;

let customHostBaseUrl = envConfig.CONTENTSTACK_API_HOST as string;
customHostBaseUrl = customHostUrl(customHostBaseUrl);

// SDK initialization
const Stack = initializeContentStackSdk();

// set host url only for custom host or non prod base url's
if (isValidCustomHostUrl(customHostBaseUrl)) {
Stack.setHost(customHostBaseUrl);
}

// Setting LP if enabled
ContentstackLivePreview.init({
//@ts-ignore
stackSdk: Stack,
clientUrlParams: {
host: envConfig.CONTENTSTACK_APP_HOST,
},
stackDetails: {
apiKey: envConfig.CONTENTSTACK_API_KEY,
environment: envConfig.CONTENTSTACK_ENVIRONMENT,
},
enable: envConfig.CONTENTSTACK_LIVE_PREVIEW === "true",
ssr: false,
})?.catch((err) => console.error(err));

export const { onEntryChange } = ContentstackLivePreview;

const renderOption = {
span: (node: any, next: any) => next(node.children),
};

/**
*
* fetches all the entries from specific content-type
* @param {* content-type uid} contentTypeUid
* @param {* reference field name} referenceFieldPath
* @param {* Json RTE path} jsonRtePath
*
*/
export const getEntry = ({
contentTypeUid,
referenceFieldPath,
jsonRtePath,
}: GetEntry) => {
return new Promise((resolve, reject) => {
const query = Stack.ContentType(contentTypeUid).Query();
if (referenceFieldPath) query.includeReference(referenceFieldPath);
query
.toJSON()
.find()
.then(
(result) => {
jsonRtePath &&
Utils.jsonToHTML({
entry: result,
paths: jsonRtePath,
renderOption,
});
resolve(result);
},
(error) => {
reject(error);
}
);
});
};

/**
*fetches specific entry from a content-type
*
* @param {* content-type uid} contentTypeUid
* @param {* url for entry to be fetched} entryUrl
* @param {* reference field name} referenceFieldPath
* @param {* Json RTE path} jsonRtePath
* @returns
*/
export const getEntryByUrl = ({
contentTypeUid,
entryUrl,
referenceFieldPath,
jsonRtePath,
}: GetEntryByUrl) => {
return new Promise((resolve, reject) => {
const blogQuery = Stack.ContentType(contentTypeUid).Query();
if (referenceFieldPath) blogQuery.includeReference(referenceFieldPath);
blogQuery.toJSON();
const data = blogQuery.where("url", `${entryUrl}`).find();
data.then(
(result) => {
jsonRtePath &&
Utils.jsonToHTML({
entry: result,
paths: jsonRtePath,
renderOption,
});
resolve(result[0]);
},
(error) => {
console.error(error);
reject(error);
}
);
});
};
82 changes: 77 additions & 5 deletions contentstack-sdk/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,80 @@
import { Region } from "contentstack";
export const customHostUrl = (baseUrl: string):string => {
import { Config, Region, LivePreview, Stack } from "contentstack";
import getConfig from "next/config";
const { publicRuntimeConfig } = getConfig();
const envConfig = process.env.CONTENTSTACK_API_KEY
? process.env
: publicRuntimeConfig;
const {
CONTENTSTACK_API_KEY,
CONTENTSTACK_DELIVERY_TOKEN,
CONTENTSTACK_ENVIRONMENT,
CONTENTSTACK_BRANCH,
CONTENTSTACK_REGION,
CONTENTSTACK_MANAGEMENT_TOKEN,
CONTENTSTACK_API_HOST,
CONTENTSTACK_APP_HOST,
CONTENTSTACK_LIVE_PREVIEW,
} = envConfig;

// basic env validation
export const isBasicConfigValid = () => {
return (
!!CONTENTSTACK_API_KEY &&
!!CONTENTSTACK_DELIVERY_TOKEN &&
!!CONTENTSTACK_ENVIRONMENT
);
};
// Live preview config validation
export const isLpConfigValid = () => {
return (
!!CONTENTSTACK_LIVE_PREVIEW &&
!!CONTENTSTACK_MANAGEMENT_TOKEN &&
!!CONTENTSTACK_API_HOST &&
!!CONTENTSTACK_APP_HOST
);
};
// set region
const setRegion = (): Region => {
let region = "US" as keyof typeof Region;
if (!!CONTENTSTACK_REGION && CONTENTSTACK_REGION !== "us") {
region = CONTENTSTACK_REGION.toLocaleUpperCase().replace(
"-",
"_"
) as keyof typeof Region;
}
return Region[region];
};
// set LivePreview config
const setLivePreviewConfig = (): LivePreview => {
if (!isLpConfigValid())
throw new Error("Your LP config is set to true. Please make you have set all required LP config in .env");
return {
management_token: CONTENTSTACK_MANAGEMENT_TOKEN as string,
enable: CONTENTSTACK_LIVE_PREVIEW === "true",
host: CONTENTSTACK_API_HOST as string,
} as LivePreview;
};
// contentstack sdk initialization
export const initializeContentStackSdk = (): Stack => {
if (!isBasicConfigValid())
throw new Error("Please set you .env file before running starter app");
const stackConfig: Config = {
api_key: CONTENTSTACK_API_KEY as string,
delivery_token: CONTENTSTACK_DELIVERY_TOKEN as string,
environment: CONTENTSTACK_ENVIRONMENT as string,
region: setRegion(),
branch: CONTENTSTACK_BRANCH || "main",
};
if (CONTENTSTACK_LIVE_PREVIEW === "true") {
stackConfig.live_preview = setLivePreviewConfig();
}
return Stack(stackConfig);
};
// api host url
export const customHostUrl = (baseUrl: string): string => {
return baseUrl.replace("api", "cdn");
};

// generate prod api urls
export const generateUrlBasedOnRegion = (): string[] => {
return Object.keys(Region).map((region) => {
if (region === "US") {
Expand All @@ -11,7 +83,7 @@ export const generateUrlBasedOnRegion = (): string[] => {
return `${region}-cdn.contentstack.com`;
});
};

// prod url validation for custom host
export const isValidCustomHostUrl = (url: string): boolean => {
return url? !generateUrlBasedOnRegion().includes(url):false;
return url ? !generateUrlBasedOnRegion().includes(url) : false;
};
Loading