diff --git a/README.md b/README.md index 4289312d..8eca33fb 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # ImageKit.io Node.js SDK [![Node CI](https://github.com/imagekit-developer/imagekit-nodejs/workflows/Node%20CI/badge.svg)](https://github.com/imagekit-developer/imagekit-nodejs/) -[![npm version](https://img.shields.io/npm/v/imagekit)](https://www.npmjs.com/package/imagekit) +[![npm version](https://img.shields.io/npm/v/imagekit)](https://www.npmjs.com/package/imagekit) [![codecov](https://codecov.io/gh/imagekit-developer/imagekit-nodejs/branch/master/graph/badge.svg)](https://codecov.io/gh/imagekit-developer/imagekit-nodejs) [![Try imagekit on RunKit](https://badge.runkitcdn.com/imagekit.svg)](https://npm.runkit.com/imagekit) [![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 ``` npm install imagekit --save # or +pnpm install imagekit --save +# or +bun install imagekit // if you are using [Bun](https://bun.sh/) compiler +# or yarn add imagekit ``` @@ -358,7 +362,7 @@ imagekit.upload({ else console.log(result); }); -// Using Promises +// Using Promises imagekit.upload({ file : , //required @@ -412,7 +416,7 @@ imagekit.listFiles({ }); -// Using Promises +// Using Promises imagekit.listFiles({ skip : 10, @@ -437,7 +441,7 @@ imagekit.getFileDetails("file_id", function(error, result) { }); -// Using Promises +// Using Promises imagekit.getFileDetails("file_id") }).then(response => { @@ -460,7 +464,7 @@ imagekit.getFileVersions("file_id", function(error, result) { }); -// Using Promises +// Using Promises imagekit.getFileVersions("file_id") }).then(response => { @@ -486,7 +490,7 @@ imagekit.getFileVersionDetails({ }); -// Using Promises +// Using Promises imagekit.getFileVersionDetails({ fileId: "file_id", @@ -508,7 +512,7 @@ Note: If `publish` is included in the update options, no other parameters are al ```js // Using Callback Function -imagekit.updateFileDetails("file_id", { +imagekit.updateFileDetails("file_id", { tags : ['image_tag'], customCoordinates : "10,10,100,100", extensions: [ @@ -524,7 +528,7 @@ imagekit.updateFileDetails("file_id", { }); -// Using Promises +// Using Promises imagekit.updateFileDetails("file_id", { publish: { @@ -614,7 +618,7 @@ imagekit.deleteFile("file_id", function(error, result) { }); -// Using Promises +// Using Promises imagekit.deleteFile("file_id").then(response => { console.log(response); @@ -639,7 +643,7 @@ imagekit.deleteFileVersion({ }); -// Using Promises +// Using Promises imagekit.deleteFile({ fileId: "file_id", @@ -664,7 +668,7 @@ imagekit.bulkDeleteFiles(["file_id_1", "file_id_2"], function(error, result) { }); -// Using Promises +// Using Promises imagekit.bulkDeleteFiles(["file_id_1", "file_id_2"]).then(response => { console.log(response); @@ -923,7 +927,7 @@ imagekit.purgeCache("full_url", function(error, result) { }); -// Using Promises +// Using Promises imagekit.purgeCache("full_url").then(response => { console.log(response); @@ -945,7 +949,7 @@ imagekit.getPurgeCacheStatus("cache_request_id", function(error, result) { }); -// Using Promises +// Using Promises imagekit.getPurgeCacheStatus("cache_request_id").then(response => { console.log(response); @@ -966,7 +970,7 @@ imagekit.getFileMetadata("file_id", function(error, result) { }); -// Using Promises +// Using Promises imagekit.getFileMetadata("file_id") }).then(response => { console.log(response); @@ -985,7 +989,7 @@ imagekit.getFileMetadata("https://ik.imagekit.io/your_imagekit_id/sample.jpg", f }); -// Using Promises +// Using Promises imagekit.getFileMetadata("https://ik.imagekit.io/your_imagekit_id/sample.jpg") }).then(response => { console.log(response); @@ -1010,7 +1014,7 @@ imagekit.createCustomMetadataField( minValue: 1000, maxValue: 3000 } - }, + }, function(error, result) { if(error) console.log(error); else console.log(result); @@ -1018,7 +1022,7 @@ imagekit.createCustomMetadataField( ); -// Using Promises +// Using Promises imagekit.createCustomMetadataField( { @@ -1047,7 +1051,7 @@ Get the list of all custom metadata fields as per the [API documentation here](h imagekit.getCustomMetadataFields( { includeDeleted: false // optional - }, + }, function(error, result) { if(error) console.log(error); else console.log(result); @@ -1055,7 +1059,7 @@ imagekit.getCustomMetadataFields( ); -// Using Promises +// Using Promises imagekit.getCustomMetadataFields( { @@ -1082,7 +1086,7 @@ imagekit.updateCustomMetadataField( minValue: 500, maxValue: 2500 } - }, + }, function(error, result) { if(error) console.log(error); else console.log(result); @@ -1090,7 +1094,7 @@ imagekit.updateCustomMetadataField( ); -// Using Promises +// Using Promises imagekit.updateCustomMetadataField( "field_id", @@ -1099,7 +1103,7 @@ imagekit.updateCustomMetadataField( minValue: 500, maxValue: 2500 } - }, + }, ).then(response => { console.log(response); }).catch(error => { @@ -1123,7 +1127,7 @@ imagekit.deleteCustomMetadataField( ); -// Using Promises +// Using Promises imagekit.deleteCustomMetadataField( "field_id" diff --git a/libs/manage/cache.ts b/libs/manage/cache.ts index 8c9de8c2..a3bb4ef7 100644 --- a/libs/manage/cache.ts +++ b/libs/manage/cache.ts @@ -13,7 +13,7 @@ import request from "../../utils/request"; Interfaces */ import { IKCallback } from "../interfaces/IKCallback"; -import { ImageKitOptions, PurgeCacheResponse, PurgeCacheStatusResponse } from "../interfaces/"; +import { ImageKitOptions, PurgeCacheResponse, PurgeCacheStatusResponse } from "../interfaces"; const purgeCache = function (url: string, defaultOptions: ImageKitOptions, callback?: IKCallback) { if (!url && !url.length) { diff --git a/libs/manage/custom-metadata-field.ts b/libs/manage/custom-metadata-field.ts index a22ec73f..752ab080 100644 --- a/libs/manage/custom-metadata-field.ts +++ b/libs/manage/custom-metadata-field.ts @@ -19,7 +19,7 @@ import { CustomMetadataField, UpdateCustomMetadataFieldOptions, GetCustomMetadataFieldsOptions, -} from "../interfaces/"; +} from "../interfaces"; const create = function (createCustomMetadataFieldOptions: CreateCustomMetadataFieldOptions, defaultOptions: ImageKitOptions, callback?: IKCallback) { const { name, label, schema } = createCustomMetadataFieldOptions; diff --git a/libs/manage/file.ts b/libs/manage/file.ts index 74cd0dc3..5ee18da7 100644 --- a/libs/manage/file.ts +++ b/libs/manage/file.ts @@ -1,4 +1,4 @@ -import _ from "lodash"; +import { isObject } from 'lodash'; /* Constants @@ -35,7 +35,7 @@ import { RestoreFileVersionOptions, RenameFileOptions, RenameFileResponse, -} from "../interfaces/"; +} from "../interfaces"; import ImageKit from "../.."; /* @@ -198,7 +198,7 @@ const updateDetails = function ( return; } - if (!_.isObject(updateData)) { + if (!isObject(updateData)) { respond(true, errorMessages.UPDATE_DATA_MISSING, callback); return; } @@ -235,12 +235,12 @@ const listFiles = function ( defaultOptions: ImageKitOptions, callback?: IKCallback, ) { - if (listOptions && !_.isObject(listOptions)) { + if (listOptions && !isObject(listOptions)) { respond(true, errorMessages.INVALID_LIST_OPTIONS, callback); return; } - if (listOptions && listOptions.tags && _.isArray(listOptions.tags) && listOptions.tags.length) { + if (listOptions && listOptions.tags && Array.isArray(listOptions.tags) && listOptions.tags.length) { listOptions.tags = listOptions.tags.join(","); } @@ -635,7 +635,7 @@ const createFolder = function ( request(requestOptions, defaultOptions, callback); }; -/* +/* Delete folder */ const deleteFolder = function (folderPath: string, defaultOptions: ImageKitOptions, callback?: IKCallback) { diff --git a/libs/upload/index.ts b/libs/upload/index.ts index 5ac7420a..7d1a8577 100644 --- a/libs/upload/index.ts +++ b/libs/upload/index.ts @@ -1,5 +1,5 @@ import _ from "lodash"; -import errorMessages from "../../libs/constants/errorMessages"; +import errorMessages from "../constants/errorMessages"; import respond from "../../utils/respond"; import request from "../../utils/request"; import { IKCallback } from "../interfaces/IKCallback"; diff --git a/utils/request.ts b/utils/request.ts index f23c7dc0..aa1bf855 100644 --- a/utils/request.ts +++ b/utils/request.ts @@ -1,9 +1,12 @@ -import respond from "../utils/respond"; -import { RequestOptions } from "../utils/authorization"; -import { ImageKitOptions } from "../libs/interfaces/"; +import respond from "./respond"; +import { RequestOptions } from "./authorization"; +import { ImageKitOptions } from "../libs/interfaces"; import { IKCallback } from "../libs/interfaces/IKCallback"; import axios, { AxiosError, AxiosHeaders, AxiosRequestConfig, AxiosResponse } from "axios"; +// constant +const UnknownError: string = "Unknown error occured"; + export default function request( requestOptions: RequestOptions, defaultOptions: ImageKitOptions, @@ -27,13 +30,13 @@ export default function request( if (typeof requestOptions.headers === "object") options.headers = requestOptions.headers; axios(options).then((response: AxiosResponse) => { - if (typeof callback != "function") return; + if (typeof callback !== "function") return; const { data, status, headers } = response; const responseMetadata = { statusCode: status, headers: (headers as AxiosHeaders).toJSON() } - var result = data ? data : {} as T; + let result = data ? data : {} as T; // define status code and headers as non-enumerable properties on data Object.defineProperty(result, "$ResponseMetadata", { value: responseMetadata, @@ -42,7 +45,7 @@ export default function request( }); respond(false, result, callback); }, (error: AxiosError) => { - if (typeof callback != "function") return; + if (typeof callback !== "function") return; if (error.response) { // The request was made and the server responded with a status code // that falls out of the range of 2xx @@ -51,7 +54,7 @@ export default function request( headers: (error.response.headers as AxiosHeaders).toJSON() } - var result = {} as Object; + let result = {} as Object; if (error.response.data && typeof error.response.data === "object") { result = error.response.data } else if (error.response.data && typeof error.response.data === "string") { @@ -82,7 +85,7 @@ export default function request( // `error.request` is an instance of XMLHttpRequest in the browser and an instance of // http.ClientRequest in node.js } else { - respond(true, new Error("Unknown error occured"), callback); + respond(true, new Error(UnknownError), callback); } }) } diff --git a/utils/respond.ts b/utils/respond.ts index 6aca4f7a..96c359e3 100644 --- a/utils/respond.ts +++ b/utils/respond.ts @@ -1,7 +1,7 @@ import { IKCallback } from "../libs/interfaces/IKCallback"; export default function respond(isError: boolean, response: any, callback?: IKCallback) { - if (typeof callback == "function") { + if (typeof callback === "function") { if (isError) { callback(response, null); } else { diff --git a/utils/urlFormatter.ts b/utils/urlFormatter.ts index ad7813bf..96e2b158 100644 --- a/utils/urlFormatter.ts +++ b/utils/urlFormatter.ts @@ -1,33 +1,73 @@ +/** + * Adds a leading slash to the given string if it does not already start with one. + * + * @param {string} str - The input string to be processed. + * @returns {string} - The modified string with a leading slash if it was missing. + */ const addLeadingSlash = function (str: string) { - if (typeof str == "string" && str[0] != "/") { + // Check if the input is a string and does not start with a slash + if (typeof str === "string" && str[0] !== "/") { + // Prepend a slash to the string str = "/" + str; } + // Return the processed string return str; }; + +/** + * Removes the leading slash from the given string if it starts with one. + * + * @param {string} str - The input string to be processed. + * @returns {string} - The modified string with the leading slash removed if it was present. + */ const removeLeadingSlash = function (str: string) { - if (typeof str == "string" && str[0] == "/") { + // Check if the input is a string and starts with a slash + if (typeof str === "string" && str[0] === "/") { + // Remove the leading slash from the string str = str.substring(1); } + // Return the processed string return str; }; + +/** + * Removes the trailing slash from the given string if it ends with one. + * + * @param {string} str - The input string to be processed. + * @returns {string} - The modified string with the trailing slash removed if it was present. + */ const removeTrailingSlash = function (str: string) { - if (typeof str == "string" && str[str.length - 1] == "/") { + // Check if the input is a string and ends with a slash + if (typeof str === "string" && str[str.length - 1] === "/") { + // Remove the trailing slash from the string str = str.substring(0, str.length - 1); } + // Return the processed string return str; }; + +/** + * Adds a trailing slash to the given string if it does not already end with one. + * + * @param {string} str - The input string to be processed. + * @returns {string} - The modified string with a trailing slash if it was missing. + */ const addTrailingSlash = function (str: string) { - if (typeof str == "string" && str[str.length - 1] != "/") { + // Check if the input is a string and does not end with a slash + if (typeof str === "string" && str[str.length - 1] !== "/") { + // Append a trailing slash to the string str = str + "/"; } + // Return the processed string return str; }; + export default { addLeadingSlash, removeLeadingSlash, removeTrailingSlash, addTrailingSlash };