Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/api_client/execute_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
const config = require("../config");
const https = /^http:/.test(config().upload_prefix) ? require('http') : require('https');
const querystring = require("querystring");
const Q = require('q');
const url = require('url');
const utils = require("../utils");
const ensureOption = require('../utils/ensureOption').defaults(config());
Expand All @@ -13,7 +12,7 @@ const agent = config.api_proxy ? new https.Agent(config.api_proxy) : null;

function execute_request(method, params, auth, api_url, callback, options = {}) {
method = method.toUpperCase();
const deferred = Q.defer();
const deferred = utils.deferredPromise();

let query_params, handle_response; // declare to user later
let key = auth.key;
Expand Down
3 changes: 1 addition & 2 deletions lib/uploader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const fs = require('fs');
const { extname, basename } = require('path');
const Q = require('q');
const Writable = require("stream").Writable;
const urlLib = require('url');

Expand Down Expand Up @@ -466,7 +465,7 @@ function call_api(action, callback, options, get_params) {

const USE_PROMISES = !options.disable_promises;

let deferred = Q.defer();
const deferred = utils.deferredPromise();
if (options == null) {
options = {};
}
Expand Down
16 changes: 16 additions & 0 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,22 @@ function jsonArrayParam(data, modifier) {
*/
exports.NOP = function () {
};

function deferredPromise() {
let resolve, reject
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
return {
resolve,
reject,
promise
};
}

exports.deferredPromise = deferredPromise;

exports.generate_auth_token = generate_auth_token;
exports.getUserAgent = getUserAgent;
exports.build_upload_params = build_upload_params;
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
},
"main": "cloudinary.js",
"dependencies": {
"lodash": "^4.17.21",
"q": "^1.5.1"
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/expect.js": "^0.3.29",
Expand Down
183 changes: 90 additions & 93 deletions test/integration/api/admin/api_spec.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions test/integration/api/admin/config_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const sinon = require('sinon');

const cloudinary = require('../../../../lib/cloudinary');
const api_http = require("https");
const { NOP } = require('../../../../lib/utils');
const ClientRequest = require('_http_client').ClientRequest;

describe('Admin API - Config', () => {
Expand All @@ -20,8 +21,8 @@ describe('Admin API - Config', () => {
});

describe('config', () => {
it('should send a request to config endpoint', () => {
cloudinary.v2.api.config();
it('should send a request to config endpoint', async () => {
await cloudinary.v2.api.config().catch(NOP);

sinon.assert.calledWith(mocked.request, sinon.match({
pathname: sinon.match('config'),
Expand All @@ -30,8 +31,8 @@ describe('Admin API - Config', () => {
}));
});

it('should send a request to config endpoint with optional parameters', () => {
cloudinary.v2.api.config({ settings: true });
it('should send a request to config endpoint with optional parameters', async () => {
await cloudinary.v2.api.config({ settings: true }).catch(NOP);

sinon.assert.calledWith(mocked.request, sinon.match({
pathname: sinon.match('config'),
Expand Down
5 changes: 3 additions & 2 deletions test/integration/api/admin/folders_api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const cloudinary = require('../../../../lib/cloudinary');
const createTestConfig = require('../../../testUtils/createTestConfig');
const helper = require('../../../spechelper');
const api_http = require("https");
const { NOP } = require('../../../../lib/utils');
const ClientRequest = require('_http_client').ClientRequest;

describe('Admin API - Folders', () => {
Expand All @@ -23,8 +24,8 @@ describe('Admin API - Folders', () => {
});

describe('rename_folder', () => {
it('should send a request to update folder endpoint with correct parameters', () => {
cloudinary.v2.api.rename_folder('old/path', 'new/path');
it('should send a request to update folder endpoint with correct parameters', async () => {
await cloudinary.v2.api.rename_folder('old/path', 'new/path').catch(NOP);

sinon.assert.calledWith(mocked.request, sinon.match({
pathname: sinon.match('old%2Fpath'),
Expand Down
33 changes: 17 additions & 16 deletions test/integration/api/admin/related_assets_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
deepStrictEqual
} = require('assert');
const {TEST_CLOUD_NAME} = require('../../../testUtils/testConstants');
const { NOP } = require('../../../../lib/utils');

describe('Asset relations API', () => {
const testPublicId = 'test-public-id';
Expand All @@ -15,8 +16,8 @@ describe('Asset relations API', () => {
describe('when creating new relation', () => {
describe('using public id', () => {
it('should allow passing a single public id to create a relation', () => {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.add_related_assets(testPublicId, singleRelatedPublicId);
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.add_related_assets(testPublicId, singleRelatedPublicId).catch(NOP);

const [calledWithUrl] = requestSpy.firstCall.args;
strictEqual(calledWithUrl.method, 'POST');
Expand All @@ -27,8 +28,8 @@ describe('Asset relations API', () => {
});

it('should allow passing multiple public ids to create a relation', () => {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.add_related_assets(testPublicId, multipleRelatedPublicId);
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.add_related_assets(testPublicId, multipleRelatedPublicId).catch(NOP);

const [calledWithUrl] = requestSpy.firstCall.args;
strictEqual(calledWithUrl.method, 'POST');
Expand All @@ -41,8 +42,8 @@ describe('Asset relations API', () => {

describe('using asset id', () => {
it('should allow passing a single public id to create a relation', () => {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.add_related_assets_by_asset_id(testAssetId, singleRelatedPublicId);
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.add_related_assets_by_asset_id(testAssetId, singleRelatedPublicId).catch(NOP);

const [calledWithUrl] = requestSpy.firstCall.args;
strictEqual(calledWithUrl.method, 'POST');
Expand All @@ -53,8 +54,8 @@ describe('Asset relations API', () => {
});

it('should allow passing multiple public ids to create a relation', () => {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.add_related_assets_by_asset_id(testAssetId, multipleRelatedPublicId);
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.add_related_assets_by_asset_id(testAssetId, multipleRelatedPublicId).catch(NOP);

const [calledWithUrl] = requestSpy.firstCall.args;
strictEqual(calledWithUrl.method, 'POST');
Expand All @@ -69,8 +70,8 @@ describe('Asset relations API', () => {
describe('when deleting existing relation', () => {
describe('using public id', () => {
it('should allow passing a single public id to delete a relation', () => {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.delete_related_assets(testPublicId, singleRelatedPublicId);
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.delete_related_assets(testPublicId, singleRelatedPublicId).catch(NOP);

const [calledWithUrl] = requestSpy.firstCall.args;
strictEqual(calledWithUrl.method, 'DELETE');
Expand All @@ -81,8 +82,8 @@ describe('Asset relations API', () => {
});

it('should allow passing multiple public ids to delete a relation', () => {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.delete_related_assets(testPublicId, multipleRelatedPublicId);
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.delete_related_assets(testPublicId, multipleRelatedPublicId).catch(NOP);

const [calledWithUrl] = requestSpy.firstCall.args;
strictEqual(calledWithUrl.method, 'DELETE');
Expand All @@ -95,8 +96,8 @@ describe('Asset relations API', () => {

describe('and using asset id', () => {
it('should allow passing a single public id to delete a relation', () => {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.delete_related_assets_by_asset_id(testAssetId, singleRelatedPublicId);
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.delete_related_assets_by_asset_id(testAssetId, singleRelatedPublicId).catch(NOP);

const [calledWithUrl] = requestSpy.firstCall.args;
strictEqual(calledWithUrl.method, 'DELETE');
Expand All @@ -107,8 +108,8 @@ describe('Asset relations API', () => {
});

it('should allow passing multiple public ids to delete a relation', () => {
return helper.provideMockObjects((mockXHR, writeSpy, requestSpy) => {
cloudinary.v2.api.delete_related_assets_by_asset_id(testAssetId, multipleRelatedPublicId);
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.delete_related_assets_by_asset_id(testAssetId, multipleRelatedPublicId).catch(NOP);

const [calledWithUrl] = requestSpy.firstCall.args;
strictEqual(calledWithUrl.method, 'DELETE');
Expand Down
Loading