Skip to content

Commit ff88b20

Browse files
vikrampariharvikram parihar
and
vikram parihar
authoredDec 5, 2024··
enhancement: minor fixes and improved documentation (#103)
* Added more package manager support * Rearrange import path for better visibility * Minor slash fixed in import statement * Import path fixed, and imported only required function from lodash for improve (minimize) build size * fixed import path and code enhanced * Code enhancement --------- Co-authored-by: vikram parihar <vikram2.parihar@ril.com>
1 parent 001fb41 commit ff88b20

File tree

8 files changed

+92
-45
lines changed

8 files changed

+92
-45
lines changed
 

‎README.md

+27-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# ImageKit.io Node.js SDK
44

55
[![Node CI](https://github.com/imagekit-developer/imagekit-nodejs/workflows/Node%20CI/badge.svg)](https://github.com/imagekit-developer/imagekit-nodejs/)
6-
[![npm version](https://img.shields.io/npm/v/imagekit)](https://www.npmjs.com/package/imagekit)
6+
[![npm version](https://img.shields.io/npm/v/imagekit)](https://www.npmjs.com/package/imagekit)
77
[![codecov](https://codecov.io/gh/imagekit-developer/imagekit-nodejs/branch/master/graph/badge.svg)](https://codecov.io/gh/imagekit-developer/imagekit-nodejs)
88
[![Try imagekit on RunKit](https://badge.runkitcdn.com/imagekit.svg)](https://npm.runkit.com/imagekit)
99
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
@@ -45,6 +45,10 @@ Use the following command to download this module. Use the optional `--save` par
4545
```
4646
npm install imagekit --save
4747
# or
48+
pnpm install imagekit --save
49+
# or
50+
bun install imagekit // if you are using [Bun](https://bun.sh/) compiler
51+
# or
4852
yarn add imagekit
4953
```
5054

@@ -358,7 +362,7 @@ imagekit.upload({
358362
else console.log(result);
359363
});
360364

361-
// Using Promises
365+
// Using Promises
362366

363367
imagekit.upload({
364368
file : <url|base_64|binary>, //required
@@ -412,7 +416,7 @@ imagekit.listFiles({
412416
});
413417

414418

415-
// Using Promises
419+
// Using Promises
416420

417421
imagekit.listFiles({
418422
skip : 10,
@@ -437,7 +441,7 @@ imagekit.getFileDetails("file_id", function(error, result) {
437441
});
438442

439443

440-
// Using Promises
444+
// Using Promises
441445

442446
imagekit.getFileDetails("file_id")
443447
}).then(response => {
@@ -460,7 +464,7 @@ imagekit.getFileVersions("file_id", function(error, result) {
460464
});
461465

462466

463-
// Using Promises
467+
// Using Promises
464468

465469
imagekit.getFileVersions("file_id")
466470
}).then(response => {
@@ -486,7 +490,7 @@ imagekit.getFileVersionDetails({
486490
});
487491

488492

489-
// Using Promises
493+
// Using Promises
490494

491495
imagekit.getFileVersionDetails({
492496
fileId: "file_id",
@@ -508,7 +512,7 @@ Note: If `publish` is included in the update options, no other parameters are al
508512
```js
509513
// Using Callback Function
510514

511-
imagekit.updateFileDetails("file_id", {
515+
imagekit.updateFileDetails("file_id", {
512516
tags : ['image_tag'],
513517
customCoordinates : "10,10,100,100",
514518
extensions: [
@@ -524,7 +528,7 @@ imagekit.updateFileDetails("file_id", {
524528
});
525529

526530

527-
// Using Promises
531+
// Using Promises
528532

529533
imagekit.updateFileDetails("file_id", {
530534
publish: {
@@ -614,7 +618,7 @@ imagekit.deleteFile("file_id", function(error, result) {
614618
});
615619

616620

617-
// Using Promises
621+
// Using Promises
618622

619623
imagekit.deleteFile("file_id").then(response => {
620624
console.log(response);
@@ -639,7 +643,7 @@ imagekit.deleteFileVersion({
639643
});
640644

641645

642-
// Using Promises
646+
// Using Promises
643647

644648
imagekit.deleteFile({
645649
fileId: "file_id",
@@ -664,7 +668,7 @@ imagekit.bulkDeleteFiles(["file_id_1", "file_id_2"], function(error, result) {
664668
});
665669

666670

667-
// Using Promises
671+
// Using Promises
668672

669673
imagekit.bulkDeleteFiles(["file_id_1", "file_id_2"]).then(response => {
670674
console.log(response);
@@ -923,7 +927,7 @@ imagekit.purgeCache("full_url", function(error, result) {
923927
});
924928

925929

926-
// Using Promises
930+
// Using Promises
927931

928932
imagekit.purgeCache("full_url").then(response => {
929933
console.log(response);
@@ -945,7 +949,7 @@ imagekit.getPurgeCacheStatus("cache_request_id", function(error, result) {
945949
});
946950

947951

948-
// Using Promises
952+
// Using Promises
949953

950954
imagekit.getPurgeCacheStatus("cache_request_id").then(response => {
951955
console.log(response);
@@ -966,7 +970,7 @@ imagekit.getFileMetadata("file_id", function(error, result) {
966970
});
967971

968972

969-
// Using Promises
973+
// Using Promises
970974
imagekit.getFileMetadata("file_id")
971975
}).then(response => {
972976
console.log(response);
@@ -985,7 +989,7 @@ imagekit.getFileMetadata("https://ik.imagekit.io/your_imagekit_id/sample.jpg", f
985989
});
986990

987991

988-
// Using Promises
992+
// Using Promises
989993
imagekit.getFileMetadata("https://ik.imagekit.io/your_imagekit_id/sample.jpg")
990994
}).then(response => {
991995
console.log(response);
@@ -1010,15 +1014,15 @@ imagekit.createCustomMetadataField(
10101014
minValue: 1000,
10111015
maxValue: 3000
10121016
}
1013-
},
1017+
},
10141018
function(error, result) {
10151019
if(error) console.log(error);
10161020
else console.log(result);
10171021
}
10181022
);
10191023

10201024

1021-
// Using Promises
1025+
// Using Promises
10221026

10231027
imagekit.createCustomMetadataField(
10241028
{
@@ -1047,15 +1051,15 @@ Get the list of all custom metadata fields as per the [API documentation here](h
10471051
imagekit.getCustomMetadataFields(
10481052
{
10491053
includeDeleted: false // optional
1050-
},
1054+
},
10511055
function(error, result) {
10521056
if(error) console.log(error);
10531057
else console.log(result);
10541058
}
10551059
);
10561060

10571061

1058-
// Using Promises
1062+
// Using Promises
10591063

10601064
imagekit.getCustomMetadataFields(
10611065
{
@@ -1082,15 +1086,15 @@ imagekit.updateCustomMetadataField(
10821086
minValue: 500,
10831087
maxValue: 2500
10841088
}
1085-
},
1089+
},
10861090
function(error, result) {
10871091
if(error) console.log(error);
10881092
else console.log(result);
10891093
}
10901094
);
10911095

10921096

1093-
// Using Promises
1097+
// Using Promises
10941098

10951099
imagekit.updateCustomMetadataField(
10961100
"field_id",
@@ -1099,7 +1103,7 @@ imagekit.updateCustomMetadataField(
10991103
minValue: 500,
11001104
maxValue: 2500
11011105
}
1102-
},
1106+
},
11031107
).then(response => {
11041108
console.log(response);
11051109
}).catch(error => {
@@ -1123,7 +1127,7 @@ imagekit.deleteCustomMetadataField(
11231127
);
11241128

11251129

1126-
// Using Promises
1130+
// Using Promises
11271131

11281132
imagekit.deleteCustomMetadataField(
11291133
"field_id"

‎libs/manage/cache.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import request from "../../utils/request";
1313
Interfaces
1414
*/
1515
import { IKCallback } from "../interfaces/IKCallback";
16-
import { ImageKitOptions, PurgeCacheResponse, PurgeCacheStatusResponse } from "../interfaces/";
16+
import { ImageKitOptions, PurgeCacheResponse, PurgeCacheStatusResponse } from "../interfaces";
1717

1818
const purgeCache = function (url: string, defaultOptions: ImageKitOptions, callback?: IKCallback<PurgeCacheResponse>) {
1919
if (!url && !url.length) {

‎libs/manage/custom-metadata-field.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
CustomMetadataField,
2020
UpdateCustomMetadataFieldOptions,
2121
GetCustomMetadataFieldsOptions,
22-
} from "../interfaces/";
22+
} from "../interfaces";
2323

2424
const create = function (createCustomMetadataFieldOptions: CreateCustomMetadataFieldOptions, defaultOptions: ImageKitOptions, callback?: IKCallback<CustomMetadataField>) {
2525
const { name, label, schema } = createCustomMetadataFieldOptions;

‎libs/manage/file.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import _ from "lodash";
1+
import { isObject } from 'lodash';
22

33
/*
44
Constants
@@ -35,7 +35,7 @@ import {
3535
RestoreFileVersionOptions,
3636
RenameFileOptions,
3737
RenameFileResponse,
38-
} from "../interfaces/";
38+
} from "../interfaces";
3939
import ImageKit from "../..";
4040

4141
/*
@@ -198,7 +198,7 @@ const updateDetails = function (
198198
return;
199199
}
200200

201-
if (!_.isObject(updateData)) {
201+
if (!isObject(updateData)) {
202202
respond(true, errorMessages.UPDATE_DATA_MISSING, callback);
203203
return;
204204
}
@@ -235,12 +235,12 @@ const listFiles = function (
235235
defaultOptions: ImageKitOptions,
236236
callback?: IKCallback<FileObject[]>,
237237
) {
238-
if (listOptions && !_.isObject(listOptions)) {
238+
if (listOptions && !isObject(listOptions)) {
239239
respond(true, errorMessages.INVALID_LIST_OPTIONS, callback);
240240
return;
241241
}
242242

243-
if (listOptions && listOptions.tags && _.isArray(listOptions.tags) && listOptions.tags.length) {
243+
if (listOptions && listOptions.tags && Array.isArray(listOptions.tags) && listOptions.tags.length) {
244244
listOptions.tags = listOptions.tags.join(",");
245245
}
246246

@@ -635,7 +635,7 @@ const createFolder = function (
635635
request(requestOptions, defaultOptions, callback);
636636
};
637637

638-
/*
638+
/*
639639
Delete folder
640640
*/
641641
const deleteFolder = function (folderPath: string, defaultOptions: ImageKitOptions, callback?: IKCallback<void>) {

‎libs/upload/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ from "lodash";
2-
import errorMessages from "../../libs/constants/errorMessages";
2+
import errorMessages from "../constants/errorMessages";
33
import respond from "../../utils/respond";
44
import request from "../../utils/request";
55
import { IKCallback } from "../interfaces/IKCallback";

‎utils/request.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import respond from "../utils/respond";
2-
import { RequestOptions } from "../utils/authorization";
3-
import { ImageKitOptions } from "../libs/interfaces/";
1+
import respond from "./respond";
2+
import { RequestOptions } from "./authorization";
3+
import { ImageKitOptions } from "../libs/interfaces";
44
import { IKCallback } from "../libs/interfaces/IKCallback";
55
import axios, { AxiosError, AxiosHeaders, AxiosRequestConfig, AxiosResponse } from "axios";
66

7+
// constant
8+
const UnknownError: string = "Unknown error occured";
9+
710
export default function request<T, E extends Error>(
811
requestOptions: RequestOptions,
912
defaultOptions: ImageKitOptions,
@@ -27,13 +30,13 @@ export default function request<T, E extends Error>(
2730
if (typeof requestOptions.headers === "object") options.headers = requestOptions.headers;
2831

2932
axios(options).then((response: AxiosResponse<T>) => {
30-
if (typeof callback != "function") return;
33+
if (typeof callback !== "function") return;
3134
const { data, status, headers } = response;
3235
const responseMetadata = {
3336
statusCode: status,
3437
headers: (headers as AxiosHeaders).toJSON()
3538
}
36-
var result = data ? data : {} as T;
39+
let result = data ? data : {} as T;
3740
// define status code and headers as non-enumerable properties on data
3841
Object.defineProperty(result, "$ResponseMetadata", {
3942
value: responseMetadata,
@@ -42,7 +45,7 @@ export default function request<T, E extends Error>(
4245
});
4346
respond(false, result, callback);
4447
}, (error: AxiosError) => {
45-
if (typeof callback != "function") return;
48+
if (typeof callback !== "function") return;
4649
if (error.response) {
4750
// The request was made and the server responded with a status code
4851
// that falls out of the range of 2xx
@@ -51,7 +54,7 @@ export default function request<T, E extends Error>(
5154
headers: (error.response.headers as AxiosHeaders).toJSON()
5255
}
5356

54-
var result = {} as Object;
57+
let result = {} as Object;
5558
if (error.response.data && typeof error.response.data === "object") {
5659
result = error.response.data
5760
} else if (error.response.data && typeof error.response.data === "string") {
@@ -82,7 +85,7 @@ export default function request<T, E extends Error>(
8285
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
8386
// http.ClientRequest in node.js
8487
} else {
85-
respond(true, new Error("Unknown error occured"), callback);
88+
respond(true, new Error(UnknownError), callback);
8689
}
8790
})
8891
}

‎utils/respond.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IKCallback } from "../libs/interfaces/IKCallback";
22

33
export default function respond<D, E extends Error>(isError: boolean, response: any, callback?: IKCallback<D, E>) {
4-
if (typeof callback == "function") {
4+
if (typeof callback === "function") {
55
if (isError) {
66
callback(response, null);
77
} else {

‎utils/urlFormatter.ts

+44-4
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,73 @@
1+
/**
2+
* Adds a leading slash to the given string if it does not already start with one.
3+
*
4+
* @param {string} str - The input string to be processed.
5+
* @returns {string} - The modified string with a leading slash if it was missing.
6+
*/
17
const addLeadingSlash = function (str: string) {
2-
if (typeof str == "string" && str[0] != "/") {
8+
// Check if the input is a string and does not start with a slash
9+
if (typeof str === "string" && str[0] !== "/") {
10+
// Prepend a slash to the string
311
str = "/" + str;
412
}
513

14+
// Return the processed string
615
return str;
716
};
817

18+
19+
/**
20+
* Removes the leading slash from the given string if it starts with one.
21+
*
22+
* @param {string} str - The input string to be processed.
23+
* @returns {string} - The modified string with the leading slash removed if it was present.
24+
*/
925
const removeLeadingSlash = function (str: string) {
10-
if (typeof str == "string" && str[0] == "/") {
26+
// Check if the input is a string and starts with a slash
27+
if (typeof str === "string" && str[0] === "/") {
28+
// Remove the leading slash from the string
1129
str = str.substring(1);
1230
}
1331

32+
// Return the processed string
1433
return str;
1534
};
1635

36+
37+
/**
38+
* Removes the trailing slash from the given string if it ends with one.
39+
*
40+
* @param {string} str - The input string to be processed.
41+
* @returns {string} - The modified string with the trailing slash removed if it was present.
42+
*/
1743
const removeTrailingSlash = function (str: string) {
18-
if (typeof str == "string" && str[str.length - 1] == "/") {
44+
// Check if the input is a string and ends with a slash
45+
if (typeof str === "string" && str[str.length - 1] === "/") {
46+
// Remove the trailing slash from the string
1947
str = str.substring(0, str.length - 1);
2048
}
2149

50+
// Return the processed string
2251
return str;
2352
};
2453

54+
55+
/**
56+
* Adds a trailing slash to the given string if it does not already end with one.
57+
*
58+
* @param {string} str - The input string to be processed.
59+
* @returns {string} - The modified string with a trailing slash if it was missing.
60+
*/
2561
const addTrailingSlash = function (str: string) {
26-
if (typeof str == "string" && str[str.length - 1] != "/") {
62+
// Check if the input is a string and does not end with a slash
63+
if (typeof str === "string" && str[str.length - 1] !== "/") {
64+
// Append a trailing slash to the string
2765
str = str + "/";
2866
}
2967

68+
// Return the processed string
3069
return str;
3170
};
3271

72+
3373
export default { addLeadingSlash, removeLeadingSlash, removeTrailingSlash, addTrailingSlash };

0 commit comments

Comments
 (0)
Please sign in to comment.