kraken 1.0.4 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,6 +16,7 @@ With this Node module you can plug into the power and speed of [Kraken.io](http:
16
16
  - [Callback URL](#callback-url)
17
17
  - [Promises](#promises)
18
18
  - [Async/Await](#asyncawait)
19
+ - [Error Handling](#error-handling)
19
20
  - [Authentication](#authentication)
20
21
  - [Usage - Image URL](#usage---image-url)
21
22
  - [Usage - Image Upload](#usage---image-upload)
@@ -38,7 +39,8 @@ With this Node module you can plug into the power and speed of [Kraken.io](http:
38
39
 
39
40
  ## Requirements
40
41
 
41
- - Minimum NodeJS version: `12.20.0`
42
+ - Node.js `12.20.0` or later
43
+ - Tested on Node.js 12, 14, 16, 18, 20, 22, 24 and 26
42
44
 
43
45
  ## Dependencies
44
46
 
@@ -174,6 +176,24 @@ async function exampleAsynFunction() {
174
176
  exampleAsynFunction()
175
177
  ```
176
178
 
179
+ ## Error Handling
180
+
181
+ Errors returned by the Kraken API (for example a wrong API key) and network errors do not reject the promise. The promise resolves with a response where `success` is `false`, so always check `success`:
182
+
183
+ ```javascript
184
+ kraken.userStatus().then((status) => {
185
+ if (status.success) {
186
+ console.log('Kraken user status:', status)
187
+ } else {
188
+ console.error('Kraken error:', status.message || status.error)
189
+ }
190
+ })
191
+ ```
192
+
193
+ With a callback, the response comes as the second argument in the same way. The first argument (`err`) is only set when the request could not be made, for example when an upload file does not exist.
194
+
195
+ The promise is rejected only when the request could not be made and no callback was given.
196
+
177
197
  ## Authentication
178
198
 
179
199
  The first step is to authenticate to Kraken API by providing your unique API Key and API Secret while creating a new Kraken instance:
@@ -253,7 +273,7 @@ If you want to upload your images directly to Kraken API use the `kraken.upload(
253
273
  `file` parameter have next options:
254
274
 
255
275
  - `String` - if file parameter is a string type, then it will be interpreted as a path to file and will be loaded using `fs.createReadStream()` function
256
- - `Stream` | `Buffer` | `String` - file content, will be generated random filename
276
+ - `Stream` | `Buffer` - file content, will be generated random filename
257
277
  - `Object` with next properties:
258
278
  - _mandatory_ `path` or `value` - if `path` is present, `value` is ignored
259
279
  - `path` - path to file, will be loaded using `fs.createReadStream()` function
@@ -389,8 +409,8 @@ Object as file options with buffer as value:
389
409
  var opts = {
390
410
  file: {
391
411
  value: fs.readFileSync('file.jpg'),
392
- fileName: 'new_file_name.jpg'
393
- }
412
+ filename: 'new_file_name.jpg'
413
+ },
394
414
  wait: true
395
415
  }
396
416
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kraken",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "author": {
5
5
  "name": "Kraken.io",
6
6
  "email": "support@kraken.io"
@@ -8,6 +8,9 @@
8
8
  "description": "Plug into the power and speed of Kraken.io Image Optimizer",
9
9
  "homepage": "https://github.com/kraken-io/kraken-node",
10
10
  "main": "./src/kraken",
11
+ "files": [
12
+ "src"
13
+ ],
11
14
  "repository": {
12
15
  "type": "git",
13
16
  "url": "https://github.com/kraken-io/kraken-node.git"
@@ -23,7 +26,7 @@
23
26
  "node": ">=12.20.0"
24
27
  },
25
28
  "dependencies": {
26
- "axios": "^0.24.0",
29
+ "axios": "^1.21.1",
27
30
  "form-data": "^4.0.1"
28
31
  },
29
32
  "devDependencies": {
@@ -38,6 +41,9 @@
38
41
  },
39
42
  "jest": {
40
43
  "collectCoverage": true,
44
+ "collectCoverageFrom": [
45
+ "src/**/*.js"
46
+ ],
41
47
  "coverageDirectory": "coverage",
42
48
  "coverageProvider": "v8",
43
49
  "coverageReporters": [
@@ -45,7 +51,15 @@
45
51
  "text",
46
52
  "lcov",
47
53
  "clover"
48
- ]
54
+ ],
55
+ "coverageThreshold": {
56
+ "global": {
57
+ "branches": 100,
58
+ "functions": 100,
59
+ "lines": 100,
60
+ "statements": 100
61
+ }
62
+ }
49
63
  },
50
64
  "eslintConfig": {
51
65
  "parserOptions": {
package/src/kraken.js CHANGED
@@ -31,12 +31,12 @@ function convertError(err) {
31
31
 
32
32
  // In some cases err.response can be empty (network errors and etc.)
33
33
  if (res) {
34
- // Got data from server response
35
- if (res.data) {
34
+ // Got JSON data from server response
35
+ if (res.data && typeof res.data === 'object') {
36
36
  return new KrakenResponse(res.data) // Standard Kraken error
37
37
  }
38
38
 
39
- // Server response without data or it can't be processed by axios
39
+ // Server response without JSON data (empty body, HTML error page and etc.)
40
40
  return new KrakenResponse({
41
41
  success: false,
42
42
  error: err.toString(),
@@ -65,14 +65,12 @@ function axiosPost(url, data, axiosOpts, cb) {
65
65
  .catch(convertError) // Errors processing
66
66
 
67
67
  if (cb) {
68
- // Callback processing
69
- result
70
- .then(function (status) {
71
- cb(null, status)
72
- }) // Result to callback
73
- .catch(function (err) {
74
- cb(err, null)
75
- }) // Error to callback
68
+ // Callback processing. `result` never rejects: errors are converted
69
+ // to a KrakenResponse above. Calling `cb` once also means an error
70
+ // thrown inside `cb` is not passed back to `cb` a second time.
71
+ result.then(function (status) {
72
+ cb(null, status)
73
+ }) // Result to callback
76
74
  }
77
75
  return result
78
76
  }
@@ -91,7 +89,7 @@ class Kraken {
91
89
  * @memberof Kraken
92
90
  */
93
91
  constructor(keyOrAuth, secret = null) {
94
- if(!keyOrAuth) throw new Error('Please provide an API key');
92
+ if (!keyOrAuth) throw new Error('Please provide an API key')
95
93
 
96
94
  if (keyOrAuth instanceof KrakenAuth) {
97
95
  this.auth = keyOrAuth
@@ -102,9 +100,8 @@ class Kraken {
102
100
  typeof secret === 'string'
103
101
  ) {
104
102
  this.auth = new KrakenAuth(keyOrAuth, secret)
105
- }
106
- else {
107
- throw new Error('Please provide an API key and secret');
103
+ } else {
104
+ throw new Error('Please provide an API key and secret')
108
105
  }
109
106
  this.api = new KrakenApiList()
110
107
  }
@@ -120,13 +117,18 @@ class Kraken {
120
117
  try {
121
118
  var data = new KrakenUploadData(this, options) // Processing options
122
119
  } catch (err) {
123
- return Promise.reject(
124
- new KrakenResponse({
125
- success: false,
126
- error: err.toString(),
127
- originalError: err
128
- })
129
- )
120
+ const res = new KrakenResponse({
121
+ success: false,
122
+ error: err.toString(),
123
+ originalError: err
124
+ })
125
+ if (cb) {
126
+ // Report to the callback instead of leaving a rejected
127
+ // promise that nobody handles
128
+ cb(res, null)
129
+ return res
130
+ }
131
+ return Promise.reject(res)
130
132
  }
131
133
  const axiosOpts = new AxiosOptions(Kraken.default.axios, {
132
134
  // Processing axios options
@@ -1,7 +1,6 @@
1
1
  // NodeJS libs
2
2
  const fs = require('fs')
3
3
  const path = require('path')
4
- const { Stream } = require('stream')
5
4
  const crypto = require('crypto')
6
5
 
7
6
  // External libs
@@ -14,6 +13,22 @@ const KrakenApiData = require('./KrakenApiData')
14
13
  const nameLen = 20
15
14
  const readOptions = { flags: 'r' }
16
15
 
16
+ // Check that a value looks like a readable stream
17
+ function isStream(value) {
18
+ return (
19
+ value !== null &&
20
+ typeof value === 'object' &&
21
+ typeof value.pipe === 'function'
22
+ )
23
+ }
24
+
25
+ // Open a file for reading. createReadStream is async, so the access
26
+ // check has to be sync to report a wrong path right away.
27
+ function openFile(filePath) {
28
+ fs.accessSync(filePath, fs.constants.R_OK)
29
+ return fs.createReadStream(filePath, readOptions)
30
+ }
31
+
17
32
  /**
18
33
  * KrakenUploadData processing a data for uploading.
19
34
  * @class KrakenUploadData
@@ -57,21 +72,24 @@ class KrakenUploadData extends FormData {
57
72
 
58
73
  // Prepare variables
59
74
  const file = options.file
60
- let fileData = file // Set default data as file prop (Buffer, Stream or whatever)
75
+ const isPath = typeof file === 'string' || file instanceof String
76
+ let fileData
61
77
  let opt = {}
62
78
 
63
- // Processing various types of data
64
- if (typeof file === 'string' || file instanceof String) {
65
- // createReadStream is async - file access check is required to be sync
66
- fs.accessSync(file, fs.constants.R_OK)
67
- fileData = fs.createReadStream(file, readOptions)
68
- opt.filename = path.basename(file) // Set the filename for form data
69
- } else if (file instanceof Object) {
79
+ // Processing various types of data.
80
+ // Buffer and stream checks must not rely on `instanceof Object`:
81
+ // a Buffer is an Object too, and `instanceof` fails across realms.
82
+ if (isPath) {
83
+ fileData = openFile(String(file))
84
+ opt.filename = path.basename(String(file)) // Set the filename for form data
85
+ } else if (Buffer.isBuffer(file) || isStream(file)) {
86
+ fileData = file
87
+ } else if (file && typeof file === 'object') {
70
88
  // TODO: In case of breaking changes - update
71
89
  // this block and method's options format to flat version
72
- if (typeof file.path === 'string' || file instanceof String) {
73
- fs.accessSync(file.path, fs.constants.R_OK)
74
- fileData = fs.createReadStream(file.path, readOptions)
90
+ if (typeof file.path === 'string') {
91
+ fileData = openFile(file.path)
92
+ opt.filename = path.basename(file.path)
75
93
  } else if (file.value) {
76
94
  fileData = file.value
77
95
  } else {
@@ -79,18 +97,23 @@ class KrakenUploadData extends FormData {
79
97
  'Not found required option `file.path` or `file.value`'
80
98
  )
81
99
  }
82
- opt = file.options || {}
100
+ opt = Object.assign(opt, file.options) // Do not mutate user options
101
+ } else {
102
+ throw new Error('Not found required option `file`')
83
103
  }
84
104
 
85
- // Set length parameter for formData options
86
- // in case if length property is present
87
- if (typeof file.length === 'number') {
105
+ // Set length parameter for formData options in case if length
106
+ // property is present. For a path, `length` is the length of the
107
+ // path string, not the file size, so it must not be used.
108
+ if (!isPath && typeof file.length === 'number') {
88
109
  opt.knownLength = file.length
89
110
  }
90
111
 
91
112
  // Generate random filename in case if it missing
92
113
  opt.filename =
93
- file.filename || crypto.randomBytes(nameLen).toString('hex')
114
+ file.filename ||
115
+ opt.filename ||
116
+ crypto.randomBytes(nameLen).toString('hex')
94
117
 
95
118
  // Add file data and file options to form
96
119
  this.append('file', fileData, opt)
@@ -1,41 +0,0 @@
1
- # This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
- # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
-
4
- name: Node.js CI
5
-
6
- on:
7
- push:
8
- branches:
9
- - master
10
- - develop
11
- pull_request:
12
- branches:
13
- - '*'
14
-
15
- jobs:
16
- build:
17
- runs-on: ubuntu-latest
18
-
19
- strategy:
20
- matrix:
21
- node-version: [12.x, 14.x, 16.x, 18.x, 20.x, 22.x, 24.x, 26.x]
22
- # See supported Node.js release schedule at https://nodejs.org/en/about/previous-releases
23
-
24
- env:
25
- KRAKEN_API_KEY: ${{ secrets.KRAKEN_API_KEY }}
26
- KRAKEN_API_SECRET: ${{ secrets.KRAKEN_API_SECRET }}
27
-
28
- steps:
29
- - uses: actions/checkout@v7
30
- - name: Use Node.js ${{ matrix.node-version }}
31
- uses: actions/setup-node@v7
32
- with:
33
- node-version: ${{ matrix.node-version }}
34
- cache: 'npm'
35
- # npm 6 (bundled with Node.js 12 and 14) cannot read lockfileVersion 3
36
- - name: Use npm 8 on Node.js 12 and 14
37
- if: matrix.node-version == '12.x' || matrix.node-version == '14.x'
38
- run: npm install -g npm@8
39
- - run: npm ci
40
- - run: npm run build --if-present
41
- - run: npm test
@@ -1,50 +0,0 @@
1
- # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
- # For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages
3
-
4
- name: Node.js Package
5
-
6
- on:
7
- release:
8
- types: [published]
9
-
10
- jobs:
11
- build:
12
- runs-on: ubuntu-latest
13
- strategy:
14
- matrix:
15
- node-version: [12.x, 14.x, 16.x, 18.x, 20.x, 22.x, 24.x, 26.x]
16
-
17
- env:
18
- KRAKEN_API_KEY: ${{ secrets.KRAKEN_API_KEY }}
19
- KRAKEN_API_SECRET: ${{ secrets.KRAKEN_API_SECRET }}
20
-
21
- steps:
22
- - uses: actions/checkout@v7
23
- - name: Use Node.js ${{ matrix.node-version }}
24
- uses: actions/setup-node@v7
25
- with:
26
- node-version: ${{ matrix.node-version }}
27
- cache: 'npm'
28
- # npm 6 (bundled with Node.js 12 and 14) cannot read lockfileVersion 3
29
- - name: Use npm 8 on Node.js 12 and 14
30
- if: matrix.node-version == '12.x' || matrix.node-version == '14.x'
31
- run: npm install -g npm@8
32
- - run: npm ci
33
- - run: npm run build --if-present
34
- - run: npm test
35
-
36
- publish-npm:
37
- needs: build
38
- runs-on: ubuntu-latest
39
- steps:
40
- - uses: actions/checkout@v7
41
- - uses: actions/setup-node@v7
42
- with:
43
- node-version: 24
44
- registry-url: https://registry.npmjs.org/
45
- - run: npm ci
46
- - run: npm publish
47
- env:
48
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
49
- KRAKEN_API_KEY: ${{ secrets.KRAKEN_API_KEY }}
50
- KRAKEN_API_SECRET: ${{ secrets.KRAKEN_API_SECRET }}
package/.jshintrc DELETED
@@ -1,21 +0,0 @@
1
- {
2
- "maxerr": 50, // {int} Maximum error before stopping
3
- "camelcase": false, // true: Identifiers must be in camelCase
4
- "curly": true, // true: Require {} for every new block or scope
5
- "eqeqeq": true, // true: Require triple equals (===) for comparison
6
- "forin": true, // true: Require filtering for..in loops with obj.hasOwnProperty()
7
- "freeze": true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
8
- "indent": 4, // {int} Number of spaces to use for indentation
9
- "noempty": true, // true: Prohibit use of empty blocks
10
- "nonbsp": true, // true: Prohibit "non-breaking whitespace" characters.
11
- "nonew": false, // true: Prohibit use of constructors for side-effects (without assignment)
12
- "quotmark": "single",
13
- "undef": true, // true: Require all non-global variables to be declared (prevents global leaks)
14
- "unused": true, // true: Require all defined variables be used
15
- "strict": false, // true: Requires all functions run in ES5 Strict Mode
16
- "devel": false, // Development/debugging (alert, confirm, etc)
17
- "node": true, // Node.js
18
- "globals": {}, // additional predefined global variables
19
- "esversion": 10,
20
- "asi": true // Missing semicolon warning off
21
- }
@@ -1,176 +0,0 @@
1
- <!-- Generated by documentation.js. Update this documentation by updating the source code. -->
2
-
3
- ### Table of Contents
4
-
5
- * [Kraken][1]
6
- * [Parameters][2]
7
- * [upload][3]
8
- * [Parameters][4]
9
- * [url][5]
10
- * [Parameters][6]
11
- * [userStatus][7]
12
- * [Parameters][8]
13
- * [AxiosOptions][9]
14
- * [KrakenApiData][10]
15
- * [toJson][11]
16
- * [KrakenApiList][12]
17
- * [KrakenAuth][13]
18
- * [KrakenResponse][14]
19
- * [KrakenUploadData][15]
20
-
21
- ## Kraken
22
-
23
- [src/kraken.js:85-156][16]
24
-
25
- This class describes a Kraken-io API
26
-
27
- ### Parameters
28
-
29
- * `keyOrAuth` **[string][17]** Kraken API key
30
- * `secret` **[string][17]** Kraken API secret (optional, default `null`)
31
-
32
- ### upload
33
-
34
- [src/kraken.js:113-131][18]
35
-
36
- Upload image to Kraken.io and optimize it
37
-
38
- #### Parameters
39
-
40
- * `options` **[object][19]** Kraken options for uploaded image
41
- * `cb` **[Function][20]** \=null (optional, default `null`)
42
-
43
- Returns **[Promise][21]<[KrakenResponse][22]>**
44
-
45
- ### url
46
-
47
- [src/kraken.js:139-143][23]
48
-
49
- Optimize image from URL
50
-
51
- #### Parameters
52
-
53
- * `options` **[object][19]** Kraken options for url
54
- * `cb` **[Function][20]** \=null Callback (optional, default `null`)
55
-
56
- Returns **[Promise][21]**
57
-
58
- ### userStatus
59
-
60
- [src/kraken.js:151-155][24]
61
-
62
- Get user status
63
-
64
- #### Parameters
65
-
66
- * `cb` **[Function][20]** Callback (optional, default `null`)
67
-
68
- Returns **[Promise][21]**
69
-
70
- ## AxiosOptions
71
-
72
- [src/lib/AxiosOptions.js:6-22][25]
73
-
74
- Provides options processing for axios
75
-
76
- ## KrakenApiData
77
-
78
- [src/lib/KrakenApiData.js:6-23][26]
79
-
80
- Provides kraken API data processing
81
-
82
- ### toJson
83
-
84
- [src/lib/KrakenApiData.js:20-22][27]
85
-
86
- Convert API Data to JSON string
87
-
88
- Returns **[string][17]**
89
-
90
- ## KrakenApiList
91
-
92
- [src/lib/KrakenApiList.js:9-25][28]
93
-
94
- Provides Kraken API methods list
95
-
96
- ## KrakenAuth
97
-
98
- [src/lib/KrakenAuth.js:6-16][29]
99
-
100
- Provides Kraken.io credentials for API
101
-
102
- ## KrakenResponse
103
-
104
- [src/lib/KrakenResponse.js:6-14][30]
105
-
106
- Just a container for Kraken API response data
107
-
108
- ## KrakenUploadData
109
-
110
- [src/lib/KrakenUploadData.js:22-98][31]
111
-
112
- **Extends FormData**
113
-
114
- KrakenUploadData processing a data for uploading.
115
-
116
- [1]: #kraken
117
-
118
- [2]: #parameters
119
-
120
- [3]: #upload
121
-
122
- [4]: #parameters-1
123
-
124
- [5]: #url
125
-
126
- [6]: #parameters-2
127
-
128
- [7]: #userstatus
129
-
130
- [8]: #parameters-3
131
-
132
- [9]: #axiosoptions
133
-
134
- [10]: #krakenapidata
135
-
136
- [11]: #tojson
137
-
138
- [12]: #krakenapilist
139
-
140
- [13]: #krakenauth
141
-
142
- [14]: #krakenresponse
143
-
144
- [15]: #krakenuploaddata
145
-
146
- [16]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/kraken.js#L85-L156 "Source code on GitHub"
147
-
148
- [17]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
149
-
150
- [18]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/kraken.js#L113-L131 "Source code on GitHub"
151
-
152
- [19]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object
153
-
154
- [20]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
155
-
156
- [21]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
157
-
158
- [22]: #krakenresponse
159
-
160
- [23]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/kraken.js#L139-L143 "Source code on GitHub"
161
-
162
- [24]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/kraken.js#L151-L155 "Source code on GitHub"
163
-
164
- [25]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/lib/AxiosOptions.js#L6-L22 "Source code on GitHub"
165
-
166
- [26]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/lib/KrakenApiData.js#L6-L23 "Source code on GitHub"
167
-
168
- [27]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/lib/KrakenApiData.js#L20-L22 "Source code on GitHub"
169
-
170
- [28]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/lib/KrakenApiList.js#L9-L25 "Source code on GitHub"
171
-
172
- [29]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/lib/KrakenAuth.js#L6-L16 "Source code on GitHub"
173
-
174
- [30]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/lib/KrakenResponse.js#L6-L14 "Source code on GitHub"
175
-
176
- [31]: https://github.com/kraken-io/kraken-node/blob/0b4bbcefcd070e8431660f20a2b6a95db38caa13/src/lib/KrakenUploadData.js#L22-L98 "Source code on GitHub"
package/test/.jshintrc DELETED
@@ -1,27 +0,0 @@
1
- {
2
- "maxerr": 50, // {int} Maximum error before stopping
3
- "camelcase": false, // true: Identifiers must be in camelCase
4
- "curly": true, // true: Require {} for every new block or scope
5
- "eqeqeq": true, // true: Require triple equals (===) for comparison
6
- "forin": true, // true: Require filtering for..in loops with obj.hasOwnProperty()
7
- "freeze": true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
8
- "indent": 4, // {int} Number of spaces to use for indentation
9
- "noempty": true, // true: Prohibit use of empty blocks
10
- "nonbsp": true, // true: Prohibit "non-breaking whitespace" characters.
11
- "nonew": false, // true: Prohibit use of constructors for side-effects (without assignment)
12
- "quotmark": "single",
13
- "undef": true, // true: Require all non-global variables to be declared (prevents global leaks)
14
- "unused": true, // true: Require all defined variables be used
15
- "strict": false, // true: Requires all functions run in ES5 Strict Mode
16
- "devel": false, // Development/debugging (alert, confirm, etc)
17
- "node": true, // Node.js
18
- "globals": {
19
- // additional predefined global variables
20
- "describe": true,
21
- "test": true,
22
- "it": true,
23
- "expect": true
24
- },
25
- "esversion": 10,
26
- "asi": true // Missing semicolon warning off
27
- }
@@ -1,177 +0,0 @@
1
- const Kraken = require('../')
2
- const fs = require('fs')
3
- require('dotenv').config();
4
-
5
- // Environment variables names with API key and secret
6
- const envKey = 'KRAKEN_API_KEY'
7
- const envSecret = 'KRAKEN_API_SECRET'
8
-
9
- const krakenKey = process.env[envKey]
10
- const krakenSecret = process.env[envSecret]
11
-
12
- const MAX_TIMEOUT = 20000;
13
-
14
- // Check key and secret variables
15
- CheckEnvVars([envKey, envSecret])
16
-
17
- // Tests extensions
18
- expect.extend({
19
- toBeKrakenSuccess(status) {
20
- const result = {
21
- message: () => 'Unknown error',
22
- pass: false
23
- }
24
-
25
- if (status == null) {
26
- result.message = () => 'Empty status'
27
- } else if (status.success === true) {
28
- result.message = () => 'Not passed'
29
- result.pass = true
30
- } else if (status.error) {
31
- result.message = () =>
32
- `Kraken API returned an error: ${status.error}`
33
- }
34
-
35
- return result
36
- }
37
- })
38
-
39
- class DevData {
40
- constructor(data) {
41
- this.wait = true
42
- this.dev = true
43
- Object.assign(this, data)
44
- }
45
- }
46
-
47
- // Main tests
48
- describe('Kraken.io API', () => {
49
- // Test variables
50
- let krakenAuth = null
51
- let kraken = null
52
-
53
- // Test prepare
54
- beforeAll(() => {
55
- krakenAuth = {
56
- api_key: krakenKey,
57
- api_secret: krakenSecret
58
- }
59
- kraken = new Kraken(krakenAuth)
60
- })
61
-
62
- // auth 1
63
- it('new Kraken({api_key, api_secret})', () => {
64
- const kraken = new Kraken(krakenAuth)
65
- expect(kraken.auth).toEqual(krakenAuth)
66
- })
67
-
68
- // auth 2
69
- it('new Kraken(api_key, api_secret)', () => {
70
- const kraken = new Kraken(krakenAuth.api_key, krakenAuth.api_secret)
71
- expect(kraken.auth).toEqual(krakenAuth)
72
- })
73
-
74
- // userStatus
75
- it('kraken.userStatus()', () => {
76
- return expect(kraken.userStatus()).resolves.toBeKrakenSuccess()
77
- })
78
-
79
- // url
80
- it('kraken.url()', () => {
81
- const data = {
82
- url: 'https://kraken-nekkraug.netdna-ssl.com/assets/images/kraken-logotype@2x.png',
83
- wait: true,
84
- dev: true
85
- }
86
-
87
- return expect(kraken.url(data)).resolves.toBeKrakenSuccess()
88
- },MAX_TIMEOUT)
89
-
90
- // url various formats
91
- describe('kraken.upload({ file })', () => {
92
- it('file<String> -> catch wrong file path', () => {
93
- const data = new DevData({
94
- file: 'test/support/images/wrong-file-path.gif'
95
- })
96
-
97
- return expect(kraken.upload(data)).rejects.toHaveProperty(
98
- 'originalError.code',
99
- 'ENOENT'
100
- )
101
- })
102
-
103
- it('file<String> -> path', () => {
104
- const data = new DevData({
105
- file: 'test/support/images/image.gif'
106
- })
107
-
108
- return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
109
- })
110
-
111
- it('file<Buffer>', () => {
112
- const fileData = fs.readFileSync('test/support/images/image.gif')
113
- const data = new DevData({
114
- file: fileData
115
- })
116
-
117
- return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
118
- })
119
-
120
- it('file<Stream>', () => {
121
- const fileData = fs.createReadStream(
122
- 'test/support/images/image.gif'
123
- )
124
- const data = new DevData({
125
- file: fileData
126
- })
127
-
128
- return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
129
- })
130
-
131
- it('file<Object>: path and options', () => {
132
- const data = new DevData({
133
- file: {
134
- path: 'test/support/images/image.gif',
135
- options: {}
136
- }
137
- })
138
-
139
- return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
140
- })
141
-
142
- it('file<Object>: <Buffer> and options', () => {
143
- const fileData = fs.readFileSync('test/support/images/image.gif')
144
- const data = new DevData({
145
- file: {
146
- value: fileData,
147
- options: {}
148
- }
149
- })
150
-
151
- return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
152
- })
153
-
154
- it('file<Object>: <Stream> and options', () => {
155
- const fileData = fs.createReadStream(
156
- 'test/support/images/image.gif'
157
- )
158
- const data = new DevData({
159
- file: {
160
- value: fileData,
161
- options: {}
162
- }
163
- })
164
-
165
- return expect(kraken.upload(data)).resolves.toBeKrakenSuccess()
166
- })
167
- })
168
- })
169
-
170
- // Check if environment variable is defined
171
- function CheckEnvVars(names) {
172
- for (let name of names) {
173
- if (!process.env[name]) {
174
- throw new Error(`Not found environment variable: ${name}`)
175
- }
176
- }
177
- }
Binary file