Skip to content

Commit

Permalink
updated types
Browse files Browse the repository at this point in the history
  • Loading branch information
ANKUR DWIVEDI authored and ANKUR DWIVEDI committed Feb 16, 2024
1 parent 035509e commit 0184748
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ ImageKit is a complete media storage, optimization, and transformation solution
* In version 4.0.0, we've removed the old overlay syntax parameters for transformations, such as `oi`, `ot`, `obg`, and [more](https://docs.imagekit.io/features/image-transformations/overlay). These parameters are deprecated and will start returning errors when used in URLs. Please migrate to the new layers syntax that supports overlay nesting, provides better positional control, and allows more transformations at the layer level. You can start with [examples](https://docs.imagekit.io/features/image-transformations/overlay-using-layers#examples) to learn quickly.
* You can migrate to the new layers syntax using the `raw` transformation parameter.

2. IKContext props update

* In version 4.0.0, we've narrowed down the options you can use with `IKContext`. Now, you can only use `urlEndpoint`, `publicKey`, `authenticator`, and `transformationPosition` as props.

### Upgrading from 2.x to 3.x version
3.x version has breaking changes as listed below.
* In version 3.0.0, we have deprecated the use of the `authenticationEndpoint` parameter. Instead, the SDK now introduces a new parameter named `authenticator`. This parameter expects an asynchronous function that resolves with an object containing the necessary security parameters i.e `signature`, `token`, and `expire`.
Expand Down
15 changes: 2 additions & 13 deletions src/components/IKContext/props.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import ImageKit from 'imagekit-javascript';
import PropTypes, { InferProps } from 'prop-types';
import IK_IMAGE_PROPS from "../IKImage/props";
import IK_UPLOAD_PROPS from "../IKUpload/props";
import IK_VIDEO_PROPS from "../IKUpload/props";

const Props = {
publicKey: PropTypes.string,
Expand All @@ -12,19 +9,11 @@ const Props = {

export const IKContextCombinedProps = {
...Props,
...IK_IMAGE_PROPS,
...IK_UPLOAD_PROPS,
...IK_VIDEO_PROPS,
transformationPosition: PropTypes.oneOf(['path', 'query']),
ikClient: PropTypes.instanceOf(ImageKit),
};

type IKContextCombinedPropsOmit = Omit<typeof IKContextCombinedProps, "src" | "path" | "transformation"> & {
src?: string;
path?: string;
transformation?: Array<string>;
};

export type IKContextCombinedProps = InferProps<IKContextCombinedPropsOmit> & {
export type IKContextCombinedProps = InferProps<typeof IKContextCombinedProps> & {
urlEndpoint?: string;
};

Expand Down
11 changes: 2 additions & 9 deletions src/components/IKUpload/index.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import React, { forwardRef, useContext, useEffect, useState } from 'react';
import COMMON_PROPS, { IKContextBaseProps } from "../IKContext/props";
import IK_UPLOAD_PROPS, { IKUploadProps } from "./props";
import { IKContextBaseProps } from "../IKContext/props";
import { IKUploadProps } from "./props";
import { ImageKitContext } from '../IKContext';
import useImageKitComponent from '../ImageKitComponent';

const PROP_TYPES = {
...COMMON_PROPS,
...IK_UPLOAD_PROPS,
};

type IKUploadState = {
xhr?: XMLHttpRequest;
};
Expand Down Expand Up @@ -213,6 +208,4 @@ const IKUpload = forwardRef<HTMLInputElement, IKUploadProps & IKContextBaseProps
);
});

IKUpload.propTypes = { ...PROP_TYPES };

export default IKUpload;
20 changes: 19 additions & 1 deletion src/components/IKUpload/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ type TransformationType = {
post?: PostTransformation[];
};

interface BgRemoval {
name: string
options: {
bg_color?: string
bg_image_url?: string
add_shadow: boolean
semitransparency: boolean
}
}

interface AutoTag {
name: string
maxTags: number
minConfidence: number
}

export type Extension = (BgRemoval | AutoTag)[];

const Props = {
fileName: PropTypes.string,
tags: PropTypes.arrayOf(PropTypes.string.isRequired),
Expand Down Expand Up @@ -56,7 +74,7 @@ export type IKUploadProps = InferProps<typeof Props> & {
isPrivateFile?: boolean;
customCoordinates?: string;
responseFields?: Array<string>;
extensions?: object[];
extensions?: Extension;
webhookUrl?: string;
overwriteFile?: boolean,
overwriteAITags?: boolean,
Expand Down
31 changes: 22 additions & 9 deletions src/utils/Utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,28 @@ export const areObjectsDifferent = <T>(prevProps: T, newProps: T, propsAffecting
type GetSrcReturnType = {originalSrc: string; lqipSrc?: string;};

export const getSrc = ({ urlEndpoint, lqip, src, path, transformation, transformationPosition, queryParameters }: IKImageBaseProps & IKVideoBaseProps & IKContextBaseProps,
ikClient: ImageKit, contextOptions: IKContextCombinedProps): GetSrcReturnType => {
let options: UrlOptions = {
urlEndpoint: urlEndpoint || contextOptions.urlEndpoint,
src: src || contextOptions.src || undefined,
path: path || contextOptions.path || undefined,
transformation: transformation || contextOptions.transformation,
transformationPosition: ((transformationPosition || contextOptions.transformationPosition || undefined) as TransformationPosition),
queryParameters: queryParameters || contextOptions.queryParameters || {}
};
ikClient: ImageKit, contextOptions: IKContextCombinedProps): GetSrcReturnType => {

let options: UrlOptions;
if (src) {
options = {
urlEndpoint: urlEndpoint || contextOptions.urlEndpoint,
src,
transformation: transformation || undefined,
transformationPosition: ((transformationPosition || contextOptions.transformationPosition || undefined) as TransformationPosition),
queryParameters: queryParameters || {},
};
} else if (path) {
options = {
urlEndpoint: urlEndpoint || contextOptions.urlEndpoint,
path,
transformation: transformation || undefined,
transformationPosition: ((transformationPosition || contextOptions.transformationPosition || undefined) as TransformationPosition),
queryParameters: queryParameters || {},
};
} else{
throw new Error("Either src and path is required");
}

const result: GetSrcReturnType = {originalSrc: ikClient.url(options)};

Expand Down

0 comments on commit 0184748

Please sign in to comment.