Skip to content

Commit eba5fcf

Browse files
author
Jagveer
committed
promified function calls
1 parent 808eb82 commit eba5fcf

File tree

3 files changed

+47
-18
lines changed

3 files changed

+47
-18
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = {
33
browser: true,
44
commonjs: true,
55
es6: true,
6+
es5: true,
67
mocha: true,
78
},
89
extends: [

index.js

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@ var ImageKit = function(opts) {
3232
throw new Error(errorMessages.MANDATORY_INITIALIZATION_MISSING.message);
3333
}
3434

35+
const promisify = function(f) {
36+
const self = this;
37+
return function(...args) {
38+
if (args.length === f.length) {
39+
if (typeof args[args.length-1] !== 'function') {
40+
throw new Error('Callback must be a function.')
41+
}
42+
f.call(self, ...args)
43+
} else {
44+
if (typeof Promise !== 'function') {
45+
throw new Error('Promises should be defined as a global function.')
46+
}
47+
return new Promise((resolve, reject) => {
48+
const callback = function(err, ...results) {
49+
if (err) {
50+
return reject(err);
51+
} else {
52+
resolve(results.length > 1 ? results : results[0])
53+
}
54+
}
55+
args.push(callback);
56+
f.call(self, ...args);
57+
});
58+
}
59+
}
60+
}
61+
3562
if(!transformationUtils.validParameters(this.options)) {
3663
throw new Error(errorMessages.INVALID_TRANSFORMATION_POSITION.message);
3764
}
@@ -46,53 +73,53 @@ var ImageKit = function(opts) {
4673
/*
4774
Upload API
4875
*/
49-
this.upload = function(uploadOptions, callback) {
76+
this.upload = promisify(function(uploadOptions, callback) {
5077
return upload(uploadOptions, this.options, callback);
51-
};
78+
});
5279

5380
/*
5481
Image Management APIs
5582
*/
5683

5784
// List and Search Files API
58-
this.listFiles = function(listOptions, callback) {
85+
this.listFiles = promisify(function(listOptions, callback) {
5986
return manage.listFiles(listOptions, this.options, callback);
60-
};
87+
});
6188

6289
// Get File Details API
63-
this.getFileDetails = function(fileId, callback) {
90+
this.getFileDetails = promisify(function(fileId, callback) {
6491
return manage.getFileDetails(fileId, this.options, callback);
65-
};
92+
});
6693

6794
// Get File Metadata API
68-
this.getFileMetadata = function(fileId, callback) {
95+
this.getFileMetadata = promisify(function(fileId, callback) {
6996
return manage.getFileMetadata(fileId, this.options, callback);
70-
};
97+
});
7198

7299
// Update File Details API
73-
this.updateFileDetails = function(fileId, updateData, callback) {
100+
this.updateFileDetails = promisify(function(fileId, updateData, callback) {
74101
return manage.updateFileDetails(fileId, updateData, this.options, callback);
75-
};
102+
});
76103

77104
// Delete File API
78-
this.deleteFile = function(fileId, callback) {
105+
this.deleteFile = promisify(function(fileId, callback) {
79106
return manage.deleteFile(fileId, this.options, callback);
80-
};
107+
});
81108

82109
// Purge Cache API
83-
this.purgeCache = function(url, callback) {
110+
this.purgeCache = promisify(function(url, callback) {
84111
return manage.purgeCache(url, this.options, callback);
85-
};
112+
});
86113

87114
// Purge Cache Status API
88-
this.getPurgeCacheStatus = function(requestId, callback) {
115+
this.getPurgeCacheStatus = promisify(function(requestId, callback) {
89116
return manage.getPurgeCacheStatus(requestId, this.options, callback);
90-
};
117+
});
91118

92119
// To generate Signature for upload request
93120
this.getAuthenticationParameters = function(token, timestamp) {
94121
return signature.getAuthenticationParameters(token, timestamp, this.options);
95-
}
122+
};
96123

97124
// To calculate distance between two pHash strings
98125
this.pHashDistance = function(firstPHash, secondPHash) {

libs/manage/file.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ module.exports.listFiles = function(listOptions, defaultOptions, callback) {
144144
var requestOptions = {
145145
url : "https://api.imagekit.io/v1/files/",
146146
method : "GET",
147-
json : listOptions
147+
qs : listOptions,
148+
json : true
148149
};
149150

150151
request(requestOptions, defaultOptions, function(err, response, body) {

0 commit comments

Comments
 (0)