From 693bf287b7be154da992f1eeb7baa3821826b6d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Kwiecie=C5=84?= Date: Tue, 9 Jul 2024 16:08:59 +0200 Subject: [PATCH 1/9] Add update_partial_overrides method to the API --- lib/api.js | 5 +++++ lib/v2/api.js | 5 ++++- package.json | 2 +- test/integration/api/admin/api_spec.js | 20 ++++++++++++++++++++ types/index.d.ts | 17 +++++++++++++++++ 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/lib/api.js b/lib/api.js index a5032544..2b0f1319 100644 --- a/lib/api.js +++ b/lib/api.js @@ -692,3 +692,8 @@ exports.update_metadata_rule = function update_metadata_rule(field_external_id, exports.delete_metadata_rule = function delete_metadata_rule(field_external_id, callback, options = {}) { return call_api('delete', ['metadata_rules', field_external_id], {}, callback, options); }; + +exports.update_partial_overrides = function update(asset_id, callback, params) { + const uri = ["resources", asset_id, "partial_overrides"]; + return call_api("put", uri, params, callback, {content_type: 'json'}); +}; diff --git a/lib/v2/api.js b/lib/v2/api.js index 338d826e..9b9eab24 100644 --- a/lib/v2/api.js +++ b/lib/v2/api.js @@ -1,3 +1,5 @@ + + const api = require('../api'); const v1_adapters = require('../utils').v1_adapters; @@ -74,5 +76,6 @@ v1_adapters(exports, api, { add_related_assets: 2, add_related_assets_by_asset_id: 2, delete_related_assets: 2, - delete_related_assets_by_asset_id: 2 + delete_related_assets_by_asset_id: 2, + update_partial_overrides: 1 }); diff --git a/package.json b/package.json index b87ccd6e..409a97c8 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "types": "types", "scripts": { "test": "tools/scripts/test.sh", - "test:unit": "tools/scripts/test.es6.unit.sh", + "test:unit": "CLOUDINARY_URL=cloudinary://xxxx tools/scripts/test.es6.unit.sh", "test-with-temp-cloud": "tools/scripts/tests-with-temp-cloud.sh", "dtslint": "tools/scripts/ditslint.sh", "lint": "tools/scripts/lint.sh", diff --git a/test/integration/api/admin/api_spec.js b/test/integration/api/admin/api_spec.js index 227026c1..a0e88d7f 100644 --- a/test/integration/api/admin/api_spec.js +++ b/test/integration/api/admin/api_spec.js @@ -1599,4 +1599,24 @@ describe("api", function () { } }); }); + describe('update_partial_overrides', () => { + it("should call the PUT /resources/:asset_id/partial_overrides endpoint", async () => { + this.timeout(TIMEOUT.LONG); + await retry(async function () { + return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { + cloudinary.v2.api.update_partial_overrides('ASSET_ID_MOCK', { + transformation_prefix: 'tx_prefix', + asset_override_uri: 'snapshot_url', + overrides: [{ action: 'gen_fill', params: { seed: 'seed', prompt: 'prompt', ignore_foreground: true } }] + }); + sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('transformation_prefix', 'tx_prefix'))); + sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('asset_override_uri', 'snapshot_url'))); + return sinon.assert.calledWith(requestSpy, sinon.match({ + pathname: sinon.match("resources/ASSET_ID_MOCK/partial_overrides"), + method: sinon.match("PUT") + })); + }); + }); + }); + }) }); diff --git a/types/index.d.ts b/types/index.d.ts index a167ff47..eb021bfd 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -500,6 +500,19 @@ declare module 'cloudinary' { [futureKey: string]: any; } + export interface UpdatePartialOverridesOptions { + transformation_prefix: string; + asset_override_uri: string; + overrides: Array<{ + action: 'gen_fill'; + params: { + seed: string; + prompt: string; + ignore_foreground: boolean; + } + }>; + } + export interface UploadApiOptions { access_mode?: AccessMode; allowed_formats?: Array | Array; @@ -1215,6 +1228,10 @@ declare module 'cloudinary' { function restore_metadata_field_datasource(field_external_id: string, entries_external_id: string[], callback?: ResponseCallback): Promise; + function update_partial_overrides(public_id: string, options: UpdatePartialOverridesOptions, callback?: ResponseCallback): Promise; + + function update_partial_overrides(public_id: string, callback?: ResponseCallback): Promise; + /****************************** Structured Metadata Rules API V2 Methods *************************************/ function add_metadata_rule(rule: MetadataRule, options?: AdminApiOptions, callback?: ResponseCallback): Promise; From bc9f99735c9457b72a12f100f5fdf9089450ed94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Kwiecie=C5=84?= Date: Tue, 9 Jul 2024 16:48:02 +0200 Subject: [PATCH 2/9] Revert temp package.json change --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 409a97c8..b87ccd6e 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "types": "types", "scripts": { "test": "tools/scripts/test.sh", - "test:unit": "CLOUDINARY_URL=cloudinary://xxxx tools/scripts/test.es6.unit.sh", + "test:unit": "tools/scripts/test.es6.unit.sh", "test-with-temp-cloud": "tools/scripts/tests-with-temp-cloud.sh", "dtslint": "tools/scripts/ditslint.sh", "lint": "tools/scripts/lint.sh", From 1dd1f79899c4397a5eeeab299847216a463b762f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Kwiecie=C5=84?= Date: Wed, 10 Jul 2024 12:05:47 +0200 Subject: [PATCH 3/9] Update method name and test --- lib/api.js | 2 +- test/integration/api/admin/api_spec.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/api.js b/lib/api.js index 2b0f1319..befa332d 100644 --- a/lib/api.js +++ b/lib/api.js @@ -693,7 +693,7 @@ exports.delete_metadata_rule = function delete_metadata_rule(field_external_id, return call_api('delete', ['metadata_rules', field_external_id], {}, callback, options); }; -exports.update_partial_overrides = function update(asset_id, callback, params) { +exports.update_partial_overrides = function update_partial_overrides(asset_id, callback, params = {}) { const uri = ["resources", asset_id, "partial_overrides"]; return call_api("put", uri, params, callback, {content_type: 'json'}); }; diff --git a/test/integration/api/admin/api_spec.js b/test/integration/api/admin/api_spec.js index a0e88d7f..71c368c0 100644 --- a/test/integration/api/admin/api_spec.js +++ b/test/integration/api/admin/api_spec.js @@ -1609,8 +1609,8 @@ describe("api", function () { asset_override_uri: 'snapshot_url', overrides: [{ action: 'gen_fill', params: { seed: 'seed', prompt: 'prompt', ignore_foreground: true } }] }); - sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('transformation_prefix', 'tx_prefix'))); - sinon.assert.calledWith(writeSpy, sinon.match(helper.apiParamMatcher('asset_override_uri', 'snapshot_url'))); + sinon.assert.calledWith(writeSpy, sinon.match(helper.apiJsonParamMatcher('transformation_prefix', 'tx_prefix'))); + sinon.assert.calledWith(writeSpy, sinon.match(helper.apiJsonParamMatcher('asset_override_uri', 'snapshot_url'))); return sinon.assert.calledWith(requestSpy, sinon.match({ pathname: sinon.match("resources/ASSET_ID_MOCK/partial_overrides"), method: sinon.match("PUT") From 4bf905ab6f21e734a9b1818b96aa7ebff1e56136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Kwiecie=C5=84?= Date: Wed, 10 Jul 2024 14:28:29 +0200 Subject: [PATCH 4/9] Lock jsdoc version to fix deps lint error --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b87ccd6e..2ccc0e27 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "eslint-plugin-import": "^2.20.2", "expect.js": "0.3.x", "glob": "^7.1.6", - "jsdoc": "^3.5.5", + "jsdoc": "3.5.5", "jsdom": "^9.12.0", "jsdom-global": "2.1.1", "mocha": "^6.2.3", From ffc3e5b604049b52f419e9b2c34df1263a48de53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Kwiecie=C5=84?= Date: Mon, 15 Jul 2024 10:06:51 +0200 Subject: [PATCH 5/9] fix: Rename method to set_partial_override --- lib/api.js | 2 +- lib/v2/api.js | 2 +- test/integration/api/admin/api_spec.js | 4 ++-- types/index.d.ts | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/api.js b/lib/api.js index befa332d..307891e7 100644 --- a/lib/api.js +++ b/lib/api.js @@ -693,7 +693,7 @@ exports.delete_metadata_rule = function delete_metadata_rule(field_external_id, return call_api('delete', ['metadata_rules', field_external_id], {}, callback, options); }; -exports.update_partial_overrides = function update_partial_overrides(asset_id, callback, params = {}) { +exports.set_partial_override = function set_partial_override(asset_id, callback, params = {}) { const uri = ["resources", asset_id, "partial_overrides"]; return call_api("put", uri, params, callback, {content_type: 'json'}); }; diff --git a/lib/v2/api.js b/lib/v2/api.js index 9b9eab24..1ba8e574 100644 --- a/lib/v2/api.js +++ b/lib/v2/api.js @@ -77,5 +77,5 @@ v1_adapters(exports, api, { add_related_assets_by_asset_id: 2, delete_related_assets: 2, delete_related_assets_by_asset_id: 2, - update_partial_overrides: 1 + set_partial_override: 1 }); diff --git a/test/integration/api/admin/api_spec.js b/test/integration/api/admin/api_spec.js index 71c368c0..3ae5104f 100644 --- a/test/integration/api/admin/api_spec.js +++ b/test/integration/api/admin/api_spec.js @@ -1599,12 +1599,12 @@ describe("api", function () { } }); }); - describe('update_partial_overrides', () => { + describe('set_partial_override', () => { it("should call the PUT /resources/:asset_id/partial_overrides endpoint", async () => { this.timeout(TIMEOUT.LONG); await retry(async function () { return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => { - cloudinary.v2.api.update_partial_overrides('ASSET_ID_MOCK', { + cloudinary.v2.api.set_partial_override('ASSET_ID_MOCK', { transformation_prefix: 'tx_prefix', asset_override_uri: 'snapshot_url', overrides: [{ action: 'gen_fill', params: { seed: 'seed', prompt: 'prompt', ignore_foreground: true } }] diff --git a/types/index.d.ts b/types/index.d.ts index eb021bfd..eadc08e5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1228,9 +1228,9 @@ declare module 'cloudinary' { function restore_metadata_field_datasource(field_external_id: string, entries_external_id: string[], callback?: ResponseCallback): Promise; - function update_partial_overrides(public_id: string, options: UpdatePartialOverridesOptions, callback?: ResponseCallback): Promise; + function set_partial_override(public_id: string, options: UpdatePartialOverridesOptions, callback?: ResponseCallback): Promise; - function update_partial_overrides(public_id: string, callback?: ResponseCallback): Promise; + function set_partial_override(public_id: string, callback?: ResponseCallback): Promise; /****************************** Structured Metadata Rules API V2 Methods *************************************/ function add_metadata_rule(rule: MetadataRule, options?: AdminApiOptions, callback?: ResponseCallback): Promise; From c2d718b3bce40da3a1d4bc56b4d1219bbcf14fab Mon Sep 17 00:00:00 2001 From: Maciej Komorowski Date: Mon, 22 Jul 2024 12:08:37 +0200 Subject: [PATCH 6/9] fix: add new API status codes --- lib/api_client/execute_request.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/api_client/execute_request.js b/lib/api_client/execute_request.js index 9c0418d9..93f5bbc0 100644 --- a/lib/api_client/execute_request.js +++ b/lib/api_client/execute_request.js @@ -74,7 +74,7 @@ function execute_request(method, params, auth, api_url, callback, options = {}) if ("Authorization" in sanitizedOptions.headers) { delete sanitizedOptions.headers.Authorization; } } - if (includes([200, 400, 401, 403, 404, 409, 420, 500], res.statusCode)) { + if (includes([200, 201, 204, 400, 401, 403, 404, 409, 420, 500], res.statusCode)) { let buffer = ""; let error = false; res.on("data", function (d) { From 7e29d28963b6daa307e1027aef90c61c657bfdec Mon Sep 17 00:00:00 2001 From: Maciej Komorowski Date: Thu, 1 Aug 2024 12:14:14 +0200 Subject: [PATCH 7/9] feat: add invalidate option to delete_derived_resources method --- lib/api.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/api.js b/lib/api.js index 307891e7..3707150d 100644 --- a/lib/api.js +++ b/lib/api.js @@ -213,9 +213,10 @@ exports.delete_related_assets_by_asset_id = (assetId, assetsToUnrelate, callback exports.delete_derived_resources = function delete_derived_resources(derived_resource_ids, callback, options = {}) { let uri; uri = ["derived_resources"]; - return call_api("delete", uri, { - "derived_resource_ids[]": derived_resource_ids - }, callback, options); + return call_api("delete", uri, extend({ + "derived_resource_ids[]": derived_resource_ids, + }, pickOnlyExistingValues(options, "invalidate")) + , callback, options); }; exports.delete_derived_by_transformation = function delete_derived_by_transformation( From 5dba0f6ec468577c14b92f582062b6fc79523fd4 Mon Sep 17 00:00:00 2001 From: Maciej Komorowski Date: Wed, 11 Sep 2024 16:40:33 +0200 Subject: [PATCH 8/9] fix: make override params optional --- types/index.d.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index eadc08e5..de70d885 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -506,9 +506,9 @@ declare module 'cloudinary' { overrides: Array<{ action: 'gen_fill'; params: { - seed: string; - prompt: string; - ignore_foreground: boolean; + seed?: string; + prompt?: string; + ignore_foreground?: boolean; } }>; } From 37e61eff61213e9a5202c04942d3f650e3a47bab Mon Sep 17 00:00:00 2001 From: Maciej Komorowski Date: Wed, 23 Oct 2024 09:59:43 +0200 Subject: [PATCH 9/9] fix: open types for UpdatePartialOverridesOptions --- types/index.d.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/types/index.d.ts b/types/index.d.ts index de70d885..02d5a57e 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -504,12 +504,8 @@ declare module 'cloudinary' { transformation_prefix: string; asset_override_uri: string; overrides: Array<{ - action: 'gen_fill'; - params: { - seed?: string; - prompt?: string; - ignore_foreground?: boolean; - } + action: string; + params: Record }>; }