Skip to content

docs: update readme with typescript sdk code snippets #154

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

Merged
merged 1 commit into from
Mar 26, 2025
Merged
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
172 changes: 93 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ To get started with JavaScript, you will need the following:
- Node.js 10 or later

## SDK Installation and Setup
> Note: If you are using JavaScript Contentstack SDK, you don’t need to run the command as ‘@contentstack/utils’ is already imported in the SDK.

Use the following command to install Contentstack JavaScript Utils SDK:
```sh
Expand Down Expand Up @@ -87,104 +86,119 @@ Contentstack Utils SDK lets you interact with the Content Delivery APIs and retr
### Fetch Embedded Item(s) from a Single Entry
#### Render HTML RTE Embedded object
To get an embedded item of a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use the `includeEmbeddedItems` and `Contentstack.Utils.render` functions as shown below:
```js
import * as Contentstack from 'contentstack'
const stack = Contentstack.Stack({
api_key: '<API_KEY>',
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
environment: '<ENVIRONMENT>'})

stack.ContentType('<CONTENT_TYPE_UID>')
.Entry('<ENTRY_UID>')
.toJSON()
.includeEmbeddedItems() // include embedded items
.fetch()
.then(entry => {
Contentstack.Utils.render({ entry, renderOption })
})
```ts
import contentstack, { StackConfig } from '@contentstack/delivery-sdk';

const params: StackConfig = {
apiKey: <API_KEY>,
deliveryToken: <DELIVERY_TOKEN>,
environment: <ENVIRONMENT>,
}
const Stack = contentstack.stack(params);

const result = await Stack
.contentType('<CONTENT_TYPE_UID>')
.entry('<ENTRY_UID>')
.includeEmbeddedItems()
.fetch<BlogPostEntry>();

Contentstack.Utils.render({
entry: result,
renderOption
})
```
If you have multiple RTE fields in an entry and want to fetch the embedded items from a particular RTE field, you need to provide a path of those RTE fields.

Refer to the example code below:
```js
```ts
//code to render embedded item from an RTE field and from another RTE field nested within a group field
Contentstack.Utils.render({ entry, path: ["rte_fieldUid", "group.rteFieldUID"], renderOption })
Contentstack.Utils.render({
entry: result,
path: ["rte_fieldUid", "group.rteFieldUID"],
renderOption
})
```

#### Render Supercharged RTE contents
To get a single entry, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use `Contentstack.Utils.jsonToHTML` function as shown below:
```js
import * as Contentstack from 'contentstack'
const stack = Contentstack.Stack({
api_key: '<API_KEY>',
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
environment: '<ENVIRONMENT>'})

stack.ContentType('<CONTENT_TYPE_UID>')
.Entry('<ENTRY_UID>')
.toJSON()
.fetch()
.then(entry => {
Contentstack.Utils.jsonToHTML({
entry,
path: ["rte_fieldUid", "group.rteFieldUID"],
renderOption
})
})
```ts
import contentstack, { StackConfig } from '@contentstack/delivery-sdk';

const params: StackConfig = {
apiKey: <API_KEY>,
deliveryToken: <DELIVERY_TOKEN>,
environment: <ENVIRONMENT>,
}
const Stack = contentstack.stack(params);

const result = await Stack
.contentType('<CONTENT_TYPE_UID>')
.entry('<ENTRY_UID>')
.includeEmbeddedItems()
.fetch<BlogPostEntry>();

Contentstack.Utils.jsonToHTML({
entry: result,
paths: ["rte_fieldUid", "group.rteFieldUID"],
renderOption
})
```
> Node: Supercharged RTE also supports Embedded items to get all embedded items while fetching entry use `includeEmbeddedItems` function.

### Fetch Embedded Item(s) from Multiple Entries
#### Render HTML RTE Embedded object

To get embedded items from multiple entries, you need to provide the content type UID. You can also use the path variable in case the entries have multiple RTE fields.
```js
import Contentstack from 'contentstack'
const stack = Contentstack.Stack({
api_key: '<API_KEY>',
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
environment: '<ENVIRONMENT>'})

stack.ContentType('<CONTENT_TYPE_UID>')
.Query()
.toJSON()
.where('title', '<entry_title_to_search>')
.includeEmbeddedItems() // include embedded items
.find()
.then(result => {
result.forEach(entry => {
Contentstack.Utils.render({
entry,
path: ['rte', 'group.rteFieldUID'],
renderOption
})
})
})
```ts
import contentstack, { StackConfig } from '@contentstack/delivery-sdk'

const params: StackConfig = {
apiKey: <API_KEY>,
deliveryToken: <DELIVERY_TOKEN>,
environment: <ENVIRONMENT>,
}
const Stack = contentstack.stack(params);

const result = await Stack
.contentType('<CONTENT_TYPE_UID>')
.entry()
.includeEmbeddedItems()
.find<BlogPostEntry>();

result.entries.forEach(entry => {
Contentstack.Utils.render({
entry,
paths: ["rte_fieldUid", "group.rteFieldUID"],
renderOption
})
})
```

#### Render Supercharged RTE contents
To get a multiple entries, you need to provide the stack API key, environment name, delivery token, content type and entry UID. Then, use `Contentstack.Utils.jsonToHTML` function as shown below:
```js
import * as Contentstack from 'contentstack'
const stack = Contentstack.Stack({
api_key: '<API_KEY>',
delivery_token: '<ENVIRONMENT_SPECIFIC_DELIVERY_TOKEN>',
environment: '<ENVIRONMENT>'})

stack.ContentType('<CONTENT_TYPE_UID>')
.Query()
.toJSON()
.where('title', '<entry_title_to_search>')
.find()
.then(result => {
result.forEach(entry => {
Contentstack.Utils.jsonToHTML({
entry,
path: ["rte_fieldUid", "group.rteFieldUID"],
renderOption
})
})
})
```ts
import contentstack, { StackConfig } from '@contentstack/delivery-sdk';

const params: StackConfig = {
apiKey: <API_KEY>,
deliveryToken: <DELIVERY_TOKEN>,
environment: <ENVIRONMENT>,
}
const Stack = contentstack.stack(params);

const result = await Stack
.contentType('<CONTENT_TYPE_UID>')
.entry()
.includeEmbeddedItems()
.find<BlogPostEntry>();

result.entries.forEach(entry => {
Contentstack.Utils.jsonToHTML({
entry,
paths: ["rte_fieldUid", "group.rteFieldUID"],
renderOption
})
})
```

> Node: Supercharged RTE also supports Embedded items to get all embedded items while fetching entry use `includeEmbeddedItems` function.
Loading