Skip to content

Commit de939e8

Browse files
authored
Merge pull request #3 from imagekit-developer/promisification
Promisification
2 parents 808eb82 + 52955b8 commit de939e8

File tree

5 files changed

+95
-20
lines changed

5 files changed

+95
-20
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: [

.github/workflows/npmpublish.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [8.x, 10.x, 12.x]
16+
17+
steps:
18+
- uses: actions/checkout@v1
19+
- name: Use Node.js ${{ matrix.node-version }}
20+
uses: actions/setup-node@v1
21+
with:
22+
node-version: ${{ matrix.node-version }}
23+
- name: npm install, build, and test
24+
run: |
25+
npm i -g yarn
26+
yarn install
27+
yarn test
28+
env:
29+
CI: true
30+
31+
publish:
32+
needs: build
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v1
36+
- uses: actions/setup-node@v1
37+
with:
38+
node-version: 12
39+
registry-url: https://registry.npmjs.org/
40+
- name: yarn publish
41+
run: |
42+
npm i -g yarn
43+
yarn config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
44+
yarn publish
45+
env:
46+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
47+
CI: true

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+
return function(...args) {
37+
const self = this;
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ module.exports.getMetadata = function(fileId, defaultOptions, callback) {
4040
respond(true, errorMessages.FILE_ID_MISSING, callback);
4141
return;
4242
}
43-
4443
var requestOptions = {
4544
url : "https://api.imagekit.io/v1/files/" + fileId + "/metadata",
4645
method : "GET",
@@ -144,7 +143,8 @@ module.exports.listFiles = function(listOptions, defaultOptions, callback) {
144143
var requestOptions = {
145144
url : "https://api.imagekit.io/v1/files/",
146145
method : "GET",
147-
json : listOptions
146+
qs : listOptions,
147+
json : true
148148
};
149149

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "imagekit",
3-
"version": "2.0.2",
3+
"version": "3.0.0",
44
"description": "Offical NodeJS SDK for ImageKit.io integration",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)