diff --git a/README.md b/README.md index cfd8189..f2d72c7 100644 --- a/README.md +++ b/README.md @@ -24,10 +24,10 @@ This TypeScript demo adopts many of Hydrogen's [framework conventions and third- Using Contentstack App in Shopify, you can connect your Contentstack stack to your Shopify store. This app will sync all your Contentstack cms data to Shopify metaobjects. In this demo all the inventory data is fetched from Shopify's Storefront API(from metaobjects). -If you want to fetch marketing data like pages, footer or other assets from Contentstack directly, you can use the `getEntry` method from our official [`contentstack-sdk`][contentstack-sdk] library. We have already implemented in app/component/contentstack-sdk.js file where you can use the `getEntry` method for achieving this. +If you want to fetch marketing data like pages, footer or other assets using Contentstack SDK function, you can use the `getEntry` method from our official [`contentstack-sdk`][contentstack-sdk] library. We have already implemented in app/utils.ts file where you can use the `getEntry` method for achieving this. ```tsx -import {getEntry} from '~/components/contentstack-sdk'; +import {getEntry} from '~/utils.ts'; const fetchData = async () => { try { diff --git a/app/components/contentstack-sdk.js b/app/components/contentstack-sdk.js deleted file mode 100644 index 3bc3fe2..0000000 --- a/app/components/contentstack-sdk.js +++ /dev/null @@ -1,26 +0,0 @@ -import * as contentstack from 'contentstack'; -const Stack = (envConfig) => { - return contentstack.Stack({ - api_key: envConfig.CONTENTSTACK_API_KEY, - delivery_token: envConfig.CONTENTSTACK_DELIVERY_TOKEN, - environment: envConfig.CONTENTSTACK_ENVIRONMENT, - }); -}; - -export const getEntry = async ({contentTypeUid, envConfig}) => { - return new Promise((resolve, reject) => { - const query = Stack(envConfig).ContentType(contentTypeUid).Query(); - query - .toJSON() - .includeCount() - .find() - .then( - (result) => { - resolve(result[0][0]); - }, - (error) => { - reject(error); - }, - ); - }); -}; diff --git a/app/utils.ts b/app/utils.ts index ffea0a7..660f857 100644 --- a/app/utils.ts +++ b/app/utils.ts @@ -1,6 +1,7 @@ import {useLocation} from '@remix-run/react'; import type {SelectedOption} from '@shopify/hydrogen/storefront-api-types'; import {useMemo} from 'react'; +import * as contentstack from 'contentstack'; export function useVariantUrl( handle: string, @@ -44,3 +45,29 @@ export function getVariantUrl({ return path + (searchString ? '?' + searchParams.toString() : ''); } + +const Stack = (envConfig: any) => { + return contentstack.Stack({ + api_key: envConfig.CONTENTSTACK_API_KEY, + delivery_token: envConfig.CONTENTSTACK_DELIVERY_TOKEN, + environment: envConfig.CONTENTSTACK_ENVIRONMENT, + }); +}; + +export const getEntry = async ({contentTypeUid, envConfig}: any) => { + return new Promise((resolve, reject) => { + const query: any = Stack(envConfig).ContentType(contentTypeUid).Query(); + query + .toJSON() + .includeCount() + .find() + .then( + (result: any) => { + resolve(result[0][0]); + }, + (error: any) => { + reject(error); + }, + ); + }); +};