Skip to content

Commit b6ea209

Browse files
Merge pull request #5 from Crowdhandler/maintenance/private-api-support-tweaks
recordPerformance() improvement and more examples
2 parents 43a98c6 + d45c34c commit b6ea209

8 files changed

Lines changed: 183 additions & 43 deletions

File tree

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,35 +273,35 @@ This method returns a `Resource` instance for interacting with the `/v1/codes/ID
273273

274274
This method returns a `Resource` instance for interacting with the `/v1/domains/ID_PLACEHOLDER` endpoint.
275275

276-
### domainsIPs()
276+
### domainIPs()
277277

278278
This method returns a `Resource` instance for interacting with the `/v1/domains/ID_PLACEHOLDER/ips` endpoint.
279279

280-
### domainsReports()
280+
### domainReports()
281281

282282
This method returns a `Resource` instance for interacting with the `/v1/domains/ID_PLACEHOLDER/reports` endpoint.
283283

284-
### domainsRequests()
284+
### domainRequests()
285285

286286
This method returns a `Resource` instance for interacting with the `/v1/domains/ID_PLACEHOLDER/requests` endpoint.
287287

288-
### domainsRooms()
288+
### domainRooms()
289289

290290
This method returns a `Resource` instance for interacting with the `/v1/domains/ID_PLACEHOLDER/rooms` endpoint.
291291

292-
### domainsURLs()
292+
### domainURLs()
293293

294294
This method returns a `Resource` instance for interacting with the `/v1/domains/ID_PLACEHOLDER/urls` endpoint.
295295

296296
### groups()
297297

298298
This method returns a `Resource` instance for interacting with the `/v1/groups/ID_PLACEHOLDER` endpoint.
299299

300-
### groupsBatch()
300+
### groupBatch()
301301

302302
This method returns a `Resource` instance for interacting with the `/v1/groups/ID_PLACEHOLDER/batch` endpoint.
303303

304-
### groupsCodes()
304+
### groupCodes()
305305

306306
This method returns a `Resource` instance for interacting with the `/v1/groups/ID_PLACEHOLDER/codes` endpoint.
307307

@@ -317,11 +317,11 @@ This method returns a `Resource` instance for interacting with the `/v1/reports/
317317

318318
This method returns a `Resource` instance for interacting with the `/v1/rooms/ID_PLACEHOLDER` endpoint.
319319

320-
### roomsReports()
320+
### roomReports()
321321

322322
This method returns a `Resource` instance for interacting with the `/v1/rooms/ID_PLACEHOLDER/reports` endpoint.
323323

324-
### roomsSessions()
324+
### roomSessions()
325325

326326
This method returns a `Resource` instance for interacting with the `/v1/rooms/ID_PLACEHOLDER/sessions` endpoint.
327327

examples/node-16-9-1/express-4-16-1/api_only_protection/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const crowdHandlerMiddleware = async (req, res, next) => {
1515
const ch_gatekeeper = new crowdhandler.Gatekeeper(
1616
public_client,
1717
ch_context,
18-
{publicKey: publicKey}
18+
{ publicKey: publicKey }
1919
);
2020

2121
let decodedBody;
@@ -81,6 +81,18 @@ router.all("*", (req, res, next) => {
8181
if (res.locals.ch_gatekeeper) {
8282
res.locals.ch_gatekeeper.recordPerformance();
8383
}
84+
85+
/*
86+
* IMPORTANT CONSIDERATION:
87+
*
88+
* The default status code sent to CrowdHandler is '200'. However, if a different status code needs to be sent,
89+
* it can be achieved by passing it as a parameter to the 'recordPerformance' method.
90+
*
91+
* Example:
92+
* chGatekeeper.recordPerformance({status: 404});
93+
*
94+
* If you are using CrowdHandler's autotune feature, is is crucial to pass accurate status codes to CrowdHandler to ensure the precision of analytics and autotune results.
95+
*/
8496
});
8597
});
8698

examples/node-16-9-1/express-4-16-1/full_protection/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const crowdHandlerMiddleware = async (req, res, next) => {
77
const publicKey = "YOUR_PUBLIC_KEY";
88
const publicClient = new crowdhandler.PublicClient(publicKey);
99
const chContext = new crowdhandler.RequestContext(req, res);
10-
const chGatekeeper = new crowdhandler.Gatekeeper(publicClient, chContext, {publicKey: publicKey});
10+
const chGatekeeper = new crowdhandler.Gatekeeper(publicClient, chContext, {
11+
publicKey: publicKey,
12+
});
1113

1214
try {
1315
const chStatus = await chGatekeeper.validateRequest();
@@ -41,7 +43,7 @@ router.use(crowdHandlerMiddleware);
4143
// Route handler for all paths
4244
router.get("*", (req, res, next) => {
4345
//YOUR CODE BELOW THIS COMMENT
44-
46+
4547
// Render the view and send the HTML
4648
res.render("index", { title: "hello" }, (err, html) => {
4749
// Handle any errors during rendering
@@ -55,6 +57,18 @@ router.get("*", (req, res, next) => {
5557
if (res.locals.chGatekeeper) {
5658
res.locals.chGatekeeper.recordPerformance();
5759
}
60+
61+
/*
62+
* IMPORTANT CONSIDERATION:
63+
*
64+
* The default status code sent to CrowdHandler is '200'. However, if a different status code needs to be sent,
65+
* it can be achieved by passing it as a parameter to the 'recordPerformance' method.
66+
*
67+
* Example:
68+
* chGatekeeper.recordPerformance({status: 404});
69+
*
70+
* If you are using CrowdHandler's autotune feature, is is crucial to pass accurate status codes to CrowdHandler to ensure the precision of analytics and autotune results.
71+
*/
5872
});
5973
});
6074

examples/node-16-9-1/express-4-16-1/private_api_interaction/index.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
1-
//This file provides a few different examples of how the SDK can be used to interact with private CrowdHandler API methods
2-
//To understand the required parameters, request methods and expected responses, please refer to the full API documentation
3-
//API documentation can be found in the CrowdHandler control panel Under "Account -> API"
1+
/*
2+
* This file provides a few different examples of how the SDK can be used to interact with private CrowdHandler API methods.
3+
* To understand the required parameters, request methods and expected responses, please refer to the full API documentation.
4+
* API documentation can be found in the CrowdHandler control panel Under "Account -> API".
5+
6+
* account() -> /v1/account
7+
* accountPlan() -> /v1/account/plan
8+
* codes() -> /v1/codes/:code-id
9+
* domains() -> /v1/domains/:domain-id
10+
* domainIPs() -> /v1/domains/:domain-id/ips
11+
* domainReports() -> /v1/domains/:domain-id/reports
12+
* domainRequests() -> /v1/domains/:domain-id/requests
13+
* domainRooms() -> /v1/domains/:domain-id/rooms
14+
* domainURLs() -> /v1/domains/:domain-id/urls
15+
* groups() -> /v1/groups/:group-id
16+
* groupsBatch() -> /v1/groups/:group-id/batch
17+
* groupsCodes() -> /v1/groups/:group-id/codes
18+
* ips() -> /v1/ips/:ip-id
19+
* reports() -> /v1/reports/:report-id
20+
* rooms() -> /v1/rooms/:room-id
21+
* roomReports() -> /v1/rooms/:room-id/reports
22+
* roomSessions() -> /v1/rooms/:room-id/sessions
23+
* sessions() -> /v1/sessions/:session-id
24+
* templates() -> /v1/templates/:template-id
25+
26+
*/
427

528
//Import the CrowdHandler SDK
629
const crowdhandler = require("crowdhandler-sdk");
@@ -21,11 +44,13 @@ await private_client.domains().put("dom_4RQg2RBH7DLA", {
2144

2245
//Fetch a domains report
2346
//Parameters are passed as an object
24-
let domainsReport = await private_client.domainsReports().get("dom_4RQg2RBH7DLA", {
25-
from: "2023-06-06T19:59:00.000Z",
26-
to: "2023-07-06T19:59:00.000Z",
27-
day: "day",
28-
});
47+
let domainsReport = await private_client
48+
.domainsReports()
49+
.get("dom_4RQg2RBH7DLA", {
50+
from: "2023-06-06T19:59:00.000Z",
51+
to: "2023-07-06T19:59:00.000Z",
52+
day: "day",
53+
});
2954

3055
//Fetch waiting rooms associated with the key
3156
let rooms = await private_client.rooms().get();
@@ -34,10 +59,9 @@ let rooms = await private_client.rooms().get();
3459
let yourRoom = await private_client.rooms().get("room_5SRg2RBH7DLA");
3560

3661
//Get a list of sessions associated with a room
37-
let roomSessions = await private_client.roomsSessions().get("room_5SRg2RBH7DLA");
62+
let roomSessions = await private_client
63+
.roomsSessions()
64+
.get("room_5SRg2RBH7DLA");
3865

3966
//Delete a session
4067
await private_client.sessions().delete("ses_5SRg2RBH7DLA");
41-
42-
43-

examples/react/App.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Description: This is a basic example of how to use the CrowdHandler SDK in a React application.
2+
3+
import React, { useEffect, useState } from "react";
4+
import { useLocation } from "react-router-dom";
5+
const crowdhandler = require("crowdhandler-sdk");
6+
7+
function App() {
8+
const [data, setData] = useState(null);
9+
10+
const LocationListener = () => {
11+
const location = useLocation();
12+
13+
// This is the main CrowdHandler code. It should be executed as early as possible each time a new page/screen is loaded.
14+
useEffect(() => {
15+
async function crowdhandler_gatekeeper() {
16+
console.log("URL changed:", location.pathname);
17+
18+
//create a new instance of the CrowdHandler SDK
19+
const publicClient = new crowdhandler.PublicClient("YOUR_PUBLIC_KEY");
20+
const chContext = new crowdhandler.RequestContext();
21+
const chGatekeeper = new crowdhandler.Gatekeeper(
22+
publicClient,
23+
chContext,
24+
{
25+
publicKey: "YOUR_PUBLIC_KEY",
26+
},
27+
//Make sure to pass this mode if CrowdHandler is going to run on the client side
28+
{ mode: "clientside" }
29+
);
30+
31+
//validate the request
32+
const chStatus = await chGatekeeper.validateRequest();
33+
34+
//session management
35+
if (chStatus.setLocalStorage) {
36+
chGatekeeper.setLocalStorage();
37+
}
38+
39+
//remove CrowdHandler guff from the URL
40+
if (chStatus.stripParams) {
41+
chGatekeeper.redirectToCleanUrl(chStatus.targetURL);
42+
}
43+
44+
//to redirect or to not redirect, that is the question
45+
if (!chStatus.promoted) {
46+
return chGatekeeper.redirectIfNotPromoted();
47+
}
48+
49+
/* Think carefull about where you include this.
50+
*
51+
*The intention is to inform CrowdHandler of the performance of the page
52+
*For example, in a single page application, you should defer this call until all critical components of the page have finished loading and the page is ready to be used.
53+
*/
54+
chGatekeeper.recordPerformance();
55+
56+
/*
57+
* IMPORTANT CONSIDERATION:
58+
*
59+
* The default status code sent to CrowdHandler is '200'. However, if a different status code needs to be sent,
60+
* it can be achieved by passing it as a parameter to the 'recordPerformance' method.
61+
*
62+
* Example:
63+
* chGatekeeper.recordPerformance({status: 404});
64+
*
65+
* If you are using CrowdHandler's autotune feature, is is crucial to pass accurate status codes to CrowdHandler to ensure the precision of analytics and autotune results.
66+
*/
67+
}
68+
69+
crowdhandler_gatekeeper();
70+
}, [location]);
71+
72+
return null; // this component doesn't render anything
73+
};
74+
}
75+
76+
export default App;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crowdhandler-sdk",
3-
"version": "1.2.2-beta.1",
3+
"version": "2.0.0-beta.1",
44
"description": "",
55
"homepage": "https://github.com/Crowdhandler/crowdhandler-javascript-sdk#readme",
66
"repository": {

src/client/private_client.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,39 +46,39 @@ export class PrivateClient extends Client {
4646
});
4747
}
4848

49-
domainsIPs() {
49+
domainIPs() {
5050
return new Resource(this.key, "/v1/domains/ID_PLACEHOLDER/ips", {
5151
timeout: this.timeout,
5252
debug: this.debug,
5353
api_url: this.api_url,
5454
});
5555
}
5656

57-
domainsReports() {
57+
domainReports() {
5858
return new Resource(this.key, "/v1/domains/ID_PLACEHOLDER/reports", {
5959
timeout: this.timeout,
6060
debug: this.debug,
6161
api_url: this.api_url,
6262
});
6363
}
6464

65-
domainsRequests() {
65+
domainRequests() {
6666
return new Resource(this.key, "/v1/domains/ID_PLACEHOLDER/requests", {
6767
timeout: this.timeout,
6868
debug: this.debug,
6969
api_url: this.api_url,
7070
});
7171
}
7272

73-
domainsRooms() {
73+
domainRooms() {
7474
return new Resource(this.key, "/v1/domains/ID_PLACEHOLDER/rooms", {
7575
timeout: this.timeout,
7676
debug: this.debug,
7777
api_url: this.api_url,
7878
});
7979
}
8080

81-
domainsURLs() {
81+
domainURLs() {
8282
return new Resource(this.key, "/v1/domains/ID_PLACEHOLDER/urls", {
8383
timeout: this.timeout,
8484
debug: this.debug,
@@ -94,15 +94,15 @@ export class PrivateClient extends Client {
9494
});
9595
}
9696

97-
groupsBatch() {
97+
groupBatch() {
9898
return new Resource(this.key, "/v1/groups/ID_PLACEHOLDER/batch", {
9999
timeout: this.timeout,
100100
debug: this.debug,
101101
api_url: this.api_url,
102102
});
103103
}
104104

105-
groupsCodes() {
105+
groupCodes() {
106106
return new Resource(this.key, "/v1/groups/ID_PLACEHOLDER/codes", {
107107
timeout: this.timeout,
108108
debug: this.debug,
@@ -134,15 +134,15 @@ export class PrivateClient extends Client {
134134
});
135135
}
136136

137-
roomsReports() {
137+
roomReports() {
138138
return new Resource(this.key, "/v1/rooms/ID_PLACEHOLDER/reports", {
139139
timeout: this.timeout,
140140
debug: this.debug,
141141
api_url: this.api_url,
142142
});
143143
}
144144

145-
roomsSessions() {
145+
roomSessions() {
146146
return new Resource(this.key, "/v1/rooms/ID_PLACEHOLDER/sessions", {
147147
timeout: this.timeout,
148148
debug: this.debug,

0 commit comments

Comments
 (0)