Skip to content

Commit 005b375

Browse files
author
Amir Tocker
committed
Support format in transformation API
1 parent f003137 commit 005b375

File tree

6 files changed

+190
-127
lines changed

6 files changed

+190
-127
lines changed

lib-es5/api.js

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ var Q = require('q');
1919

2020
var api = module.exports;
2121

22+
var TRANSFORMATIONS_URI = "transformations";
23+
2224
function call_api(method, uri, params, callback, options) {
2325
var handle_response = void 0,
2426
query_params = void 0;
@@ -386,46 +388,43 @@ exports.tags = function tags(callback) {
386388
exports.transformations = function transformations(callback) {
387389
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
388390

389-
return call_api("get", ["transformations"], only(options, "next_cursor", "max_results", "named"), callback, options);
391+
var params = only(options, "next_cursor", "max_results", "named");
392+
return call_api("get", TRANSFORMATIONS_URI, params, callback, options);
390393
};
391394

392395
exports.transformation = function transformation(transformation, callback) {
393396
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
394397

395-
var uri = void 0;
396-
uri = ["transformations", transformationString(transformation)];
397-
return call_api("get", uri, only(options, "next_cursor", "max_results"), callback, options);
398+
var params = only(options, "next_cursor", "max_results");
399+
params.transformation = utils.build_eager(transformation);
400+
return call_api("get", TRANSFORMATIONS_URI, params, callback, options);
398401
};
399402

400403
exports.delete_transformation = function delete_transformation(transformation, callback) {
401404
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
402405

403-
var uri = void 0;
404-
uri = ["transformations", transformationString(transformation)];
405-
return call_api("delete", uri, {}, callback, options);
406+
var params = {};
407+
params.transformation = utils.build_eager(transformation);
408+
return call_api("delete", TRANSFORMATIONS_URI, params, callback, options);
406409
};
407410

408411
exports.update_transformation = function update_transformation(transformation, updates, callback) {
409412
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
410413

411-
var params = void 0,
412-
uri = void 0;
413-
uri = ["transformations", transformationString(transformation)];
414-
params = only(updates, "allowed_for_strict");
414+
var params = only(updates, "allowed_for_strict");
415+
params.transformation = utils.build_eager(transformation);
415416
if (updates.unsafe_update != null) {
416-
params.unsafe_update = transformationString(updates.unsafe_update);
417+
params.unsafe_update = utils.build_eager(updates.unsafe_update);
417418
}
418-
return call_api("put", uri, params, callback, options);
419+
return call_api("put", TRANSFORMATIONS_URI, params, callback, options);
419420
};
420421

421422
exports.create_transformation = function create_transformation(name, definition, callback) {
422423
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
423424

424-
var uri = void 0;
425-
uri = ["transformations", name];
426-
return call_api("post", uri, {
427-
transformation: transformationString(definition)
428-
}, callback, options);
425+
var params = { name };
426+
params.transformation = utils.build_eager(definition);
427+
return call_api("post", TRANSFORMATIONS_URI, params, callback, options);
429428
};
430429

431430
exports.upload_presets = function upload_presets(callback) {

lib-es5/utils/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,9 @@ exports.encode_context = function encode_context(arg) {
425425

426426
exports.build_eager = function build_eager(transformations) {
427427
return utils.build_array(transformations).map(function (transformation) {
428-
return [utils.generate_transformation_string(clone(transformation)), transformation.format].filter(utils.present).join('/');
428+
var transformationString = utils.generate_transformation_string(clone(transformation));
429+
var format = transformation.format;
430+
return format == null ? transformationString : `${transformationString}/${format}`;
429431
}).join('|');
430432
};
431433

@@ -458,6 +460,9 @@ var TRANSFORMATION_PARAMS = ['angle', 'aspect_ratio', 'audio_codec', 'audio_freq
458460
];
459461

460462
exports.generate_transformation_string = function generate_transformation_string(options) {
463+
if (utils.isString(options)) {
464+
return options;
465+
}
461466
if (isArray(options)) {
462467
return options.map(function (t) {
463468
return utils.generate_transformation_string(clone(t));

lib/api.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const Q = require('q');
1313

1414
const api = module.exports;
1515

16+
const TRANSFORMATIONS_URI = "transformations";
17+
1618
function call_api(method, uri, params, callback, options) {
1719
let handle_response, query_params;
1820
ensurePresenceOf({method, uri});
@@ -291,37 +293,35 @@ exports.tags = function tags(callback, options={}) {
291293
};
292294

293295
exports.transformations = function transformations(callback, options={}) {
294-
return call_api("get", ["transformations"], only(options, "next_cursor", "max_results", "named"), callback, options);
296+
const params = only(options, "next_cursor", "max_results", "named");
297+
return call_api("get", TRANSFORMATIONS_URI, params, callback, options);
295298
};
296299

297300
exports.transformation = function transformation(transformation, callback, options={}) {
298-
let uri;
299-
uri = ["transformations", transformationString(transformation)];
300-
return call_api("get", uri, only(options, "next_cursor", "max_results"), callback, options);
301+
const params = only(options, "next_cursor", "max_results");
302+
params.transformation = utils.build_eager(transformation);
303+
return call_api("get", TRANSFORMATIONS_URI, params, callback, options);
301304
};
302305

303306
exports.delete_transformation = function delete_transformation(transformation, callback, options={}) {
304-
let uri;
305-
uri = ["transformations", transformationString(transformation)];
306-
return call_api("delete", uri, {}, callback, options);
307+
const params = {};
308+
params.transformation = utils.build_eager(transformation);
309+
return call_api("delete", TRANSFORMATIONS_URI, params, callback, options);
307310
};
308311

309312
exports.update_transformation = function update_transformation(transformation, updates, callback, options={}) {
310-
let params, uri;
311-
uri = ["transformations", transformationString(transformation)];
312-
params = only(updates, "allowed_for_strict");
313+
const params = only(updates, "allowed_for_strict");
314+
params.transformation = utils.build_eager(transformation);
313315
if (updates.unsafe_update != null) {
314-
params.unsafe_update = transformationString(updates.unsafe_update);
316+
params.unsafe_update = utils.build_eager(updates.unsafe_update);
315317
}
316-
return call_api("put", uri, params, callback, options);
318+
return call_api("put", TRANSFORMATIONS_URI, params, callback, options);
317319
};
318320

319321
exports.create_transformation = function create_transformation(name, definition, callback, options={}) {
320-
let uri;
321-
uri = ["transformations", name];
322-
return call_api("post", uri, {
323-
transformation: transformationString(definition)
324-
}, callback, options);
322+
const params = {name};
323+
params.transformation = utils.build_eager(definition);
324+
return call_api("post", TRANSFORMATIONS_URI, params, callback, options);
325325
};
326326

327327
exports.upload_presets = function upload_presets(callback, options={}) {

lib/utils/index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,13 @@ exports.encode_context = function encode_context(arg) {
409409
};
410410

411411
exports.build_eager = function build_eager(transformations) {
412-
return utils.build_array(transformations).map(
413-
transformation => [
414-
utils.generate_transformation_string(clone(transformation)),
415-
transformation.format
416-
].filter(utils.present).join('/')
417-
).join('|');
412+
return utils.build_array(transformations)
413+
.map(transformation => {
414+
const transformationString = utils.generate_transformation_string(clone(transformation));
415+
const format = transformation.format;
416+
return format == null ? transformationString : `${transformationString}/${format}`;
417+
}
418+
).join('|');
418419
};
419420

420421
/**
@@ -485,6 +486,9 @@ const TRANSFORMATION_PARAMS = [
485486
];
486487

487488
exports.generate_transformation_string = function generate_transformation_string(options) {
489+
if(utils.isString(options)) {
490+
return options;
491+
}
488492
if (isArray(options)) {
489493
return options.map(t=>utils.generate_transformation_string(clone(t))).filter(utils.present).join('/');
490494
}

test/api_spec.js

Lines changed: 79 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ const PUBLIC_ID_3 = PUBLIC_ID + "_3";
2929
const PUBLIC_ID_4 = PUBLIC_ID + "_4";
3030
const PUBLIC_ID_5 = PUBLIC_ID + "_5";
3131
const PUBLIC_ID_6 = PUBLIC_ID + "_6";
32-
const NAMED_TRANSFORMATION = "npm_api_test_transformation" + SUFFIX;
32+
const NAMED_TRANSFORMATION = "npm_api_test_transformation_" + SUFFIX;
33+
const NAMED_TRANSFORMATION2 = "npm_api_test_transformation_2_" + SUFFIX;
3334
const API_TEST_UPLOAD_PRESET1 = "npm_api_test_upload_preset_1_" + SUFFIX;
3435
const API_TEST_UPLOAD_PRESET2 = "npm_api_test_upload_preset_2_" + SUFFIX;
3536
const API_TEST_UPLOAD_PRESET3 = "npm_api_test_upload_preset_3_" + SUFFIX;
@@ -469,7 +470,13 @@ describe("api", function() {
469470
itBehavesLike("a list with a cursor", cloudinary.v2.api.transformations);
470471
transformationName = "api_test_transformation3" + SUFFIX;
471472
after(function() {
472-
return Q.allSettled([cloudinary.v2.api.delete_transformation(transformationName), cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION)]).finally(function() {});
473+
return Q.allSettled(
474+
[
475+
cloudinary.v2.api.delete_transformation(transformationName),
476+
cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION),
477+
cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION2)
478+
]
479+
).finally(function() {});
473480
});
474481
it("should allow listing transformations", function() {
475482
this.timeout(helper.TIMEOUT_MEDIUM);
@@ -509,62 +516,84 @@ describe("api", function() {
509516
expect(transformation.allowed_for_strict).not.to.be.ok();
510517
});
511518
});
512-
it("should allow creating named transformation", function() {
513-
this.timeout(helper.TIMEOUT_MEDIUM);
514-
return cloudinary.v2.api.create_transformation(NAMED_TRANSFORMATION, {
515-
crop: "scale",
516-
width: 102
517-
}).then(()=> cloudinary.v2.api.transformation(NAMED_TRANSFORMATION)
518-
).then(function(transformation) {
519-
expect(transformation).not.to.eql(void 0);
520-
expect(transformation.allowed_for_strict).to.be.ok();
521-
expect(transformation.info).to.eql([
522-
{
523-
crop: "scale",
524-
width: 102
525-
}
526-
]);
527-
expect(transformation.used).not.to.be.ok();
519+
describe("Named Transformations", function(){
520+
it("should allow creating named transformation", function() {
521+
this.timeout(helper.TIMEOUT_MEDIUM);
522+
return cloudinary.v2.api.create_transformation(NAMED_TRANSFORMATION, {
523+
crop: "scale",
524+
width: 102
525+
}).then(()=> cloudinary.v2.api.transformation(NAMED_TRANSFORMATION)
526+
).then(function(transformation) {
527+
expect(transformation).not.to.eql(void 0);
528+
expect(transformation.allowed_for_strict).to.be.ok();
529+
expect(transformation.info).to.eql([
530+
{
531+
crop: "scale",
532+
width: 102
533+
}
534+
]);
535+
expect(transformation.used).not.to.be.ok();
536+
});
528537
});
529-
});
530-
it("should allow listing of named transformations", function() {
531-
return helper.mockPromise(function(xhr, write, request) {
532-
cloudinary.v2.api.transformations({
533-
named: true
538+
it("should allow creating named transformation with an empty format", function() {
539+
this.timeout(helper.TIMEOUT_MEDIUM);
540+
return cloudinary.v2.api.create_transformation(NAMED_TRANSFORMATION2, {
541+
crop: "scale",
542+
width: 102,
543+
format: ''
544+
}).then(()=> cloudinary.v2.api.transformation(NAMED_TRANSFORMATION2)
545+
).then(function(transformation) {
546+
expect(transformation).not.to.eql(void 0);
547+
expect(transformation.allowed_for_strict).to.be.ok();
548+
expect(transformation.info).to.eql([
549+
{
550+
crop: "scale",
551+
width: 102,
552+
extension: 'none'
553+
}
554+
]);
555+
expect(transformation.used).not.to.be.ok();
534556
});
535-
return sinon.assert.calledWith(request, sinon.match({
536-
query: sinon.match('named=true')
537-
}, "named=true"));
538557
});
539-
});
540-
it("should allow unsafe update of named transformation", function() {
541-
this.timeout(helper.TIMEOUT_MEDIUM);
542-
return cloudinary.v2.api.create_transformation(transformationName, {
543-
crop: "scale",
544-
width: 102
545-
}).then(result => cloudinary.v2.api.update_transformation(transformationName, {
546-
unsafe_update: {
558+
it("should allow listing of named transformations", function() {
559+
return helper.mockPromise(function(xhr, write, request) {
560+
cloudinary.v2.api.transformations({
561+
named: true
562+
});
563+
return sinon.assert.calledWith(request, sinon.match({
564+
query: sinon.match('named=true')
565+
}, "named=true"));
566+
});
567+
});
568+
it("should allow unsafe update of named transformation", function() {
569+
this.timeout(helper.TIMEOUT_MEDIUM);
570+
return cloudinary.v2.api.create_transformation(transformationName, {
547571
crop: "scale",
548-
width: 103
549-
}
550-
})).then(result => cloudinary.v2.api.transformation(transformationName)
551-
).then(transformation => {
552-
expect(transformation).not.to.eql(void 0);
553-
expect(transformation.info).to.eql([
554-
{
572+
width: 102
573+
}).then(result => cloudinary.v2.api.update_transformation(transformationName, {
574+
unsafe_update: {
555575
crop: "scale",
556576
width: 103
557577
}
558-
]);
559-
expect(transformation.used).not.to.be.ok();
578+
})).then(result => cloudinary.v2.api.transformation(transformationName)
579+
).then(transformation => {
580+
expect(transformation).not.to.eql(void 0);
581+
expect(transformation.info).to.eql([
582+
{
583+
crop: "scale",
584+
width: 103
585+
}
586+
]);
587+
expect(transformation.used).not.to.be.ok();
588+
});
589+
});
590+
it("should allow deleting named transformation", function() {
591+
this.timeout(helper.TIMEOUT_MEDIUM);
592+
return cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION).then(()=>{
593+
return cloudinary.v2.api.transformation(NAMED_TRANSFORMATION);
594+
}).then(()=> expect().fail()
595+
).catch(({error}) => expect(error.http_code).to.eql(404));
560596
});
561-
});
562-
it("should allow deleting named transformation", function() {
563-
this.timeout(helper.TIMEOUT_MEDIUM);
564-
return cloudinary.v2.api.delete_transformation(NAMED_TRANSFORMATION).then(()=>{
565-
return cloudinary.v2.api.transformation(NAMED_TRANSFORMATION);
566-
}).then(()=> expect().fail()
567-
).catch(({error}) => expect(error.http_code).to.eql(404));
568597
});
569598
it("should allow deleting implicit transformation", function() {
570599
this.timeout(helper.TIMEOUT_MEDIUM);

0 commit comments

Comments
 (0)