@caruuto/caruuto-js 0.4.0 → 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/extensions.js CHANGED
@@ -1,6 +1,3 @@
1
- const fs = require('fs')
2
- const path = require('path')
3
-
4
1
  /*
5
2
  Each extension should be added to the CaruutoClient prototype, so that
6
3
  for example, the email extension can be used as:
@@ -36,18 +33,18 @@ module.exports = function (CaruutoClient, clientInstance) {
36
33
  ```
37
34
  */
38
35
 
36
+ const emailExtension = require('./extensions/email')
37
+ const tokensExtension = require('./extensions/tokens')
38
+
39
39
  // Load all extensions from the lib/extensions directory/
40
40
  module.exports = function (CaruutoClient, clientInstance) {
41
- const extensionsDir = path.join(__dirname, 'extensions')
42
- const files = fs.readdirSync(extensionsDir)
41
+ if (typeof emailExtension === 'function') {
42
+ emailExtension(CaruutoClient, clientInstance)
43
+ }
43
44
 
44
- files.forEach(file => {
45
- if (file.endsWith('.js')) {
46
- const extension = require(path.join(extensionsDir, file))
45
+ if (typeof tokensExtension === 'function') {
46
+ tokensExtension(CaruutoClient, clientInstance)
47
+ }
47
48
 
48
- if (typeof extension === 'function') {
49
- extension(CaruutoClient, clientInstance)
50
- }
51
- }
52
- })
49
+ // Add any other extensions here.
53
50
  }
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 = new url.URL(options.uri)
161
+ const parsedUri = url.parse(options.uri)
158
162
  const requestObject = {
159
163
  ...options,
160
164
  uri: {
@@ -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
- } else if (typeof fetch === 'undefined') {
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
- options.body = JSON.stringify(requestObject.body)
42
-
43
- if (options.headers['content-type'] !== 'application/json') {
44
- options.headers['content-type'] = 'application/json'
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
  })
@@ -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
- * Update the documents affect by the saved query
172
+ * Upload a file to the media bucket.
169
173
  *
170
- * @param {Object} update
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 (file) {
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
- // this._setHeader('content-type', 'application/json')
198
+ const formData = new FormData()
183
199
 
184
- // if (this._isValidHook()) {
185
- // requestPayload.body = update
200
+ console.log(directory, fileName, mimeType, buffer, contentLength)
186
201
 
187
- // this._setHeader('content-type', 'text/plain')
188
- // } else {
189
- // if (this.isClient) {
190
- // if (!this.isClient.id && !this.isClient.self) {
191
- // throw new Error(
192
- // 'Unable to run update on all clients. Please use whereClientIs() or whereClientIsSelf() filters.'
193
- // )
194
- // }
195
-
196
- // // Remove `clientId` from the payload.
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
- console.log('requestPayload :>> ', requestPayload)
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.0",
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.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://localcaruuto.com/api',
6
- '12345678900987654321',
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
- const zz = await x.email.find({ sent: true })
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
  })()