Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 27 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
```

Expand Down Expand Up @@ -358,7 +362,7 @@ imagekit.upload({
else console.log(result);
});

// Using Promises
// Using Promises

imagekit.upload({
file : <url|base_64|binary>, //required
Expand Down Expand Up @@ -412,7 +416,7 @@ imagekit.listFiles({
});


// Using Promises
// Using Promises

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


// Using Promises
// Using Promises

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


// Using Promises
// Using Promises

imagekit.getFileVersions("file_id")
}).then(response => {
Expand All @@ -486,7 +490,7 @@ imagekit.getFileVersionDetails({
});


// Using Promises
// Using Promises

imagekit.getFileVersionDetails({
fileId: "file_id",
Expand All @@ -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: [
Expand All @@ -524,7 +528,7 @@ imagekit.updateFileDetails("file_id", {
});


// Using Promises
// Using Promises

imagekit.updateFileDetails("file_id", {
publish: {
Expand Down Expand Up @@ -614,7 +618,7 @@ imagekit.deleteFile("file_id", function(error, result) {
});


// Using Promises
// Using Promises

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


// Using Promises
// Using Promises

imagekit.deleteFile({
fileId: "file_id",
Expand All @@ -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);
Expand Down Expand Up @@ -923,7 +927,7 @@ imagekit.purgeCache("full_url", function(error, result) {
});


// Using Promises
// Using Promises

imagekit.purgeCache("full_url").then(response => {
console.log(response);
Expand All @@ -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);
Expand All @@ -966,7 +970,7 @@ imagekit.getFileMetadata("file_id", function(error, result) {
});


// Using Promises
// Using Promises
imagekit.getFileMetadata("file_id")
}).then(response => {
console.log(response);
Expand All @@ -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);
Expand All @@ -1010,15 +1014,15 @@ imagekit.createCustomMetadataField(
minValue: 1000,
maxValue: 3000
}
},
},
function(error, result) {
if(error) console.log(error);
else console.log(result);
}
);


// Using Promises
// Using Promises

imagekit.createCustomMetadataField(
{
Expand Down Expand Up @@ -1047,15 +1051,15 @@ 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);
}
);


// Using Promises
// Using Promises

imagekit.getCustomMetadataFields(
{
Expand All @@ -1082,15 +1086,15 @@ imagekit.updateCustomMetadataField(
minValue: 500,
maxValue: 2500
}
},
},
function(error, result) {
if(error) console.log(error);
else console.log(result);
}
);


// Using Promises
// Using Promises

imagekit.updateCustomMetadataField(
"field_id",
Expand All @@ -1099,7 +1103,7 @@ imagekit.updateCustomMetadataField(
minValue: 500,
maxValue: 2500
}
},
},
).then(response => {
console.log(response);
}).catch(error => {
Expand All @@ -1123,7 +1127,7 @@ imagekit.deleteCustomMetadataField(
);


// Using Promises
// Using Promises

imagekit.deleteCustomMetadataField(
"field_id"
Expand Down
2 changes: 1 addition & 1 deletion libs/manage/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PurgeCacheResponse>) {
if (!url && !url.length) {
Expand Down
2 changes: 1 addition & 1 deletion libs/manage/custom-metadata-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
CustomMetadataField,
UpdateCustomMetadataFieldOptions,
GetCustomMetadataFieldsOptions,
} from "../interfaces/";
} from "../interfaces";

const create = function (createCustomMetadataFieldOptions: CreateCustomMetadataFieldOptions, defaultOptions: ImageKitOptions, callback?: IKCallback<CustomMetadataField>) {
const { name, label, schema } = createCustomMetadataFieldOptions;
Expand Down
12 changes: 6 additions & 6 deletions libs/manage/file.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import _ from "lodash";
import { isObject } from 'lodash';

/*
Constants
Expand Down Expand Up @@ -35,7 +35,7 @@ import {
RestoreFileVersionOptions,
RenameFileOptions,
RenameFileResponse,
} from "../interfaces/";
} from "../interfaces";
import ImageKit from "../..";

/*
Expand Down Expand Up @@ -198,7 +198,7 @@ const updateDetails = function (
return;
}

if (!_.isObject(updateData)) {
if (!isObject(updateData)) {
respond(true, errorMessages.UPDATE_DATA_MISSING, callback);
return;
}
Expand Down Expand Up @@ -235,12 +235,12 @@ const listFiles = function (
defaultOptions: ImageKitOptions,
callback?: IKCallback<FileObject[]>,
) {
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(",");
}

Expand Down Expand Up @@ -635,7 +635,7 @@ const createFolder = function (
request(requestOptions, defaultOptions, callback);
};

/*
/*
Delete folder
*/
const deleteFolder = function (folderPath: string, defaultOptions: ImageKitOptions, callback?: IKCallback<void>) {
Expand Down
2 changes: 1 addition & 1 deletion libs/upload/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
19 changes: 11 additions & 8 deletions utils/request.ts
Original file line number Diff line number Diff line change
@@ -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<T, E extends Error>(
requestOptions: RequestOptions,
defaultOptions: ImageKitOptions,
Expand All @@ -27,13 +30,13 @@ export default function request<T, E extends Error>(
if (typeof requestOptions.headers === "object") options.headers = requestOptions.headers;

axios(options).then((response: AxiosResponse<T>) => {
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,
Expand All @@ -42,7 +45,7 @@ export default function request<T, E extends Error>(
});
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
Expand All @@ -51,7 +54,7 @@ export default function request<T, E extends Error>(
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") {
Expand Down Expand Up @@ -82,7 +85,7 @@ export default function request<T, E extends Error>(
// `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);
}
})
}
2 changes: 1 addition & 1 deletion utils/respond.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IKCallback } from "../libs/interfaces/IKCallback";

export default function respond<D, E extends Error>(isError: boolean, response: any, callback?: IKCallback<D, E>) {
if (typeof callback == "function") {
if (typeof callback === "function") {
if (isError) {
callback(response, null);
} else {
Expand Down
Loading