Skip to content

Commit dbaaa41

Browse files
authored
Merge pull request #22 from imagekit-developer/new_apis
New apis
2 parents 83a4fd6 + 792fcf2 commit dbaaa41

File tree

12 files changed

+2074
-788
lines changed

12 files changed

+2074
-788
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules/*
22
.vscode
33
.nyc_output
44
coverage.lcov
5-
coverage
5+
coverage
6+
.DS_Store

README.md

Lines changed: 213 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ See the complete list of transformations supported in ImageKit [here](https://do
202202
| overlayImageDPR | oidpr |
203203
| overlayImageQuality | oiq |
204204
| overlayImageCropping | oic |
205+
| overlayImageFocus | oifo |
205206
| overlayImageTrim | oit |
206207
| overlayText | ot |
207208
| overlayTextFontSize | ots |
@@ -328,15 +329,13 @@ Accepts the file ID and fetches the metadata as per the [API documentation here]
328329

329330
```js
330331
// Using Callback Function
331-
332332
imagekit.getFileMetadata("file_id", function(error, result) {
333333
if(error) console.log(error);
334334
else console.log(result);
335335
});
336336

337337

338338
// Using Promises
339-
340339
imagekit.getFileMetadata("file_id")
341340
}).then(response => {
342341
console.log(response);
@@ -345,6 +344,25 @@ imagekit.getFileMetadata("file_id")
345344
});
346345
```
347346

347+
You can also pass the remote URL of the image to get metadata.
348+
349+
```js
350+
// Using Callback Function
351+
imagekit.getFileMetadata("https://ik.imagekit.io/your_imagekit_id/sample.jpg", function(error, result) {
352+
if(error) console.log(error);
353+
else console.log(result);
354+
});
355+
356+
357+
// Using Promises
358+
imagekit.getFileMetadata("https://ik.imagekit.io/your_imagekit_id/sample.jpg")
359+
}).then(response => {
360+
console.log(response);
361+
}).catch(error => {
362+
console.log(error);
363+
});
364+
```
365+
348366
**4. Update File Details**
349367

350368
Update parameters associated with the file as per the [API documentation here](https://docs.imagekit.io/api-reference/media-api/update-file-details). The first argument to the `updateFileDetails` method is the file ID, and a second argument is an object with the parameters to be updated.
@@ -388,8 +406,7 @@ imagekit.deleteFile("file_id", function(error, result) {
388406

389407
// Using Promises
390408

391-
imagekit.deleteFile("file_id")
392-
}).then(response => {
409+
imagekit.deleteFile("file_id").then(response => {
393410
console.log(response);
394411
}).catch(error => {
395412
console.log(error);
@@ -411,8 +428,7 @@ imagekit.bulkDeleteFiles(["fileIds"], function(error, result) {
411428

412429
// Using Promises
413430

414-
imagekit.bulkDeleteFiles(["fileIds"])
415-
}).then(response => {
431+
imagekit.bulkDeleteFiles(["fileIds"]).then(response => {
416432
console.log(response);
417433
}).catch(error => {
418434
console.log(error);
@@ -434,8 +450,7 @@ imagekit.purgeCache("full_url", function(error, result) {
434450

435451
// Using Promises
436452

437-
imagekit.purgeCache("full_url")
438-
}).then(response => {
453+
imagekit.purgeCache("full_url").then(response => {
439454
console.log(response);
440455
}).catch(error => {
441456
console.log(error);
@@ -457,8 +472,196 @@ imagekit.getPurgeCacheStatus("cache_request_id", function(error, result) {
457472

458473
// Using Promises
459474

460-
imagekit.getPurgeCacheStatus("cache_request_id")
461-
}).then(response => {
475+
imagekit.getPurgeCacheStatus("cache_request_id").then(response => {
476+
console.log(response);
477+
}).catch(error => {
478+
console.log(error);
479+
});
480+
```
481+
482+
**9. Bulk Add tags**
483+
484+
Add tags to multiple files in a single request as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/add-tags-bulk). The method accepts an array of fileIDs of the files and an array of tags that have to be added to those files.
485+
486+
```js
487+
// Using Callback Function
488+
489+
imagekit.bulkAddTags(["fileIds"], ["tags"], function(error, result) {
490+
if(error) console.log(error);
491+
else console.log(result);
492+
});
493+
494+
// Using Promises
495+
496+
imagekit.bulkAddTags(["fileIds"], ["tags"]).then(response => {
497+
console.log(response);
498+
}).catch(error => {
499+
console.log(error);
500+
});
501+
```
502+
503+
**10. Bulk Remove tags**
504+
505+
Remove tags from multiple files in a single request as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/remove-tags-bulk). The method accepts an array of fileIDs of the files and an array of tags that have to be removed from those files.
506+
507+
```js
508+
// Using Callback Function
509+
510+
imagekit.bulkRemoveTags(["fileIds"], ["tags"], function(error, result) {
511+
if(error) console.log(error);
512+
else console.log(result);
513+
});
514+
515+
// Using Promises
516+
517+
imagekit.bulkRemoveTags(["fileIds"], ["tags"]).then(response => {
518+
console.log(response);
519+
}).catch(error => {
520+
console.log(error);
521+
});
522+
```
523+
524+
**11. Copy File**
525+
526+
This will copy a file from one location to another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-file). This method accepts the source file's path and destination folder path.
527+
528+
```js
529+
// Using Callback Function
530+
531+
imagekit.copyFile("sourceFilePath", "destinationPath", function(error, result) {
532+
if(error) console.log(error);
533+
else console.log(result);
534+
});
535+
536+
// Using Promises
537+
538+
imagekit.copyFile("sourceFilePath", "destinationPath").then(response => {
539+
console.log(response);
540+
}).catch(error => {
541+
console.log(error);
542+
});
543+
```
544+
545+
**12. Move File**
546+
547+
This will move a file from one location to another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-file). This method accepts the source file's path and destination folder path.
548+
549+
```js
550+
// Using Callback Function
551+
552+
imagekit.moveFile("sourceFilePath", "destinationPath", function(error, result) {
553+
if(error) console.log(error);
554+
else console.log(result);
555+
});
556+
557+
// Using Promises
558+
559+
imagekit.moveFile("sourceFilePath", "destinationPath").then(response => {
560+
console.log(response);
561+
}).catch(error => {
562+
console.log(error);
563+
});
564+
```
565+
566+
**13. Copy Folder**
567+
568+
This will copy one folder into another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-folder). This method accepts the source folder's path and destination folder path.
569+
570+
```js
571+
// Using Callback Function
572+
573+
imagekit.copyFolder("sourceFolderPath", "destinationPath", function(error, result) {
574+
if(error) console.log(error);
575+
else console.log(result);
576+
});
577+
578+
// Using Promises
579+
580+
imagekit.copyFolder("sourceFolderPath", "destinationPath").then(response => {
581+
console.log(response);
582+
}).catch(error => {
583+
console.log(error);
584+
});
585+
```
586+
587+
**14. Move Folder**
588+
589+
This will move one folder into another as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/move-folder). This method accepts the source folder's path and destination folder path.
590+
591+
```js
592+
// Using Callback Function
593+
594+
imagekit.moveFolder("sourceFolderPath", "destinationPath", function(error, result) {
595+
if(error) console.log(error);
596+
else console.log(result);
597+
});
598+
599+
// Using Promises
600+
601+
imagekit.moveFolder("sourceFolderPath", "destinationPath").then(response => {
602+
console.log(response);
603+
}).catch(error => {
604+
console.log(error);
605+
});
606+
```
607+
608+
**15. Get bulk job status**
609+
610+
This allows us to get a bulk operation status e.g. copy or move folder as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/copy-move-folder-status). This method accepts `jobId` that is returned by copy and move folder operations.
611+
612+
```js
613+
// Using Callback Function
614+
615+
imagekit.getBulkJobStatus("jobId", function(error, result) {
616+
if(error) console.log(error);
617+
else console.log(result);
618+
});
619+
620+
// Using Promises
621+
622+
imagekit.getBulkJobStatus("jobId").then(response => {
623+
console.log(response);
624+
}).catch(error => {
625+
console.log(error);
626+
});
627+
```
628+
629+
**16. Create Folder**
630+
631+
This will create a new folder as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/create-folder). This method accepts folder name and parent folder path.
632+
633+
```js
634+
// Using Callback Function
635+
636+
imagekit.createFolder("folderName", "parentFolderPath", function(error, result) {
637+
if(error) console.log(error);
638+
else console.log(result);
639+
});
640+
641+
// Using Promises
642+
643+
imagekit.createFolder("folderName", "parentFolderPath").then(response => {
644+
console.log(response);
645+
}).catch(error => {
646+
console.log(error);
647+
});
648+
```
649+
650+
**17. Delete Folder**
651+
652+
This will delete the specified folder and all nested files & folders as per [API documentation here](https://docs.imagekit.io/api-reference/media-api/delete-folder). This method accepts the full path of the folder that is to be deleted.
653+
654+
```js
655+
// Using Callback Function
656+
657+
imagekit.deleteFolder("folderPath", function(error, result) {
658+
if(error) console.log(error);
659+
else console.log(result);
660+
});
661+
662+
// Using Promises
663+
664+
imagekit.deleteFolder("folderPath").then(response => {
462665
console.log(response);
463666
}).catch(error => {
464667
console.log(error);

constants/errorMessages.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,26 @@ module.exports = {
77
"CACHE_PURGE_URL_MISSING" : { message : "Missing URL parameter for this request", help : "" },
88
"CACHE_PURGE_STATUS_ID_MISSING" : { message : "Missing Request ID parameter for this request", help : "" },
99
"FILE_ID_MISSING" : { message : "Missing File ID parameter for this request", help : "" },
10+
"FILE_ID_OR_URL_MISSING" : { message : "Pass either File ID or remote URL of the image as first parameter", help : "" },
1011
"UPDATE_DATA_MISSING" : { message : "Missing file update data for this request", help : "" },
1112
"UPDATE_DATA_TAGS_INVALID" : { message : "Invalid tags parameter for this request", help : "tags should be passed as null or an array like ['tag1', 'tag2']" },
1213
"UPDATE_DATA_COORDS_INVALID" : { message : "Invalid customCoordinates parameter for this request", help : "customCoordinates should be passed as null or a string like 'x,y,width,height'" },
1314
"LIST_FILES_INPUT_MISSING" : { message : "Missing options for list files", help : "If you do not want to pass any parameter for listing, pass an empty object" },
1415
"MISSING_UPLOAD_DATA" : { message : "Missing data for upload", help : "" },
1516
"MISSING_UPLOAD_FILE_PARAMETER" : { message : "Missing file parameter for upload", help : "" },
1617
"MISSING_UPLOAD_FILENAME_PARAMETER" : { message : "Missing fileName parameter for upload", help : "" },
18+
"JOB_ID_MISSING" : { message : "Missing jobId parameter", help : "" },
19+
"INVALID_DESTINATION_FOLDER_PATH" : { messages : "Invalid destinationPath value", help : "It should be a string like '/path/to/folder'"},
20+
"INVALID_SOURCE_FILE_PATH" : { messages : "Invalid sourceFilePath value", help : "It should be a string like /path/to/file.jpg'"},
21+
"INVALID_SOURCE_FOLDER_PATH" : { messages : "Invalid sourceFolderPath value", help : "It should be a string like '/path/to/folder'"},
22+
"INVALID_FOLDER_NAME": { messages : "Invalid folderName value" , help : ""},
23+
"INVALID_PARENT_FOLDER_PATH": { messages : "Invalid parentFolderPath value" , help : "It should be a string like '/path/to/folder'"},
24+
"INVALID_FOLDER_PATH": { messages : "Invalid folderPath value" , help : "It should be a string like '/path/to/folder'"},
1725
// pHash errors
1826
"INVALID_PHASH_VALUE": { message: "Invalid pHash value", help: "Both pHash strings must be valid hexadecimal numbers" },
1927
"MISSING_PHASH_VALUE": { message: "Missing pHash value", help: "Please pass two pHash values" },
2028
"UNEQUAL_STRING_LENGTH": { message: "Unequal pHash string length", help: "For distance calucation, the two pHash strings must have equal length" },
2129
//bulk delete errors
22-
"INVALID_FILEIDS_VALUE": {message: "Invalid value for fileIds", help: "fileIds should be an string array of fileId of the files to delete. The array should have atleast one fileId."}
30+
"INVALID_FILEIDS_VALUE": {message: "Invalid value for fileIds", help: "fileIds should be an array of fileId of the files. The array should have atleast one fileId."},
31+
"BULK_ADD_TAGS_INVALID": {message: "Invalid value for tags", help: "tags should be a non empty array of string like ['tag1', 'tag2']."}
2332
};

constants/supportedTransforms.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
"overlayImageDPR": "oidpr",
2424
"overlayImageQuality": "oiq",
2525
"overlayImageCropping": "oic",
26+
"overlayImageFocus": "oifo",
2627
"overlayImageTrim": "oit",
2728
"overlayX": "ox",
2829
"overlayY": "oy",

index.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ var ImageKit = function(opts) {
100100
return manage.updateFileDetails(fileId, updateData, this.options, callback);
101101
});
102102

103+
// Add bulk tags
104+
this.bulkAddTags = promisify(function(fileIdArray, tags, callback) {
105+
return manage.bulkAddTags(fileIdArray, tags, this.options, callback);
106+
});
107+
108+
// Remove bulk tags
109+
this.bulkRemoveTags = promisify(function(fileIds, tags, callback) {
110+
return manage.bulkRemoveTags(fileIds, tags, this.options, callback);
111+
});
112+
103113
// Delete File API
104114
this.deleteFile = promisify(function(fileId, callback) {
105115
return manage.deleteFile(fileId, this.options, callback);
@@ -118,12 +128,47 @@ var ImageKit = function(opts) {
118128
this.bulkDeleteFiles = promisify(function(fileIdArray, callback) {
119129
return manage.bulkDeleteFiles(fileIdArray, this.options, callback);
120130
});
121-
131+
132+
// Copy files API
133+
this.copyFile = promisify(function(sourceFilePath, destinationPath, callback) {
134+
return manage.copyFile(sourceFilePath, destinationPath, this.options, callback);
135+
});
136+
137+
// Move files API
138+
this.moveFile = promisify(function(sourceFilePath, destinationPath, callback) {
139+
return manage.moveFile(sourceFilePath, destinationPath, this.options, callback);
140+
});
141+
142+
// Create folder API
143+
this.createFolder = promisify(function(folderName, parentFolderPath, callback) {
144+
return manage.createFolder(folderName, parentFolderPath, this.options, callback);
145+
});
146+
147+
// Delete folder API
148+
this.deleteFolder = promisify(function(folderPath, callback) {
149+
return manage.deleteFolder(folderPath, this.options, callback);
150+
});
151+
152+
// Copy folder API
153+
this.copyFolder = promisify(function(sourceFolderPath, destinationPath, callback) {
154+
return manage.copyFolder(sourceFolderPath, destinationPath, this.options, callback);
155+
});
156+
157+
// Move folder API
158+
this.moveFolder = promisify(function(sourceFolderPath, destinationPath, callback) {
159+
return manage.moveFolder(sourceFolderPath, destinationPath, this.options, callback);
160+
});
161+
122162
// To generate Signature for upload request
123163
this.getAuthenticationParameters = function(token, timestamp) {
124164
return signature.getAuthenticationParameters(token, timestamp, this.options);
125165
};
126166

167+
// Get bulk job status API
168+
this.getBulkJobStatus = promisify(function(jobId, callback) {
169+
return manage.getBulkJobStatus(jobId, this.options, callback);
170+
});
171+
127172
// To calculate distance between two pHash strings
128173
this.pHashDistance = function(firstPHash, secondPHash) {
129174
return pHashUtils.pHashDistance(firstPHash, secondPHash);

0 commit comments

Comments
 (0)