Skip to content

Commit 64bdc9d

Browse files
fix: defaults for related asset methods and proper content_type (#705)
1 parent 25e04e6 commit 64bdc9d

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

lib/api.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -204,21 +204,29 @@ const deleteRelationParams = (publicIds = []) => {
204204

205205
exports.add_related_assets = (publicId, assetsToRelate, callback, options = {}) => {
206206
const params = createRelationParams(assetsToRelate);
207-
return call_api('post', ['resources', 'related_assets', options.resource_type, options.type, publicId], params, callback, options);
207+
const resourceType = options.resource_type || 'image';
208+
const type = options.type || 'upload';
209+
options.content_type = 'json';
210+
return call_api('post', ['resources', 'related_assets', resourceType, type, publicId], params, callback, options);
208211
};
209212

210213
exports.add_related_assets_by_asset_id = (assetId, assetsToRelate, callback, options = {}) => {
211214
const params = createRelationParams(assetsToRelate);
215+
options.content_type = 'json';
212216
return call_api('post', ['resources', 'related_assets', assetId], params, callback, options);
213217
};
214218

215219
exports.delete_related_assets = (publicId, assetsToUnrelate, callback, options = {}) => {
216220
const params = deleteRelationParams(assetsToUnrelate);
217-
return call_api('delete', ['resources', 'related_assets', options.resource_type, options.type, publicId], params, callback, options);
221+
const resourceType = options.resource_type || 'image';
222+
const type = options.type || 'upload';
223+
options.content_type = 'json';
224+
return call_api('delete', ['resources', 'related_assets', resourceType, type, publicId], params, callback, options);
218225
};
219226

220227
exports.delete_related_assets_by_asset_id = (assetId, assetsToUnrelate, callback, options = {}) => {
221228
const params = deleteRelationParams(assetsToUnrelate);
229+
options.content_type = 'json';
222230
return call_api('delete', ['resources', 'related_assets', assetId], params, callback, options);
223231
};
224232

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"eslint-plugin-import": "^2.20.2",
2727
"expect.js": "0.3.x",
2828
"glob": "^7.1.6",
29-
"jsdoc": "^3.6.11",
29+
"jsdoc": "^4.0.4",
3030
"jsdom": "^9.12.0",
3131
"jsdom-global": "2.1.1",
3232
"mocha": "^6.2.3",

test/integration/api/admin/related_assets_spec.js

+12-24
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,25 @@ describe('Asset relations API', () => {
1616
describe('using public id', () => {
1717
it('should allow passing a single public id to create a relation', () => {
1818
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
19-
cloudinary.v2.api.add_related_assets(testPublicId, singleRelatedPublicId, {
20-
resource_type: 'image',
21-
type: 'upload'
22-
});
19+
cloudinary.v2.api.add_related_assets(testPublicId, singleRelatedPublicId);
2320

2421
const [calledWithUrl] = requestSpy.firstCall.args;
2522
strictEqual(calledWithUrl.method, 'POST');
2623
strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/related_assets/image/upload/test-public-id`);
2724
const callApiArguments = writeSpy.firstCall.args;
28-
deepStrictEqual(callApiArguments, ['assets_to_relate=related-public-id']);
25+
deepStrictEqual(callApiArguments, ["{\"assets_to_relate\":[\"related-public-id\"]}"]);
2926
});
3027
});
3128

3229
it('should allow passing multiple public ids to create a relation', () => {
3330
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
34-
cloudinary.v2.api.add_related_assets(testPublicId, multipleRelatedPublicId, {
35-
resource_type: 'image',
36-
type: 'upload'
37-
});
31+
cloudinary.v2.api.add_related_assets(testPublicId, multipleRelatedPublicId);
3832

3933
const [calledWithUrl] = requestSpy.firstCall.args;
4034
strictEqual(calledWithUrl.method, 'POST');
4135
strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/related_assets/image/upload/${testPublicId}`);
4236
const callApiArguments = writeSpy.firstCall.args;
43-
deepStrictEqual(callApiArguments, ['assets_to_relate=related-public-id-1&assets_to_relate=related-public-id-2']);
37+
deepStrictEqual(callApiArguments, ["{\"assets_to_relate\":[\"related-public-id-1\",\"related-public-id-2\"]}"]);
4438
});
4539
});
4640
});
@@ -54,7 +48,7 @@ describe('Asset relations API', () => {
5448
strictEqual(calledWithUrl.method, 'POST');
5549
strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/related_assets/test-asset-id`);
5650
const callApiArguments = writeSpy.firstCall.args;
57-
deepStrictEqual(callApiArguments, ['assets_to_relate=related-public-id']);
51+
deepStrictEqual(callApiArguments, ["{\"assets_to_relate\":[\"related-public-id\"]}"]);
5852
});
5953
});
6054

@@ -66,7 +60,7 @@ describe('Asset relations API', () => {
6660
strictEqual(calledWithUrl.method, 'POST');
6761
strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/related_assets/test-asset-id`);
6862
const callApiArguments = writeSpy.firstCall.args;
69-
deepStrictEqual(callApiArguments, ['assets_to_relate=related-public-id-1&assets_to_relate=related-public-id-2']);
63+
deepStrictEqual(callApiArguments, ["{\"assets_to_relate\":[\"related-public-id-1\",\"related-public-id-2\"]}"]);
7064
});
7165
});
7266
});
@@ -76,31 +70,25 @@ describe('Asset relations API', () => {
7670
describe('using public id', () => {
7771
it('should allow passing a single public id to delete a relation', () => {
7872
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
79-
cloudinary.v2.api.delete_related_assets(testPublicId, singleRelatedPublicId, {
80-
resource_type: 'image',
81-
type: 'upload'
82-
});
73+
cloudinary.v2.api.delete_related_assets(testPublicId, singleRelatedPublicId);
8374

8475
const [calledWithUrl] = requestSpy.firstCall.args;
8576
strictEqual(calledWithUrl.method, 'DELETE');
8677
strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/related_assets/image/upload/test-public-id`);
8778
const callApiArguments = writeSpy.firstCall.args;
88-
deepStrictEqual(callApiArguments, ['assets_to_unrelate=related-public-id']);
79+
deepStrictEqual(callApiArguments, ["{\"assets_to_unrelate\":[\"related-public-id\"]}"]);
8980
});
9081
});
9182

9283
it('should allow passing multiple public ids to delete a relation', () => {
9384
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
94-
cloudinary.v2.api.delete_related_assets(testPublicId, multipleRelatedPublicId, {
95-
resource_type: 'image',
96-
type: 'upload'
97-
});
85+
cloudinary.v2.api.delete_related_assets(testPublicId, multipleRelatedPublicId);
9886

9987
const [calledWithUrl] = requestSpy.firstCall.args;
10088
strictEqual(calledWithUrl.method, 'DELETE');
10189
strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/related_assets/image/upload/test-public-id`);
10290
const callApiArguments = writeSpy.firstCall.args;
103-
deepStrictEqual(callApiArguments, ['assets_to_unrelate=related-public-id-1&assets_to_unrelate=related-public-id-2']);
91+
deepStrictEqual(callApiArguments, ["{\"assets_to_unrelate\":[\"related-public-id-1\",\"related-public-id-2\"]}"]);
10492
});
10593
});
10694
});
@@ -114,7 +102,7 @@ describe('Asset relations API', () => {
114102
strictEqual(calledWithUrl.method, 'DELETE');
115103
strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/related_assets/test-asset-id`);
116104
const callApiArguments = writeSpy.firstCall.args;
117-
deepStrictEqual(callApiArguments, ['assets_to_unrelate=related-public-id']);
105+
deepStrictEqual(callApiArguments, ["{\"assets_to_unrelate\":[\"related-public-id\"]}"]);
118106
});
119107
});
120108

@@ -126,7 +114,7 @@ describe('Asset relations API', () => {
126114
strictEqual(calledWithUrl.method, 'DELETE');
127115
strictEqual(calledWithUrl.path, `/v1_1/${TEST_CLOUD_NAME}/resources/related_assets/test-asset-id`);
128116
const callApiArguments = writeSpy.firstCall.args;
129-
deepStrictEqual(callApiArguments, ['assets_to_unrelate=related-public-id-1&assets_to_unrelate=related-public-id-2']);
117+
deepStrictEqual(callApiArguments, ["{\"assets_to_unrelate\":[\"related-public-id-1\",\"related-public-id-2\"]}"]);
130118
});
131119
});
132120
});

0 commit comments

Comments
 (0)