Skip to content

Commit 1485115

Browse files
author
Ruslan Tereshchenko
authored
Merge pull request signnow#30 from signnow/SN-2611
Remove document method
2 parents 48ddc99 + cff353d commit 1485115

File tree

5 files changed

+110
-18
lines changed

5 files changed

+110
-18
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Implemented *Remove Document* method
13+
- Added example how to utilize *Remove Document* method into [README](https://github.com/signnow/SignNowNodeSDK/blob/master/README.md#remove-document)
1214
- Created [Applet](https://github.com/signnow/SignNowNodeSDK/blob/master/samples/applets/extract-fields.js) for testing *Upload Document with Field Extraction* method
1315
- Created [Applet](https://github.com/signnow/SignNowNodeSDK/blob/master/samples/applets/view-document.js) for testing *View Document* method
1416
- Created [Applet](https://github.com/signnow/SignNowNodeSDK/blob/master/samples/applets/download-document.js) for testing *Download Document* method
17+
- Created [Applet](https://github.com/signnow/SignNowNodeSDK/blob/master/samples/applets/remove-document.js) for testing *Remove Document* method
1518

1619
## [v1.2.0] - 2019-07-18
1720

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SignNow REST Service Wrapper
3131
* [Create a One-time Use Download URL](#share-document)
3232
* [Merge Existing Documents](#merge-documents)
3333
* [Get Document History](#get-history)
34+
* [Remove Document](#remove-document)
3435
* [Enumerations](#enumerations)
3536
* [Add Enumeration Field to a Document](#add-enumeration)
3637
* [Add Enumeration Options to the Field](#enumeration-options)
@@ -324,6 +325,17 @@ api.document.history({
324325
});
325326
```
326327

328+
#### <a name="remove-document"></a>Remove Document
329+
330+
```javascript
331+
api.document.remove({
332+
token: 'your auth token',
333+
id: 'document id',
334+
}, (err, res) => {
335+
// handle error or process response data
336+
});
337+
```
338+
327339
### <a name="enumerations"></a>Enumerations
328340

329341
#### <a name="add-enumeration"></a>Add Enumeration Field to a Document

lib/document.js

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -666,17 +666,49 @@ class Document {
666666

667667
// get the history of a document
668668
static history ({ id, token }, callback) {
669-
const req = https.request(buildRequestOptions({
670-
method: 'GET',
671-
path: `/document/${id}/history`,
672-
authorization: {
673-
type: 'Bearer',
674-
token,
675-
},
676-
}), responseHandler(callback));
677-
678-
req.on('error', errorHandler(callback));
679-
req.end();
669+
https
670+
.request(buildRequestOptions({
671+
method: 'GET',
672+
path: `/document/${id}/history`,
673+
authorization: {
674+
type: 'Bearer',
675+
token,
676+
},
677+
}), responseHandler(callback))
678+
.on('error', errorHandler(callback))
679+
.end();
680+
}
681+
682+
/**
683+
* Remove document payload
684+
* @typedef {Object} DocumentRemoveParams
685+
* @property {string} id - id of specific document
686+
* @property {string} token - your auth token
687+
*/
688+
689+
/**
690+
* Remove document response data
691+
* @typedef {Object} DocumentRemoveResponse
692+
* @property {string} status - status of deletion, e.g. 'success'
693+
*/
694+
695+
/**
696+
* Removes a specified document from folder
697+
* @param {DocumentRemoveParams} data - remove document payload
698+
* @param {function(err: ApiErrorResponse, res: DocumentRemoveResponse)} [callback] - error first node.js callback
699+
*/
700+
static remove ({ id, token }, callback) {
701+
https
702+
.request(buildRequestOptions({
703+
method: 'DELETE',
704+
path: `/document/${id}`,
705+
authorization: {
706+
type: 'Bearer',
707+
token,
708+
},
709+
}), responseHandler(callback))
710+
.on('error', errorHandler(callback))
711+
.end();
680712
}
681713

682714
}

samples/applets/download-document.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const api = require('../../lib')({
2323
});
2424

2525
const { oauth2: { requestToken: getAccessToken } } = api;
26-
const { document: { download: documentDownload } } = api;
26+
const { document: { download: downloadDocument } } = api;
2727

2828
getAccessToken({
2929
username,
@@ -35,16 +35,16 @@ getAccessToken({
3535
const { access_token: token } = tokenRes;
3636
const absolutePath = `${pathToSaveFile}/${documentId}.pdf`;
3737

38-
documentDownload({
38+
downloadDocument({
3939
id: documentId,
4040
token,
41-
}, (downloadError, downloadResponse) => {
42-
if (downloadError) {
43-
console.log(downloadError);
41+
}, (downloadErr, downloadRes) => {
42+
if (downloadErr) {
43+
console.error(downloadErr);
4444
} else {
4545
try {
46-
fs.writeFileSync(absolutePath, downloadResponse, { encoding: 'binary' });
47-
console.log(`Document has been downloaded. Check your ${absolutePath} directory`);
46+
fs.writeFileSync(absolutePath, downloadRes, { encoding: 'binary' });
47+
console.log(`Document has been downloaded. Check your ${pathToSaveFile} directory`);
4848
} catch (err) {
4949
console.log(err);
5050
}

samples/applets/remove-document.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* to run remove document applet from the project root folder type in your console:
3+
* > node samples/applets/download-document <cliend_id> <client_secret> <username> <password> <document_id>
4+
* <cliend_id>, <client_secret>, <username>, <password>, <document_id> - are required params
5+
*/
6+
7+
'use strict';
8+
9+
const [
10+
clientId,
11+
clientSecret,
12+
username,
13+
password,
14+
documentId,
15+
] = process.argv.slice(2);
16+
17+
const api = require('../../lib')({
18+
credentials: Buffer.from(`${clientId}:${clientSecret}`).toString('base64'),
19+
production: false,
20+
});
21+
22+
const { oauth2: { requestToken: getAccessToken } } = api;
23+
const { document: { remove: removeDocument } } = api;
24+
25+
getAccessToken({
26+
username,
27+
password,
28+
}, (tokenErr, tokenRes) => {
29+
if (tokenErr) {
30+
console.error(tokenErr);
31+
} else {
32+
const { access_token: token } = tokenRes;
33+
34+
removeDocument({
35+
id: documentId,
36+
token,
37+
}, (removeErr, removeRes) => {
38+
if (removeErr) {
39+
console.error(removeErr);
40+
} else {
41+
console.log(removeRes);
42+
}
43+
});
44+
}
45+
});

0 commit comments

Comments
 (0)