Skip to content

Commit b533393

Browse files
authored
Merge pull request #4 from imagekit-developer/sampleApp_Tests
Sample app and test cases
2 parents 895daee + 27cbbae commit b533393

File tree

18 files changed

+2642
-77
lines changed

18 files changed

+2642
-77
lines changed

.github/workflows/nodejs.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Node CI
2+
3+
on: [push]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
node-version: [10.x, 12.x]
13+
14+
steps:
15+
- uses: actions/checkout@v1
16+
- name: Use Node.js ${{ matrix.node-version }}
17+
uses: actions/setup-node@v1
18+
with:
19+
node-version: ${{ matrix.node-version }}
20+
- name: build and test
21+
run: |
22+
npm i -g yarn
23+
yarn build
24+
yarn test
25+
env:
26+
CI: true

.github/workflows/npmpublish.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Publish
2+
3+
on:
4+
release:
5+
types: [published]
6+
7+
8+
jobs:
9+
build:
10+
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
matrix:
15+
node-version: [10.x, 12.x]
16+
17+
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: build and test
24+
run: |
25+
npm i -g yarn
26+
yarn build
27+
yarn test
28+
env:
29+
CI: true
30+
31+
publish:
32+
needs: build
33+
runs-on: ubuntu-latest
34+
steps:
35+
- uses: actions/checkout@v1
36+
- uses: actions/setup-node@v1
37+
with:
38+
node-version: 12
39+
registry-url: https://registry.npmjs.org/
40+
- name: yarn publish
41+
run: |
42+
npm i -g yarn
43+
yarn config set //registry.npmjs.org/:_authToken=$NODE_AUTH_TOKEN
44+
yarn publish
45+
env:
46+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
47+
CI: true

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
node_modules/*
2+
samples/sample-app/node_modules/*
3+
.vscode
4+
dist
5+
.DS_Store

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,21 @@ If the upload succeed, `err` will be `null` and the `result` will be the same as
241241
If the upload fails, `err` will be the same as what is received from ImageKit's servers and the `result` will be null.
242242

243243

244+
### Demo Application
245+
246+
The fastest way to get started is running the demo application. You can run the code locally. The source code is in [samples/sample-app](https://github.com/imagekit-developer/imagekit-javascript/tree/master/samples/sample-app).
247+
248+
To run it:
249+
250+
```
251+
git clone https://github.com/imagekit-developer/imagekit-javascript.git
252+
253+
cd imagekit-javascript
254+
```
255+
256+
Find the file `.env` in the directory `samples/sample-app` and fill in your `PRIVATE_KEY`, `PUBLIC_KEY` and `URL_ENDPOINT` from your [imageKit dashboard](https://imagekit.io/dashboard#developers). The just run:
257+
258+
```
259+
yarn startSampleApp
260+
```
261+

dist/imagekit.js

Lines changed: 105 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ var isObject = require('lodash/isObject');
125125
*/
126126
var errorMessages = require("../../constants/errorMessages");
127127
var respond = require("../../utils/respond");
128-
var request = require("../../utils/request");
128+
var request = require("../../utils/request").request;
129129

130130
module.exports = function(uploadOptions, defaultOptions, callback) {
131131
if(!isObject(uploadOptions)) {
@@ -3684,45 +3684,103 @@ module.exports = {
36843684
},{}],65:[function(require,module,exports){
36853685

36863686

3687-
module.exports = function(formData, defaultOptions, callback) {
3687+
// module.exports = function(formData, defaultOptions, callback) {
3688+
// var xhr = new XMLHttpRequest();
3689+
// xhr.open('GET', defaultOptions.authenticationEndpoint);
3690+
// xhr.onload = function() {
3691+
// if (xhr.status === 200) {
3692+
// try {
3693+
// var body = JSON.parse(xhr.responseText);
3694+
// formData.append("signature", body.signature || "");
3695+
// formData.append("expire", body.expire || 0);
3696+
// formData.append("token", body.token);
3697+
3698+
// var uploadFileXHR= new XMLHttpRequest();
3699+
// uploadFileXHR.open('POST', 'https://upload.imagekit.io/api/v1/files/upload');
3700+
// uploadFileXHR.onload = function() {
3701+
// if (uploadFileXHR.status === 200) {
3702+
// if(typeof callback != "function") return;
3703+
// var uploadResponse = JSON.parse(uploadFileXHR.responseText);
3704+
// callback(null, uploadResponse);
3705+
// }
3706+
// else if (uploadFileXHR.status !== 200) {
3707+
// if(typeof callback != "function") return;
3708+
// callback(JSON.parse(uploadFileXHR.responseText));
3709+
// }
3710+
// };
3711+
// uploadFileXHR.send(formData);
3712+
// } catch(ex) {
3713+
// console.log(ex);
3714+
// if(typeof callback != "function") return;
3715+
// callback(ex);
3716+
// }
3717+
// }
3718+
// else {
3719+
// try {
3720+
// var error = JSON.parse(xhr.responseText);
3721+
// console.log(error);
3722+
// if(typeof callback != "function") return;
3723+
// callback(error);
3724+
// } catch (ex) {
3725+
// console.log(ex);
3726+
// if(typeof callback != "function") return;
3727+
// callback(ex);
3728+
// }
3729+
// }
3730+
// };
3731+
// xhr.send();
3732+
// return;
3733+
// }
3734+
3735+
function request (formData, defaultOptions, callback) {
3736+
module.exports.generateSignatureToken(defaultOptions, function (err, signaturObj) {
3737+
if (err) {
3738+
console.log(err);
3739+
if(typeof callback != "function") return;
3740+
callback(err);
3741+
return;
3742+
} else {
3743+
formData.append("signature", signaturObj.signature || "");
3744+
formData.append("expire", signaturObj.expire || 0);
3745+
formData.append("token", signaturObj.token);
3746+
3747+
module.exports.uploadFile(formData, function(err, responseSucessText) {
3748+
if (err) {
3749+
console.log(error);
3750+
if(typeof callback != "function") return;
3751+
callback(error);
3752+
} else {
3753+
callback(null, responseSucessText);
3754+
}
3755+
});
3756+
}
3757+
});
3758+
}
3759+
3760+
function _generateSignatureToken(defaultOptions, callback) {
36883761
var xhr = new XMLHttpRequest();
36893762
xhr.open('GET', defaultOptions.authenticationEndpoint);
36903763
xhr.onload = function() {
36913764
if (xhr.status === 200) {
36923765
try {
36933766
var body = JSON.parse(xhr.responseText);
3694-
formData.append("signature", body.signature || "");
3695-
formData.append("expire", body.expire || 0);
3696-
formData.append("token", body.token);
3697-
3698-
var uploadFileXHR= new XMLHttpRequest();
3699-
uploadFileXHR.open('POST', 'https://upload.imagekit.io/api/v1/files/upload');
3700-
uploadFileXHR.onload = function() {
3701-
if (uploadFileXHR.status === 200) {
3702-
if(typeof callback != "function") return;
3703-
var uploadResponse = JSON.parse(uploadFileXHR.responseText);
3704-
callback(null, uploadResponse);
3705-
}
3706-
else if (uploadFileXHR.status !== 200) {
3707-
if(typeof callback != "function") return;
3708-
callback(JSON.parse(uploadFileXHR.responseText));
3709-
}
3710-
};
3711-
uploadFileXHR.send(formData);
3767+
var obj = {
3768+
signature: body.signature,
3769+
expire: body.expire,
3770+
token: body.token
3771+
}
3772+
callback(null, obj);
3773+
return;
37123774
} catch(ex) {
3713-
console.log(ex);
37143775
if(typeof callback != "function") return;
37153776
callback(ex);
37163777
}
3717-
}
3718-
else {
3778+
} else {
37193779
try {
37203780
var error = JSON.parse(xhr.responseText);
3721-
console.log(error);
37223781
if(typeof callback != "function") return;
37233782
callback(error);
37243783
} catch (ex) {
3725-
console.log(ex);
37263784
if(typeof callback != "function") return;
37273785
callback(ex);
37283786
}
@@ -3732,7 +3790,29 @@ module.exports = function(formData, defaultOptions, callback) {
37323790
return;
37333791
}
37343792

3793+
function _uploadFile (formData, callback) {
3794+
var uploadFileXHR= new XMLHttpRequest();
3795+
uploadFileXHR.open('POST', 'https://upload.imagekit.io/api/v1/files/upload');
3796+
uploadFileXHR.onload = function() {
3797+
if (uploadFileXHR.status === 200) {
3798+
if(typeof callback != "function") return;
3799+
var uploadResponse = JSON.parse(uploadFileXHR.responseText);
3800+
callback(null, uploadResponse);
3801+
}
3802+
else if (uploadFileXHR.status !== 200) {
3803+
if(typeof callback != "function") return;
3804+
callback(JSON.parse(uploadFileXHR.responseText));
3805+
}
3806+
};
3807+
uploadFileXHR.send(formData);
3808+
return
3809+
}
37353810

3811+
module.exports = {
3812+
request,
3813+
generateSignatureToken: _generateSignatureToken,
3814+
uploadFile: _uploadFile,
3815+
}
37363816
},{}],66:[function(require,module,exports){
37373817
module.exports = function(isError, response, callback) {
37383818
if(typeof callback == "function") {

libs/upload/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var isObject = require('lodash/isObject');
55
*/
66
var errorMessages = require("../../constants/errorMessages");
77
var respond = require("../../utils/respond");
8-
var request = require("../../utils/request");
8+
var request = require("../../utils/request").request;
99

1010
module.exports = function(uploadOptions, defaultOptions, callback) {
1111
if(!isObject(uploadOptions)) {

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@
88
"url-polyfill": "^1.1.7"
99
},
1010
"devDependencies": {
11+
"chai": "^4.2.0",
12+
"formdata-node": "^2.1.1",
1113
"grunt": "^1.0.4",
12-
"grunt-browserify": "^5.3.0"
14+
"grunt-browserify": "^5.3.0",
15+
"mocha": "^7.0.1",
16+
"sinon": "^8.1.1"
1317
},
1418
"scripts": {
15-
"test": "echo \"Error: no test specified\" && exit 1"
19+
"build": "yarn install && ./node_modules/.bin/grunt",
20+
"test": "yarn install && export NODE_ENV=test && ./node_modules/mocha/bin/mocha --exit -t 40000 test/*.js;ex=$? ; unset NODE_ENV ; exit $ex;",
21+
"startSampleApp": "yarn build && cd samples/sample-app/ && yarn install && node index.js"
1622
},
1723
"repository": {
1824
"type": "git",
@@ -37,4 +43,4 @@
3743
"url": "https://github.com/imagekit-developer/imagekit-javascript/issues"
3844
},
3945
"homepage": "https://github.com/imagekit-developer/imagekit-javascript#readme"
40-
}
46+
}

samples/sample-app/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PRIVATE_KEY = your_private_key
2+
PUBLIC_KEY = your_public_key
3+
URL_ENDPOINT = your_url_endpoint
4+
SERVER_PORT = 3000

samples/sample-app/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const path = require('path');
2+
const open = require('open');
3+
const dotenv = require('dotenv').config({path: path.join(__dirname, ".env")});
4+
const server = require('./server/server.js');
5+
6+
7+
const STATIC_PAGE = path.join(__dirname, "client/index.html")
8+
9+
if (dotenv.error) {
10+
throw new Error(dotenv.error);
11+
process.exit(1);
12+
}
13+
14+
const {PRIVATE_KEY, PUBLIC_KEY, URL_ENDPOINT, SERVER_PORT} = dotenv.parsed;
15+
16+
if (!PRIVATE_KEY || !PUBLIC_KEY || !URL_ENDPOINT || !SERVER_PORT) {
17+
throw new Error("Missing values in the '.env' file.")
18+
}
19+
20+
21+
server
22+
.startServer(SERVER_PORT, PUBLIC_KEY, PRIVATE_KEY, URL_ENDPOINT)
23+
.then(() => {
24+
try {
25+
return open(`http://localhost:${SERVER_PORT}`, {wait: true});
26+
} catch (err){
27+
console.error(JSON.stringify(err, undefined, 2))
28+
throw new Error(`Error opening the static page ${STATIC_PAGE}.`)
29+
}
30+
})
31+
.then(() => {
32+
console.log("Exiting app.");
33+
process.exit(0);
34+
})
35+
.catch(err => {
36+
console.log("Error:", JSON.stringify(err, undefined, 2));
37+
process.exit(1);
38+
});
39+
40+
41+

samples/sample-app/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "imagekit-javascript-sample",
3+
"version": "1.0.0",
4+
"description": "Sample app for javascript SDK of ImageKit",
5+
"main": "index.js",
6+
"license": "MIT",
7+
"dependencies": {
8+
"cors": "^2.8.5",
9+
"crypto": "^1.0.1",
10+
"dotenv": "^8.2.0",
11+
"express": "^4.17.1",
12+
"imagekit": "^3.0.3",
13+
"open": "^7.0.2",
14+
"pug": "^2.0.4",
15+
"uuid": "^3.4.0"
16+
}
17+
}

0 commit comments

Comments
 (0)