Skip to content

Commit 62c0d4d

Browse files
committed
custom metadata field APIs
1 parent 088d227 commit 62c0d4d

File tree

6 files changed

+245
-1
lines changed

6 files changed

+245
-1
lines changed

index.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@ import {
2929
FileVersionDetailsOptions,
3030
DeleteFileVersionOptions,
3131
RestoreFileVersionOptions,
32+
CreateCustomMetadataFieldOptions,
33+
CustomMetadataField,
34+
UpdateCustomMetadataFieldOptions,
3235
} from "./libs/interfaces";
3336
import { IKCallback } from "./libs/interfaces/IKCallback";
3437
import manage from "./libs/manage";
3538
import signature from "./libs/signature";
3639
import upload from "./libs/upload";
40+
import customMetadataField from "./libs/manage/custom-metadata-field";
3741
/*
3842
Implementations
3943
*/
@@ -458,6 +462,57 @@ class ImageKit {
458462
return promisify(this, manage.getBulkJobStatus)(jobId, this.options, callback);
459463
}
460464

465+
/**
466+
* Create custom metadata field
467+
*
468+
* @see {@link https://docs.imagekit.io/api-reference/custom-metadata-fields-api/create-custom-metadata-field}
469+
*
470+
* @param createCustomMetadataFieldOptions
471+
*/
472+
createCustomMetadataField(createCustomMetadataFieldOptions: CreateCustomMetadataFieldOptions): Promise<CustomMetadataField>;
473+
createCustomMetadataField(createCustomMetadataFieldOptions: CreateCustomMetadataFieldOptions, callback: IKCallback<CustomMetadataField>): Promise<CustomMetadataField>;
474+
createCustomMetadataField(createCustomMetadataFieldOptions: CreateCustomMetadataFieldOptions, callback?: IKCallback<CustomMetadataField>): void | Promise<CustomMetadataField> {
475+
return promisify<CustomMetadataField>(this, customMetadataField.create)(createCustomMetadataFieldOptions, this.options, callback);
476+
}
477+
478+
/**
479+
*Get a list of all the custom metadata fields.
480+
*
481+
* @see {@link https://docs.imagekit.io/api-reference/custom-metadata-fields-api/get-custom-metadata-field}
482+
*
483+
*/
484+
getCustomMetadataFields(): Promise<CustomMetadataField[]>;
485+
getCustomMetadataFields(callback: IKCallback<CustomMetadataField[]>): Promise<CustomMetadataField[]>;
486+
getCustomMetadataFields(callback?: IKCallback<CustomMetadataField[]>): void | Promise<CustomMetadataField[]> {
487+
return promisify<CustomMetadataField[]>(this, customMetadataField.list)(this.options, callback);
488+
}
489+
490+
/**
491+
* Update custom metadata field
492+
*
493+
* @see {@link https://docs.imagekit.io/api-reference/custom-metadata-fields-api/update-custom-metadata-field}
494+
*
495+
* @param updateCustomMetadataFieldOptions
496+
*/
497+
updateCustomMetadataField(updateCustomMetadataFieldOptions: UpdateCustomMetadataFieldOptions): Promise<CustomMetadataField>;
498+
updateCustomMetadataField(updateCustomMetadataFieldOptions: UpdateCustomMetadataFieldOptions, callback: IKCallback<CustomMetadataField>): Promise<CustomMetadataField>;
499+
updateCustomMetadataField(updateCustomMetadataFieldOptions: UpdateCustomMetadataFieldOptions, callback?: IKCallback<CustomMetadataField>): void | Promise<CustomMetadataField> {
500+
return promisify<CustomMetadataField>(this, customMetadataField.update)(updateCustomMetadataFieldOptions, this.options, callback);
501+
}
502+
503+
/**
504+
* Delete a custom metadata field
505+
*
506+
* @see {@link https://docs.imagekit.io/api-reference/custom-metadata-fields-api/delete-custom-metadata-field}
507+
*
508+
* @param fieldId
509+
*/
510+
deleteCustomMetadataField(fieldId: string): Promise<void>;
511+
deleteCustomMetadataField(fieldId: string, callback: IKCallback<void>): void;
512+
deleteCustomMetadataField(fieldId: string, callback?: IKCallback<void>): void | Promise<void> {
513+
return promisify(this, customMetadataField.deleteField)(fieldId, this.options, callback);
514+
}
515+
461516
/**
462517
* Perceptual hashing allows you to construct a hash value that uniquely identifies an input image based on an image's contents. ImageKit.io metadata API returns the pHash value of an image in the response. You can use this value to find a duplicate (or similar) image by calculating the distance between the two images' pHash value.
463518
*

libs/constants/errorMessages.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,10 @@ export default {
3131
//bulk delete errors
3232
"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." },
3333
"BULK_ADD_TAGS_INVALID": { message: "Invalid value for tags", help: "tags should be a non empty array of string like ['tag1', 'tag2']." },
34-
"BULK_AI_TAGS_INVALID": { message: "Invalid value for AITags", help: "AITags should be a non empty array of string like ['tag1', 'tag2']." }
34+
"BULK_AI_TAGS_INVALID": { message: "Invalid value for AITags", help: "AITags should be a non empty array of string like ['tag1', 'tag2']." },
35+
"CMF_NAME_MISSING": { message: "Missing name parameter for this request", help: "" },
36+
"CMF_LABEL_MISSING": { message: "Missing label parameter for this request", help: "" },
37+
"CMF_SCHEMA_MISSING": { message: "Missing schema parameter for this request", help: "" },
38+
"CMF_SCHEMA_INVALID": { message: "Invalid value for schema", help: "schema should have a mandatory type field." },
39+
"CMF_LABEL_SCHEMA_MISSING": { message: "Both label and schema is missing", help: "" },
3540
};
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
export interface CustomMetadataFieldSchema {
2+
type: string;
3+
selectOptions?: (string | number | boolean)[];
4+
defaultValue?: string | number | boolean | (string | number | boolean)[];
5+
isValueRequired?: boolean;
6+
minValue?: string | number;
7+
maxValue?: string | number;
8+
minLength?: number;
9+
maxLength?: number;
10+
}
11+
12+
export interface CustomMetadataFieldSchemaMinusType {
13+
selectOptions?: (string | number | boolean)[];
14+
defaultValue?: string | number | boolean | (string | number | boolean)[];
15+
isValueRequired?: boolean;
16+
minValue?: string | number;
17+
maxValue?: string | number;
18+
minLength?: number;
19+
maxLength?: number;
20+
}
21+
/**
22+
* Create a new custom metadata field
23+
*
24+
* @see {@link https://docs.imagekit.io/api-reference/custom-metadata-fields-api/create-custom-metadata-field}
25+
*/
26+
export interface CreateCustomMetadataFieldOptions {
27+
/**
28+
* Name of the metadata field, unique across all (deleted or not deleted) custom metadata fields.
29+
*/
30+
name: string;
31+
/**
32+
* Label of the metadata field, unique across all non deleted custom metadata fields
33+
*/
34+
label: string;
35+
/**
36+
* An object that describes the rules for the custom metadata key.
37+
*/
38+
schema: CustomMetadataFieldSchema
39+
}
40+
41+
export interface CustomMetadataField {
42+
id: string;
43+
/**
44+
* Name of the metadata field, unique across all (deleted or not deleted) custom metadata fields.
45+
*/
46+
name: string;
47+
/**
48+
* Label of the metadata field, unique across all non deleted custom metadata fields
49+
*/
50+
label: string;
51+
/**
52+
* An object that describes the rules for the custom metadata key.
53+
*/
54+
schema: CustomMetadataFieldSchema
55+
}
56+
57+
/**
58+
* Update the label or schema of an existing custom metadata field.
59+
*
60+
* @see {@link https://docs.imagekit.io/api-reference/custom-metadata-fields-api/update-custom-metadata-field}
61+
*/
62+
export interface UpdateCustomMetadataFieldOptions {
63+
/**
64+
* Label of the metadata field, unique across all non deleted custom metadata fields. This parameter is required if schema is not provided.
65+
*/
66+
label?: string;
67+
/**
68+
* An object that describes the rules for the custom metadata key. This parameter is required if label is not provided.
69+
* Note: type cannot be updated and will be ignored if sent with the schema. The schema will be validated as per the existing type.
70+
*/
71+
schema?: CustomMetadataFieldSchemaMinusType
72+
}

libs/interfaces/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { BulkDeleteFilesResponse, BulkDeleteFilesError } from "./BulkDeleteFiles
1515
import { CopyFolderOptions, CopyFolderResponse, CopyFolderError } from "./CopyFolder";
1616
import { MoveFolderOptions, MoveFolderResponse, MoveFolderError } from "./MoveFolder";
1717
import { DeleteFileVersionOptions, RestoreFileVersionOptions } from "./FileVersion"
18+
import { CreateCustomMetadataFieldOptions, CustomMetadataField, UpdateCustomMetadataFieldOptions } from "./CustomMetatadaField"
1819

1920
type FinalUrlOptions = ImageKitOptions & UrlOptions; // actual options used to construct url
2021

@@ -48,5 +49,8 @@ export {
4849
MoveFolderOptions,
4950
DeleteFileVersionOptions,
5051
RestoreFileVersionOptions,
52+
CreateCustomMetadataFieldOptions,
53+
CustomMetadataField,
54+
UpdateCustomMetadataFieldOptions,
5155
};
5256
export { IKCallback } from "./IKCallback";
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
Constants
3+
*/
4+
import errorMessages from "../constants/errorMessages";
5+
6+
/*
7+
Utils
8+
*/
9+
import respond from "../../utils/respond";
10+
import request from "../../utils/request";
11+
12+
/*
13+
Interfaces
14+
*/
15+
import { IKCallback } from "../interfaces/IKCallback";
16+
import {
17+
ImageKitOptions,
18+
CreateCustomMetadataFieldOptions,
19+
CustomMetadataField,
20+
UpdateCustomMetadataFieldOptions
21+
} from "../interfaces/";
22+
23+
const create = function (createCustomMetadataFieldOptions: CreateCustomMetadataFieldOptions, defaultOptions: ImageKitOptions, callback?: IKCallback<CustomMetadataField>) {
24+
const { name, label, schema } = createCustomMetadataFieldOptions;
25+
if (!name || !name.length) {
26+
respond(true, errorMessages.CMF_NAME_MISSING, callback);
27+
return;
28+
}
29+
30+
if (!label || !label.length) {
31+
respond(true, errorMessages.CMF_LABEL_MISSING, callback);
32+
return;
33+
}
34+
35+
if (!schema) {
36+
respond(true, errorMessages.CMF_SCHEMA_MISSING, callback);
37+
return;
38+
}
39+
40+
if (!!schema.type) {
41+
respond(true, errorMessages.CMF_SCHEMA_INVALID, callback);
42+
return;
43+
}
44+
45+
var requestOptions = {
46+
url: "https://api.imagekit.io/v1/customMetadataFields",
47+
method: "POST",
48+
json: {
49+
name,
50+
label,
51+
schema
52+
},
53+
};
54+
55+
request(requestOptions, defaultOptions, callback);
56+
};
57+
58+
const list = function (
59+
defaultOptions: ImageKitOptions,
60+
callback?: IKCallback<CustomMetadataField[]>,
61+
) {
62+
var requestOptions = {
63+
url: "https://api.imagekit.io/v1/customMetadataFields",
64+
method: "GET",
65+
};
66+
67+
request(requestOptions, defaultOptions, callback);
68+
};
69+
70+
const update = function (updateCustomMetadataFieldOptions: UpdateCustomMetadataFieldOptions, defaultOptions: ImageKitOptions, callback?: IKCallback<CustomMetadataField>) {
71+
const { label, schema } = updateCustomMetadataFieldOptions;
72+
if (!label && !schema) {
73+
respond(true, errorMessages.CMF_LABEL_SCHEMA_MISSING, callback);
74+
return;
75+
}
76+
77+
var requestBody: UpdateCustomMetadataFieldOptions = {};
78+
if (label) requestBody.label = label;
79+
if (schema) requestBody.schema = schema;
80+
81+
var requestOptions = {
82+
url: "https://api.imagekit.io/v1/customMetadataFields",
83+
method: "POST",
84+
json: requestBody
85+
};
86+
87+
request(requestOptions, defaultOptions, callback);
88+
};
89+
90+
const deleteField = function (
91+
fieldId: string,
92+
defaultOptions: ImageKitOptions,
93+
callback?: IKCallback<void>,
94+
) {
95+
var requestOptions = {
96+
url: `https://api.imagekit.io/v1/customMetadataFields/${fieldId}`,
97+
method: "DELETE",
98+
};
99+
100+
request(requestOptions, defaultOptions, callback);
101+
};
102+
103+
export default { create, list, update, deleteField };

libs/manage/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import cache from "./cache";
22
import file from "./file";
3+
import customMetadataField from "./custom-metadata-field";
34

45
export default {
56
listFiles: file.listFiles,
@@ -24,4 +25,8 @@ export default {
2425
getBulkJobStatus: file.getBulkJobStatus,
2526
purgeCache: cache.purgeCache,
2627
getPurgeCacheStatus: cache.getPurgeCacheStatus,
28+
createCustomMetadataField: customMetadataField.create,
29+
getCustomMetadataFields: customMetadataField.list,
30+
updateCustomMetadataField: customMetadataField.update,
31+
deleteCustomMetadataField: customMetadataField.deleteField,
2732
};

0 commit comments

Comments
 (0)