Skip to content

Commit 4d5175d

Browse files
authored
Merge pull request #78 from contentstack/fix/workflow-and-CT-GF-overwrite-fix
Fix/workflow and ct gf overwrite fix
2 parents e6580d9 + fb4052a commit 4d5175d

File tree

10 files changed

+94
-23
lines changed

10 files changed

+94
-23
lines changed

lib/entity.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ export const publishUnpublish = async (http, url, httpBody, headers, locale = nu
5656
export const upload = async ({ http, urlPath, stackHeaders, formData, params, method = 'POST' }) => {
5757
const headers = {
5858
headers: {
59-
...params,
6059
...cloneDeep(stackHeaders)
60+
},
61+
params: {
62+
...cloneDeep(params)
6163
}
6264
} || {}
63-
6465
if (method === 'POST') {
6566
return http.post(urlPath, formData, headers)
6667
} else {

lib/stack/contentType/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,18 @@ export function ContentType (http, data = {}) {
192192
* const data = {
193193
* content_type: 'path/to/file.json',
194194
* }
195-
* client.stack({ api_key: 'api_key'}).contentType().import(data)
195+
* client.stack({ api_key: 'api_key'}).contentType().import(data, { overwrite: true })
196196
* .then((contentType) => console.log(contentType))
197197
*
198198
*/
199-
this.import = async function (data) {
199+
this.import = async function (data, params = {}) {
200200
try {
201201
const response = await upload({
202202
http: http,
203203
urlPath: `${this.urlPath}/import`,
204204
stackHeaders: this.stackHeaders,
205-
formData: createFormData(data)
205+
formData: createFormData(data),
206+
params: params
206207
})
207208
if (response.data) {
208209
return new this.constructor(http, parseData(response, this.stackHeaders))

lib/stack/globalField/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,18 @@ export function GlobalField (http, data = {}) {
115115
* const data = {
116116
* global_field: 'path/to/file.json',
117117
* }
118-
* client.stack({ api_key: 'api_key'}).globalField().import(data)
118+
* client.stack({ api_key: 'api_key'}).globalField().import(data, { overwrite: true })
119119
* .then((globalField) => console.log(globalField))
120120
*
121121
*/
122-
this.import = async function (data) {
122+
this.import = async function (data, params = {}) {
123123
try {
124124
const response = await upload({
125125
http: http,
126126
urlPath: `${this.urlPath}/import`,
127127
stackHeaders: this.stackHeaders,
128-
formData: createFormData(data)
128+
formData: createFormData(data),
129+
params: params
129130
})
130131
if (response.data) {
131132
return new this.constructor(http, parseData(response, this.stackHeaders))

lib/stack/workflow/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,11 @@ export function Workflow (http, data = {}) {
296296
}
297297

298298
export function WorkflowCollection (http, data) {
299-
const obj = cloneDeep(data.workflows) || []
299+
let obj = cloneDeep(data.workflows) || []
300+
if (!Array.isArray(obj)) {
301+
obj = [obj]
302+
}
303+
300304
return obj.map((userData) => {
301305
return new Workflow(http, { workflow: userData, stackHeaders: data.stackHeaders })
302306
})

test/unit/auditLog-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ describe('Contentstack AuditLog test', () => {
3232
makeAuditLog()
3333
.fetchAll()
3434
.then((response) => {
35-
console.log(response);
3635
expect(response.items[0].created_at).to.be.equal('created_at_date')
3736
expect(response.items[0].uid).to.be.equal('UID')
3837
done()

test/unit/contentType-test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,31 @@ describe('Contentstack ContentType test', () => {
234234
})
235235
.catch(done)
236236
})
237+
238+
it('ContentType import test with overwrite flag', done => {
239+
var mock = new MockAdapter(Axios)
240+
mock.onPost('/content_types/import').reply(200, {
241+
content_type: {
242+
...contentTypeMock
243+
}
244+
})
245+
const contentTypeUpload = { content_type: path.join(__dirname, '../api/mock/contentType.json') }
246+
const form = createFormData(contentTypeUpload)()
247+
var boundary = form.getBoundary()
248+
249+
expect(boundary).to.be.equal(form.getBoundary())
250+
expect(boundary.length).to.be.equal(50)
251+
makeContentType()
252+
.import(contentTypeUpload, { overwrite: true })
253+
.then((contentType) => {
254+
checkContentType(contentType)
255+
done()
256+
})
257+
.catch((err) => {
258+
console.log('><><><><><><><', err)
259+
done()
260+
})
261+
})
237262
})
238263

239264
function makeContentType (data) {

test/unit/globalField-test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,28 @@ describe('Contentstack GlobalField test', () => {
185185
})
186186
.catch(done)
187187
})
188+
189+
it('Global Field import test with overwrite flag', done => {
190+
var mock = new MockAdapter(Axios)
191+
mock.onPost('/global_fields/import').reply(200, {
192+
global_field: {
193+
...globalFieldMock
194+
}
195+
})
196+
const gfUpload = { global_field: path.join(__dirname, '../api/mock/globalfield.json') }
197+
const form = createFormData(gfUpload)()
198+
var boundary = form.getBoundary()
199+
200+
expect(boundary).to.be.equal(form.getBoundary())
201+
expect(boundary.length).to.be.equal(50)
202+
makeGlobalField()
203+
.import(gfUpload, { overwrite: true })
204+
.then((webhook) => {
205+
checkGlobalField(webhook)
206+
done()
207+
})
208+
.catch(done)
209+
})
188210
})
189211

190212
function makeGlobalField (data) {

test/unit/workflow-test.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ describe('Contentstack Workflow test', () => {
8383

8484
it('Workflow Collection test with blank data', done => {
8585
const workflow = new WorkflowCollection(Axios, {})
86-
expect(workflow.length).to.be.equal(0)
86+
expect(workflow[0]).to.be.equal(undefined)
8787
done()
8888
})
8989

@@ -93,7 +93,15 @@ describe('Contentstack Workflow test', () => {
9393
workflowMock
9494
]
9595
})
96-
expect(workflow.length).to.be.equal(1)
96+
expect(typeof workflow[0]).to.be.equal('object')
97+
checkWorkflow(workflow[0])
98+
done()
99+
})
100+
101+
it('Workflow Collection test with data without array', done => {
102+
const workflow = new WorkflowCollection(Axios, {
103+
workflows: workflowMock
104+
})
97105
checkWorkflow(workflow[0])
98106
done()
99107
})
@@ -130,6 +138,20 @@ describe('Contentstack Workflow test', () => {
130138
.catch(done)
131139
})
132140

141+
it('Workflow Fetch all without Stack Headers test with response in object format instead of array of workflows', done => {
142+
var mock = new MockAdapter(Axios)
143+
mock.onGet('/workflows').reply(200, {
144+
workflows: workflowMock
145+
})
146+
makeWorkflow()
147+
.fetchAll()
148+
.then((workflows) => {
149+
checkWorkflow(workflows.items[0])
150+
done()
151+
})
152+
.catch(done)
153+
})
154+
133155
it('Workflow Fetch all with params test', done => {
134156
var mock = new MockAdapter(Axios)
135157
mock.onGet('/workflows').reply(200, {
@@ -269,7 +291,6 @@ describe('Contentstack Workflow test', () => {
269291
.catch(done)
270292
})
271293

272-
273294
it('Workflow content type get publish rules', done => {
274295
var mock = new MockAdapter(Axios)
275296
mock.onGet('/workflows/content_type/ct_UID').reply(200, {
@@ -288,7 +309,6 @@ describe('Contentstack Workflow test', () => {
288309
.catch(done)
289310
})
290311

291-
292312
it('Workflow content type get publish rules', done => {
293313
var mock = new MockAdapter(Axios)
294314
mock.onGet('/workflows/content_type/ct_UID').reply(200, {
@@ -297,7 +317,7 @@ describe('Contentstack Workflow test', () => {
297317
]
298318
})
299319
makeWorkflow().contentType('ct_UID')
300-
.getPublishRules({ action: "publish", locale: "en-us" })
320+
.getPublishRules({ action: 'publish', locale: 'en-us' })
301321
.then((response) => {
302322
checkPublishRules(response.items[0])
303323
done()
@@ -306,10 +326,9 @@ describe('Contentstack Workflow test', () => {
306326
})
307327
})
308328

309-
310329
function makeWorkflow (data) {
311-
return new Workflow(Axios, data)
312-
}
330+
return new Workflow(Axios, data)
331+
}
313332

314333
function checkWorkflow (workflow) {
315334
checkSystemFields(workflow)
@@ -325,7 +344,6 @@ function checkPublishRules (publishRules) {
325344
checkSystemFields(publishRules)
326345
expect(publishRules.locale).to.be.equal('en-us')
327346
expect(publishRules.action).to.be.equal('publish')
328-
expect(publishRules.environment).to.be.equal("env")
329-
expect(publishRules.workflow_stage).to.be.equal("stage")
347+
expect(publishRules.environment).to.be.equal('env')
348+
expect(publishRules.workflow_stage).to.be.equal('stage')
330349
}
331-

types/stack/contentType/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export interface ContentType extends SystemFields, SystemFunction<ContentType> {
88
}
99

1010
export interface ContentTypes extends Queryable<ContentType, {content_type: ContentTypeData}> {
11-
import(data: {content_type: string}): Promise<ContentType>
11+
import(data: {content_type: string}, params?: any): Promise<ContentType>
1212
generateUid(name: string): string
1313
}
1414

types/stack/globalField/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export interface GlobalField extends SystemFields, SystemFunction<GlobalField> {
77
}
88

99
export interface GlobalFields extends Queryable<GlobalField, {global_field: GlobalFieldData}> {
10-
import(data: {global_field: string}): Promise<GlobalField>
10+
import(data: {global_field: string}, params?: any): Promise<GlobalField>
1111
}
1212

1313
export interface GlobalFieldData extends AnyProperty {

0 commit comments

Comments
 (0)