Skip to content

Commit dcbeb49

Browse files
Variants Implementation with Unit testcases (#3)
* 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
1 parent 1149b5f commit dcbeb49

File tree

16 files changed

+1939
-11572
lines changed

16 files changed

+1939
-11572
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 0 additions & 68 deletions
This file was deleted.

.github/workflows/npm-publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v3
1414
- uses: actions/setup-node@v3
1515
with:
16-
node-version: '12.x'
16+
node-version: '18.x'
1717
registry-url: 'https://registry.npmjs.org'
1818
- run: npm ci
1919
- run: npm publish
@@ -25,7 +25,7 @@ jobs:
2525
- uses: actions/checkout@v3
2626
- uses: actions/setup-node@v3
2727
with:
28-
node-version: '12.x'
28+
node-version: '18.x'
2929
registry-url: 'https://npm.pkg.github.com'
3030
scope: '@contentstack'
3131
- run: npm ci

.github/workflows/unit-test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v2 # checkout the repo
1414
- uses: actions/setup-node@v3
1515
with:
16-
node-version: '12.x'
16+
node-version: '18.x'
1717
registry-url: 'https://registry.npmjs.org'
1818
- run: npm ci # install packages
1919
- run: npm run test:unit:report:json # run tests (configured to use jest-junit reporter)

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
2-
2+
## [v1.16.0](https://github.com/contentstack/contentstack-management-javascript/tree/v1.16.0) (2024-04-02)
3+
- Feature
4+
- Variants support added
35
## [v1.15.3](https://github.com/contentstack/contentstack-management-javascript/tree/v1.15.3) (2024-02-16)
46
- Fix
57
- Fix for updating entry

lib/entity.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,6 @@ export const fetch = (http, type, params = {}) => {
205205
if (this.organization_uid) {
206206
headers.headers.organization_uid = this.organization_uid
207207
}
208-
209208
const response = await http.get(this.urlPath, headers)
210209
if (response.data) {
211210
if (type === 'entry') {

lib/stack/contentType/entry/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { create,
1212
import FormData from 'form-data'
1313
import { createReadStream } from 'fs'
1414
import error from '../../../core/contentstackError'
15+
import { Variants } from './variants/index'
1516

1617
/**
1718
* 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>.
@@ -265,6 +266,66 @@ export function Entry (http, data) {
265266
throw error(err)
266267
}
267268
}
269+
270+
/**
271+
* @description The variants requestan entry call is used to fetch a specific entry with variants from a content type.
272+
* @memberof Entry
273+
* @func variants
274+
* @returns {Promise<Object>} Response Object.
275+
* @param {Object} publishing_rule Details for the publish request
276+
* @param {String} locale Enter the code of the locale that the entry belongs to.
277+
* @example
278+
* import * as contentstack from '@contentstack/management'
279+
* const client = contentstack.client()
280+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('uid').variants('uid').fetch()
281+
* .then((response) => console.log(response.notice));
282+
*/
283+
this.variants = (uid = null) => {
284+
const data = { stackHeaders: this.stackHeaders }
285+
data.content_type_uid = this.content_type_uid
286+
data.entry_uid = this.uid
287+
if (uid) {
288+
data.variants_uid = uid
289+
}
290+
return new Variants(http, data)
291+
}
292+
293+
/**
294+
* @description The includeVariants an entry call is used to fetch a specific base entry with variants from a content type.
295+
* @memberof Variants
296+
* @func includeVariants
297+
* @returns {Object} Response Object.
298+
* @example
299+
* import * as contentstack from '@contentstack/management'
300+
* const client = contentstack.client()
301+
*
302+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('uid').includeVariants('true','variants_uid')
303+
* .then((response) => console.log(response))
304+
*/
305+
306+
this.includeVariants = async (include_variant, variants_uid) => {
307+
try {
308+
const headers = {
309+
...cloneDeep(this.stackHeaders) // Clone existing headers
310+
};
311+
312+
// Add custom header
313+
headers['x-cs-variant-uid'] = variants_uid; // add variant UID
314+
let params = {};
315+
if (include_variant) {
316+
params.include_variant = include_variant; // if include_variant present
317+
}
318+
const response = await http.get(this.urlPath, { headers, params });
319+
if (response.data) {
320+
return response.data;
321+
} else {
322+
throw error(response);
323+
}
324+
} catch (err) {
325+
error(err);
326+
}
327+
};
328+
268329
} else {
269330
/**
270331
* @description The Create an entry call creates a new entry for the selected content type.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import {
3+
update,
4+
deleteEntity,
5+
fetch,
6+
upload,
7+
query,
8+
parseData
9+
}
10+
from '../../../../entity'
11+
import FormData from 'form-data'
12+
import {
13+
createReadStream
14+
} from 'fs'
15+
import error from '../../../../core/contentstackError'
16+
/**
17+
* 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>.
18+
* @namespace Variants
19+
*/
20+
export function Variants(http, data) {
21+
Object.assign(this, cloneDeep(data))
22+
this.urlPath = `/content_types/${this.content_type_uid}/entries/${this.entry_uid}/variants`
23+
if (data && data.variants_uid) {
24+
this.urlPath += `/${this.variants_uid}`
25+
/**
26+
* @description The Create an variants call creates a new variants for the selected content type.
27+
* @memberof Variants
28+
* @func update
29+
* @returns {Promise<Variants.Variants>} Promise for Variants instance
30+
* @example
31+
* import * as contentstack from '@contentstack/management'
32+
* const client = contentstack.client()
33+
*
34+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').fetch()
35+
* .then((variants) => {
36+
* variants.title = 'My New Variants'
37+
* variants.description = 'Variants description'
38+
* return variants.update()
39+
* })
40+
* .then((variants) => console.log(variants))
41+
*/
42+
this.update = update(http, 'variants')
43+
44+
/**
45+
* @description The Delete an variants call is used to delete a specific variants from a content type.
46+
* @memberof Variants
47+
* @func delete
48+
* @returns {Object} Response Object.
49+
* @example
50+
* import * as contentstack from '@contentstack/management'
51+
* const client = contentstack.client()
52+
*
53+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').delete()
54+
* .then((response) => console.log(response.notice))
55+
*/
56+
this.delete = deleteEntity(http)
57+
58+
/**
59+
* @description The fetch Variants call fetches Variants details.
60+
* @memberof Variants
61+
* @func fetch
62+
* @returns {Promise<Variants.Variants>} Promise for Variants instance
63+
* @example
64+
* import * as contentstack from '@contentstack/management'
65+
* const client = contentstack.client()
66+
*
67+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').fetch()
68+
* .then((variants) => console.log(variants))
69+
*
70+
*/
71+
this.fetch = fetch(http, 'variants')
72+
} else {
73+
/**
74+
* @description The Query on Variants will allow to fetch details of all or specific Variants
75+
* @memberof Variants
76+
* @func query
77+
* @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.
78+
* @param {Object} query Queries that you can use to fetch filtered results.
79+
* @returns {Array<Variants>} Array of Variants.
80+
*
81+
* @example
82+
* import * as contentstack from '@contentstack/management'
83+
* const client = contentstack.client()
84+
*
85+
* client.stack().contentType('content_type_uid').entry('entry_uid').variants().query({ query: { title: 'Variants title' } }).find()
86+
* .then((entries) => console.log(entries))
87+
*/
88+
this.query = query({ http: http, wrapperCollection: VariantsCollection })
89+
}
90+
}
91+
export function VariantsCollection(http, data) {
92+
const obj = cloneDeep(data.entries) || []
93+
const variantCollection = obj.map((variants) => {
94+
return new Variants(http, {
95+
content_type_uid: data.content_type_uid,
96+
entry_uid: data.entry_uid,
97+
variants_uid: data.variants_uid,
98+
stackHeaders: data.stackHeaders
99+
})
100+
})
101+
return variantCollection
102+
}
103+
104+
export function createFormData(variants) {
105+
return () => {
106+
const formData = new FormData()
107+
const uploadStream = createReadStream(variants)
108+
formData.append('variants', uploadStream)
109+
return formData
110+
}
111+
}

0 commit comments

Comments
 (0)