Skip to content

Commit bcd571c

Browse files
paigesrossiInbarGazitkarissarjacobsenmeihDS
authored
API dropdown (manual restart solution) (#45)
* Updating large refactoring of the Node.js code examples without removing the old files yet. * update step comments * first working version * changes so user restarts app * removing dependency * adding more instructions for restarting app Co-authored-by: Inbar Gazit <inbar.gazit@docusign.com> Co-authored-by: Karissa Jacobsen <karissa.jacobsen@docusign.com> Co-authored-by: meihDS <70775251+meihDS@users.noreply.github.com>
1 parent 95d92c8 commit bcd571c

File tree

14 files changed

+139
-35
lines changed

14 files changed

+139
-35
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
### GitHub repo: [code-examples-node](./README.md)
44

5-
This GitHub repo includes code examples for the DocuSign Admin API, Click API, eSignature REST API, Monitor API, and Rooms API. To switch between API code examples, modify the `examplesApi` setting in the appsettings.json file. Set only one API type to `true` and set the remaining to `false`.
6-
7-
If none of the API types are set to `true`, the DocuSign eSignature REST API code examples will be shown. If multiple API types are set to `true`, only the first will be shown.
5+
This GitHub repo includes code examples for the DocuSign Admin API, Click API, eSignature REST API, Monitor API, and Rooms API. By default, the launcher will display the eSignature examples. To switch between API code examples, select "Choose API" in the top menu.
86

97

108
## Introduction

config/appsettings.example.json

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,6 @@
2424
"documentation": null,
2525
"multiSourceChooser": false,
2626
"quickstart" : "{QUICKSTART_VALUE}",
27-
"examplesApi": {
28-
"isESignatureApi": true,
29-
"isRoomsApi": false,
30-
"isClickApi": false,
31-
"isMonitorApi": false,
32-
"isAdminApi": false
33-
},
3427
"roomsApiUrl": "https://demo.rooms.docusign.com",
3528
"clickAPIUrl": "https://demo.docusign.net/clickapi",
3629
"adminAPIUrl": "https://api-d.docusign.net/management",

config/examplesApi.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"examplesApi":{"isESignatureApi":true,"isRoomsApi":false,"isClickApi":false,"isMonitorApi":false,"isAdminApi":false}}

index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const flash = require('express-flash');
1818
const helmet = require('helmet'); // https://expressjs.com/en/advanced/best-practice-security.html
1919
const moment = require('moment');
2020
const csrf = require('csurf'); // https://www.npmjs.com/package/csurf
21+
const examplesApi = require('./config/examplesApi.json');
2122

2223
const eg001 = require('./lib/eSignature/controllers/eg001EmbeddedSigning');
2324

@@ -75,6 +76,7 @@ let app = express()
7576
res.locals.user = req.user;
7677
res.locals.session = req.session;
7778
res.locals.dsConfig = { ...dsConfig, docOptions: docOptions, docNames: docNames };
79+
res.locals.examplesApi = examplesApi
7880
res.locals.hostUrl = hostUrl; // Used by DSAuthCodeGrant#logout
7981
next()
8082
})) // Send user info to views
@@ -93,6 +95,8 @@ let app = express()
9395
})
9496
// Routes
9597
.get('/', commonControllers.indexController)
98+
.get('/ds/choose_api', commonControllers.chooseApi)
99+
.get('/ds/switch_api', commonControllers.switchApi)
96100
.get('/ds/login', commonControllers.login)
97101
.get('/ds/callback', [dsLoginCB1, dsLoginCB2]) // OAuth callbacks. See below
98102
.get('/ds/logout', commonControllers.logout)
@@ -101,7 +105,7 @@ let app = express()
101105
.get('/ds-return', commonControllers.returnController)
102106
.use(csrfProtection) // CSRF protection for the following routes
103107
;
104-
if (dsConfig.examplesApi.isRoomsApi) {
108+
if (examplesApi.examplesApi.isRoomsApi) {
105109
app.get('/eg001rooms', eg001rooms.getController)
106110
.post('/eg001rooms', eg001rooms.createController)
107111
.get('/eg002rooms', eg002rooms.getController)
@@ -120,7 +124,7 @@ if (dsConfig.examplesApi.isRoomsApi) {
120124
.post('/eg008rooms', eg008rooms.createController)
121125
.get('/eg009rooms', eg009rooms.getController)
122126
.post('/eg009rooms', eg009rooms.createController)
123-
} else if (dsConfig.examplesApi.isClickApi) {
127+
} else if (examplesApi.examplesApi.isClickApi) {
124128
app.get('/eg001', eg001click.getController)
125129
.post('/eg001', eg001click.createController)
126130
.get('/eg002', eg002click.getController)
@@ -131,10 +135,10 @@ if (dsConfig.examplesApi.isRoomsApi) {
131135
.post('/eg004', eg004click.createController)
132136
.get('/eg005', eg005click.getController)
133137
.post('/eg005', eg005click.createController)
134-
} else if (dsConfig.examplesApi.isMonitorApi) {
138+
} else if (examplesApi.examplesApi.isMonitorApi) {
135139
app.get('/eg001', eg001monitor.getController)
136140
.post('/eg001', eg001monitor.createController)
137-
} else if (dsConfig.examplesApi.isAdminApi) {
141+
} else if (examplesApi.examplesApi.isAdminApi) {
138142
app.get('/eg001', eg001admin.getController)
139143
.post('/eg001', eg001admin.createController)
140144
.get('/eg002', eg002admin.getController)
@@ -260,13 +264,13 @@ const ADMIN_SCOPES = [
260264
"domain_read", "identity_provider_read", "signature"
261265
];
262266
let scope;
263-
if (dsConfig.examplesApi.isRoomsApi) {
267+
if (examplesApi.examplesApi.isRoomsApi) {
264268
scope = ROOM_SCOPES;
265-
} else if (dsConfig.examplesApi.isClickApi) {
269+
} else if (examplesApi.examplesApi.isClickApi) {
266270
scope = CLICK_SCOPES;
267-
} else if (dsConfig.examplesApi.isMonitorApi) {
271+
} else if (examplesApi.examplesApi.isMonitorApi) {
268272
scope = MONITOR_SCOPES;
269-
} else if (dsConfig.examplesApi.isAdminApi) {
273+
} else if (examplesApi.examplesApi.isAdminApi) {
270274
scope = ADMIN_SCOPES;
271275
} else {
272276
scope = SCOPES;

lib/DSAuthCodeGrant.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const moment = require('moment')
2121
// minutes later or it will be replaced
2222

2323
;
24+
2425
/**
2526
* Manages OAuth Authentication Code Grant with DocuSign.
2627
* @constructor

lib/DSJwtAuth.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @author DocuSign
1010
*/
1111

12+
const examplesApi = require('../config/examplesApi.json');
1213

1314
'use strict';
1415
let DsJwtAuth = function _DsJwtAuth(req) {
@@ -20,11 +21,11 @@ let DsJwtAuth = function _DsJwtAuth(req) {
2021
this.basePath = req.user && req.user.basePath;
2122
this._tokenExpiration = req.user && req.user.tokenExpirationTimestamp;
2223
this.scopes = "signature"
23-
if (dsConfig.examplesApi.isRoomsApi) {
24+
if (examplesApi.examplesApi.isRoomsApi) {
2425
this.scopes += " dtr.rooms.read dtr.rooms.write dtr.documents.read dtr.documents.write dtr.profile.read dtr.profile.write dtr.company.read dtr.company.write room_forms";
25-
} else if(dsConfig.examplesApi.isClickApi) {
26+
} else if(examplesApi.examplesApi.isClickApi) {
2627
this.scopes += " click.manage click.send"
27-
} else if(dsConfig.examplesApi.isAdminApi) {
28+
} else if(examplesApi.examplesApi.isAdminApi) {
2829
this.scopes += " organization_read group_read permission_read user_read user_write account_read domain_read identity_provider_read";
2930
}
3031

lib/admin/examples/createUser.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const createUser = async(args) => {
2121
apiClient.addDefaultHeader("Authorization", "Bearer " + args.accessToken);
2222
// Step 2 end
2323

24-
// Step 3 start
24+
// Step 5 start
2525
const userData = {
2626
user_name: args.user_name,
2727
first_name: args.first_name,
@@ -42,25 +42,29 @@ const createUser = async(args) => {
4242
}
4343
]
4444
};
45-
// Step 3 end
45+
// Step 5 end
4646

47-
// Step 4 start
47+
// Step 6 start
4848
const usersApi = new docusignAdmin.UsersApi(apiClient);
4949
return usersApi.createUser(userData, args.organizationId);
50-
// Step 4 end
50+
// Step 6 end
5151
}
5252

5353
const getPermissionProfilesAndGroups = async(args) => {
5454
const apiClient = new docusignESign.ApiClient();
5555
apiClient.setBasePath(args.basePath);
5656
apiClient.addDefaultHeader("Authorization", "Bearer " + args.accessToken);
5757

58+
// Step 3 start
5859
const accountsApi = new docusignESign.AccountsApi(apiClient);
5960
const profiles = await accountsApi.listPermissions(args.accountId);
61+
// Step 3 end
6062

63+
// Step 4 start
6164
const groupsApi = new docusignESign.GroupsApi(apiClient);
6265
const groups = await groupsApi.listGroups(args.accountId);
63-
66+
// Step 4 end
67+
6468
return { profiles, groups };
6569
}
6670

lib/common/DSJwtAuth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* @author DocuSign
1010
*/
1111

12+
const examplesApi = require('.../config/examplesApi.json');
1213

1314
'use strict';
1415
let DsJwtAuth = function _DsJwtAuth(req) {
@@ -20,9 +21,9 @@
2021
this.basePath = req.user && req.user.basePath;
2122
this._tokenExpiration = req.user && req.user.tokenExpirationTimestamp;
2223
this.scopes = "signature"
23-
if (dsConfig.examplesApi.isRoomsApi) {
24+
if (examplesApi.examplesApi.isRoomsApi) {
2425
this.scopes += " dtr.rooms.read dtr.rooms.write dtr.documents.read dtr.documents.write dtr.profile.read dtr.profile.write dtr.company.read dtr.company.write room_forms";
25-
} else if(dsConfig.examplesApi.isClickApi) {
26+
} else if(examplesApi.examplesApi.isClickApi) {
2627
this.scopes += " click.manage click.send"
2728
}
2829

lib/commonControllers.js

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
const fs = require('fs')
88
, dsConfig = require('../config/appsettings.json')
9+
, examplesApi = require('../config/examplesApi.json')
910
, documentationTopic = 'auth-code-grant-node'
1011
;
1112

@@ -19,7 +20,7 @@ commonControllers.indexController = (req, res) => {
1920
console.debug ('quickstart mode on');
2021
return res.redirect('/eg001');
2122
}
22-
else if (dsConfig.examplesApi['isMonitorApi']){
23+
else if (examplesApi.examplesApi['isMonitorApi']){
2324
res.render('pages/index_monitor_examples', {
2425
title: "Home",
2526
documentation: dsConfig.documentation + documentationTopic,
@@ -38,7 +39,7 @@ commonControllers.indexController = (req, res) => {
3839
commonControllers.mustAuthenticateController = (req, res) => {
3940
if (dsConfig.quickstart == 'true')
4041
res.redirect('login');
41-
else if (dsConfig.examplesApi.isMonitorApi) {
42+
else if (examplesApi.examplesApi.isMonitorApi) {
4243
// Monitor API supports JWT only
4344
req.dsAuth = req.dsAuthJwt;
4445
req.dsAuth.login(req, res, '');
@@ -47,6 +48,73 @@ commonControllers.mustAuthenticateController = (req, res) => {
4748
res.render('pages/ds_must_authenticate', {title: "Authenticate with DocuSign"});
4849
}
4950

51+
commonControllers.chooseApi = (req, res) => {
52+
res.render('pages/choose_api', {title: "Choose API"});
53+
}
54+
55+
commonControllers.switchApi = (req, res) => {
56+
const { chosenApi } = req.query;
57+
const fs = require('fs');
58+
59+
let apiChoice = {
60+
examplesApi: {
61+
isESignatureApi: false,
62+
isRoomsApi: false,
63+
isClickApi: false,
64+
isMonitorApi: false,
65+
isAdminApi: false
66+
}
67+
}
68+
69+
if (chosenApi == "eSignature") {
70+
apiChoice.examplesApi.isESignatureApi = true;
71+
let data = JSON.stringify(apiChoice);
72+
fs.writeFile ("config/examplesApi.json", data, function(err) {
73+
if (err) throw err;
74+
console.log('complete');
75+
}
76+
);
77+
}
78+
else if (chosenApi == "rooms") {
79+
apiChoice.examplesApi.isRoomsApi = true;
80+
let data = JSON.stringify(apiChoice);
81+
fs.writeFile ("config/examplesApi.json", data, function(err) {
82+
if (err) throw err;
83+
console.log('complete');
84+
}
85+
);
86+
}
87+
else if (chosenApi == "click") {
88+
apiChoice.examplesApi.isClickApi = true;
89+
let data = JSON.stringify(apiChoice);
90+
fs.writeFile ("config/examplesApi.json", data, function(err) {
91+
if (err) throw err;
92+
console.log('complete');
93+
}
94+
);
95+
}
96+
else if (chosenApi == "monitor") {
97+
apiChoice.examplesApi.isMonitorApi = true;
98+
let data = JSON.stringify(apiChoice);
99+
fs.writeFile ("config/examplesApi.json", data, function(err) {
100+
if (err) throw err;
101+
console.log('complete');
102+
}
103+
);
104+
}
105+
else if (chosenApi == "admin") {
106+
apiChoice.examplesApi.isAdminApi = true;
107+
let data = JSON.stringify(apiChoice);
108+
fs.writeFile ("config/examplesApi.json", data, function(err) {
109+
if (err) throw err;
110+
console.log('complete');
111+
}
112+
);
113+
}
114+
115+
res.render('pages/restart', {title: "Restart the app"});
116+
}
117+
50118
commonControllers.login = (req, res, next) => {
51119
const { auth } = req.query;
52120
if (auth === 'grand-auth') {

views/pages/choose_api.ejs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<% include ../partials/genericHead %>
2+
<div style="margin:1% 5%;">
3+
<h1 class="display-4">Please Choose an API</h1>
4+
<p class="lead">
5+
<form class="eg" action="/ds/switch_api" method="get" data-busy="form">
6+
<div class="form-group">
7+
<label>Please choose your API</label>
8+
<select id="chosenApi" name="chosenApi" class="form-control">
9+
<option value="eSignature" selected>eSignature</option>
10+
<option value="rooms">Rooms</option>
11+
<option value="click">Click</option>
12+
<option value="monitor">Monitor</option>
13+
<option value="admin">Admin</option>
14+
</select><br>
15+
<button class="btn btn-docu btn-lg">Choose your desired API</button>
16+
</div>
17+
</form>
18+
</p>
19+
<hr class="my-4">
20+
21+
</div>
22+
<% include ../partials/genericFoot %>

0 commit comments

Comments
 (0)