From 87d3fa855658e8761ac26ed0cd1a1e026af415fa Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Tue, 22 Aug 2023 16:00:24 +0530 Subject: [PATCH 1/4] fix: :bug: handles workflow response in object format --- lib/stack/workflow/index.js | 6 +++++- test/unit/workflow-test.js | 40 +++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/lib/stack/workflow/index.js b/lib/stack/workflow/index.js index 1a8f2821..7eb48a08 100644 --- a/lib/stack/workflow/index.js +++ b/lib/stack/workflow/index.js @@ -296,7 +296,11 @@ export function Workflow (http, data = {}) { } export function WorkflowCollection (http, data) { - const obj = cloneDeep(data.workflows) || [] + let obj = cloneDeep(data.workflows) || [] + if (!Array.isArray(obj)) { + obj = [obj] + } + return obj.map((userData) => { return new Workflow(http, { workflow: userData, stackHeaders: data.stackHeaders }) }) diff --git a/test/unit/workflow-test.js b/test/unit/workflow-test.js index 63911e21..c6157b1c 100644 --- a/test/unit/workflow-test.js +++ b/test/unit/workflow-test.js @@ -83,7 +83,7 @@ describe('Contentstack Workflow test', () => { it('Workflow Collection test with blank data', done => { const workflow = new WorkflowCollection(Axios, {}) - expect(workflow.length).to.be.equal(0) + expect(workflow[0]).to.be.equal(undefined) done() }) @@ -93,7 +93,15 @@ describe('Contentstack Workflow test', () => { workflowMock ] }) - expect(workflow.length).to.be.equal(1) + expect(typeof workflow[0]).to.be.equal('object') + checkWorkflow(workflow[0]) + done() + }) + + it('Workflow Collection test with data without array', done => { + const workflow = new WorkflowCollection(Axios, { + workflows: workflowMock + }) checkWorkflow(workflow[0]) done() }) @@ -130,6 +138,20 @@ describe('Contentstack Workflow test', () => { .catch(done) }) + it('Workflow Fetch all without Stack Headers test with response in object format instead of array of workflows', done => { + var mock = new MockAdapter(Axios) + mock.onGet('/workflows').reply(200, { + workflows: workflowMock + }) + makeWorkflow() + .fetchAll() + .then((workflows) => { + checkWorkflow(workflows.items[0]) + done() + }) + .catch(done) + }) + it('Workflow Fetch all with params test', done => { var mock = new MockAdapter(Axios) mock.onGet('/workflows').reply(200, { @@ -269,7 +291,6 @@ describe('Contentstack Workflow test', () => { .catch(done) }) - it('Workflow content type get publish rules', done => { var mock = new MockAdapter(Axios) mock.onGet('/workflows/content_type/ct_UID').reply(200, { @@ -288,7 +309,6 @@ describe('Contentstack Workflow test', () => { .catch(done) }) - it('Workflow content type get publish rules', done => { var mock = new MockAdapter(Axios) mock.onGet('/workflows/content_type/ct_UID').reply(200, { @@ -297,7 +317,7 @@ describe('Contentstack Workflow test', () => { ] }) makeWorkflow().contentType('ct_UID') - .getPublishRules({ action: "publish", locale: "en-us" }) + .getPublishRules({ action: 'publish', locale: 'en-us' }) .then((response) => { checkPublishRules(response.items[0]) done() @@ -306,10 +326,9 @@ describe('Contentstack Workflow test', () => { }) }) - function makeWorkflow (data) { - return new Workflow(Axios, data) - } + return new Workflow(Axios, data) +} function checkWorkflow (workflow) { checkSystemFields(workflow) @@ -325,7 +344,6 @@ function checkPublishRules (publishRules) { checkSystemFields(publishRules) expect(publishRules.locale).to.be.equal('en-us') expect(publishRules.action).to.be.equal('publish') - expect(publishRules.environment).to.be.equal("env") - expect(publishRules.workflow_stage).to.be.equal("stage") + expect(publishRules.environment).to.be.equal('env') + expect(publishRules.workflow_stage).to.be.equal('stage') } - \ No newline at end of file From e304f0b2476292644b1e88624ad85aafa9c052e9 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Tue, 22 Aug 2023 16:01:45 +0530 Subject: [PATCH 2/4] feat: :sparkles: allows overwrite flag param in import of contenttype and global field --- lib/stack/contentType/index.js | 7 ++++--- lib/stack/globalField/index.js | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/stack/contentType/index.js b/lib/stack/contentType/index.js index 05dcf800..305f9098 100644 --- a/lib/stack/contentType/index.js +++ b/lib/stack/contentType/index.js @@ -192,17 +192,18 @@ export function ContentType (http, data = {}) { * const data = { * content_type: 'path/to/file.json', * } - * client.stack({ api_key: 'api_key'}).contentType().import(data) + * client.stack({ api_key: 'api_key'}).contentType().import(data, { overwrite: true }) * .then((contentType) => console.log(contentType)) * */ - this.import = async function (data) { + this.import = async function (data, params = {}) { try { const response = await upload({ http: http, urlPath: `${this.urlPath}/import`, stackHeaders: this.stackHeaders, - formData: createFormData(data) + formData: createFormData(data), + params: params }) if (response.data) { return new this.constructor(http, parseData(response, this.stackHeaders)) diff --git a/lib/stack/globalField/index.js b/lib/stack/globalField/index.js index a993d95f..ad254a2a 100644 --- a/lib/stack/globalField/index.js +++ b/lib/stack/globalField/index.js @@ -115,17 +115,18 @@ export function GlobalField (http, data = {}) { * const data = { * global_field: 'path/to/file.json', * } - * client.stack({ api_key: 'api_key'}).globalField().import(data) + * client.stack({ api_key: 'api_key'}).globalField().import(data, { overwrite: true }) * .then((globalField) => console.log(globalField)) * */ - this.import = async function (data) { + this.import = async function (data, params = {}) { try { const response = await upload({ http: http, urlPath: `${this.urlPath}/import`, stackHeaders: this.stackHeaders, - formData: createFormData(data) + formData: createFormData(data), + params: params }) if (response.data) { return new this.constructor(http, parseData(response, this.stackHeaders)) From c73a28715465da0a6ec64c3888e3840b9e5d4473 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Tue, 22 Aug 2023 17:03:01 +0530 Subject: [PATCH 3/4] test: :white_check_mark: adds CT and GF import test for overwrite flag --- lib/entity.js | 5 +++-- test/unit/auditLog-test.js | 1 - test/unit/contentType-test.js | 25 +++++++++++++++++++++++++ test/unit/globalField-test.js | 22 ++++++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/entity.js b/lib/entity.js index 4ed15de8..5ca89fc2 100644 --- a/lib/entity.js +++ b/lib/entity.js @@ -56,11 +56,12 @@ export const publishUnpublish = async (http, url, httpBody, headers, locale = nu export const upload = async ({ http, urlPath, stackHeaders, formData, params, method = 'POST' }) => { const headers = { headers: { - ...params, ...cloneDeep(stackHeaders) + }, + params: { + ...cloneDeep(params) } } || {} - if (method === 'POST') { return http.post(urlPath, formData, headers) } else { diff --git a/test/unit/auditLog-test.js b/test/unit/auditLog-test.js index 2028cbc6..25ecc0c8 100644 --- a/test/unit/auditLog-test.js +++ b/test/unit/auditLog-test.js @@ -32,7 +32,6 @@ describe('Contentstack AuditLog test', () => { makeAuditLog() .fetchAll() .then((response) => { - console.log(response); expect(response.items[0].created_at).to.be.equal('created_at_date') expect(response.items[0].uid).to.be.equal('UID') done() diff --git a/test/unit/contentType-test.js b/test/unit/contentType-test.js index 6f7f08ce..1ba531ea 100644 --- a/test/unit/contentType-test.js +++ b/test/unit/contentType-test.js @@ -234,6 +234,31 @@ describe('Contentstack ContentType test', () => { }) .catch(done) }) + + it('ContentType import test with overwrite flag', done => { + var mock = new MockAdapter(Axios) + mock.onPost('/content_types/import').reply(200, { + content_type: { + ...contentTypeMock + } + }) + const contentTypeUpload = { content_type: path.join(__dirname, '../api/mock/contentType.json') } + const form = createFormData(contentTypeUpload)() + var boundary = form.getBoundary() + + expect(boundary).to.be.equal(form.getBoundary()) + expect(boundary.length).to.be.equal(50) + makeContentType() + .import(contentTypeUpload, { overwrite: true }) + .then((contentType) => { + checkContentType(contentType) + done() + }) + .catch((err) => { + console.log('><><><><><><><', err) + done() + }) + }) }) function makeContentType (data) { diff --git a/test/unit/globalField-test.js b/test/unit/globalField-test.js index 6d694c33..0dae7502 100644 --- a/test/unit/globalField-test.js +++ b/test/unit/globalField-test.js @@ -185,6 +185,28 @@ describe('Contentstack GlobalField test', () => { }) .catch(done) }) + + it('Global Field import test with overwrite flag', done => { + var mock = new MockAdapter(Axios) + mock.onPost('/global_fields/import').reply(200, { + global_field: { + ...globalFieldMock + } + }) + const gfUpload = { global_field: path.join(__dirname, '../api/mock/globalfield.json') } + const form = createFormData(gfUpload)() + var boundary = form.getBoundary() + + expect(boundary).to.be.equal(form.getBoundary()) + expect(boundary.length).to.be.equal(50) + makeGlobalField() + .import(gfUpload, { overwrite: true }) + .then((webhook) => { + checkGlobalField(webhook) + done() + }) + .catch(done) + }) }) function makeGlobalField (data) { From 8e7ae31908b40d9372aa5d7e56217262dd1d703e Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Tue, 22 Aug 2023 17:05:21 +0530 Subject: [PATCH 4/4] refactor: :recycle: adds type support for overwrite flag param --- types/stack/contentType/index.d.ts | 2 +- types/stack/globalField/index.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/types/stack/contentType/index.d.ts b/types/stack/contentType/index.d.ts index cd8deebc..761aed39 100644 --- a/types/stack/contentType/index.d.ts +++ b/types/stack/contentType/index.d.ts @@ -8,7 +8,7 @@ export interface ContentType extends SystemFields, SystemFunction { } export interface ContentTypes extends Queryable { - import(data: {content_type: string}): Promise + import(data: {content_type: string}, params?: any): Promise generateUid(name: string): string } diff --git a/types/stack/globalField/index.d.ts b/types/stack/globalField/index.d.ts index c47faeee..70a4ab8b 100644 --- a/types/stack/globalField/index.d.ts +++ b/types/stack/globalField/index.d.ts @@ -7,7 +7,7 @@ export interface GlobalField extends SystemFields, SystemFunction { } export interface GlobalFields extends Queryable { - import(data: {global_field: string}): Promise + import(data: {global_field: string}, params?: any): Promise } export interface GlobalFieldData extends AnyProperty {