Skip to content

Commit 21048fc

Browse files
2 parents 596b1f6 + b547f64 commit 21048fc

39 files changed

+2421
-82
lines changed

.github/workflows/codeql-analysis.yml

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

.github/workflows/jira.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ on:
33
pull_request:
44
types: [opened]
55
jobs:
6-
security:
6+
security-jira:
77
if: ${{ github.actor == 'dependabot[bot]' || github.actor == 'snyk-bot' || contains(github.event.pull_request.head.ref, 'snyk-fix-') || contains(github.event.pull_request.head.ref, 'snyk-upgrade-')}}
88
runs-on: ubuntu-latest
99
steps:
@@ -26,3 +26,8 @@ jobs:
2626
PR: ${{ github.event.pull_request.html_url }}
2727
2828
fields: "${{ secrets.JIRA_FIELDS }}"
29+
- name: Transition issue
30+
uses: atlassian/gajira-transition@v3
31+
with:
32+
issue: ${{ steps.create.outputs.issue }}
33+
transition: ${{ secrets.JIRA_TRANSITION }}

.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/sca-scan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ on:
33
pull_request:
44
types: [opened, synchronize, reopened]
55
jobs:
6-
security:
6+
security-sca:
77
runs-on: ubuntu-latest
88
steps:
99
- uses: actions/checkout@master

.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)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# contentstack-management-javascript-variants
12
[![Contentstack](https://www.contentstack.com/docs/static/images/contentstack.png)](https://www.contentstack.com/)
23

34
## Contentstack Management JavaScript SDK

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: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
* const data = {
34+
* "entry": {
35+
* "title": "example",
36+
* "url": "/example",
37+
* "_variant": {
38+
* "_change_set": [
39+
* "title",
40+
* "url"
41+
* ]
42+
* }
43+
* }
44+
* }
45+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').update(data)
46+
* .then((variants) => console.log(variants))
47+
*/
48+
this.update = async (data) => {
49+
try {
50+
const response = await http.put(this.urlPath,
51+
data,
52+
{
53+
headers: {
54+
...cloneDeep(this.stackHeaders)
55+
}
56+
})
57+
if (response.data) {
58+
return response.data
59+
} else {
60+
return error(response)
61+
}
62+
} catch (err) {
63+
return error(err)
64+
}
65+
}
66+
67+
/**
68+
* @description The Delete an variants call is used to delete a specific variants from a content type.
69+
* @memberof Variants
70+
* @func delete
71+
* @returns {Object} Response Object.
72+
* @example
73+
* import * as contentstack from '@contentstack/management'
74+
* const client = contentstack.client()
75+
*
76+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').delete()
77+
* .then((response) => console.log(response.notice))
78+
*/
79+
this.delete = deleteEntity(http)
80+
81+
/**
82+
* @description The fetch Variants call fetches Variants details.
83+
* @memberof Variants
84+
* @func fetch
85+
* @returns {Promise<Variants.Variants>} Promise for Variants instance
86+
* @example
87+
* import * as contentstack from '@contentstack/management'
88+
* const client = contentstack.client()
89+
*
90+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').fetch()
91+
* .then((variants) => console.log(variants))
92+
*
93+
*/
94+
this.fetch = fetch(http, 'variants')
95+
96+
/**
97+
* @description The version Variants call fetches Variants version details.
98+
* @memberof Variants
99+
* @func versions
100+
* @returns {Promise<Variants.Variants>} Promise for Variants instance
101+
* @example
102+
* import * as contentstack from '@contentstack/management'
103+
* const client = contentstack.client()
104+
*
105+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').entry('entry_uid').variants('uid').versions()
106+
* .then((variants) => console.log(variants))
107+
*
108+
*/
109+
this.versions = async () => {
110+
try {
111+
const response = await http.get(`${this.urlPath}/versions`, {
112+
headers: {
113+
...cloneDeep(this.stackHeaders)
114+
}
115+
})
116+
if (response.data) {
117+
return response.data
118+
} else {
119+
return error(response)
120+
}
121+
} catch (err) {
122+
return error(err)
123+
}
124+
}
125+
126+
} else {
127+
/**
128+
* @description The Query on Variants will allow to fetch details of all or specific Variants
129+
* @memberof Variants
130+
* @func query
131+
* @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.
132+
* @param {Object} query Queries that you can use to fetch filtered results.
133+
* @returns {Array<Variants>} Array of Variants.
134+
*
135+
* @example
136+
* import * as contentstack from '@contentstack/management'
137+
* const client = contentstack.client()
138+
*
139+
* client.stack().contentType('content_type_uid').entry('entry_uid').variants().query({ query: { title: 'Variants title' } }).find()
140+
* .then((entries) => console.log(entries))
141+
*/
142+
this.query = query({ http: http, wrapperCollection: VariantsCollection })
143+
}
144+
}
145+
export function VariantsCollection(http, data) {
146+
const obj = cloneDeep(data.entries) || []
147+
const variantCollection = obj.map((variant) => {
148+
return new Variants(http, {
149+
content_type_uid: variant.content_type_uid,
150+
entry_uid: variant.entry_uid,
151+
variants_uid: variant.uid,
152+
stackHeaders: data.stackHeaders
153+
})
154+
})
155+
return variantCollection
156+
}
157+
158+
export function createFormData(variants) {
159+
return () => {
160+
const formData = new FormData()
161+
const uploadStream = createReadStream(variants)
162+
formData.append('variants', uploadStream)
163+
return formData
164+
}
165+
}

0 commit comments

Comments
 (0)