Skip to content

Commit 13095f9

Browse files
Merge pull request #2 from Crowdhandler/improvement/browser-support
Improvement/browser support
2 parents 56b4ceb + 1032de8 commit 13095f9

14 files changed

Lines changed: 932 additions & 388 deletions

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Install and Require
99

1010
const crowdhandler = require("crowdhandler-sdk")
1111

12+
**or...**
13+
14+
import crowdhandler from 'crowdhandler-sdk';
15+
1216
Instantiate a Public API client
1317
--------------------------------
1418

@@ -74,6 +78,14 @@ The GateKeeper class is a controller for interacting with the user request and t
7478
| mode | string | full | full, hybrid, clientside | Validation method. See below for more information on choosing a mode. |
7579
| timeout | integer | 5000 | 1 - 30000 | Outbound API communication timeout in milliseconds. |
7680
| trustOnFail | boolean | true | false, true | If false, if an API call fails, or a malformed response is received, you will be redirected to CrowdHandler's ultimate catch-all waiting room until the API responds with more inforamtion. |
81+
82+
**Mode: Full - Instantiation Example.**
83+
84+
const ch_gatekeeper = new crowdhandler.Gatekeeper(public_clent, ch_context, {publicKey: your_public_key}, {mode: full})
85+
86+
**Mode: Hybrid - Instantiation Example.**
87+
88+
const ch_gatekeeper = new crowdhandler.Gatekeeper(public_clent, ch_context, {publicKey: your_public_key, privateKey: your_private_key}, {mode: hybrid})
7789

7890
options.mode
7991
-------

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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": "0.0.2-beta.1",
3+
"version": "1.0.0-beta.1",
44
"description": "",
55
"homepage": "https://github.com/Crowdhandler/crowdhandler-javascript-sdk#readme",
66
"repository": {

src/client/client.ts

Lines changed: 116 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import { logger } from "../common/logger";
44

55
const APIResponse = z.object({}).catchall(z.any());
66

7-
const CustomResponse = z.object({
8-
result: z.object({
9-
error: z.string(),
10-
status: z.number(),
11-
}),
12-
}).catchall(z.any());
7+
const CustomResponse = z
8+
.object({
9+
result: z.object({
10+
error: z.string(),
11+
status: z.number(),
12+
}),
13+
})
14+
.catchall(z.any());
1315

1416
class ResponseGenerator {
1517
message: string;
@@ -31,118 +33,117 @@ class ResponseGenerator {
3133
}
3234

3335
export class Client {
34-
protected debug: boolean;
35-
protected api_url: string;
36-
protected key: string;
37-
protected timeout: number;
38-
39-
constructor(
40-
api_url: string,
41-
key: string,
42-
options: { timeout?: number; debug?: boolean; api_url?: string } = {}
43-
) {
44-
this.debug = options.debug || false;
45-
this.api_url = options.api_url || api_url;
46-
this.key = key;
47-
this.timeout = options.timeout || 5000;
48-
axios.defaults.timeout = this.timeout;
36+
protected debug: boolean;
37+
protected api_url: string;
38+
protected key: string;
39+
protected timeout: number;
40+
41+
constructor(
42+
api_url: string,
43+
key: string,
44+
options: { timeout?: number; debug?: boolean; api_url?: string } = {}
45+
) {
46+
this.debug = options.debug || false;
47+
this.api_url = options.api_url || api_url;
48+
this.key = key;
49+
this.timeout = options.timeout || 5000;
50+
axios.defaults.timeout = this.timeout;
51+
}
52+
53+
async errorHandler(error: any) {
54+
if (error.response) {
55+
// The request was made and the server responded with a status code
56+
// that falls out of the range of 2xx
57+
58+
logger(this.debug, "error", error.response.data);
59+
logger(this.debug, "error", error.response.status);
60+
logger(this.debug, "error", error.response.headers);
61+
62+
let response = new ResponseGenerator(
63+
error.response.data.error,
64+
error.response.status
65+
).genErrorResponse();
66+
return CustomResponse.parse(response);
67+
} else if (error.request) {
68+
// The request was made but no response was received
69+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
70+
// http.ClientRequest in node.js
71+
72+
logger(this.debug, "error", error.message);
73+
74+
let response = new ResponseGenerator(
75+
error.message,
76+
500
77+
).genErrorResponse();
78+
return CustomResponse.parse(response);
79+
} else {
80+
logger(this.debug, "error", error.message);
81+
82+
let response = new ResponseGenerator(
83+
error.message,
84+
500
85+
).genErrorResponse();
86+
return CustomResponse.parse(response);
4987
}
50-
51-
async errorHandler(error: any) {
52-
if (error.response) {
53-
// The request was made and the server responded with a status code
54-
// that falls out of the range of 2xx
55-
56-
logger(this.debug, "error", error.response.data);
57-
logger(this.debug, "error", error.response.status);
58-
logger(this.debug, "error", error.response.headers);
59-
60-
let response = new ResponseGenerator(
61-
error.response.data.error,
62-
error.response.status
63-
).genErrorResponse();
64-
return CustomResponse.parse(response);
65-
} else if (error.request) {
66-
// The request was made but no response was received
67-
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
68-
// http.ClientRequest in node.js
69-
70-
logger(this.debug, "error", error.message);
71-
72-
let response = new ResponseGenerator(
73-
error.message,
74-
500
75-
).genErrorResponse();
76-
return CustomResponse.parse(response);
77-
} else {
78-
logger(this.debug, "error", error.message);
79-
80-
let response = new ResponseGenerator(
81-
error.message,
82-
500
83-
).genErrorResponse();
84-
return CustomResponse.parse(response);
85-
}
88+
}
89+
90+
async httpDELETE(path: string, body: object) {
91+
try {
92+
const response = await axios.delete(this.api_url + path, {
93+
headers: {
94+
"x-api-key": this.key,
95+
},
96+
});
97+
98+
return APIResponse.parse(response.data);
99+
} catch (error) {
100+
return this.errorHandler(error);
86101
}
87-
88-
async httpDELETE(path: string, body: object) {
89-
try {
90-
const response = await axios.delete(this.api_url + path, {
91-
headers: {
92-
"x-api-key": this.key,
93-
},
94-
});
95-
96-
return APIResponse.parse(response.data);
97-
} catch (error) {
98-
return this.errorHandler(error);
99-
}
100-
}
101-
102-
async httpGET(path?: string, params?: object) {
103-
try {
104-
const response = await axios.get(this.api_url + path, {
105-
params: params,
106-
headers: {
107-
"x-api-key": this.key,
108-
},
109-
});
110-
return APIResponse.parse(response.data);
111-
} catch (error) {
112-
return this.errorHandler(error);
113-
}
102+
}
103+
104+
async httpGET(path?: string, params?: object) {
105+
try {
106+
const response = await axios.get(this.api_url + path, {
107+
params: params,
108+
headers: {
109+
"x-api-key": this.key,
110+
},
111+
});
112+
return APIResponse.parse(response.data);
113+
} catch (error) {
114+
return this.errorHandler(error);
114115
}
115-
116-
async httpPOST(
117-
path: string,
118-
body?: Record<string, any>,
119-
headers?: Record<string, any>,
120-
schema: z.Schema = APIResponse
121-
) {
122-
try {
123-
const response = await axios.post(this.api_url + path, body, {
124-
headers: {
125-
"x-api-key": this.key,
126-
...headers,
127-
},
128-
});
129-
return schema.parse(response.data);
130-
} catch (error) {
131-
return this.errorHandler(error);
132-
}
116+
}
117+
118+
async httpPOST(
119+
path: string,
120+
body?: Record<string, any>,
121+
headers?: Record<string, any>,
122+
schema: z.Schema = APIResponse
123+
) {
124+
try {
125+
const response = await axios.post(this.api_url + path, body, {
126+
headers: {
127+
"x-api-key": this.key,
128+
...headers,
129+
},
130+
});
131+
return schema.parse(response.data);
132+
} catch (error) {
133+
return this.errorHandler(error);
133134
}
134-
135-
async httpPUT(path: string, body: object) {
136-
try {
137-
const response = await axios.put(this.api_url + path, body, {
138-
headers: {
139-
"x-api-key": this.key,
140-
},
141-
});
142-
return APIResponse.parse(response.data);
143-
} catch (error) {
144-
return this.errorHandler(error);
145-
}
135+
}
136+
137+
async httpPUT(path: string, body: object) {
138+
try {
139+
const response = await axios.put(this.api_url + path, body, {
140+
headers: {
141+
"x-api-key": this.key,
142+
},
143+
});
144+
return APIResponse.parse(response.data);
145+
} catch (error) {
146+
return this.errorHandler(error);
146147
}
148+
}
147149
}
148-

src/client/public_client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export class PublicClient extends Client {
99
const { timeout = 5000, debug = false, api_url = "https://api.crowdhandler.com" } =
1010
options ?? {};
1111
super(api_url, key, options);
12-
console.log(this.timeout, this.debug, this.api_url);
1312
}
1413

1514
requests() {

src/common/processURL.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ export class ProcessURL {
121121
this.path = this.path.split("?")[0];
122122

123123
if (processedQueryString) {
124-
//console.log(`targetURL has query string: ${this.queryString}`)
125124
this.targetURL = encodeURIComponent(
126125
`https://${this.host}${this.path}?${processedQueryString}`
127126
);

src/common/types.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const SpecialParametersObject = z.object({
3333
chRequested: z.string(),
3434
});
3535

36-
export const processURLResultObject = z.object({
36+
export const ProcessURLResultObject = z.object({
3737
targetURL: z.string(),
3838
specialParameters: SpecialParametersObject,
3939
});
@@ -52,6 +52,17 @@ export const CookieObject = z
5252
})
5353
.catchall(z.any());
5454

55+
export const LocalStorageObject = z.object({
56+
countdown: z.record(z.unknown()),
57+
positions: z.record(z.unknown()),
58+
token: z.record(z.string()),
59+
});
60+
61+
export const LocalStorageOptions = z.object({
62+
storageName: z.string(),
63+
localStorageValue: z.string(),
64+
});
65+
5566
//Response structure validation
5667
export const RoomMetaObject = z
5768
.object({
@@ -76,14 +87,28 @@ export const SignatureResponseObject = z.object({
7687
success: z.nullable(z.boolean()),
7788
});
7889

79-
export const tokenObject = z.object({
90+
export const SignatureSourceObject = z.object({
91+
chIDSignature: z.string().optional(),
92+
crowdhandlerCookieValue: CookieObject.optional(),
93+
});
94+
95+
export const GetTokenOptions = z.object({
96+
//object can contain anything and we don't know any of the possible values
97+
crowdhandlerCookieValue: CookieObject.optional(),
98+
chID: z.string().optional(),
99+
localStorageValue: LocalStorageObject.optional(),
100+
simpleCookieValue: z.string().optional(),
101+
});
102+
103+
export const TokenObject = z.object({
80104
token: z.string(),
81105
touched: z.number(),
82106
touchedSig: z.string(),
83107
signatures: z.array(z.any()),
84108
});
85109

86-
export const tokenObjectConstructor = z.object({
110+
111+
export const TokenObjectConstructor = z.object({
87112
tokenDatestamp: z.number(),
88113
tokenDatestampSignature: z.string(),
89114
tokenSignature: z.string(),
@@ -92,17 +117,19 @@ export const tokenObjectConstructor = z.object({
92117
tokenValue: z.string(),
93118
});
94119

95-
export const validateRequestObject = z.object({
120+
export const ValidateRequestObject = z.object({
96121
promoted: z.boolean(),
97122
stripParams: z.boolean(),
98123
setCookie: z.boolean(),
99124
cookieValue: z.string().optional(),
125+
setLocalStorage: z.boolean(),
126+
localStorageValue: z.string().optional(),
100127
responseID: z.string().optional(),
101128
slug: z.string().optional(),
102129
targetURL: z.string().optional(),
103130
});
104131

105-
export const httpErrorWrapper = z.object({
132+
export const HttpErrorWrapper = z.object({
106133
result: z.object({
107134
error: z.string().nullable(),
108135
status: z.number().nullable(),

0 commit comments

Comments
 (0)