Skip to content

Commit f1b2f87

Browse files
Added api version management
1 parent 391fcc0 commit f1b2f87

5 files changed

Lines changed: 31 additions & 21 deletions

File tree

lib/core/serviceVersion.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export const DEFAULT_API_VERSION = '3.0'
2+
3+
export const SERVICE_VERSIONS = {
4+
bulk_publish: '3.2',
5+
bulk_unpublish: '3.2',
6+
bulk_job_status: '3.2',
7+
bulk_job_items: '3.2',
8+
global_field: '3.2',
9+
release: '3.2'
10+
}
11+
12+
export function getServiceVersion (serviceKey) {
13+
return SERVICE_VERSIONS[serviceKey] ?? DEFAULT_API_VERSION
14+
}

lib/stack/bulkOperation/index.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import cloneDeep from 'lodash/cloneDeep'
22
import { publishUnpublish } from '../../entity'
33
import { bindModuleHeaders } from '../../core/moduleHeaderSupport.js'
4+
import { getServiceVersion } from '../../core/serviceVersion.js'
45

56
/**
67
* Bulk operations such as Publish, Unpublish, and Delete on multiple entries or assets.
@@ -101,7 +102,7 @@ export function BulkOperation (http, data = {}) {
101102
* .then((response) => { console.log(response) })
102103
*/
103104
// eslint-disable-next-line camelcase
104-
this.jobStatus = async ({ job_id, bulk_version = '', api_version = '' }) => {
105+
this.jobStatus = async ({ job_id, bulk_version = '', api_version = getServiceVersion('bulk_job_status') }) => {
105106
// eslint-disable-next-line camelcase
106107
this.urlPath = `/bulk/jobs/${job_id}`
107108
const headers = {
@@ -112,17 +113,13 @@ export function BulkOperation (http, data = {}) {
112113
// eslint-disable-next-line camelcase
113114
if (bulk_version) headers.headers.bulk_version = bulk_version
114115
// eslint-disable-next-line camelcase
115-
if (api_version) headers.headers.api_version = api_version
116+
headers.headers.api_version = api_version
116117
try {
117118
const response = await http.get(this.urlPath, headers)
118119
if (response.data) {
119-
// eslint-disable-next-line camelcase
120-
if (api_version) delete headers.headers.api_version
121120
return response.data
122121
}
123122
} catch (error) {
124-
// eslint-disable-next-line camelcase
125-
if (api_version) delete headers.headers.api_version
126123
console.error(error)
127124
}
128125
}
@@ -145,7 +142,7 @@ export function BulkOperation (http, data = {}) {
145142
// eslint-disable-next-line camelcase
146143
this.getJobItems = async (job_id, params = {}) => {
147144
// eslint-disable-next-line camelcase
148-
const { api_version = '3.2', ...queryParams } = cloneDeep(params)
145+
const { api_version = getServiceVersion('bulk_job_items'), ...queryParams } = cloneDeep(params)
149146
// eslint-disable-next-line camelcase
150147
this.urlPath = `/bulk/jobs/${job_id}/items`
151148
const headers = {
@@ -154,18 +151,14 @@ export function BulkOperation (http, data = {}) {
154151
}
155152
}
156153
// eslint-disable-next-line camelcase
157-
if (api_version) headers.headers.api_version = api_version
154+
headers.headers.api_version = api_version
158155
if (Object.keys(queryParams).length > 0) headers.params = queryParams
159156
try {
160157
const response = await http.get(this.urlPath, headers)
161158
if (response.data) {
162-
// eslint-disable-next-line camelcase
163-
if (api_version) delete headers.headers.api_version
164159
return response.data
165160
}
166161
} catch (error) {
167-
// eslint-disable-next-line camelcase
168-
if (api_version) delete headers.headers.api_version
169162
console.error(error)
170163
}
171164
}
@@ -231,7 +224,7 @@ export function BulkOperation (http, data = {}) {
231224
*
232225
*/
233226
// eslint-disable-next-line camelcase
234-
this.publish = async ({ details, skip_workflow_stage = false, approvals = false, is_nested = false, api_version = '', publishAllLocalized = false }) => {
227+
this.publish = async ({ details, skip_workflow_stage = false, approvals = false, is_nested = false, api_version = getServiceVersion('bulk_publish'), publishAllLocalized = false }) => {
235228
var httpBody = {}
236229
if (details) {
237230
httpBody = cloneDeep(details)
@@ -264,7 +257,7 @@ export function BulkOperation (http, data = {}) {
264257
}
265258

266259
// eslint-disable-next-line camelcase
267-
if (api_version) headers.headers.api_version = api_version
260+
headers.headers.api_version = api_version
268261

269262
return publishUnpublish(http, '/bulk/publish', httpBody, headers)
270263
}
@@ -329,7 +322,7 @@ export function BulkOperation (http, data = {}) {
329322
* .then((response) => { console.log(response.notice) })
330323
*/
331324
// eslint-disable-next-line camelcase
332-
this.unpublish = async ({ details, skip_workflow_stage = false, approvals = false, is_nested = false, api_version = '', unpublishAllLocalized = false }) => {
325+
this.unpublish = async ({ details, skip_workflow_stage = false, approvals = false, is_nested = false, api_version = getServiceVersion('bulk_unpublish'), unpublishAllLocalized = false }) => {
333326
var httpBody = {}
334327
if (details) {
335328
httpBody = cloneDeep(details)
@@ -355,7 +348,7 @@ export function BulkOperation (http, data = {}) {
355348
headers.headers.approvals = approvals
356349
}
357350
// eslint-disable-next-line camelcase
358-
if (api_version) headers.headers.api_version = api_version
351+
headers.headers.api_version = api_version
359352

360353
if (unpublishAllLocalized) {
361354
if (!headers.params) {

lib/stack/globalField/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import error from '../../core/contentstackError'
44
import FormData from 'form-data'
55
import { createReadStream } from 'fs'
66
import { bindModuleHeaders } from '../../core/moduleHeaderSupport.js'
7+
import { getServiceVersion } from '../../core/serviceVersion.js'
78

89
/**
910
* GlobalField defines the structure or schema of a page or a section of your web or mobile property. To create global Fields for your application, you are required to first create a global field. Read more about <a href='https://www.contentstack.com/docs/guide/global-fields'>Global Fields</a>.
@@ -15,7 +16,7 @@ export function GlobalField (http, data = {}) {
1516

1617
this.stackHeaders = {
1718
...cloneDeep(rawHeaders),
18-
...(data.apiVersion ? { api_version: data.apiVersion } : {}),
19+
api_version: data.apiVersion ?? getServiceVersion('global_field'),
1920
...(data.branch ? { branch: data.branch } : {})
2021
}
2122

lib/stack/release/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import error from '../../core/contentstackError'
99
import { bindModuleHeaders } from '../../core/moduleHeaderSupport.js'
1010
import { ReleaseItem, ReleaseItemCollection } from './items'
11+
import { getServiceVersion } from '../../core/serviceVersion.js'
1112
/**
1213
* @description You can pin a set of entries and assets (along with the deploy action, i.e., publish/unpublish) to a ‘release’, and then deploy this release to an environment.
1314
* This will publish/unpublish all the items of the release to the specified environment. Read more about <a href='https://www.contentstack.com/docs/developers/create-releases'>Releases</a>.
@@ -167,9 +168,10 @@ export function Release (http, data = {}) {
167168
var release = { environments, locales, scheduledAt, action }
168169
const headers = {
169170
headers: {
170-
...cloneDeep(this.stackHeaders)
171+
...cloneDeep(this.stackHeaders),
172+
api_version: getServiceVersion('release')
171173
}
172-
} || {}
174+
}
173175

174176
try {
175177
const response = await http.post(`${this.urlPath}/deploy`, { release }, headers)

test/unit/globalField-test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('Contentstack GlobalField test', () => {
1010
it('GlobalField test without uid', (done) => {
1111
const globalField = makeGlobalField()
1212
expect(globalField.urlPath).to.be.equal('/global_fields')
13-
expect(globalField.stackHeaders).to.deep.equal({})
13+
expect(globalField.stackHeaders).to.deep.equal({ api_version: '3.2' })
1414
expect(globalField.update).to.be.equal(undefined)
1515
expect(globalField.delete).to.be.equal(undefined)
1616
expect(globalField.fetch).to.be.equal(undefined)
@@ -27,7 +27,7 @@ describe('Contentstack GlobalField test', () => {
2727
})
2828

2929
expect(globalField.urlPath).to.be.equal(`/global_fields/${systemUidMock.uid}`)
30-
expect(globalField.stackHeaders).to.deep.equal({})
30+
expect(globalField.stackHeaders).to.deep.equal({ api_version: '3.2' })
3131
expect(globalField.update).to.not.equal(undefined)
3232
expect(globalField.delete).to.not.equal(undefined)
3333
expect(globalField.fetch).to.not.equal(undefined)

0 commit comments

Comments
 (0)