@caruuto/caruuto-js 0.4.1 → 0.4.2
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/lib/helpers.js +6 -2
- package/lib/performFetch.js +31 -8
- package/lib/terminators.js +32 -28
- package/package.json +4 -3
- package/x.js +9 -4
package/lib/helpers.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const querystring = require('query-string')
|
|
2
|
-
const url = require('url')
|
|
2
|
+
const url = require('node:url')
|
|
3
3
|
|
|
4
4
|
module.exports = function (CaruutoClient) {
|
|
5
5
|
/**
|
|
@@ -49,6 +49,10 @@ module.exports = function (CaruutoClient) {
|
|
|
49
49
|
url +=
|
|
50
50
|
'/media' +
|
|
51
51
|
(typeof this.mediaBucket === 'string' ? '/' + this.mediaBucket : '')
|
|
52
|
+
|
|
53
|
+
if (this.terminator === 'uploadFile') {
|
|
54
|
+
url += '/upload'
|
|
55
|
+
}
|
|
52
56
|
} else if (this.collection) {
|
|
53
57
|
url += '/' + options.version + '/' + this.collection
|
|
54
58
|
} else if (this.searchPhrase) {
|
|
@@ -154,7 +158,7 @@ module.exports = function (CaruutoClient) {
|
|
|
154
158
|
* @api private
|
|
155
159
|
*/
|
|
156
160
|
CaruutoClient.prototype._createRequestObject = function (options) {
|
|
157
|
-
const parsedUri =
|
|
161
|
+
const parsedUri = url.parse(options.uri)
|
|
158
162
|
const requestObject = {
|
|
159
163
|
...options,
|
|
160
164
|
uri: {
|
package/lib/performFetch.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
const crossFetch = require('cross-fetch')
|
|
1
|
+
// const crossFetch = require('cross-fetch')
|
|
2
2
|
const debug = require('debug')('caruuto-js')
|
|
3
|
+
const undici = require('undici')
|
|
3
4
|
|
|
4
5
|
const resolveFetch = customFetch => {
|
|
5
6
|
let _fetch
|
|
6
7
|
|
|
7
8
|
if (customFetch) {
|
|
8
9
|
_fetch = customFetch
|
|
9
|
-
|
|
10
|
-
_fetch = crossFetch
|
|
10
|
+
// } else if (typeof fetch === 'undefined') {
|
|
11
|
+
// _fetch = crossFetch
|
|
11
12
|
} else {
|
|
12
|
-
_fetch = fetch
|
|
13
|
+
_fetch = undici.fetch
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
return (...args) => _fetch(...args)
|
|
@@ -38,13 +39,34 @@ module.exports = function (CaruutoClient) {
|
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
if (requestObject.body) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
42
|
+
// If the body is a FormData object, we need to handle it differently
|
|
43
|
+
if (this.terminator === 'uploadFile') {
|
|
44
|
+
options.body = requestObject.body
|
|
45
|
+
} else if (typeof requestObject.body === 'object') {
|
|
46
|
+
// If the body is an object, we need to stringify it
|
|
47
|
+
options.body = JSON.stringify(requestObject.body)
|
|
48
|
+
} else {
|
|
49
|
+
options.body = requestObject.body
|
|
45
50
|
}
|
|
46
51
|
}
|
|
47
52
|
|
|
53
|
+
if (
|
|
54
|
+
options.headers['content-type'] !== 'application/json' &&
|
|
55
|
+
this.terminator !== 'uploadFile'
|
|
56
|
+
) {
|
|
57
|
+
options.headers['content-type'] = 'application/json'
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// if (this.terminator === 'uploadFile') {
|
|
61
|
+
// If we are uploading a file, we need to set the content type to multipart/form-data
|
|
62
|
+
// options.headers['content-type'] = 'multipart/form-data'
|
|
63
|
+
// }
|
|
64
|
+
|
|
65
|
+
// If the request is a GET request, we don't need to set the body
|
|
66
|
+
if (requestObject.method === 'GET' || requestObject.method === 'HEAD') {
|
|
67
|
+
delete options.body
|
|
68
|
+
}
|
|
69
|
+
|
|
48
70
|
// if (
|
|
49
71
|
// requestObject.headers &&
|
|
50
72
|
// requestObject.headers['content-type'] &&
|
|
@@ -57,6 +79,7 @@ module.exports = function (CaruutoClient) {
|
|
|
57
79
|
|
|
58
80
|
debug(`Querying URI: ${decodeURIComponent(options.uri)}`)
|
|
59
81
|
|
|
82
|
+
// Make the request using fetch
|
|
60
83
|
const response = await fetch(options.uri, options).catch(err => {
|
|
61
84
|
return Promise.reject(err)
|
|
62
85
|
})
|
package/lib/terminators.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
// const FormData = require('form-data')
|
|
2
|
+
const undici = require('undici')
|
|
3
|
+
const { FormData } = undici
|
|
4
|
+
|
|
1
5
|
module.exports = function (CaruutoClient) {
|
|
2
6
|
/**
|
|
3
7
|
* Create one/multiple documents or hooks
|
|
@@ -165,13 +169,25 @@ module.exports = function (CaruutoClient) {
|
|
|
165
169
|
}
|
|
166
170
|
|
|
167
171
|
/**
|
|
168
|
-
*
|
|
172
|
+
* Upload a file to the media bucket.
|
|
169
173
|
*
|
|
170
|
-
* @param {
|
|
174
|
+
* @param {String} directory - The directory in the media bucket to upload the file
|
|
175
|
+
* @param {Object} buffer - The file buffer to upload
|
|
176
|
+
* @param {String} fileName - The name of the file to upload
|
|
177
|
+
* @param {String} mimeType - The MIME type of the file
|
|
178
|
+
* @param {Number} contentLength - The size of the file in bytes
|
|
179
|
+
* @description Upload a file to the media bucket.
|
|
180
|
+
* This method constructs a multipart/form-data request to upload a file.
|
|
171
181
|
* @return Promise
|
|
172
182
|
* @api public
|
|
173
183
|
*/
|
|
174
|
-
CaruutoClient.prototype.uploadFile = function (
|
|
184
|
+
CaruutoClient.prototype.uploadFile = function (
|
|
185
|
+
directory,
|
|
186
|
+
fileName,
|
|
187
|
+
mimeType,
|
|
188
|
+
buffer,
|
|
189
|
+
contentLength
|
|
190
|
+
) {
|
|
175
191
|
this.terminator = 'uploadFile'
|
|
176
192
|
|
|
177
193
|
const requestPayload = {
|
|
@@ -179,35 +195,23 @@ module.exports = function (CaruutoClient) {
|
|
|
179
195
|
uri: this._buildURL()
|
|
180
196
|
}
|
|
181
197
|
|
|
182
|
-
|
|
198
|
+
const formData = new FormData()
|
|
183
199
|
|
|
184
|
-
|
|
185
|
-
// requestPayload.body = update
|
|
200
|
+
console.log(directory, fileName, mimeType, buffer, contentLength)
|
|
186
201
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
// if (update.clientId) {
|
|
198
|
-
// delete update.clientId
|
|
199
|
-
// }
|
|
200
|
-
|
|
201
|
-
// requestPayload.body = update
|
|
202
|
-
// } else {
|
|
203
|
-
|
|
204
|
-
requestPayload.body = {
|
|
205
|
-
file
|
|
202
|
+
try {
|
|
203
|
+
formData.append(
|
|
204
|
+
'file',
|
|
205
|
+
new Blob([buffer], { type: mimeType, size: contentLength }),
|
|
206
|
+
fileName
|
|
207
|
+
)
|
|
208
|
+
formData.append('directory', directory)
|
|
209
|
+
} catch (error) {
|
|
210
|
+
console.error('Error appending file to FormData:', error)
|
|
211
|
+
throw new Error('Failed to append file to FormData')
|
|
206
212
|
}
|
|
207
|
-
// }
|
|
208
|
-
// }
|
|
209
213
|
|
|
210
|
-
|
|
214
|
+
requestPayload.body = formData
|
|
211
215
|
|
|
212
216
|
return this._createRequestObject(requestPayload)
|
|
213
217
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caruuto/caruuto-js",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"description": "A high-level library for interacting with Caruuto",
|
|
5
5
|
"exports": "./index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
"got": "^11.0.0",
|
|
17
17
|
"human-interval": "^2.0.1",
|
|
18
18
|
"mocha": "^10.2.0",
|
|
19
|
-
"query-string": "5.0.1"
|
|
19
|
+
"query-string": "5.0.1",
|
|
20
|
+
"undici": "^5.29.0"
|
|
20
21
|
},
|
|
21
22
|
"repository": {
|
|
22
23
|
"type": "git",
|
|
@@ -48,7 +49,7 @@
|
|
|
48
49
|
"eslint-config-prettier": "^8.6.0",
|
|
49
50
|
"eslint-plugin-prettier": "^4.2.1",
|
|
50
51
|
"eslint-plugin-react": "^7.32.2",
|
|
51
|
-
"form-data": "^4.0.
|
|
52
|
+
"form-data": "^4.0.2",
|
|
52
53
|
"husky": "^8.0.0",
|
|
53
54
|
"lint-staged": "^12.5.0",
|
|
54
55
|
"mockery": "^1.7.0",
|
package/x.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
const { createClient } = require('./index')
|
|
2
2
|
|
|
3
|
+
// https://caruuto.27.works/api
|
|
4
|
+
// EYeRSSEtmcVF9OFhVHjcf1TcPLHbTpPgBbHVHIce1buYKHOCNB1A2NC4UoThmQhk
|
|
5
|
+
|
|
3
6
|
;(async () => {
|
|
4
7
|
const x = createClient(
|
|
5
|
-
'https://
|
|
6
|
-
'
|
|
8
|
+
'https://caruuto.27.works/api',
|
|
9
|
+
'EYeRSSEtmcVF9OFhVHjcf1TcPLHbTpPgBbHVHIce1buYKHOCNB1A2NC4UoThmQhk',
|
|
7
10
|
{ reservedProperties: ['created_at'] }
|
|
8
11
|
)
|
|
9
12
|
|
|
@@ -31,7 +34,6 @@ const { createClient } = require('./index')
|
|
|
31
34
|
|
|
32
35
|
// console.log('p :>> ', await x.create({ x: 1 }))
|
|
33
36
|
|
|
34
|
-
console.log('x :>> ', x)
|
|
35
37
|
// console.log('x :>> ', x.email)
|
|
36
38
|
// console.log('x :>> ', x.email.queue)
|
|
37
39
|
// const xx = await x.email.queue({
|
|
@@ -42,7 +44,10 @@ const { createClient } = require('./index')
|
|
|
42
44
|
|
|
43
45
|
// console.log('xx :>> ', xx)
|
|
44
46
|
|
|
45
|
-
|
|
47
|
+
x.in('retailers')
|
|
48
|
+
console.log('x :>> ', x)
|
|
49
|
+
|
|
50
|
+
const zz = await x.find({ sent: true })
|
|
46
51
|
|
|
47
52
|
console.log('zz :>> ', zz)
|
|
48
53
|
})()
|