Skip to content

Commit ed16142

Browse files
committed
refactor: remove IKResponse interface and update related imports; introduce SrcOptions interface for upload functionality
1 parent bbe982b commit ed16142

File tree

12 files changed

+137
-150
lines changed

12 files changed

+137
-150
lines changed

src/index.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { UploadOptions, UploadResponse, UrlOptions } from "./interfaces";
2-
import { upload } from "./upload";
3-
import { buildURL, generateTransformationString } from "./url";
4-
5-
export { buildURL, generateTransformationString, upload };
1+
import { SrcOptions, Transformation, UploadOptions, UploadResponse } from "./interfaces";
2+
import { ImageKitAbortError, ImageKitInvalidRequestError, ImageKitServerError, ImageKitUploadNetworkError, upload } from "./upload";
3+
import { buildSrc, buildTransformationString } from "./url";
64

5+
export { buildSrc, buildTransformationString, upload, ImageKitInvalidRequestError, ImageKitAbortError, ImageKitServerError, ImageKitUploadNetworkError };
76
export type {
8-
UrlOptions,
7+
Transformation,
8+
SrcOptions,
99
UploadOptions,
1010
UploadResponse
1111
};
12+
13+

src/interfaces/IKResponse.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Transformation } from "./Transformation";
22
import { TransformationPosition } from ".";
33

4-
export interface UrlOptions {
4+
export interface SrcOptions {
55
/**
66
* Accepts relative or absolute path of the resource. If relative path is provided, it is appended to the `urlEndpoint`. If absolute path is provided, `urlEndpoint` is ignored.
77
*/

src/interfaces/UploadOptions.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ interface Transformation {
2121
post?: PostTransformation[]
2222
}
2323
/**
24-
* Options used when uploading a file
25-
*
26-
* {@link https://imagekit.io/docs/api-reference/upload-file/upload-file#Request}
24+
* Options used when uploading a file. Checkout [upload docs](https://imagekit.io/docs/api-reference/upload-file/upload-file#Request) for more details.
2725
*/
2826
export interface UploadOptions {
2927
/**

src/interfaces/UploadResponse.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* {@link https://imagekit.io/docs/api-reference/digital-asset-management-dam/list-and-search-assets}
88
*/
9-
export type FileType = "all" | "image" | "non-image";
9+
type FileType = "all" | "image" | "non-image";
1010

1111
/**
1212
* Metadata object structure
@@ -23,7 +23,7 @@ export type FileType = "all" | "image" | "non-image";
2323
*
2424
* Perceptual hashing allows you to construct a hash value that uniquely identifies an input image based on the image's contents. It is different from cryptographic hash functions like MD5 and SHA1. pHash provides similar hash value after minor distortions, like small rotations, blurring, and compression in the image.
2525
*/
26-
export interface Metadata {
26+
interface Metadata {
2727
height: number;
2828
width: number;
2929
size: number;

src/interfaces/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { TransformationPosition } from "./Transformation";
1+
import { Transformation, TransformationPosition } from "./Transformation";
22
import { UploadOptions } from "./UploadOptions";
3-
import { FileType, UploadResponse } from "./UploadResponse";
4-
import { UrlOptions } from "./UrlOptions";
3+
import { ResponseMetadata, UploadResponse } from "./UploadResponse";
4+
import { SrcOptions } from "./SrcOptions";
55

6-
export type { FileType, TransformationPosition, UploadOptions, UploadResponse, UrlOptions };
6+
export type { ResponseMetadata, Transformation, TransformationPosition, UploadOptions, UploadResponse, SrcOptions };
77

src/upload.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import errorMessages from "./constants/errorMessages";
2-
import { UploadOptions } from "./interfaces/UploadOptions";
3-
import { ResponseMetadata, UploadResponse } from "./interfaces/UploadResponse";
4-
2+
import { ResponseMetadata, UploadOptions, UploadResponse } from "./interfaces";
53

64
export class ImageKitInvalidRequestError extends Error {
75
/**
@@ -47,12 +45,12 @@ export class ImageKitServerError extends Error {
4745
}
4846

4947
/**
50-
* Uploads a file with the given upload options.
48+
* Uploads a file to ImageKit with the given upload options.
5149
*
52-
* @throws {ImageKitInvalidRequestError} If the request is invalid.
53-
* @throws {ImageKitAbortError} If the request is aborted.
54-
* @throws {ImageKitUploadNetworkError} If there is a network error.
55-
* @throws {ImageKitServerError} If there is a server error.
50+
* @throws {@link ImageKitInvalidRequestError} If the request is invalid.
51+
* @throws {@link ImageKitAbortError} If the request is aborted.
52+
* @throws {@link ImageKitUploadNetworkError} If there is a network error.
53+
* @throws {@link ImageKitServerError} If there is a server error.
5654
*
5755
* @param {UploadOptions} uploadOptions - The options for uploading the file.
5856
* @returns A Promise resolving to a successful {@link UploadResponse}

src/url.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UrlOptions } from "./interfaces";
1+
import { SrcOptions } from "./interfaces";
22
import { ImageOverlay, SolidColorOverlay, SubtitleOverlay, TextOverlay, Transformation, VideoOverlay } from "./interfaces/Transformation";
33
import transformationUtils, { safeBtoa } from "./utils/transformation";
44
const TRANSFORMATION_PARAMETER = "tr";
@@ -25,7 +25,7 @@ function pathJoin(parts: string[], sep?: string) {
2525
return parts.join(separator).replace(replace, separator);
2626
}
2727

28-
export const buildURL = (opts: UrlOptions) => {
28+
export const buildSrc = (opts: SrcOptions) => {
2929
opts.urlEndpoint = opts.urlEndpoint || "";
3030
opts.src = opts.src || "";
3131
opts.transformationPosition = opts.transformationPosition || "query";
@@ -55,7 +55,7 @@ export const buildURL = (opts: UrlOptions) => {
5555
urlObj.searchParams.append(i, String(opts.queryParameters[i]));
5656
}
5757

58-
var transformationString = generateTransformationString(opts.transformation);
58+
var transformationString = buildTransformationString(opts.transformation);
5959

6060
if (transformationString && transformationString.length) {
6161
if (!transformationUtils.addAsQueryParameter(opts) && !isSrcParameterUsedForURL) {
@@ -209,7 +209,7 @@ function processOverlay(overlay: Transformation["overlay"]): string | undefined
209209
entries.push(`ldu-${duration}`);
210210
}
211211

212-
const transformationString = generateTransformationString(transformation);
212+
const transformationString = buildTransformationString(transformation);
213213

214214
if (transformationString && transformationString.trim() !== "") entries.push(transformationString);
215215

@@ -218,7 +218,7 @@ function processOverlay(overlay: Transformation["overlay"]): string | undefined
218218
return entries.join(transformationUtils.getTransformDelimiter());
219219
}
220220

221-
export const generateTransformationString = function (transformation: Transformation[] | undefined) {
221+
export const buildTransformationString = function (transformation: Transformation[] | undefined) {
222222
if (!Array.isArray(transformation)) {
223223
return "";
224224
}

src/utils/transformation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import supportedTransforms from "../constants/supportedTransforms";
2-
import { TransformationPosition, UrlOptions } from "../interfaces";
2+
import { TransformationPosition, SrcOptions } from "../interfaces";
33

44
const QUERY_TRANSFORMATION_POSITION: TransformationPosition = "query";
55
const PATH_TRANSFORMATION_POSITION: TransformationPosition = "path";
@@ -10,7 +10,7 @@ const TRANSFORM_DELIMITER: string = ",";
1010
const TRANSFORM_KEY_VALUE_DELIMITER: string = "-";
1111

1212
export default {
13-
addAsQueryParameter: (options: UrlOptions) => {
13+
addAsQueryParameter: (options: SrcOptions) => {
1414
return options.transformationPosition === QUERY_TRANSFORMATION_POSITION;
1515
},
1616
getTransformKey: function (transform: string) {

test/upload.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ const chai = require("chai");
22
const sinon = require("sinon");
33
const expect = chai.expect;
44
import 'regenerator-runtime/runtime';
5-
import { upload } from "../src/index";
65
import {
76
ImageKitAbortError,
87
ImageKitInvalidRequestError,
98
ImageKitServerError,
10-
ImageKitUploadNetworkError
11-
} from '../src/upload';
9+
ImageKitUploadNetworkError, upload
10+
} from "../src/index";
1211

1312
var requests, server;
1413

0 commit comments

Comments
 (0)