Skip to content

Commit

Permalink
Variants Implementation with Unit testcases (#3)
Browse files Browse the repository at this point in the history
* Variants Implementation with Unit testcases

* Upgraded node version

* Updated packagelock.json file

* Added Typescript support

* Added feature for fetch base entry with variants details

* Fixed PR comments

* Will enable once api's get ready

* Updated node version

* Deleted CodeQL action in private repo

* Fixed error in includeVariants method and docs corrections

* Fixed unit test error
  • Loading branch information
sunil-lakshman authored May 2, 2024
1 parent 1149b5f commit dcbeb49
Show file tree
Hide file tree
Showing 16 changed files with 1,939 additions and 11,572 deletions.
68 changes: 0 additions & 68 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '12.x'
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci
- run: npm publish
Expand All @@ -25,7 +25,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '12.x'
node-version: '18.x'
registry-url: 'https://npm.pkg.github.com'
scope: '@contentstack'
- run: npm ci
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2 # checkout the repo
- uses: actions/setup-node@v3
with:
node-version: '12.x'
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'
- run: npm ci # install packages
- run: npm run test:unit:report:json # run tests (configured to use jest-junit reporter)
Expand Down
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Changelog

## [v1.16.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.16.0) (2024-04-02)
- Feature
- Variants support added
## [v1.15.3](https://github.com/contentstack/contentstack-management-javascript/tree/v1.15.3) (2024-02-16)
- Fix
- Fix for updating entry
Expand Down
1 change: 0 additions & 1 deletion lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ export const fetch = (http, type, params = {}) => {
if (this.organization_uid) {
headers.headers.organization_uid = this.organization_uid
}

const response = await http.get(this.urlPath, headers)
if (response.data) {
if (type === 'entry') {
Expand Down
61 changes: 61 additions & 0 deletions lib/stack/contentType/entry/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { create,
import FormData from 'form-data'
import { createReadStream } from 'fs'
import error from '../../../core/contentstackError'
import { Variants } from './variants/index'

/**
* An entry is the actual piece of content created using one of the defined content types. Read more about <a href='https://www.contentstack.com/docs/guide/content-management'>Entries</a>.
Expand Down Expand Up @@ -265,6 +266,66 @@ export function Entry (http, data) {
throw error(err)
}
}

/**
* @description The variants requestan entry call is used to fetch a specific entry with variants from a content type.
* @memberof Entry
* @func variants
* @returns {Promise<Object>} Response Object.
* @param {Object} publishing_rule Details for the publish request
* @param {String} locale Enter the code of the locale that the entry belongs to.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('uid').variants('uid').fetch()
* .then((response) => console.log(response.notice));
*/
this.variants = (uid = null) => {
const data = { stackHeaders: this.stackHeaders }
data.content_type_uid = this.content_type_uid
data.entry_uid = this.uid
if (uid) {
data.variants_uid = uid
}
return new Variants(http, data)
}

/**
* @description The includeVariants an entry call is used to fetch a specific base entry with variants from a content type.
* @memberof Variants
* @func includeVariants
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('uid').includeVariants('true','variants_uid')
* .then((response) => console.log(response))
*/

this.includeVariants = async (include_variant, variants_uid) => {
try {
const headers = {
...cloneDeep(this.stackHeaders) // Clone existing headers
};

// Add custom header
headers['x-cs-variant-uid'] = variants_uid; // add variant UID
let params = {};
if (include_variant) {
params.include_variant = include_variant; // if include_variant present
}
const response = await http.get(this.urlPath, { headers, params });
if (response.data) {
return response.data;
} else {
throw error(response);
}
} catch (err) {
error(err);
}
};

} else {
/**
* @description The Create an entry call creates a new entry for the selected content type.
Expand Down
111 changes: 111 additions & 0 deletions lib/stack/contentType/entry/variants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import cloneDeep from 'lodash/cloneDeep'
import {
update,
deleteEntity,
fetch,
upload,
query,
parseData
}
from '../../../../entity'
import FormData from 'form-data'
import {
createReadStream
} from 'fs'
import error from '../../../../core/contentstackError'
/**
* An variants is the actual piece of content created using one of the defined content types. Read more about <a href='https://www.contentstack.com/docs/guide/content-management'>Entries</a>.
* @namespace Variants
*/
export function Variants(http, data) {
Object.assign(this, cloneDeep(data))
this.urlPath = `/content_types/${this.content_type_uid}/entries/${this.entry_uid}/variants`
if (data && data.variants_uid) {
this.urlPath += `/${this.variants_uid}`
/**
* @description The Create an variants call creates a new variants for the selected content type.
* @memberof Variants
* @func update
* @returns {Promise<Variants.Variants>} Promise for Variants instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').fetch()
* .then((variants) => {
* variants.title = 'My New Variants'
* variants.description = 'Variants description'
* return variants.update()
* })
* .then((variants) => console.log(variants))
*/
this.update = update(http, 'variants')

/**
* @description The Delete an variants call is used to delete a specific variants from a content type.
* @memberof Variants
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)

/**
* @description The fetch Variants call fetches Variants details.
* @memberof Variants
* @func fetch
* @returns {Promise<Variants.Variants>} Promise for Variants instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').fetch()
* .then((variants) => console.log(variants))
*
*/
this.fetch = fetch(http, 'variants')
} else {
/**
* @description The Query on Variants will allow to fetch details of all or specific Variants
* @memberof Variants
* @func query
* @param {Int} locale Enter the code of the language of which the entries need to be included. Only the entries published in this locale will be displayed.
* @param {Object} query Queries that you can use to fetch filtered results.
* @returns {Array<Variants>} Array of Variants.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().contentType('content_type_uid').entry('entry_uid').variants().query({ query: { title: 'Variants title' } }).find()
* .then((entries) => console.log(entries))
*/
this.query = query({ http: http, wrapperCollection: VariantsCollection })
}
}
export function VariantsCollection(http, data) {
const obj = cloneDeep(data.entries) || []
const variantCollection = obj.map((variants) => {
return new Variants(http, {
content_type_uid: data.content_type_uid,
entry_uid: data.entry_uid,
variants_uid: data.variants_uid,
stackHeaders: data.stackHeaders
})
})
return variantCollection
}

export function createFormData(variants) {
return () => {
const formData = new FormData()
const uploadStream = createReadStream(variants)
formData.append('variants', uploadStream)
return formData
}
}
Loading

0 comments on commit dcbeb49

Please sign in to comment.