mysendingbox 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/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2025 Exaland.app
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,672 @@
1
+
2
+ # mysendingbox-node
3
+
4
+ [![Node.js Package](https://github.com/exaland/mysendingbox-node/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/exaland/mysendingbox-node/actions/workflows/npm-publish.yml)
5
+
6
+ [downloads-image]: http://img.shields.io/npm/dm/mysendingbox.svg
7
+
8
+ [npm-url]: https://npmjs.org/package/mysendingbox
9
+
10
+ [npm-image]: https://badge.fury.io/js/mysendingbox.svg
11
+
12
+
13
+
14
+ [![NPM version][npm-image]][npm-url] [![Dependency Status](https://gemnasium.com/badges/github.com/exaland/mysendingbox-node.svg)](https://gemnasium.com/github.com/exaland/mysendingbox-node)
15
+
16
+
17
+
18
+
19
+
20
+ MySendingBox.com Node.js Client is a simple but flexible wrapper for the [MySendingBox.fr](https://www.mysendingbox.fr) API.
21
+
22
+
23
+
24
+ See full Seeuletter.com documentation [here](https://docs.mysendingbox.fr/).
25
+
26
+
27
+
28
+ For best results, be sure that you're using the latest version of the Seeuletter API and the latest version of the Node.js wrapper.
29
+
30
+
31
+
32
+ #### French
33
+
34
+ Un module NPM pour envoyer du courrier postal ou electronique en ligne depuis votre application Node.Js.
35
+
36
+
37
+
38
+ Seeuletter propose une API permettant d'envoyer très facilement du courrier postal ou électronique depuis votre ERP, CRM ou application web.
39
+
40
+
41
+
42
+ Pas de frais d'installation. Pas d'engagement. Vous payez ce que vous consommez.
43
+
44
+
45
+
46
+ Documentation : https://docs.mysendingbox.fr/
47
+
48
+
49
+
50
+ Bien démarrer : https://www.mysendingbox.fr/guide/bien-demarrer-avec-l-api-d-envoi-de-courrier
51
+
52
+
53
+
54
+ ## Table of Contents
55
+
56
+
57
+
58
+ - [Getting Started](#getting-started)
59
+
60
+ - [Registration](#registration)
61
+
62
+ - [Installation](#installation)
63
+
64
+ - [Letters](#letters)
65
+
66
+ - [Postcards](#postcards)
67
+
68
+ - [Price](#price) **new update**
69
+
70
+ - [Accounts](#accounts)
71
+
72
+ - [Invoices](#invoices)
73
+
74
+ - [Examples](#examples)
75
+
76
+
77
+
78
+ ## Getting Started
79
+
80
+
81
+
82
+ Here's a general overview of the Seeuletter services available, click through to read more.
83
+
84
+
85
+
86
+ Please read through the official [API Documentation](https://docs.mysendingbox.fr/) to get a complete sense of what to expect from each endpoint.
87
+
88
+
89
+
90
+ ### Registration
91
+
92
+
93
+
94
+ First, you will need to first create an account at [Seeuletter.com](https://www.mysendingbox.fr/signup) and obtain your Test and Live API Keys.
95
+
96
+
97
+
98
+ Once you have created an account, you can access your API Keys from the [Settings Panel](https://www.mysendingbox.fr/app/dashboard/keys).
99
+
100
+
101
+
102
+
103
+ ### Installation
104
+
105
+
106
+
107
+ mysendingbox-node can be installed through the npm:
108
+
109
+
110
+
111
+ ```
112
+
113
+ $ npm install -S mysendingbox
114
+
115
+ ```
116
+
117
+
118
+
119
+ To build and install from the latest source:
120
+
121
+
122
+
123
+ ```
124
+
125
+ $ git clone git@github.com:exaland/mysendingbox-node.git
126
+
127
+ $ npm install
128
+
129
+ ```
130
+
131
+
132
+
133
+ ### Letters
134
+
135
+
136
+
137
+ #### Create a new letter - Callback style
138
+
139
+ ```javascript
140
+
141
+ var Seeuletter = require('mysendingbox')('YOUR API KEY');
142
+
143
+
144
+
145
+ // callback pattern
146
+
147
+ Seeuletter.letters.create({
148
+
149
+ description: 'Test Letter from the Node.js Wrapper',
150
+
151
+ to: {
152
+
153
+ name: 'Erlich',
154
+
155
+ address_line1: '30 rue de rivoli',
156
+
157
+ address_line2: '',
158
+
159
+ address_city: 'Paris',
160
+
161
+ address_country: 'France',
162
+
163
+ address_postalcode: '75004'
164
+
165
+ },
166
+
167
+ postage_type: 'prioritaire',
168
+
169
+ color: 'bw',
170
+
171
+ source_file: '<html>Hello, {{nom}}</html>',
172
+
173
+ source_file_type: 'html',
174
+
175
+ variables: {
176
+
177
+ nom : 'Seeuletter'
178
+
179
+ }
180
+
181
+ }, function (err, body) {
182
+
183
+ if (err) console.log('err : ' , err.message);
184
+
185
+ console.log('body : ', body)
186
+
187
+ })
188
+
189
+ ```
190
+
191
+
192
+
193
+ #### Create a new letter - Promise style
194
+
195
+
196
+
197
+ ```javascript
198
+
199
+ var Seeuletter = require('seeuletter')('YOUR API KEY');
200
+
201
+
202
+
203
+ // promise pattern
204
+
205
+ Seeuletter.letters.create({
206
+
207
+ description: 'Test Letter from the Node.js Wrapper',
208
+
209
+ to: {
210
+
211
+ name: 'Erlich',
212
+
213
+ address_line1: '30 rue de rivoli',
214
+
215
+ address_line2: '',
216
+
217
+ address_city: 'Paris',
218
+
219
+ address_country: 'France',
220
+
221
+ address_postalcode: '75004'
222
+
223
+ },
224
+
225
+ postage_type: 'prioritaire',
226
+
227
+ color: 'bw',
228
+
229
+ source_file: '<html>Hello, {{nom}}</html>',
230
+
231
+ source_file_type: 'html',
232
+
233
+ variables: {
234
+
235
+ nom : 'Seeuletter'
236
+
237
+ }
238
+
239
+ })
240
+
241
+ .then(function (response) {
242
+
243
+ console.log('response : ', response);
244
+
245
+ })
246
+
247
+ .catch(function (err) {
248
+
249
+ console.log('err : ', err);
250
+
251
+ });
252
+
253
+ ```
254
+
255
+
256
+
257
+ #### Create a new electronic letter - Promise style
258
+
259
+
260
+
261
+ ```javascript
262
+
263
+ var Seeuletter = require('seeuletter')('YOUR API KEY');
264
+
265
+
266
+
267
+ // promise pattern
268
+
269
+ Seeuletter.letters.createElectronic({
270
+
271
+ description: 'Test electronic letter from the Node.js Wrapper',
272
+
273
+ to: {
274
+
275
+ email: 'erlich.dumas@example.com',
276
+
277
+ first_name: 'Erlich',
278
+
279
+ last_name: 'Dumas',
280
+
281
+ status: 'individual'
282
+
283
+ },
284
+
285
+ postage_type: 'lre',
286
+
287
+ content: 'Please review the attached documents:',
288
+
289
+ source_file: '<html>Hello, {{nom}}</html>',
290
+
291
+ source_file_type: 'html',
292
+
293
+ variables: {
294
+
295
+ nom : 'Seeuletter'
296
+
297
+ }
298
+
299
+ })
300
+
301
+ .then(function (response) {
302
+
303
+ console.log('response : ', response);
304
+
305
+ })
306
+
307
+ .catch(function (err) {
308
+
309
+ console.log('err : ', err);
310
+
311
+ });
312
+
313
+ ```
314
+
315
+
316
+
317
+ #### List all Letters
318
+
319
+
320
+
321
+ ```javascript
322
+
323
+ var Seeuletter = require('seeuletter')('test_12345678901234567890')
324
+
325
+
326
+
327
+ Seeuletter.letters.list()
328
+
329
+ .then(function (response) {
330
+
331
+ console.log('response : ', response);
332
+
333
+ })
334
+
335
+ .catch(function (err) {
336
+
337
+ console.log('err : ', err);
338
+
339
+ });
340
+
341
+ ```
342
+
343
+
344
+
345
+ #### Retrieve a specific Letter
346
+
347
+
348
+
349
+ ```javascript
350
+
351
+ var Seeuletter = require('seeuletter')('test_12345678901234567890')
352
+
353
+
354
+
355
+ Seeuletter.letters.retrieve('LETTER_ID')
356
+
357
+ .then(function (response) {
358
+
359
+ console.log('response : ', response);
360
+
361
+ })
362
+
363
+ .catch(function (err) {
364
+
365
+ console.log('err : ', err);
366
+
367
+ });
368
+
369
+ ```
370
+
371
+
372
+
373
+ ### Postcards
374
+
375
+
376
+
377
+ #### Create a new postcard - Promise style
378
+
379
+
380
+
381
+ ```javascript
382
+
383
+ var Seeuletter = require('seeuletter')('YOUR API KEY');
384
+
385
+
386
+
387
+ // Create the address
388
+
389
+ Seeuletter.postcards.create({
390
+
391
+ description: 'Test Postcard from the Node.js Wrapper',
392
+
393
+ to: {
394
+
395
+ name: 'Erlich',
396
+
397
+ address_line1: '30 rue de rivoli',
398
+
399
+ address_line2: '',
400
+
401
+ address_city: 'Paris',
402
+
403
+ address_country: 'France',
404
+
405
+ address_postalcode: '75004'
406
+
407
+ },
408
+
409
+ // https://www.mysendingbox.fr/templates
410
+
411
+ source_file_front: 'YOUR TEMPLATE ID',
412
+
413
+ source_file_front_type: 'template_id',
414
+
415
+ source_file_back: 'YOUR TEMPLATE ID',
416
+
417
+ source_file_back_type: 'template_id',
418
+
419
+
420
+
421
+ variables: {
422
+
423
+ PRENOM: 'Erlich',
424
+
425
+ NOM: 'Bachman',
426
+
427
+ CODE_PROMO_BIENVENUE: 'CODE',
428
+
429
+ URL_COURTE_BIENVENUE: 'https://goo.gl/uqTHnD',
430
+
431
+ ADRESSE: '30 rue de Rivoli',
432
+
433
+ CODE_POSTAL : '75004',
434
+
435
+ VILLE : 'Paris',
436
+
437
+ PAYS : 'France'
438
+
439
+ }
440
+
441
+ })
442
+
443
+ .then(function (letter) {
444
+
445
+ console.log('The Seeuletter API Postcard responded : ', letter)
446
+
447
+ })
448
+
449
+ .catch(function (err) {
450
+
451
+ console.log('Error message : ', err.message)
452
+
453
+ })
454
+
455
+
456
+
457
+ ```
458
+ ### Price
459
+
460
+
461
+
462
+ #### Get Price for MySendingBox- Promise style
463
+
464
+
465
+
466
+ ```javascript
467
+
468
+ var Seeuletter = require('seeuletter')('YOUR API KEY');
469
+
470
+
471
+
472
+ // Get the Letter Price
473
+ Seeuletter.LettersPrice.getPrice({
474
+
475
+ // Letter type
476
+
477
+ postage_type: 'ecopli',
478
+
479
+ page_count: 11,
480
+
481
+ channel: 'paper',
482
+
483
+ color: 'color',
484
+
485
+ pack: 'business',
486
+
487
+ both_sides: false,
488
+
489
+ postage_speed: 'express'
490
+
491
+ }).then(function (response) {
492
+
493
+ console.log('[Get Price] The Seeuletter API responded : ', response)
494
+
495
+ }).catch(function (err) {
496
+
497
+ console.log('[Get Price] Error message : ', err.message)
498
+
499
+ console.log('[Get Price] Error status_code : ', err.status_code)
500
+
501
+ })
502
+ ```
503
+
504
+ ### Accounts
505
+
506
+
507
+
508
+ #### Create a new account for the company
509
+
510
+
511
+
512
+ ```javascript
513
+
514
+ var Seeuletter = require('seeuletter')('YOUR API KEY');
515
+
516
+
517
+
518
+ // Create the account
519
+
520
+ Seeuletter.accounts.create({
521
+
522
+ email: "msb.partner@example.com",
523
+
524
+ name: "Erlich Bachman",
525
+
526
+ phone: "+33104050607",
527
+
528
+ company_name: "MSB Partner",
529
+
530
+ address_line1: '30 rue de rivoli',
531
+
532
+ address_line2: '',
533
+
534
+ address_city: 'Paris',
535
+
536
+ address_country: 'France',
537
+
538
+ address_postalcode: '75004'
539
+
540
+ })
541
+
542
+ .then(function (account) {
543
+
544
+ console.log('The Seeuletter API Account responded : ', account)
545
+
546
+ })
547
+
548
+ .catch(function (err) {
549
+
550
+ console.log('Error message : ', err.message)
551
+
552
+ })
553
+
554
+
555
+
556
+ ```
557
+
558
+
559
+
560
+ #### Update the account company email
561
+
562
+
563
+
564
+ ```javascript
565
+
566
+ var Seeuletter = require('seeuletter')('YOUR API KEY');
567
+
568
+
569
+
570
+ // Update the account
571
+
572
+ Seeuletter.accounts.updateEmail("ACCOUNT COMPANY ID", "UPDATED EMAIL")
573
+
574
+ .then(function () {
575
+
576
+ console.log('The Seeuletter API Account responded with success')
577
+
578
+ })
579
+
580
+ .catch(function (err) {
581
+
582
+ console.log('Error message : ', err.message)
583
+
584
+ })
585
+
586
+
587
+
588
+ ```
589
+
590
+
591
+
592
+ ### Invoices
593
+
594
+
595
+
596
+ #### List all invoices for a company
597
+
598
+
599
+
600
+ ```javascript
601
+
602
+ var Seeuletter = require('seeuletter')('YOUR API KEY');
603
+
604
+
605
+
606
+ // Getting the invoice list
607
+
608
+ Seeuletter.invoices.list({
609
+
610
+ // Pass optional filter here as object
611
+
612
+ })
613
+
614
+ .then(function (response) {
615
+
616
+ console.log('The Seeuletter API Invoices responded : ', response)
617
+
618
+ })
619
+
620
+ .catch(function (err) {
621
+
622
+ console.log('Error message : ', err.message)
623
+
624
+ })
625
+
626
+
627
+
628
+ ```
629
+
630
+
631
+
632
+ #### Retrieve a specific invoice
633
+
634
+
635
+
636
+ ```javascript
637
+
638
+ var Seeuletter = require('seeuletter')('YOUR API KEY');
639
+
640
+
641
+
642
+ // Getting the invoice list
643
+
644
+ Seeuletter.invoices.retrieve("INVOICE ID")
645
+
646
+ .then(function (invoice) {
647
+
648
+ console.log('The Seeuletter API Invoice responded : ', invoice)
649
+
650
+ })
651
+
652
+ .catch(function (err) {
653
+
654
+ console.log('Error message : ', err.message)
655
+
656
+ })
657
+
658
+
659
+
660
+ ```
661
+
662
+
663
+
664
+ =======================
665
+
666
+
667
+
668
+ Copyright &copy; 2025 Exaland.app
669
+
670
+
671
+
672
+ Released under the MIT License, which can be found in the repository in `LICENSE.txt`.
package/lib/index.js ADDED
@@ -0,0 +1,60 @@
1
+ 'use strict';
2
+
3
+ var resources = require('./resources');
4
+ var clientVersion = require('../package.json').version;
5
+
6
+ var SEEULETTER_HOST = 'https://api.mysendingbox.fr/';
7
+ var SEEULETTER_USERAGENT = 'Seeuletter/v1 NodeBindings/' + clientVersion;
8
+
9
+ var Seeuletter = function (apiKey, options) {
10
+
11
+ if (!(this instanceof Seeuletter)) {
12
+ return new Seeuletter(apiKey, options);
13
+ }
14
+
15
+ if (apiKey && typeof apiKey === 'object') {
16
+ if (apiKey.options && typeof apiKey.options === 'object') {
17
+ options = apiKey.options;
18
+ apiKey = apiKey.options.apiKey || null;
19
+ } else {
20
+ options = apiKey;
21
+ apiKey = apiKey.apiKey || null;
22
+ }
23
+ }
24
+
25
+ this.options = {
26
+ apiKey: apiKey,
27
+ host: SEEULETTER_HOST,
28
+ userAgent: SEEULETTER_USERAGENT,
29
+ headers: { 'user-agent': SEEULETTER_USERAGENT }
30
+ };
31
+
32
+ if (options && typeof options === 'object') {
33
+
34
+ if (options.hasOwnProperty('apiVersion')) {
35
+ this.options.headers['Seeuletter-Version'] = options.apiVersion;
36
+ }
37
+
38
+ for (var key in options) {
39
+ this.options[key] = options[key];
40
+ }
41
+
42
+ }
43
+
44
+ this._initResources();
45
+ };
46
+
47
+ (function () {
48
+
49
+ this._initResources = function () {
50
+ var services = Object.keys(resources);
51
+
52
+ for (var i = 0; i < services.length; i++) {
53
+ var service = services[i];
54
+ this[service] = new resources[service](this);
55
+ }
56
+ };
57
+
58
+ }).call(Seeuletter.prototype);
59
+
60
+ module.exports = Seeuletter;
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ var ResourceBase = require('./resourceBase');
5
+
6
+ var Accounts = function (config) {
7
+ ResourceBase.call(this, 'accounts', Object.assign({}, config, { useBody: true }));
8
+ };
9
+
10
+ util.inherits(Accounts, ResourceBase);
11
+
12
+ (function () {
13
+
14
+ this.create = function (params, callback) {
15
+ return this._transmit('POST', null, null, params, callback);
16
+ };
17
+
18
+ this.updateEmail = function (id, email, callback) {
19
+ return this._transmit('PUT', id, null, { email: email }, callback);
20
+ }
21
+
22
+ }).call(Accounts.prototype);
23
+
24
+ module.exports = Accounts;
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ var fs = require('fs');
4
+ var files = fs.readdirSync(__dirname);
5
+ var resources = {};
6
+
7
+ for (var i = 0; i < files.length; i++) {
8
+ var resource = files[i];
9
+
10
+ if (resource === 'index.js' ||
11
+ resource === 'resourceBase.js' ||
12
+ resource.indexOf('.js') < 0) {
13
+ continue;
14
+ }
15
+
16
+ resource = resource.replace('.js', '');
17
+ resources[resource] = require('./' + resource);
18
+ }
19
+
20
+ module.exports = resources;
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ var ResourceBase = require('./resourceBase');
5
+
6
+ var Invoices = function (config) {
7
+ ResourceBase.call(this, 'invoices', config);
8
+ };
9
+
10
+ util.inherits(Invoices, ResourceBase);
11
+
12
+ (function () {
13
+
14
+ this.list = function (options, callback) {
15
+ if (typeof options === 'function') {
16
+ callback = options;
17
+ options = {};
18
+ }
19
+
20
+ return this._transmit('GET', null, options, null, callback);
21
+ };
22
+
23
+ this.retrieve = function (id, callback) {
24
+ return this._transmit('GET', id, null, null, callback);
25
+ }
26
+
27
+ }).call(Invoices.prototype);
28
+
29
+ module.exports = Invoices;
@@ -0,0 +1,44 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ var ResourceBase = require('./resourceBase');
5
+
6
+ const { processCreateResourceParams } = require('../utils/params');
7
+
8
+ var Letters = function (config) {
9
+ ResourceBase.call(this, 'letters', config);
10
+ };
11
+
12
+ util.inherits(Letters, ResourceBase);
13
+
14
+ (function () {
15
+
16
+ this.list = function (options, callback) {
17
+
18
+ if (typeof options === 'function') {
19
+ callback = options;
20
+ options = {};
21
+ }
22
+
23
+ return this._transmit('GET', null, options, null, callback);
24
+ };
25
+
26
+ this.retrieve = function (id, callback) {
27
+ return this._transmit('GET', id, null, null, callback);
28
+ };
29
+
30
+ this.delete = function (id, callback) {
31
+ return this._transmit('DELETE', id, null, null, callback);
32
+ };
33
+
34
+ this.create = function (params, callback) {
35
+ return this._transmit('POST', null, null, processCreateResourceParams(params), callback);
36
+ };
37
+
38
+ this.createElectronic = function (params, callback) {
39
+ return this._transmit('POST', 'electronic', null, processCreateResourceParams(params), callback);
40
+ }
41
+
42
+ }).call(Letters.prototype);
43
+
44
+ module.exports = Letters;
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ var ResourceBase = require('./resourceBase');
5
+
6
+ const { processCreateResourceParams } = require('../utils/params');
7
+
8
+ var Postcards = function (config) {
9
+ ResourceBase.call(this, 'postcards', config);
10
+ };
11
+
12
+ util.inherits(Postcards, ResourceBase);
13
+
14
+ (function () {
15
+
16
+ this.list = function (options, callback) {
17
+
18
+ if (typeof options === 'function') {
19
+ callback = options;
20
+ options = {};
21
+ }
22
+
23
+ return this._transmit('GET', null, options, null, callback);
24
+ };
25
+
26
+ this.retrieve = function (id, callback) {
27
+ return this._transmit('GET', id, null, null, callback);
28
+ };
29
+
30
+ this.delete = function (id, callback) {
31
+ return this._transmit('DELETE', id, null, null, callback);
32
+ };
33
+
34
+ this.create = function (params, callback) {
35
+ return this._transmit('POST', null, null, processCreateResourceParams(params), callback);
36
+ };
37
+
38
+ }).call(Postcards.prototype);
39
+
40
+ module.exports = Postcards;
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ var util = require('util');
4
+ var ResourceBase = require('./resourceBase');
5
+
6
+ const { processCreateResourceParams } = require('../utils/params');
7
+
8
+ var LettersPrice = function (config) {
9
+ ResourceBase.call(this, 'letters/price', config);
10
+ };
11
+
12
+ util.inherits(LettersPrice, ResourceBase);
13
+
14
+ (function () {
15
+
16
+ this.list = function (options, callback) {
17
+
18
+ if (typeof options === 'function') {
19
+ callback = options;
20
+ options = {};
21
+ }
22
+
23
+ return this._transmit('GET', null, options, null, callback);
24
+ };
25
+
26
+
27
+ this.getPrice = function (params, callback) {
28
+ return this._transmit('POST', null, null, processCreateResourceParams(params), callback);
29
+ };
30
+
31
+
32
+ }).call(LettersPrice.prototype);
33
+
34
+ module.exports = LettersPrice;
@@ -0,0 +1,117 @@
1
+ 'use strict'
2
+
3
+ const axios = require('axios')
4
+ const stream = require('stream')
5
+
6
+ var ResourceBase = function (endpoint, config) {
7
+ this.uri = config.options.host + endpoint
8
+ this.config = config.options
9
+ this.useBody = config.useBody || false
10
+
11
+ };
12
+ (function () {
13
+ this._transmit = function (method, uri, qs, form, headers, callback) {
14
+ if (typeof headers === 'function') {
15
+ callback = headers
16
+ headers = {}
17
+ } else {
18
+ headers = headers || {}
19
+ }
20
+
21
+ // Ajoutez ou mettez à jour les headers
22
+ for (let headerKey in this.config.headers) {
23
+ headers[headerKey] = this.config.headers[headerKey]
24
+ }
25
+
26
+ const url = this.uri + (uri ? '/' + uri : '')
27
+
28
+ // Préparer la configuration axios
29
+ const axiosConfig = {
30
+ url: url,
31
+ method: method,
32
+ headers: headers,
33
+ auth: {
34
+ username: this.config.apiKey,
35
+ password: ''
36
+ },
37
+ // Par défaut, axios sérialise en JSON
38
+ // Ajoutez des options spécifiques selon votre besoin
39
+ }
40
+
41
+ // Gestion des paramètres de requête
42
+ if (qs) {
43
+ axiosConfig.params = qs
44
+ }
45
+
46
+ // Gestion du corps de la requête
47
+ if (form) {
48
+ // Vérifier si le corps doit être envoyé en tant que formData
49
+ let isMultiPartForm = false
50
+ for (const key in form) {
51
+ const val = form[key]
52
+ if ((val instanceof stream.Stream) || (val && val.hasOwnProperty('value'))) {
53
+ isMultiPartForm = true
54
+ break
55
+ }
56
+ }
57
+
58
+ if (this.useBody) {
59
+ axiosConfig.data = form
60
+ } else if (isMultiPartForm) {
61
+ // Si vous utilisez des fichiers stream ou formData, utilisez form-data
62
+ const FormData = require('form-data')
63
+ const formData = new FormData()
64
+ for (const key in form) {
65
+ formData.append(key, form[key])
66
+ }
67
+ axiosConfig.data = formData
68
+ axiosConfig.headers = {
69
+ ...headers,
70
+ ...formData.getHeaders()
71
+ }
72
+ } else {
73
+ axiosConfig.data = form
74
+ }
75
+ }
76
+
77
+ // Appel axios avec gestion Promises
78
+ const promise = axios(axiosConfig)
79
+ .then(response => {
80
+ const body = response.data
81
+ // Ajout de la propriété _response
82
+ Object.defineProperty(body, '_response', {
83
+ enumerable: false,
84
+ writable: false,
85
+ value: response
86
+ })
87
+ return body
88
+ })
89
+ .catch(err => {
90
+ if (err.response) {
91
+ const resp = err.response
92
+ const body = resp.data
93
+ if (body && body.error) {
94
+ const error = new Error(body.error.message)
95
+ error.status_code = body.error.status_code || resp.status
96
+ error._response = resp
97
+ throw error
98
+ }
99
+ if (resp.status >= 500) {
100
+ const error = new Error(resp.statusText)
101
+ error.status_code = resp.status
102
+ error._response = resp
103
+ throw error
104
+ }
105
+ }
106
+ throw err
107
+ })
108
+
109
+ if (callback && typeof callback === 'function') {
110
+ promise.then(result => callback(null, result)).catch(err => callback(err))
111
+ } else {
112
+ return promise
113
+ }
114
+ }
115
+ }).call(ResourceBase.prototype)
116
+
117
+ module.exports = ResourceBase
@@ -0,0 +1,23 @@
1
+ const flatten = require('flat');
2
+
3
+ function processCreateResourceParams(params) {
4
+ const files = {};
5
+
6
+ Object.keys(params)
7
+ .filter(key => key.startsWith('source_file') && !key.endsWith('_type'))
8
+ .forEach(key => {
9
+ files[key] = Buffer.isBuffer(params[key])
10
+ ? {
11
+ value: params[key],
12
+ options: { filename: `${key}.pdf` }
13
+ }
14
+ : params[key];
15
+ delete params[key];
16
+ });
17
+
18
+ return { ...flatten(params), ...files };
19
+ }
20
+
21
+ module.exports = {
22
+ processCreateResourceParams,
23
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "mysendingbox",
3
+ "description": "Mysendingbox API wrapper",
4
+ "keywords": [
5
+ "Mysendingbox",
6
+ "Mysendingbox-node",
7
+ "rest",
8
+ "exaland",
9
+ "api",
10
+ "wrapper",
11
+ "Mysendingbox.com",
12
+ "printing"
13
+ ],
14
+ "version": "1.1.0",
15
+ "homepage": "https://github.com/exaland/mysendingbox-node",
16
+ "author": "Exaland Concept For -> Mysendingbox <hello@mysendingbox.fr> (https://www.mysendingbox.fr/)",
17
+ "dependencies": {
18
+ "bluebird": "^3.7.2",
19
+ "flat": "^6.0.1",
20
+ "axios": "^1.16.1"
21
+ },
22
+ "devDependencies": {
23
+ "moment": "^2.30.1"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/exaland/mysendingbox-node.git"
28
+ },
29
+ "bugs:": "https://github.com/exaland/mysendingbox-node/issues",
30
+ "main": "./lib/index",
31
+ "engines": {
32
+ "node": ">= 0.10.0"
33
+ },
34
+ "files": [
35
+ "lib",
36
+ "LICENSE.txt"
37
+ ]
38
+ }