Skip to content

Commit 5d0fbd9

Browse files
committed
feat: ability to pass options with the copy api method
1 parent 5d81b29 commit 5d0fbd9

4 files changed

Lines changed: 60 additions & 24 deletions

File tree

src/Api.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,17 +122,22 @@ export class Api {
122122
* @param {String} objID ID of object to copy
123123
* @param {Object} updates Which fields to set on copied object. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode.
124124
* @param {String|String[]} [fields] Which fields to return. See {@link https://developers.attask.com/api-docs/api-explorer/|Workfront API Explorer} for the list of available fields for the given objCode.
125+
* @param {String[]} options A list of options that are attached to the copy request (object specific)
125126
* @return {Promise} A promise which will resolved with results if everything went ok and rejected otherwise
126127
*/
127-
copy(objCode: string, objID: string, updates: object, fields?: TFields) {
128+
copy(objCode: string, objID: string, updates: object, fields?: TFields, options?: string[]) {
128129
const params: {
129130
copySourceID: string,
130-
updates?: object
131+
updates?: string,
132+
options?: string
131133
} = {
132134
copySourceID: objID
133135
}
134136
if (updates) {
135-
params.updates = updates
137+
params.updates = JSON.stringify(updates)
138+
}
139+
if (options) {
140+
params.options = JSON.stringify(options)
136141
}
137142
return this.request(objCode, params, fields, Api.Methods.POST)
138143
}

test/Api.spec.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ import {Api} from '../src/index'
2121
describe('Create new instance for API', function() {
2222
it('should have methods', function() {
2323
const api = new Api({url: 'http://localhost'})
24-
should(api.copy).be.a.Function()
25-
should(api.count).be.a.Function()
26-
should(api.create).be.a.Function()
27-
should(api.edit).be.a.Function()
28-
should(api.execute).be.a.Function()
29-
should(api.get).be.a.Function()
30-
should(api.getApiKey).be.a.Function()
31-
should(api.login).be.a.Function()
32-
should(api.logout).be.a.Function()
33-
should(api.metadata).be.a.Function()
34-
should(api.namedQuery).be.a.Function()
35-
should(api.remove).be.a.Function()
36-
should(api.report).be.a.Function()
37-
should(api.search).be.a.Function()
38-
should(api.uploadFromStream).be.a.Function()
39-
should(api.uploadFileContent).be.a.Function()
40-
should(api.setApiKey).be.a.Function()
41-
should(api.clearApiKey).be.a.Function()
24+
should(api.copy).be.a.Function().and.has.lengthOf(5)
25+
should(api.count).be.a.Function().and.has.lengthOf(2)
26+
should(api.create).be.a.Function().and.has.lengthOf(3)
27+
should(api.edit).be.a.Function().and.has.lengthOf(4)
28+
should(api.execute).be.a.Function().and.has.lengthOf(4)
29+
should(api.get).be.a.Function().and.has.lengthOf(3)
30+
should(api.getApiKey).be.a.Function().and.has.lengthOf(2)
31+
should(api.login).be.a.Function().and.has.lengthOf(2)
32+
should(api.logout).be.a.Function().and.has.lengthOf(0)
33+
should(api.metadata).be.a.Function().and.has.lengthOf(1)
34+
should(api.namedQuery).be.a.Function().and.has.lengthOf(4)
35+
should(api.remove).be.a.Function().and.has.lengthOf(3)
36+
should(api.report).be.a.Function().and.has.lengthOf(2)
37+
should(api.search).be.a.Function().and.has.lengthOf(3)
38+
should(api.uploadFromStream).be.a.Function().and.has.lengthOf(2)
39+
should(api.uploadFileContent).be.a.Function().and.has.lengthOf(2)
40+
should(api.setApiKey).be.a.Function().and.has.lengthOf(1)
41+
should(api.clearApiKey).be.a.Function().and.has.lengthOf(0)
4242
})
4343

4444
it('should set correct API path based on passed configuration (version is passed)', function() {

test/e2e.test.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ describe('E2E Tests', function () {
208208
})
209209
})
210210

211-
it('Creates a new project with name "API Project", then copies it', function () {
211+
it('Creates a new project with name "API Project", then copies it without any option', function () {
212212
const name = 'API Project'
213213
const copyName = 'API Project Copy'
214214
const description = 'This project has been created using API'
@@ -222,6 +222,24 @@ describe('E2E Tests', function () {
222222
})
223223
})
224224
})
225+
it('Copies a project with options', function () {
226+
const name = 'API Project'
227+
const copyName = 'API Project Copy'
228+
const description = 'This project has been created using API'
229+
const objCode = 'PROJ'
230+
const copyOptions = [
231+
'clearApprovers',
232+
'clearProgress'
233+
]
234+
return this.instance.create(objCode, {name, description}).then(putObjToRemoveQueue).then((data) => {
235+
const originalProjectID = data.ID
236+
return this.instance.copy(objCode, originalProjectID, {name: copyName}, null, copyOptions)
237+
.then(putObjToRemoveQueue)
238+
.then((data) => {
239+
should(data).have.property('ID').not.eql(originalProjectID)
240+
})
241+
})
242+
})
225243

226244
it('Deletes all projects with name containing "API Project', function () {
227245
const query = {}

test/integration/copy.spec.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,11 @@ describe('Copy', function() {
5656
it('makes request to objCode with copySourceID in the params with some edits to the object', function() {
5757
return this.api.copy('foo', 'bar', {name: 'Copy of bar'}).then(function() {
5858
const [url, opts] = fetchMock.lastCall('copy')
59-
should(url).endWith('foo?copySourceID=bar')
59+
should(url).endWith('foo')
6060
should(opts.method).equal('POST')
61-
should(opts.body).equal('{"name":"Copy of bar"}')
61+
should(opts.body).containEql('updates=' + encodeURIComponent('{"name":"Copy of bar"}'))
62+
should(opts.body).containEql('copySourceID=bar')
63+
should(opts.body).not.containEql('options')
6264
})
6365
})
6466
it('returns data with a new ID', function() {
@@ -67,5 +69,16 @@ describe('Copy', function() {
6769
should(data.ID).not.containEql('bar')
6870
})
6971
})
72+
it('makes a copy request with copy options', function() {
73+
const copyOptions = ['cpyOpt1', 'cpyOpt2']
74+
return this.api.copy('foo', 'bar', null, null, copyOptions).then(function(data) {
75+
const [url, opts] = fetchMock.lastCall('copy')
76+
should(url).endWith('foo')
77+
should(opts.method).equal('POST')
78+
should(opts.body).not.containEql('updates')
79+
should(opts.body).containEql('copySourceID=bar')
80+
should(opts.body).containEql('options=' + encodeURIComponent(JSON.stringify(copyOptions)))
81+
})
82+
})
7083
})
7184
})

0 commit comments

Comments
 (0)