Skip to content

Commit

Permalink
Merge branch 'master' into IK-1068
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur-dwivedi authored Dec 5, 2024
2 parents b0c7819 + ff88b20 commit a82c2ae
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 57 deletions.
68 changes: 35 additions & 33 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 @@ -351,13 +355,14 @@ imagekit.upload({
}
]
},
checks={`"file.size" < "1mb"`} // To run server side checks before uploading files. Notice the quotes around file.size and 1mb.
checks: {`"file.size" < "1mb"`}, // To run server side checks before uploading files. Notice the quotes around file.size and 1mb.
isPublished: true
}, function(error, result) {
if(error) console.log(error);
else console.log(result);
});

// Using Promises
// Using Promises

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


// Using Promises
// Using Promises

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


// Using Promises
// Using Promises

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


// Using Promises
// Using Promises

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


// Using Promises
// Using Promises

imagekit.getFileVersionDetails({
fileId: "file_id",
Expand All @@ -502,10 +507,12 @@ imagekit.getFileVersionDetails({

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 the second argument is an object with the parameters to be updated.

Note: If `publish` is included in the update options, no other parameters are allowed. If any are present, an error will be returned: `Your request cannot contain any other parameters when publish is present`.

```js
// Using Callback Function

imagekit.updateFileDetails("file_id", {
imagekit.updateFileDetails("file_id", {
tags : ['image_tag'],
customCoordinates : "10,10,100,100",
extensions: [
Expand All @@ -521,18 +528,13 @@ imagekit.updateFileDetails("file_id", {
});


// Using Promises
// Using Promises

imagekit.updateFileDetails("file_id", {
tags : ['image_tag'],
customCoordinates : "10,10,100,100",
extensions: [
{
name: "google-auto-tagging",
maxTags: 5,
minConfidence: 95
}
]
publish: {
isPublished: true,
includeFileVersions: true
}
}).then(response => {
console.log(response);
}).catch(error => {
Expand Down Expand Up @@ -616,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 @@ -641,7 +643,7 @@ imagekit.deleteFileVersion({
});


// Using Promises
// Using Promises

imagekit.deleteFile({
fileId: "file_id",
Expand All @@ -666,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 @@ -925,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 @@ -947,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 @@ -968,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 @@ -987,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 @@ -1012,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 @@ -1049,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 @@ -1084,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 @@ -1101,7 +1103,7 @@ imagekit.updateCustomMetadataField(
minValue: 500,
maxValue: 2500
}
},
},
).then(response => {
console.log(response);
}).catch(error => {
Expand All @@ -1125,7 +1127,7 @@ imagekit.deleteCustomMetadataField(
);


// Using Promises
// Using Promises

imagekit.deleteCustomMetadataField(
"field_id"
Expand Down
7 changes: 7 additions & 0 deletions libs/interfaces/FileDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export interface FileDetailsOptions {
* A key-value data to be associated with the asset. To unset a key, send null value for that key. Before setting any custom metadata on an asset you have to create the field using custom metadata fields API.
*/
customMetadata?: CMValues;
/**
* Configure the publication status of a file and its versions.
*/
publish?: {
isPublished: boolean;
includeFileVersions?: boolean;
};
}

/**
Expand Down
6 changes: 6 additions & 0 deletions libs/interfaces/UploadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,10 @@ export interface UploadOptions {
* Optional `checks` parameters can be used to run server-side checks before files are uploaded to the Media Library.
*/
checks?: string
/**
* Optional. Determines whether the file should be uploaded as published.
* If set to false, the file will be marked as unpublished, restricting access to the file through the media library only.
* Files in draft or unpublished states can only be publicly accessed after they are published.
*/
isPublished?: boolean
}
4 changes: 4 additions & 0 deletions libs/interfaces/UploadResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@ export interface UploadResponse {
* A key-value data associated with the asset. Before setting any custom metadata on an asset, you have to create the field using custom metadata fields API.
*/
customMetadata?: CMValues;
/**
* Is the file published or in draft state. It can be either true or false.
*/
isPublished?: boolean
}
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
24 changes: 16 additions & 8 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,18 +198,26 @@ const updateDetails = function (
return;
}

if (!_.isObject(updateData)) {
if (!isObject(updateData)) {
respond(true, errorMessages.UPDATE_DATA_MISSING, callback);
return;
}
var data = {

var data = {};
data = {
tags: updateData.tags,
customCoordinates: updateData.customCoordinates,
extensions: updateData.extensions,
webhookUrl: updateData.webhookUrl,
customMetadata: updateData.customMetadata
customMetadata: updateData.customMetadata,
};

if (updateData.publish)
data = {
...data,
publish: updateData.publish,
};

var requestOptions = {
url: "https://api.imagekit.io/v1/files/" + fileId + "/details",
method: "PATCH",
Expand All @@ -227,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 @@ -627,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
Loading

0 comments on commit a82c2ae

Please sign in to comment.