@caruuto/caruuto-js 0.3.5 → 0.4.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/CHANGELOG.md +8 -1
- package/index.js +16 -14
- package/lib/extensions/email.js +57 -0
- package/lib/extensions/tokens.js +33 -0
- package/lib/extensions.js +53 -0
- package/lib/performFetch.js +3 -1
- package/lib/terminators.js +41 -0
- package/package.json +3 -1
- package/scripts/coverage.js +2 -1
- package/x.js +33 -4
package/CHANGELOG.md
CHANGED
|
@@ -5,7 +5,14 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
|
6
6
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
|
7
7
|
|
|
8
|
-
## 0.3.
|
|
8
|
+
## 0.3.6 - 2024-09-06
|
|
9
|
+
|
|
10
|
+
### Added
|
|
11
|
+
|
|
12
|
+
- Added `createToken` method for creating new tokens for reset password, etc.
|
|
13
|
+
- Added `verifyToken` method for verifying tokens created by `createToken`.
|
|
14
|
+
|
|
15
|
+
## 0.3.5 - 2024-05-08
|
|
9
16
|
|
|
10
17
|
### Fixed
|
|
11
18
|
|
package/index.js
CHANGED
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
const CaruutoClient = function (caruutoUrl, caruutoApiKey) {
|
|
2
|
-
this.options =
|
|
1
|
+
const CaruutoClient = function (caruutoUrl, caruutoApiKey, options = {}) {
|
|
2
|
+
this.options = options
|
|
3
3
|
this.options.caruutoUrl = caruutoUrl
|
|
4
4
|
this.options.caruutoApiKey = caruutoApiKey
|
|
5
5
|
|
|
6
6
|
this.options.version = '1'
|
|
7
7
|
this.options.callback = this._processRequest.bind(this)
|
|
8
8
|
|
|
9
|
-
this.reservedProperties = [
|
|
10
|
-
'
|
|
11
|
-
'
|
|
12
|
-
'
|
|
13
|
-
'
|
|
14
|
-
'lastModifiedAt',
|
|
15
|
-
'lastModifiedBy',
|
|
16
|
-
'v',
|
|
17
|
-
'history',
|
|
18
|
-
'composed'
|
|
9
|
+
this.reservedProperties = this.options.reservedProperties || [
|
|
10
|
+
'created_at',
|
|
11
|
+
'created_by',
|
|
12
|
+
'updated_at',
|
|
13
|
+
'updated_by'
|
|
19
14
|
]
|
|
20
15
|
|
|
21
16
|
if (this.options.caruutoUrl === undefined) {
|
|
@@ -51,11 +46,18 @@ require('./lib/filters')(CaruutoClient)
|
|
|
51
46
|
|
|
52
47
|
require('./lib/terminators')(CaruutoClient)
|
|
53
48
|
|
|
49
|
+
// -----------------------------------------
|
|
50
|
+
|
|
54
51
|
module.exports = CaruutoClient
|
|
55
52
|
|
|
56
53
|
/**
|
|
57
54
|
* Creates a new Caruuto Client.
|
|
58
55
|
*/
|
|
59
|
-
module.exports.createClient = (caruutoUrl, caruutoApiKey) => {
|
|
60
|
-
|
|
56
|
+
module.exports.createClient = (caruutoUrl, caruutoApiKey, options) => {
|
|
57
|
+
const client = new CaruutoClient(caruutoUrl, caruutoApiKey, options)
|
|
58
|
+
|
|
59
|
+
// Attach extensions once the client is created.
|
|
60
|
+
require('./lib/extensions')(CaruutoClient, client)
|
|
61
|
+
|
|
62
|
+
return client
|
|
61
63
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
module.exports = function (CaruutoClient, clientInstance) {
|
|
2
|
+
CaruutoClient.prototype.email = {
|
|
3
|
+
find: function (query) {
|
|
4
|
+
this.endpoint = 'email'
|
|
5
|
+
|
|
6
|
+
this.query = query
|
|
7
|
+
|
|
8
|
+
const requestPayload = {
|
|
9
|
+
method: 'GET',
|
|
10
|
+
uri: this._buildURL({ useParams: true })
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
this._setHeader('content-type', 'application/json')
|
|
14
|
+
|
|
15
|
+
return this._createRequestObject(requestPayload)
|
|
16
|
+
}.bind(clientInstance),
|
|
17
|
+
queue: function (data) {
|
|
18
|
+
this.endpoint = 'email'
|
|
19
|
+
|
|
20
|
+
const requestPayload = {
|
|
21
|
+
body: data,
|
|
22
|
+
method: 'POST',
|
|
23
|
+
uri: this._buildURL()
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
this._setHeader('content-type', 'application/json')
|
|
27
|
+
|
|
28
|
+
return this._createRequestObject(requestPayload)
|
|
29
|
+
}.bind(clientInstance),
|
|
30
|
+
update: function (data) {
|
|
31
|
+
this.endpoint = 'email'
|
|
32
|
+
|
|
33
|
+
const requestPayload = {
|
|
34
|
+
body: data,
|
|
35
|
+
method: 'PUT',
|
|
36
|
+
uri: this._buildURL()
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
this._setHeader('content-type', 'application/json')
|
|
40
|
+
|
|
41
|
+
return this._createRequestObject(requestPayload)
|
|
42
|
+
}.bind(clientInstance),
|
|
43
|
+
delete: function (data) {
|
|
44
|
+
this.endpoint = 'email'
|
|
45
|
+
|
|
46
|
+
const requestPayload = {
|
|
47
|
+
body: data,
|
|
48
|
+
method: 'DELETE',
|
|
49
|
+
uri: this._buildURL()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this._setHeader('content-type', 'application/json')
|
|
53
|
+
|
|
54
|
+
return this._createRequestObject(requestPayload)
|
|
55
|
+
}.bind(clientInstance)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module.exports = function (CaruutoClient, clientInstance) {
|
|
2
|
+
CaruutoClient.prototype.tokens = {
|
|
3
|
+
create: function (data) {
|
|
4
|
+
this.endpoint = 'tokens'
|
|
5
|
+
|
|
6
|
+
const requestPayload = {
|
|
7
|
+
body: data,
|
|
8
|
+
method: 'POST',
|
|
9
|
+
uri: this._buildURL()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
this._setHeader('content-type', 'application/json')
|
|
13
|
+
|
|
14
|
+
return this._createRequestObject(requestPayload)
|
|
15
|
+
}.bind(clientInstance),
|
|
16
|
+
verify: function (token) {
|
|
17
|
+
this.endpoint = 'tokens'
|
|
18
|
+
|
|
19
|
+
const requestPayload = {
|
|
20
|
+
body: {
|
|
21
|
+
token,
|
|
22
|
+
verify: true
|
|
23
|
+
},
|
|
24
|
+
method: 'POST',
|
|
25
|
+
uri: this._buildURL()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
this._setHeader('content-type', 'application/json')
|
|
29
|
+
|
|
30
|
+
return this._createRequestObject(requestPayload)
|
|
31
|
+
}.bind(clientInstance)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
Each extension should be added to the CaruutoClient prototype, so that
|
|
6
|
+
for example, the email extension can be used as:
|
|
7
|
+
|
|
8
|
+
```
|
|
9
|
+
const client = new CaruutoClient()
|
|
10
|
+
client.email.send({ ... })
|
|
11
|
+
client.email.queue({ ... })
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
The extension function should take the CaruutoClient prototype as an
|
|
15
|
+
argument and add methods to it.
|
|
16
|
+
For example:
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
module.exports = function (CaruutoClient, clientInstance) {
|
|
20
|
+
CaruutoClient.prototype.email = {
|
|
21
|
+
queue: function (data) {
|
|
22
|
+
this.endpoint = 'email/queue'
|
|
23
|
+
|
|
24
|
+
const requestPayload = {
|
|
25
|
+
body: data,
|
|
26
|
+
method: 'POST',
|
|
27
|
+
uri: this._buildURL()
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this._setHeader('content-type', 'application/json')
|
|
31
|
+
|
|
32
|
+
return this._createRequestObject(requestPayload)
|
|
33
|
+
}.bind(clientInstance)
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
// Load all extensions from the lib/extensions directory/
|
|
40
|
+
module.exports = function (CaruutoClient, clientInstance) {
|
|
41
|
+
const extensionsDir = path.join(__dirname, 'extensions')
|
|
42
|
+
const files = fs.readdirSync(extensionsDir)
|
|
43
|
+
|
|
44
|
+
files.forEach(file => {
|
|
45
|
+
if (file.endsWith('.js')) {
|
|
46
|
+
const extension = require(path.join(extensionsDir, file))
|
|
47
|
+
|
|
48
|
+
if (typeof extension === 'function') {
|
|
49
|
+
extension(CaruutoClient, clientInstance)
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
}
|
package/lib/performFetch.js
CHANGED
package/lib/terminators.js
CHANGED
|
@@ -297,4 +297,45 @@ module.exports = function (CaruutoClient) {
|
|
|
297
297
|
|
|
298
298
|
return this._createRequestObject(requestPayload)
|
|
299
299
|
}
|
|
300
|
+
|
|
301
|
+
/*
|
|
302
|
+
* Create a new token.
|
|
303
|
+
* @param {Object} data
|
|
304
|
+
* @api public
|
|
305
|
+
*/
|
|
306
|
+
CaruutoClient.prototype.createToken = function (data) {
|
|
307
|
+
this.endpoint = 'tokens'
|
|
308
|
+
|
|
309
|
+
const requestPayload = {
|
|
310
|
+
body: data,
|
|
311
|
+
method: 'POST',
|
|
312
|
+
uri: this._buildURL()
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
this._setHeader('content-type', 'application/json')
|
|
316
|
+
|
|
317
|
+
return this._createRequestObject(requestPayload)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/*
|
|
321
|
+
* Verify a token.
|
|
322
|
+
* @param {Object} data
|
|
323
|
+
* @api public
|
|
324
|
+
*/
|
|
325
|
+
CaruutoClient.prototype.verifyToken = function (token) {
|
|
326
|
+
this.endpoint = 'tokens'
|
|
327
|
+
|
|
328
|
+
const requestPayload = {
|
|
329
|
+
body: {
|
|
330
|
+
token,
|
|
331
|
+
verify: true
|
|
332
|
+
},
|
|
333
|
+
method: 'POST',
|
|
334
|
+
uri: this._buildURL()
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
this._setHeader('content-type', 'application/json')
|
|
338
|
+
|
|
339
|
+
return this._createRequestObject(requestPayload)
|
|
340
|
+
}
|
|
300
341
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@caruuto/caruuto-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "A high-level library for interacting with Caruuto",
|
|
5
5
|
"exports": "./index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"debug": "^2.6.1",
|
|
15
15
|
"encoding": "^0.1.13",
|
|
16
16
|
"got": "^11.0.0",
|
|
17
|
+
"human-interval": "^2.0.1",
|
|
17
18
|
"mocha": "^10.2.0",
|
|
18
19
|
"query-string": "5.0.1"
|
|
19
20
|
},
|
|
@@ -40,6 +41,7 @@
|
|
|
40
41
|
},
|
|
41
42
|
"homepage": "https://github.com/Caruuto/caruuto-js",
|
|
42
43
|
"devDependencies": {
|
|
44
|
+
"chai": "^5.2.0",
|
|
43
45
|
"coveralls": "^3.0.2",
|
|
44
46
|
"eslint": "8.5.0",
|
|
45
47
|
"eslint-config-next": "13.0.5",
|
package/scripts/coverage.js
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
|
|
3
3
|
const exec = require('child_process').exec
|
|
4
4
|
|
|
5
|
-
if (process.env
|
|
5
|
+
if (process.env.CI) {
|
|
6
6
|
exec(
|
|
7
7
|
'cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js',
|
|
8
8
|
(err, out) => {
|
|
9
9
|
if (err) console.log(err)
|
|
10
|
+
console.log(out)
|
|
10
11
|
}
|
|
11
12
|
)
|
|
12
13
|
}
|
package/x.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
const { createClient } = require('./index')
|
|
2
2
|
|
|
3
3
|
;(async () => {
|
|
4
|
-
const x = createClient(
|
|
4
|
+
const x = createClient(
|
|
5
|
+
'https://localcaruuto.com/api',
|
|
6
|
+
'12345678900987654321',
|
|
7
|
+
{ reservedProperties: ['created_at'] }
|
|
8
|
+
)
|
|
5
9
|
|
|
6
10
|
// const signedUrl = await x.inMedia('radical').getSignedUrl('test.jpg')
|
|
7
11
|
|
|
@@ -13,7 +17,32 @@ const { createClient } = require('./index')
|
|
|
13
17
|
// questionId: 'f373b0a2-9c87-41f1-af49-f643da8f836c'
|
|
14
18
|
// })
|
|
15
19
|
|
|
16
|
-
x.search({ phrase: 'grower' })
|
|
17
|
-
const y = await x.find()
|
|
18
|
-
console.log('y :>> ', y)
|
|
20
|
+
// x.search({ phrase: 'grower' })
|
|
21
|
+
// const y = await x.find()
|
|
22
|
+
// console.log('y :>> ', y)
|
|
23
|
+
|
|
24
|
+
// const z = await x.tokens.create({
|
|
25
|
+
// email: 'jameslambie@gmail.com',
|
|
26
|
+
// expiry: 7200000,
|
|
27
|
+
// type: 'RESET'
|
|
28
|
+
// })
|
|
29
|
+
// const z = await x.verifyToken('q8ug8epjx5hzcg48ku')
|
|
30
|
+
// console.log('z :>> ', z)
|
|
31
|
+
|
|
32
|
+
// console.log('p :>> ', await x.create({ x: 1 }))
|
|
33
|
+
|
|
34
|
+
console.log('x :>> ', x)
|
|
35
|
+
// console.log('x :>> ', x.email)
|
|
36
|
+
// console.log('x :>> ', x.email.queue)
|
|
37
|
+
// const xx = await x.email.queue({
|
|
38
|
+
// to: 'x@x.com',
|
|
39
|
+
// from: 'z@z.com',
|
|
40
|
+
// templateId: 'test'
|
|
41
|
+
// })
|
|
42
|
+
|
|
43
|
+
// console.log('xx :>> ', xx)
|
|
44
|
+
|
|
45
|
+
const zz = await x.email.find({ sent: true })
|
|
46
|
+
|
|
47
|
+
console.log('zz :>> ', zz)
|
|
19
48
|
})()
|