Skip to content

Commit

Permalink
moved the contentstack sdk function components to utils files and acc…
Browse files Browse the repository at this point in the history
…ordingly change the readme file
  • Loading branch information
ChinmayeeMestry committed Jul 24, 2024
1 parent 93b8009 commit bb46f51
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 0 additions & 26 deletions app/components/contentstack-sdk.js

This file was deleted.

27 changes: 27 additions & 0 deletions app/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
},
);
});
};

0 comments on commit bb46f51

Please sign in to comment.