Skip to content

Commit 90e0d12

Browse files
Fixed migration rte issue in CLI
1 parent 5b0af75 commit 90e0d12

File tree

4 files changed

+58
-157
lines changed

4 files changed

+58
-157
lines changed

lib/entity.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ export const fetchAll = (http, wrapperCollection, params = {}) => {
265265

266266
export function parseData (response, stackHeaders, contentTypeUID, taxonomyUid, http) {
267267
const data = response.data || {}
268+
if (stackHeaders && 'api_version' in stackHeaders) {
269+
delete stackHeaders.api_version
270+
}
268271
if (stackHeaders) {
269272
data.stackHeaders = stackHeaders
270273
}

lib/stack/globalField/index.js

Lines changed: 8 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import cloneDeep from 'lodash/cloneDeep'
2-
import { query, upload, parseData } from '../../entity'
2+
import { create, update, deleteEntity, fetch, query, upload, parseData } from '../../entity'
33
import error from '../../core/contentstackError'
44
import FormData from 'form-data'
55
import { createReadStream } from 'fs'
@@ -12,7 +12,7 @@ import { createReadStream } from 'fs'
1212
export function GlobalField (http, data = {}) {
1313
this.stackHeaders = data.stackHeaders
1414
if (data.api_version) {
15-
this.apiVersion = data.api_version
15+
this.stackHeaders.api_version = data.apiVersion
1616
}
1717
this.urlPath = `/global_fields`
1818

@@ -37,40 +37,7 @@ export function GlobalField (http, data = {}) {
3737
* .then((globalField) => console.log(globalField))
3838
*
3939
*/
40-
this.update = async (config) => {
41-
try {
42-
if (this.apiVersion) {
43-
this.stackHeaders.api_version = this.apiVersion
44-
}
45-
const headers = {
46-
headers: { ...cloneDeep(this.stackHeaders) }
47-
}
48-
let payload = config
49-
if (!config) {
50-
const {
51-
stackHeaders,
52-
apiVersion,
53-
update,
54-
delete: deleteFn,
55-
fetch,
56-
...globalFieldPayload
57-
} = cloneDeep(this)
58-
59-
payload = { global_field: globalFieldPayload }
60-
}
61-
const response = await http.put(`${this.urlPath}`, payload, headers)
62-
if (this.apiVersion) {
63-
delete this.stackHeaders.api_version
64-
}
65-
if (response.data) {
66-
return new this.constructor(http, parseData(response, this.stackHeaders))
67-
} else {
68-
throw error(response)
69-
}
70-
} catch (err) {
71-
throw error(err)
72-
}
73-
}
40+
this.update = update(http, 'global_field')
7441

7542
/**
7643
* @description The Delete GlobalField call is used to delete an existing GlobalField permanently from your Stack.
@@ -84,33 +51,7 @@ export function GlobalField (http, data = {}) {
8451
* client.stack({ api_key: 'api_key'}).globalField('global_field_uid').delete()
8552
* .then((response) => console.log(response.notice))
8653
*/
87-
this.delete = async () => {
88-
const param = {}
89-
try {
90-
if (this.apiVersion) {
91-
this.stackHeaders.api_version = this.apiVersion
92-
}
93-
const headers = {
94-
headers: {
95-
...cloneDeep(this.stackHeaders)
96-
},
97-
params: {
98-
...cloneDeep(param)
99-
}
100-
}
101-
const response = await http.delete(this.urlPath, headers)
102-
if (this.apiVersion) {
103-
delete this.stackHeaders.api_version
104-
}
105-
if (response.data) {
106-
return response.data
107-
} else {
108-
throw error(response)
109-
}
110-
} catch (err) {
111-
throw error(err)
112-
}
113-
}
54+
this.delete = deleteEntity(http)
11455

11556
/**
11657
* @description The fetch GlobalField call fetches GlobalField details.
@@ -125,32 +66,7 @@ export function GlobalField (http, data = {}) {
12566
* .then((globalField) => console.log(globalField))
12667
*
12768
*/
128-
this.fetch = async function (param = {}) {
129-
try {
130-
if (this.apiVersion) {
131-
this.stackHeaders.api_version = this.apiVersion
132-
}
133-
const headers = {
134-
headers: {
135-
...cloneDeep(this.stackHeaders)
136-
},
137-
params: {
138-
...cloneDeep(param)
139-
}
140-
}
141-
const response = await http.get(this.urlPath, headers)
142-
if (this.apiVersion) {
143-
delete this.stackHeaders.api_version
144-
}
145-
if (response.data) {
146-
return new this.constructor(http, parseData(response, this.stackHeaders))
147-
} else {
148-
throw error(response)
149-
}
150-
} catch (err) {
151-
throw error(err)
152-
}
153-
}
69+
this.fetch = fetch(http, 'global_field')
15470
} else {
15571
/**
15672
* @description The Create a GlobalField call creates a new globalField in a particular stack of your Contentstack account.
@@ -173,29 +89,7 @@ export function GlobalField (http, data = {}) {
17389
* client.stack().globalField().create({ global_field })
17490
* .then((globalField) => console.log(globalField))
17591
*/
176-
this.create = async (payload) => {
177-
try {
178-
if (this.apiVersion) {
179-
this.stackHeaders.api_version = this.apiVersion
180-
}
181-
const headers = {
182-
headers: {
183-
...cloneDeep(this.stackHeaders)
184-
}
185-
}
186-
const response = await http.post(`${this.urlPath}`, payload, headers)
187-
if (this.apiVersion) {
188-
delete this.stackHeaders.api_version
189-
}
190-
if (response.data) {
191-
return new this.constructor(http, parseData(response, this.stackHeaders))
192-
} else {
193-
throw error(response)
194-
}
195-
} catch (err) {
196-
return error(err)
197-
}
198-
}
92+
this.create = create({ http: http })
19993

20094
/**
20195
* @description The Query on GlobalField will allow to fetch details of all or specific GlobalField
@@ -210,7 +104,7 @@ export function GlobalField (http, data = {}) {
210104
* client.stack().globalField().query({ query: { name: 'Global Field Name' } }).find()
211105
* .then((globalFields) => console.log(globalFields))
212106
*/
213-
this.query = query({ http: http, wrapperCollection: GlobalFieldCollection, apiVersion: this.apiVersion })
107+
this.query = query({ http: http, wrapperCollection: GlobalFieldCollection, apiVersion: this.apiVersion })
214108

215109
/**
216110
* @description The Import a global field call imports a global field into a stack.
@@ -266,4 +160,4 @@ export function createFormData (data) {
266160
formData.append('global_field', uploadStream)
267161
return formData
268162
}
269-
}
163+
}

lib/stack/index.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,25 +164,14 @@ export function Stack (http, data) {
164164
* .then((globalField) => console.log(globalField))
165165
*
166166
*/
167-
this.globalField = (globalFieldUidOrOptions = null, options = {}) => {
168-
const data = {
169-
stackHeaders: this.stackHeaders
167+
this.globalField = (globalFieldUid = null, api_version = '3.0') => {
168+
const data = {
169+
stackHeaders: this.stackHeaders,
170+
api_version: api_version
170171
}
171-
if (typeof globalFieldUidOrOptions === 'object' && globalFieldUidOrOptions !== null) {
172-
options = globalFieldUidOrOptions
173-
} else if (globalFieldUidOrOptions) {
174-
data.global_field = { uid: globalFieldUidOrOptions }
172+
if (globalFieldUid) {
173+
data.global_field = { uid: globalFieldUid }
175174
}
176-
177-
// Safely handle `options` and check for `api_version`
178-
options = options || {} // Ensure `options` is always an object
179-
if (options && typeof options === 'object' && options.api_version) {
180-
data.api_version = options.api_version
181-
if (options.api_version === '3.2') {
182-
data.nested_global_fields = true
183-
}
184-
}
185-
186175
return new GlobalField(http, data)
187176
}
188177

test/sanity-check/api/globalfield-test.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import path from 'path'
22
import { expect } from 'chai'
3+
import { cloneDeep } from 'lodash'
34
import { describe, it, setup } from 'mocha'
45
import { jsonReader } from '../utility/fileOperations/readwrite'
5-
import { createGlobalField } from '../mock/globalfield'
6+
import { createGlobalField, createNestedGlobalField, createNestedGlobalFieldForReference } from '../mock/globalfield'
67
import { contentstackClient } from '../utility/ContentstackClient.js'
78
import dotenv from 'dotenv'
89

@@ -59,25 +60,16 @@ describe('Global Field api Test', () => {
5960
.catch(done)
6061
})
6162

62-
it('should update global Field', (done) => {
63-
makeGlobalField(createGlobalField.global_field.uid)
64-
.update(createGlobalField)
63+
it('should update global Field', done => {
64+
const globalField = makeGlobalField(createGlobalField.global_field.uid)
65+
Object.assign(globalField, cloneDeep(createGlobalField.global_field))
66+
globalField.update()
6567
.then((updateGlobal) => {
66-
expect(updateGlobal.uid).to.be.equal(
67-
createGlobalField.global_field.uid
68-
)
69-
expect(updateGlobal.title).to.be.equal(
70-
createGlobalField.global_field.title
71-
)
72-
expect(updateGlobal.schema[0].uid).to.be.equal(
73-
createGlobalField.global_field.schema[0].uid
74-
)
75-
expect(updateGlobal.schema[0].data_type).to.be.equal(
76-
createGlobalField.global_field.schema[0].data_type
77-
)
78-
expect(updateGlobal.schema[0].display_name).to.be.equal(
79-
createGlobalField.global_field.schema[0].display_name
80-
)
68+
expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid)
69+
expect(updateGlobal.title).to.be.equal(createGlobalField.global_field.title)
70+
expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid)
71+
expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type)
72+
expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name)
8173
done()
8274
})
8375
.catch(done)
@@ -142,7 +134,7 @@ describe('Global Field api Test', () => {
142134
// it('should create nested global field for reference', done => {
143135
// makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalFieldForReference)
144136
// .then(globalField => {
145-
// expect(globalField.global_field.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
137+
// expect(globalField.uid).to.be.equal(createNestedGlobalFieldForReference.global_field.uid);
146138
// done();
147139
// })
148140
// .catch(err => {
@@ -154,7 +146,7 @@ describe('Global Field api Test', () => {
154146
// it('should create nested global field', done => {
155147
// makeGlobalField({ api_version: '3.2' }).create(createNestedGlobalField)
156148
// .then(globalField => {
157-
// expect(globalField.global_field.uid).to.be.equal(createNestedGlobalField.global_field.uid);
149+
// expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
158150
// done();
159151
// })
160152
// .catch(err => {
@@ -166,7 +158,7 @@ describe('Global Field api Test', () => {
166158
// it('should fetch nested global field', done => {
167159
// makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
168160
// .then(globalField => {
169-
// expect(globalField.global_field.uid).to.be.equal(createNestedGlobalField.global_field.uid);
161+
// expect(globalField.uid).to.be.equal(createNestedGlobalField.global_field.uid);
170162
// done();
171163
// })
172164
// .catch(err => {
@@ -175,16 +167,39 @@ describe('Global Field api Test', () => {
175167
// });
176168
// });
177169

178-
// it('should update nested global fields without fetch', done => {
179-
// makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' })
180-
// .update(createNestedGlobalField)
170+
// it('should fetch and update nested global Field', done => {
171+
// makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' }).fetch()
181172
// .then((globalField) => {
182-
// expect(globalField.global_field.schema.length).to.be.equal(2)
173+
// globalField.title = 'Update title'
174+
// return globalField.update()
175+
// })
176+
// .then((updateGlobal) => {
177+
// expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid)
178+
// expect(updateGlobal.title).to.be.equal('Update title')
179+
// expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid)
180+
// expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type)
181+
// expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name)
183182
// done()
184183
// })
185184
// .catch(done)
186185
// })
187186

187+
// it('should update nested global Field', done => {
188+
// const globalField = makeGlobalField(createGlobalField.global_field.uid, { api_version: '3.2' })
189+
// Object.assign(globalField, cloneDeep(createGlobalField.global_field))
190+
// globalField.update()
191+
// .then((updateGlobal) => {
192+
// expect(updateGlobal.uid).to.be.equal(createGlobalField.global_field.uid)
193+
// expect(updateGlobal.title).to.be.equal(createGlobalField.global_field.title)
194+
// expect(updateGlobal.schema[0].uid).to.be.equal(createGlobalField.global_field.schema[0].uid)
195+
// expect(updateGlobal.schema[0].data_type).to.be.equal(createGlobalField.global_field.schema[0].data_type)
196+
// expect(updateGlobal.schema[0].display_name).to.be.equal(createGlobalField.global_field.schema[0].display_name)
197+
// done()
198+
// })
199+
// .catch(done)
200+
// })
201+
202+
188203
// it("should delete nested global field", (done) => {
189204
// makeGlobalField(createNestedGlobalField.global_field.uid, { api_version: '3.2' })
190205
// .delete()

0 commit comments

Comments
 (0)