Skip to content

Commit

Permalink
Merge pull request #161 from imagekit-developer/add-checks-parm
Browse files Browse the repository at this point in the history
Add checks parm
  • Loading branch information
imagekitio authored Jul 22, 2024
2 parents 0b30679 + 4283090 commit 5c98ae2
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ import { IKImage, IKVideo, IKContext, IKUpload } from 'imagekitio-react'
}
]
}}
checks={`"file.size" < "1mb"`} // To run server side checks before uploading files. Notice the quotes around file.size and 1mb.
/>
</IKContext>
```
Expand Down Expand Up @@ -620,12 +621,13 @@ The SDK provides the `IKUpload` component to upload files to the [ImageKit Media
| onUploadStart | Function callback | Optional. Called before the upload is started. The first and only argument is the HTML input's change event |
| onUploadProgress | Function callback | Optional. Called while an upload is in progress. The first and only argument is the ProgressEvent |
| validateFile | Function callback | Optional. This function accepts the `File` object as an argument and exoects a Boolean return value. This is called before the upload is started to run custom validation. The first and only argument is the file selected for upload. If the callback returns `true`, the upload is allowed to continue. But, if it returns `false`, the upload is not done |
| overrideParameters | Function callback | Optional. This function accepts the `File` object as an argument and should return a JSON value, e.g., `{fileName: "new-file-name.jpg"}.` Use this to programmatically override `fileName`, `useUniqueFileName`, `tags`, `folder`, `isPrivateFile`, `customCoordinates`, `extensions`, `webhookUrl`, `overwriteFile`, `overwriteAITags`, `overwriteTags`, `overwriteCustomMetadata`, `customMetadata`, and `transformation` parameters. |
| overrideParameters | Function callback | Optional. This function accepts the `File` object as an argument and should return a JSON value, e.g., `{fileName: "new-file-name.jpg"}.` Use this to programmatically override `fileName`, `useUniqueFileName`, `tags`, `folder`, `isPrivateFile`, `customCoordinates`, `extensions`, `webhookUrl`, `overwriteFile`, `overwriteAITags`, `overwriteTags`, `overwriteCustomMetadata`, `customMetadata`, `transformation`, and `checks` parameters. |
| onSuccess | Function callback | Optional. Called if the upload is successful. The first and only argument is the response JSON from the upload API. The request-id, response headers, and HTTP status code are also accessible using the $ResponseMetadata key that is exposed from the [javascript sdk](https://github.com/imagekit-developer/imagekit-javascript#access-request-id-other-response-headers-and-http-status-code) |
| onError | Function callback | Optional. Called if upload results in an error. The first and only argument is the error received from the upload API |
| urlEndpoint | String | Optional. If not specified, the URL-endpoint specified in the parent `IKContext` component is used. For example, https://ik.imagekit.io/your_imagekit_id/endpoint/ |
| publicKey | String | Optional. If not specified, the `publicKey` specified in the parent `IKContext` component is used. |
| authenticator | ()=>Promise<{signature:string,token:string,expiry:number}> | Optional. If not specified, the `authenticator` specified in the parent `IKContext` component is used. |
| checks | String | Optional. Run server-side checks before uploading files. For example, `"file.size" < "1mb"` will check if the file size is less than 1 MB. Check [Upload API docs](https://imagekit.io/docs/api-reference/upload-file/upload-file#upload-api-checks) to learn more. Notice the quotes around `file.size` and `1mb`; otherwise, you will get an error `Your request contains invalid syntax for the checks parameter.` |


> Make sure that you have specified `authenticator` and `publicKey` in `IKUpload` or in the parent `IKContext` component as a prop. The authenticator expects an asynchronous function that resolves with an object containing the necessary security parameters i.e `signature`, `token`, and `expire`.
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imagekitio-react",
"version": "4.1.0",
"version": "4.2.0",
"description": "React SDK for ImageKit.io which implements client-side upload and URL generation for use inside a react application.",
"scripts": {
"build:js": "rollup -c",
Expand Down Expand Up @@ -80,7 +80,7 @@
"typescript": "^4.8.2"
},
"dependencies": {
"imagekit-javascript": "^3.0.0",
"imagekit-javascript": "^3.0.2",
"prop-types": "^15.7.2"
},
"peerDependencies": {
Expand Down
2 changes: 2 additions & 0 deletions src/components/IKUpload/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const IKUpload = forwardRef<HTMLInputElement, IKUploadProps & IKContextBaseProps
extensions,
customMetadata,
transformation,
checks,
overrideParameters,
...restProps
} = props;
Expand Down Expand Up @@ -151,6 +152,7 @@ const IKUpload = forwardRef<HTMLInputElement, IKUploadProps & IKContextBaseProps
token: '',
xhr,
transformation: overrideValues.transformation || transformation,
checks: overrideValues.checks || checks
};

const authPromise = authenticator();
Expand Down
3 changes: 3 additions & 0 deletions src/components/IKUpload/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const Props = {
ref: PropTypes.any,
transformation: PropTypes.object,
overrideParameters: PropTypes.func,
checks: PropTypes.string
}

export type OverrideValues = {
Expand All @@ -88,6 +89,7 @@ export type OverrideValues = {
overwriteCustomMetadata?: IKUploadProps['overwriteCustomMetadata'];
customMetadata?: IKUploadProps['customMetadata'];
transformation?: IKUploadProps['transformation'];
checks?: IKUploadProps['checks'];
};

export type IKUploadProps = Omit<InferProps<typeof Props>, "customMetadata" | "transformation"> & {
Expand All @@ -111,6 +113,7 @@ export type IKUploadProps = Omit<InferProps<typeof Props>, "customMetadata" | "t
validateFile?: (file: File) => boolean;
transformation?: TransformationType;
overrideParameters?: (file: File) => OverrideValues;
checks?: string
} & React.InputHTMLAttributes<HTMLInputElement>;

export default Props;

0 comments on commit 5c98ae2

Please sign in to comment.