kraken 0.4.0 → 1.0.1
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/.github/workflows/node.js.yml +37 -0
- package/.github/workflows/npm-publish.yml +46 -0
- package/.jshintrc +20 -18
- package/LICENSE +9 -0
- package/README.md +424 -138
- package/docs/code/README.md +176 -0
- package/package.json +73 -26
- package/src/kraken.js +172 -0
- package/src/lib/AxiosOptions.js +24 -0
- package/src/lib/KrakenApiData.js +25 -0
- package/src/lib/KrakenApiList.js +36 -0
- package/src/lib/KrakenAuth.js +18 -0
- package/src/lib/KrakenResponse.js +16 -0
- package/src/lib/KrakenUploadData.js +100 -0
- package/test/.jshintrc +27 -0
- package/test/kraken.test.js +177 -0
- package/test/support/images/image.gif +0 -0
- package/.travis.yml +0 -4
- package/lib/kraken.js +0 -104
|
@@ -0,0 +1,176 @@
|
|
|
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/package.json
CHANGED
|
@@ -1,28 +1,75 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
2
|
+
"name": "kraken",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"author": {
|
|
5
|
+
"name": "Kraken.io",
|
|
6
|
+
"email": "support@kraken.io"
|
|
7
|
+
},
|
|
8
|
+
"description": "Plug into the power and speed of Kraken.io Image Optimizer",
|
|
9
|
+
"homepage": "https://github.com/kraken-io/kraken-node",
|
|
10
|
+
"main": "./src/kraken",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/kraken-io/kraken-node.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"kraken",
|
|
17
|
+
"kraken.io",
|
|
18
|
+
"image optimizer",
|
|
19
|
+
"image optimiser"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"engine": {
|
|
23
|
+
"node": ">=12.20.0"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"axios": "^0.24.0",
|
|
27
|
+
"form-data": "^4.0.0"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"dotenv": "^16.3.1",
|
|
31
|
+
"eslint": "^8.2.0",
|
|
32
|
+
"jest": "^28.1.3",
|
|
33
|
+
"prettier": "^2.4.1"
|
|
34
|
+
},
|
|
35
|
+
"scripts": {
|
|
36
|
+
"test": "node --trace-warnings node_modules/.bin/jest --no-cache",
|
|
37
|
+
"buildDocs": "documentation build src/** -f md --github > docs/code/README.md"
|
|
38
|
+
},
|
|
39
|
+
"jest": {
|
|
40
|
+
"collectCoverage": true,
|
|
41
|
+
"coverageDirectory": "coverage",
|
|
42
|
+
"coverageProvider": "v8",
|
|
43
|
+
"coverageReporters": [
|
|
44
|
+
"json",
|
|
45
|
+
"text",
|
|
46
|
+
"lcov",
|
|
47
|
+
"clover"
|
|
48
|
+
]
|
|
49
|
+
},
|
|
50
|
+
"eslintConfig": {
|
|
51
|
+
"parserOptions": {
|
|
52
|
+
"ecmaVersion": 2017
|
|
53
|
+
},
|
|
54
|
+
"env": {
|
|
55
|
+
"es6": true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"prettier": {
|
|
59
|
+
"printWidth": 80,
|
|
60
|
+
"tabWidth": 4,
|
|
61
|
+
"singleQuote": true,
|
|
62
|
+
"trailingComma": "none",
|
|
63
|
+
"bracketSpacing": true,
|
|
64
|
+
"semi": false,
|
|
65
|
+
"useTabs": false,
|
|
66
|
+
"overrides": [
|
|
67
|
+
{
|
|
68
|
+
"files": "*.yml",
|
|
69
|
+
"options": {
|
|
70
|
+
"tabWidth": 2
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
]
|
|
74
|
+
}
|
|
28
75
|
}
|
package/src/kraken.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Kraken
|
|
3
|
+
* Copyright (c) 2013-2021 Kraken.io <support@kraken.io>
|
|
4
|
+
* MIT Licensed
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// External libs
|
|
8
|
+
const axios = require('axios')
|
|
9
|
+
|
|
10
|
+
// Internal libs
|
|
11
|
+
const AxiosOptions = require('./lib/AxiosOptions')
|
|
12
|
+
const KrakenApiData = require('./lib/KrakenApiData')
|
|
13
|
+
const KrakenApiList = require('./lib/KrakenApiList')
|
|
14
|
+
const KrakenAuth = require('./lib/KrakenAuth')
|
|
15
|
+
const KrakenResponse = require('./lib/KrakenResponse')
|
|
16
|
+
const KrakenUploadData = require('./lib/KrakenUploadData')
|
|
17
|
+
|
|
18
|
+
// Constants and settings
|
|
19
|
+
const packageInfo = require('../package.json') // Load package info
|
|
20
|
+
const userAgent = `kraken-node/${packageInfo.version}` // Set default user agent
|
|
21
|
+
|
|
22
|
+
// Internal static methods - private static methods doesn't well supported in node yet.
|
|
23
|
+
// Convert axios response to kraken response
|
|
24
|
+
function convertResposne(res) {
|
|
25
|
+
return new KrakenResponse(res.data)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Convert axios or standard error to kraken response
|
|
29
|
+
function convertError(err) {
|
|
30
|
+
const res = err.response
|
|
31
|
+
|
|
32
|
+
// In some cases err.response can be empty (network errors and etc.)
|
|
33
|
+
if (res) {
|
|
34
|
+
// Got data from server response
|
|
35
|
+
if (res.data) {
|
|
36
|
+
return new KrakenResponse(res.data) // Standard Kraken error
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Server response without data or it can't be processed by axios
|
|
40
|
+
return new KrakenResponse({
|
|
41
|
+
success: false,
|
|
42
|
+
error: err.toString(),
|
|
43
|
+
status: {
|
|
44
|
+
// Extended error info
|
|
45
|
+
code: res.status,
|
|
46
|
+
text: res.statusText
|
|
47
|
+
},
|
|
48
|
+
originalError: err
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Response is empty - processing all other errors kinds
|
|
53
|
+
return new KrakenResponse({
|
|
54
|
+
success: false,
|
|
55
|
+
error: err.toString(),
|
|
56
|
+
originalError: err
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Send POST request using axios
|
|
61
|
+
function axiosPost(url, data, axiosOpts, cb) {
|
|
62
|
+
const result = axios
|
|
63
|
+
.post(url, data, axiosOpts) // Sending request
|
|
64
|
+
.then(convertResposne) // Response processing
|
|
65
|
+
.catch(convertError) // Errors processing
|
|
66
|
+
|
|
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
|
|
76
|
+
}
|
|
77
|
+
return result
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* This class describes a Kraken-io API
|
|
82
|
+
*
|
|
83
|
+
* @class Kraken (name)
|
|
84
|
+
*/
|
|
85
|
+
class Kraken {
|
|
86
|
+
/**
|
|
87
|
+
* Constructs a new Kraken instance
|
|
88
|
+
* @param {object} keyOrAuth Kraken API key and secret
|
|
89
|
+
* @param {string} keyOrAuth Kraken API key
|
|
90
|
+
* @param {string} [secret=null] Kraken API secret
|
|
91
|
+
* @memberof Kraken
|
|
92
|
+
*/
|
|
93
|
+
constructor(keyOrAuth, secret = null) {
|
|
94
|
+
if(!keyOrAuth) throw new Error('Please provide an API key');
|
|
95
|
+
|
|
96
|
+
if (keyOrAuth instanceof KrakenAuth) {
|
|
97
|
+
this.auth = keyOrAuth
|
|
98
|
+
} else if (keyOrAuth instanceof Object) {
|
|
99
|
+
this.auth = new KrakenAuth(keyOrAuth.api_key, keyOrAuth.api_secret)
|
|
100
|
+
} else if (
|
|
101
|
+
typeof keyOrAuth === 'string' &&
|
|
102
|
+
typeof secret === 'string'
|
|
103
|
+
) {
|
|
104
|
+
this.auth = new KrakenAuth(keyOrAuth, secret)
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
throw new Error('Please provide an API key and secret');
|
|
108
|
+
}
|
|
109
|
+
this.api = new KrakenApiList()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Upload image to Kraken.io and optimize it
|
|
114
|
+
* @param {object} options Kraken options for uploaded image
|
|
115
|
+
* @param {Function} cb=null
|
|
116
|
+
* @returns {Promise<KrakenResponse>}
|
|
117
|
+
*/
|
|
118
|
+
async upload(options, cb = null) {
|
|
119
|
+
// FS operations - need to catch them too and return as rejected promise
|
|
120
|
+
try {
|
|
121
|
+
var data = new KrakenUploadData(this, options) // Processing options
|
|
122
|
+
} catch (err) {
|
|
123
|
+
return Promise.reject(
|
|
124
|
+
new KrakenResponse({
|
|
125
|
+
success: false,
|
|
126
|
+
error: err.toString(),
|
|
127
|
+
originalError: err
|
|
128
|
+
})
|
|
129
|
+
)
|
|
130
|
+
}
|
|
131
|
+
const axiosOpts = new AxiosOptions(Kraken.default.axios, {
|
|
132
|
+
// Processing axios options
|
|
133
|
+
headers: data.getHeaders()
|
|
134
|
+
})
|
|
135
|
+
return axiosPost(this.api.upload, data, axiosOpts, cb) // Sending request
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Optimize image from URL
|
|
140
|
+
* @param {object} options Kraken options for url
|
|
141
|
+
* @param {Function} cb=null Callback
|
|
142
|
+
* @returns {Promise}
|
|
143
|
+
*/
|
|
144
|
+
async url(options, cb = null) {
|
|
145
|
+
const data = new KrakenApiData(this, options) // Processing options
|
|
146
|
+
const axiosOpts = new AxiosOptions(Kraken.default.axios) // Processing axios options
|
|
147
|
+
return axiosPost(this.api.url, data, axiosOpts, cb) // Sending request
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Get user status
|
|
152
|
+
* @param {Function} [cb=null] Callback
|
|
153
|
+
* @return {Promise}
|
|
154
|
+
* @memberof Kraken
|
|
155
|
+
*/
|
|
156
|
+
async userStatus(cb = null) {
|
|
157
|
+
const data = new KrakenApiData(this) // Processing options
|
|
158
|
+
const axiosOpts = new AxiosOptions(Kraken.default.axios) // Processing axios options
|
|
159
|
+
return axiosPost(this.api.userStatus, data, axiosOpts, cb) // Sending request
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
Kraken.KrakenApiList = KrakenApiList
|
|
164
|
+
Kraken.KrakenAuth = KrakenAuth
|
|
165
|
+
Kraken.default = {
|
|
166
|
+
axios: new AxiosOptions({
|
|
167
|
+
responseType: 'json',
|
|
168
|
+
headers: { 'User-Agent': userAgent }
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
module.exports = Kraken
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides options processing for axios
|
|
3
|
+
*
|
|
4
|
+
* @class AxiosOptions
|
|
5
|
+
*/
|
|
6
|
+
class AxiosOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Constructs a new AxiosOptions instance
|
|
9
|
+
* @param {object} defaults Default options
|
|
10
|
+
* @param {object} options New options
|
|
11
|
+
*/
|
|
12
|
+
constructor(defaults, options) {
|
|
13
|
+
Object.assign(this, defaults, options)
|
|
14
|
+
if (defaults && options) {
|
|
15
|
+
const h1 = defaults.headers
|
|
16
|
+
const h2 = options.headers
|
|
17
|
+
if (h1 || h2) {
|
|
18
|
+
this.headers = Object.assign({}, h1, h2)
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = AxiosOptions
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides kraken API data processing
|
|
3
|
+
*
|
|
4
|
+
* @class KrakenApiData
|
|
5
|
+
*/
|
|
6
|
+
class KrakenApiData {
|
|
7
|
+
/**
|
|
8
|
+
* Constructs a new KrakenApiData instance
|
|
9
|
+
* @param {Kraken} kraken Kraken instance
|
|
10
|
+
* @param {any} options Kraken API options
|
|
11
|
+
*/
|
|
12
|
+
constructor(kraken, options) {
|
|
13
|
+
Object.assign(this, { auth: kraken.auth }, options)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Convert API Data to JSON string
|
|
18
|
+
* @returns {string}
|
|
19
|
+
*/
|
|
20
|
+
toJson() {
|
|
21
|
+
return JSON.stringify(this)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = KrakenApiData
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// Settings and constants
|
|
2
|
+
const apiKrakenIo = 'https://api.kraken.io'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Provides Kraken API methods list
|
|
6
|
+
*
|
|
7
|
+
* @class KrakenApiList
|
|
8
|
+
*/
|
|
9
|
+
class KrakenApiList {
|
|
10
|
+
/**
|
|
11
|
+
* Constructs a new KrakenApiList instance. Basicaly just list of object props with url to method.
|
|
12
|
+
* @param {string} url=KrakenApiList.default.url Kraken API URL
|
|
13
|
+
* @param {object} methods=KrakenApiList.default.methods Methods lists object in format [method]: 'path/to/method'
|
|
14
|
+
* @param {string} methods.methodname 'method': '/v/path'
|
|
15
|
+
*/
|
|
16
|
+
constructor(
|
|
17
|
+
url = KrakenApiList.default.url,
|
|
18
|
+
methods = KrakenApiList.default.methods
|
|
19
|
+
) {
|
|
20
|
+
// Convert object with methods/urls to api list
|
|
21
|
+
for (let method of Object.keys(methods)) {
|
|
22
|
+
this[method] = `${url}/${methods[method]}`
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
KrakenApiList.default = {
|
|
28
|
+
url: apiKrakenIo,
|
|
29
|
+
methods: {
|
|
30
|
+
url: 'v1/url',
|
|
31
|
+
upload: 'v1/upload',
|
|
32
|
+
userStatus: 'user_status'
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = KrakenApiList
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides Kraken.io credentials for API
|
|
3
|
+
*
|
|
4
|
+
* @class KrakenAuth
|
|
5
|
+
*/
|
|
6
|
+
class KrakenAuth {
|
|
7
|
+
/**
|
|
8
|
+
* Constructs a new KrakenAuth instance
|
|
9
|
+
* @param {string} key Kraken.io API key
|
|
10
|
+
* @param {string} secret Kraken.io API secret
|
|
11
|
+
*/
|
|
12
|
+
constructor(key, secret) {
|
|
13
|
+
this.api_key = key
|
|
14
|
+
this.api_secret = secret
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = KrakenAuth
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Just a container for Kraken API response data
|
|
3
|
+
*
|
|
4
|
+
* @class KrakenResponse
|
|
5
|
+
*/
|
|
6
|
+
class KrakenResponse {
|
|
7
|
+
/**
|
|
8
|
+
* Constructs a new KrakenResponse instance
|
|
9
|
+
* @param {*} data
|
|
10
|
+
*/
|
|
11
|
+
constructor(data) {
|
|
12
|
+
Object.assign(this, data)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
module.exports = KrakenResponse
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// NodeJS libs
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const path = require('path')
|
|
4
|
+
const { Stream } = require('stream')
|
|
5
|
+
const crypto = require('crypto')
|
|
6
|
+
|
|
7
|
+
// External libs
|
|
8
|
+
const FormData = require('form-data')
|
|
9
|
+
|
|
10
|
+
// Internal libs
|
|
11
|
+
const KrakenApiData = require('./KrakenApiData')
|
|
12
|
+
|
|
13
|
+
// Constants and settings
|
|
14
|
+
const nameLen = 20
|
|
15
|
+
const readOptions = { flags: 'r' }
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* KrakenUploadData processing a data for uploading.
|
|
19
|
+
* @class KrakenUploadData
|
|
20
|
+
* @extends {FormData}
|
|
21
|
+
*/
|
|
22
|
+
class KrakenUploadData extends FormData {
|
|
23
|
+
/**
|
|
24
|
+
* @typedef {Object} FileOptions
|
|
25
|
+
* @property {String|Object} header Header or headers
|
|
26
|
+
* @property {Number} knownLength File known length
|
|
27
|
+
* @property {String} filename File name
|
|
28
|
+
* @property {String} filepath File path
|
|
29
|
+
* @property {String} contentType Content type
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @typedef {Object} FileDetails
|
|
34
|
+
* @property {String|Stream|Buffer} value Path to file or file data
|
|
35
|
+
* @property {FileOptions} options File options {@link FileOptions}
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @typedef {Object} UploadOptions
|
|
40
|
+
* @property {String|FileDetails} file Path to file or file details {@link FileDetails}
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* Constructs a new instance of KrakenUploadData and process options for uploading.
|
|
46
|
+
*
|
|
47
|
+
* @param {Kraken} kraken The kraken object instance
|
|
48
|
+
* @param {UploadOptions} options The upload options {@link UploadOptions} *
|
|
49
|
+
*/
|
|
50
|
+
constructor(kraken, options) {
|
|
51
|
+
super()
|
|
52
|
+
const data = new KrakenApiData(kraken, options) // Process general kraken options
|
|
53
|
+
delete data.file
|
|
54
|
+
|
|
55
|
+
// Add options to the form as json
|
|
56
|
+
this.append('data', data.toJson())
|
|
57
|
+
|
|
58
|
+
// Prepare variables
|
|
59
|
+
const file = options.file
|
|
60
|
+
let fileData = file // Set default data as file prop (Buffer, Stream or whatever)
|
|
61
|
+
let opt = {}
|
|
62
|
+
|
|
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) {
|
|
70
|
+
// TODO: In case of breaking changes - update
|
|
71
|
+
// 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)
|
|
75
|
+
} else if (file.value) {
|
|
76
|
+
fileData = file.value
|
|
77
|
+
} else {
|
|
78
|
+
throw new Error(
|
|
79
|
+
'Not found required option `file.path` or `file.value`'
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
opt = file.options || {}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Set length parameter for formData options
|
|
86
|
+
// in case if length property is present
|
|
87
|
+
if (typeof file.length === 'number') {
|
|
88
|
+
opt.knownLength = file.length
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Generate random filename in case if it missing
|
|
92
|
+
opt.filename =
|
|
93
|
+
file.filename || crypto.randomBytes(nameLen).toString('hex')
|
|
94
|
+
|
|
95
|
+
// Add file data and file options to form
|
|
96
|
+
this.append('file', fileData, opt)
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
module.exports = KrakenUploadData
|