-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:SRE issue fix removed JS with TS files
Signed-off-by: Amitkanswal <[email protected]>
- Loading branch information
1 parent
051e21f
commit 241f883
Showing
12 changed files
with
407 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.