Skip to content

Commit 9b9bc59

Browse files
authored
Merge pull request #4 from imagekit-developer/test-case
Test cases & Call backs for upload success & failure
2 parents dbb9789 + 4af3519 commit 9b9bc59

27 files changed

+16170
-37692
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ jobs:
1919
node-version: ${{ matrix.node-version }}
2020
- name: npm install, build, and test
2121
run: |
22-
npm i -g yarn
23-
yarn install
24-
yarn test:ci
22+
npm install
23+
npm run test:ci
2524
env:
2625
CI: true
2726
REACT_APP_PUBLIC_KEY: ${{ secrets.ik_public_key }}

.github/workflows/npmpublish.yml

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,46 @@ on:
44
release:
55
types: [published]
66

7-
87
jobs:
98
build:
10-
119
runs-on: ubuntu-latest
1210

1311
strategy:
1412
matrix:
1513
node-version: [8.x, 10.x, 12.x]
1614

1715
steps:
18-
- uses: actions/checkout@v1
19-
- name: Use Node.js ${{ matrix.node-version }}
20-
uses: actions/setup-node@v1
21-
with:
22-
node-version: ${{ matrix.node-version }}
23-
- name: npm install, build, and test
24-
run: |
25-
npm i -g yarn
26-
yarn
27-
yarn test:ci
28-
yarn build
29-
env:
30-
CI: true
31-
REACT_APP_PUBLIC_KEY: ${{ secrets.ik_public_key }}
32-
REACT_APP_PRIVATE_KEY: ${{ secrets.ik_private_key }}
33-
REACT_APP_URL_ENDPOINT: ${{ secrets.ik_url_endopint }}
16+
- uses: actions/checkout@v1
17+
- name: Use Node.js ${{ matrix.node-version }}
18+
uses: actions/setup-node@v1
19+
with:
20+
node-version: ${{ matrix.node-version }}
21+
- name: npm install, build, and test
22+
run: |
23+
npm i
24+
npm run test:ci
25+
npm run build
26+
env:
27+
CI: true
28+
REACT_APP_PUBLIC_KEY: ${{ secrets.ik_public_key }}
29+
REACT_APP_PRIVATE_KEY: ${{ secrets.ik_private_key }}
30+
REACT_APP_URL_ENDPOINT: ${{ secrets.ik_url_endopint }}
3431

3532
publish:
3633
needs: build
3734
runs-on: ubuntu-latest
3835
steps:
39-
- uses: actions/checkout@v1
40-
- uses: actions/setup-node@v1
41-
with:
42-
node-version: 12
43-
registry-url: https://registry.npmjs.org/
44-
- name: yarn publish
45-
run: |
46-
npm i -g yarn
47-
yarn
48-
yarn build
49-
yarn config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
50-
yarn publish
51-
env:
52-
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
53-
CI: true
36+
- uses: actions/checkout@v1
37+
- uses: actions/setup-node@v1
38+
with:
39+
node-version: 12
40+
registry-url: https://registry.npmjs.org/
41+
- name: npm publish
42+
run: |
43+
npm i
44+
npm run build
45+
npm config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
46+
npm publish
47+
env:
48+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
49+
CI: true

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@ node_modules
33
.vscode
44
lib
55
dist
6-
src/test/__snapshots__

DEVELOPMENT.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Building package
2+
3+
Execute following command from the root folder to build the library. This creates a package in `dist/imagekitio-react.js` folder.
4+
```sh
5+
npm install # run for first time
6+
npm run build
7+
```
8+
9+
## Running test cases
10+
11+
The designated directory for tests is `/src/test` folder. All tests will be run against the assertion present in the `/src/test/__snapshot__` folder. To create this file you need to just run below command just once. Any update in the tests can be updated to by pressing `u` while the test environment is running.
12+
13+
Execute following command from the root folder to start testing.
14+
```sh
15+
npm run test
16+
```
17+
18+
## Running sample frontend react app
19+
20+
Please refer to the sample app `Readme.md` for details.
21+
22+
## Running sample backend server
23+
24+
Please refer to the sample app `Readme.md` for details.

README.md

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
66
[![Twitter Follow](https://img.shields.io/twitter/follow/imagekitio?label=Follow&style=social)](https://twitter.com/ImagekitIo)
77

8-
React SDK for [ImageKit.io](https://imagekit.io) which implements client-side upload and URL generation for use inside a react application.
8+
React SDK for [ImageKit.io](https://imagekit.io), which implements client-side upload and URL generation for use inside a react application.
99

10-
ImageKit is a complete image optimization and transformation solution that comes with an [image CDN](https://imagekit.io/features/imagekit-infrastructure) and media storage. It can be integrated with your existing infrastructure - storages like AWS S3, web servers, your CDN and custom domain names, allowing you to deliver optimized images in minutes with minimal code changes.
10+
ImageKit is a complete image optimization and transformation solution that comes with an [image CDN](https://imagekit.io/features/imagekit-infrastructure) and media storage. It can be integrated with your existing infrastructure - storages like AWS S3, web servers, your CDN, and custom domain names, allowing you to deliver optimized images in minutes with minimal code changes.
1111

1212
## Installation
1313

@@ -22,63 +22,62 @@ Include the components in your code:
2222
The library includes 3 Components:
2323
* [IKContext](#IKContext)
2424

25-
* [IKImage](#IKImage)
25+
* [IKImage - URL generation](#IKImage)
2626

27-
* [IKUpload](#file-upload)
27+
* [IKUpload - File upload](#file-upload)
2828

2929
### IKContext
3030

3131
In order to use the SDK, you need to provide it with a few configuration parameters. The configuration parameters can be applied directly to the `IKImage` component or using an `IKContext` component. example:
3232

3333
```js
34-
<IKContext
35-
publicKey="your_public_api_key"
36-
urlEndpoint="<https://ik.imagekit.io/your_imagekit_id>"
37-
transformationPosition="path"
38-
authenticationEndpoint="<http://www.yourserver.com/auth">
39-
</IKContext>
34+
<IKContext
35+
publicKey="your_public_api_key"
36+
urlEndpoint="<https://ik.imagekit.io/your_imagekit_id>"
37+
transformationPosition="path"
38+
authenticationEndpoint="<http://www.yourserver.com/auth">
39+
</IKContext>
4040
```
4141

4242
`publicKey` and `urlEndpoint` are mandatory parameters for SDK initialization.
4343
`authenticationEndpoint` is essential if you want to use the SDK for client-side uploads.
44-
`transformationPosition` is optional. The default value for the parametere is `path`. Acceptable values are `path` & `query`
44+
`transformationPosition` is optional. The default value for this parameter is `path`. Acceptable values are `path` & `query`
4545

46-
_Note: Do not include your Private Key in any client side code, including this SDK or its initialization. If you pass the `privateKey` parameter while initializing this SDK, it throws an error_
46+
_Note: Do not include your Private Key in any client-side code, including this SDK or its initialization. If you pass the `privateKey` parameter while initializing this SDK, it throws an error_
4747

48-
### IKImage
48+
### IKImage - URL generation
4949

50-
The IKImage component component defines a ImageKit Image tag. example usage:
50+
The IKImage component component defines an ImageKit Image tag. example usage:
5151

5252
#### Using image path and image hostname or endpoint
5353

5454
```js
55-
<IKContext publicKey="your_public_api_key" urlEndpoint="https://ik.imagekit.io/your_imagekit_id/endpoint/" >
56-
<IKImage path="/default-image.jpg" transformation={[{
57-
"height": "300",
58-
"width": "400"
59-
}]} />
60-
</IKContext>
61-
62-
```
55+
<IKContext publicKey="your_public_api_key" urlEndpoint="https://ik.imagekit.io/your_imagekit_id/endpoint/" >
56+
<IKImage path="/default-image.jpg" transformation={[{
57+
"height": "300",
58+
"width": "400"
59+
}]} />
60+
</IKContext>
61+
```
6362
#### Using full image URL
6463

65-
```js
66-
<IKImage
67-
src="<full_image_url_from_db>"
68-
transformation={[{
69-
"height": "300",
70-
"width": "400"
71-
}]}
72-
/>
73-
```
64+
```js
65+
<IKImage
66+
src="<full_image_url_from_db>"
67+
transformation={[{
68+
"height": "300",
69+
"width": "400"
70+
}]}
71+
/>
72+
```
7473

7574
`src` is the complete URL that is already mapped to ImageKit.
7675
`path` is the location of the image in the ImageKit cloud. `urlEndpoint` + `path` makes the complete url.
7776
`transformations` is optional. The transformations to be applied to a given image. It is declared in the form of an array of objects, where each object specifies the transformation you need. The values are mentioned below.
7877

7978
#### List of supported transformations
8079

81-
The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/imagekit-docs/image-transformations). The SDK gives a name to each transformation parameter, making the code simpler and readable. If a transformation is supported in ImageKit, but a name for it cannot be found in the table below, then use the transformation code from ImageKit docs as the name when using in the `url` function.
80+
The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/features/image-transformations). The SDK gives a name to each transformation parameter, making the code simpler and readable. If a transformation is supported in ImageKit, but a name for it cannot be found in the table below, then use the transformation code from ImageKit docs as the name when using in the `url` function.
8281

8382
| Supported Transformation Name | Translates to parameter |
8483
| ----------------------------- | ----------------------- |
@@ -137,7 +136,7 @@ const transformations = [{
137136
The above image will apply transformation of width = 90 and height = 180 on the image. Since some transformatinos are destructive you might want to control the order in which the transforms are applied.
138137

139138
##### Chained Transforms
140-
Chained transforms make it easy to specify the order the transform are applied. example:
139+
Chained transforms make it easy to specify the order in which transformations are applied. example:
141140

142141
```js
143142
const transformations = [
@@ -150,7 +149,7 @@ const transformations = [
150149
}
151150
];
152151
```
153-
In the above case, rotation will be performed first and resizing according to width and aspect ratio will be performed afterwards.
152+
In the above case, the rotation will be performed first, and resizing according to width and aspect ratio will be performed afterward.
154153

155154
#### Low Quality Image Placeholders (LQIP) for images
156155
The SDK supports automatic support for LQIP for your images, if you set lqip to true in the image component. example:
@@ -162,30 +161,53 @@ The SDK supports automatic support for LQIP for your images, if you set lqip to
162161
`quality` decided the quaility of placeholder image. It can be any numeric value, a low number means low quality, and high number means high quality.
163162

164163
##### How does the lqip work?
165-
The component tries to keep the it simple, it loads a lower quality image using the quality parameter to load a lower quality image, which is then replaced with the actual quality image later.
164+
The component tries to keep it simple. It loads a lower quality image using the quality parameter to load a lower quality image, which is then replaced with the actual quality image later.
166165

167-
#### File Upload
166+
### IKUpload - File Upload
168167
The SDK provides a simple Component to upload files to the ImageKit Media Library. It accepts `fileName` parameter as a prop. The file parameter is provided as an input from the user.
169168

170-
Also make sure that you have specified `authenticationEndpoint` during SDK initialization. The SDK makes an HTTP GET request to this endpoint and expects a JSON response with three fields i.e. `signature`, `token` and `expire`.
169+
Also, make sure that you have specified `authenticationEndpoint` during SDK initialization. The SDK makes an HTTP GET request to this endpoint and expects a JSON response with three fields i.e. `signature`, `token` and `expire`.
171170

172171
[Learn how to implement authenticationEndpoint](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload#how-to-implement-authenticationendpoint-endpoint) on your server.
173172

174173
An example of this server is provided in the samples folder of the SDK.
175174

176175
Sample Usage
177176
```js
178-
<IKContext
179-
publicKey="your_public_api_key"
180-
urlEndpoint="<https://ik.imagekit.io/your_imagekit_id>"
181-
authenticationEndpoint="<http://www.yourserver.com/auth">
182-
<IKUpload fileName="my-upload" />
183-
</IKContext>
177+
<IKContext
178+
publicKey="your_public_api_key"
179+
urlEndpoint="<https://ik.imagekit.io/your_imagekit_id>"
180+
authenticationEndpoint="<http://www.yourserver.com/auth">
181+
<IKUpload fileName="my-upload" />
182+
</IKContext>
184183
```
185184

186185
`IKUpload` component accepts all the parameters supported by the [ImageKit Upload API](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload#request-structure-multipart-form-data) as props e.g. `tags`, `useUniqueFileName`, `folder` etc.
187186

188-
#### Error Handling
187+
You can use `onSuccess` and `onError` callbacks to handle success and failure, respectively. You can pass your custom functions to handle the response from API.
188+
189+
```js
190+
const onError = err => {
191+
console.log("Error");
192+
console.log(err);
193+
};
194+
195+
const onSuccess = res => {
196+
console.log("Success");
197+
console.log(res);
198+
};
199+
200+
<IKContext
201+
publicKey="your_public_api_key"
202+
urlEndpoint="<https://ik.imagekit.io/your_imagekit_id>"
203+
authenticationEndpoint="<http://www.yourserver.com/auth">
204+
<IKUpload fileName="custom_file_name" onError={onError} onSuccess={onSuccess} />
205+
</IKContext>
206+
```
207+
## Demo Application
208+
The fastest way to get started is by running the demo application. You can run the code locally. The source code is in samples/sample-app. For the instructions in [readme.md](https://github.com/imagekit-developer/imagekit-react/blob/test-case/samples/sample-app/README.md) file within [samples/sample-app](https://github.com/imagekit-developer/imagekit-react/tree/test-case/samples/sample-app) folder.
209+
210+
## Error Handling
189211
We are using Error Boundaries to handle errors in the UI. `ErrorBoundary` is used to handle errors anywhere in their child component tree, log those errors, and display a fallback UI instead of the component tree that crashed. example:
190212

191213
```js
@@ -196,3 +218,15 @@ We are using Error Boundaries to handle errors in the UI. `ErrorBoundary` is use
196218
}]} />
197219
</ErrorBoundary>
198220
```
221+
222+
## Support
223+
224+
For any feedback or to report any issues or general implementation support please reach out to [[email protected]](mailto:[email protected])
225+
226+
## Links
227+
* [Documentation](https://docs.imagekit.io)
228+
* [Main website](https://imagekit.io)
229+
230+
## License
231+
232+
Released under the MIT license.

0 commit comments

Comments
 (0)