@synthient/synthient 1.0.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/dist/models.js ADDED
@@ -0,0 +1,221 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DeleteResponse = exports.VisitResponse = exports.LookupResponse = exports.Location = exports.IpData = exports.Browser = exports.Device = exports.BaseResponse = exports.RiskLevel = exports.ActionType = exports.TokenType = void 0;
4
+ /**
5
+ * The three types of tokens that can be issued.
6
+ * METRICS - Metrics token, used for gathering data.
7
+ * SIGN - Sign token, used for verifying the challenge.
8
+ * STRICT - Strict token, used for strict verification.
9
+ */
10
+ var TokenType;
11
+ (function (TokenType) {
12
+ TokenType["METRICS"] = "metrics";
13
+ TokenType["SIGN"] = "sign";
14
+ TokenType["STRICT"] = "strict";
15
+ })(TokenType = exports.TokenType || (exports.TokenType = {}));
16
+ /**
17
+ * Translates the risk level into an action to take.
18
+ * ALLOW - Allow the visitor to continue.
19
+ * REDIRECT - Redirect the visitor to a different page or have them perform another form of verification.
20
+ * BLOCK - Block the visitor from accessing.
21
+ */
22
+ var ActionType;
23
+ (function (ActionType) {
24
+ ActionType[ActionType["ALLOW"] = 0] = "ALLOW";
25
+ ActionType[ActionType["REDIRECT"] = 1] = "REDIRECT";
26
+ ActionType[ActionType["BLOCK"] = 2] = "BLOCK";
27
+ })(ActionType = exports.ActionType || (exports.ActionType = {}));
28
+ /**
29
+ * Risk level of the visitor. Simplified for ease of use.
30
+ * Under certain circumstances, you may want to use the risk score.
31
+ */
32
+ var RiskLevel;
33
+ (function (RiskLevel) {
34
+ RiskLevel[RiskLevel["LOW"] = 0] = "LOW";
35
+ RiskLevel[RiskLevel["MEDIUM"] = 1] = "MEDIUM";
36
+ RiskLevel[RiskLevel["HIGH"] = 2] = "HIGH";
37
+ })(RiskLevel = exports.RiskLevel || (exports.RiskLevel = {}));
38
+ /**
39
+ * Base response model.
40
+ * Represents a response containing a message.
41
+ */
42
+ class BaseResponse {
43
+ /**
44
+ * Constructs a new BaseResponse.
45
+ *
46
+ * @param message - The response message.
47
+ */
48
+ constructor(message) {
49
+ this.message = message;
50
+ }
51
+ }
52
+ exports.BaseResponse = BaseResponse;
53
+ /**
54
+ * Device model, contains core information relating to the visitor device.
55
+ */
56
+ class Device {
57
+ /**
58
+ * Constructs a new Device.
59
+ *
60
+ * @param model - The model of the device, if available.
61
+ * @param brand - The brand of the device.
62
+ * @param os - The operating system of the device.
63
+ * @param version - The version of the operating system.
64
+ */
65
+ constructor(model, brand, os, version) {
66
+ this.model = model;
67
+ this.brand = brand;
68
+ this.os = os;
69
+ this.version = version;
70
+ }
71
+ }
72
+ exports.Device = Device;
73
+ /**
74
+ * Browser model, contains information about the browser being used.
75
+ */
76
+ class Browser {
77
+ /**
78
+ * Constructs a new Browser.
79
+ *
80
+ * @param name - The name of the browser.
81
+ * @param version - The version of the browser.
82
+ */
83
+ constructor(name, version) {
84
+ this.name = name;
85
+ this.version = version;
86
+ }
87
+ }
88
+ exports.Browser = Browser;
89
+ /**
90
+ * IpData model, contains information about the IP address
91
+ */
92
+ class IpData {
93
+ /**
94
+ * Constructs a new IpData.
95
+ *
96
+ * @param is_vpn - If the IP address is a VPN.
97
+ * @param is_proxy - If the IP address is a proxy.
98
+ * @param is_tor - If the IP address is a TOR node.
99
+ * @param is_relay - If the IP address is a relay.
100
+ * @param asn - The ASN of the IP address.
101
+ */
102
+ constructor(is_vpn, is_proxy, is_tor, is_relay, asn) {
103
+ this.is_vpn = is_vpn;
104
+ this.is_proxy = is_proxy;
105
+ this.is_tor = is_tor;
106
+ this.is_relay = is_relay;
107
+ this.asn = asn;
108
+ }
109
+ }
110
+ exports.IpData = IpData;
111
+ /**
112
+ * Location model, contains information about the location of the IP address.
113
+ */
114
+ class Location {
115
+ /**
116
+ * Constructs a new Location.
117
+ *
118
+ * @param city - The city of the IP address.
119
+ * @param region - The region of the IP address.
120
+ * @param country - The country of the IP address.
121
+ * @param region_code - The region code of the IP address.
122
+ * @param country_code - The country code of the IP address.
123
+ * @param latitude - The latitude of the IP address.
124
+ * @param longitude - The longitude of the IP address.
125
+ */
126
+ constructor(city, region, country, region_code, country_code, latitude, longitude) {
127
+ this.city = city;
128
+ this.region = region;
129
+ this.country = country;
130
+ this.region_code = region_code;
131
+ this.country_code = country_code;
132
+ this.latitude = latitude;
133
+ this.longitude = longitude;
134
+ }
135
+ }
136
+ exports.Location = Location;
137
+ /**
138
+ * Response model for the lookup endpoint.
139
+ */
140
+ class LookupResponse {
141
+ /**
142
+ * Constructs a new LookupResponse.
143
+ *
144
+ * @param token - The token of the visitor.
145
+ * @param token_type - The type of token that was issued (metrics, sign, strict).
146
+ * @param session - The persistent session of the visitor, will not change.
147
+ * @param device - The device information of the visitor.
148
+ * @param browser - The browser information of the visitor.
149
+ * @param ip - The IP address of the visitor.
150
+ * @param ip_data - The IP data of the visitor.
151
+ * @param location - The location of the visitor.
152
+ * @param page - The page the visitor is on.
153
+ * @param risk_score - The risk score of the visitor.
154
+ * @param is_incognito - If the visitor is in incognito mode.
155
+ * @param is_bot - If the visitor is a bot.
156
+ * @param is_vm - If the visitor is using a VM.
157
+ * @param solved - If the visitor has solved the captcha (if applicable).
158
+ * @param consumed - If the token has been consumed (used before).
159
+ */
160
+ constructor(token, token_type, session, device, browser, ip, ip_data, location, page, risk_score, is_incognito, is_bot, is_vm, solved, consumed) {
161
+ this.token = token;
162
+ this.token_type = token_type;
163
+ this.session = session;
164
+ this.device = device;
165
+ this.browser = browser;
166
+ this.ip = ip;
167
+ this.ip_data = ip_data;
168
+ this.location = location;
169
+ this.page = page;
170
+ this.risk_score = risk_score;
171
+ this.is_incognito = is_incognito;
172
+ this.is_bot = is_bot;
173
+ this.is_vm = is_vm;
174
+ this.solved = solved;
175
+ this.consumed = consumed;
176
+ }
177
+ /**
178
+ * Computes the risk level based on the risk score.
179
+ */
180
+ get risk_level() {
181
+ if (this.risk_score < 20)
182
+ return RiskLevel.LOW;
183
+ if (this.risk_score < 50)
184
+ return RiskLevel.MEDIUM;
185
+ return RiskLevel.HIGH;
186
+ }
187
+ }
188
+ exports.LookupResponse = LookupResponse;
189
+ /**
190
+ * Response model for the visits endpoint.
191
+ */
192
+ class VisitResponse extends BaseResponse {
193
+ /**
194
+ * Constructs a new VisitResponse.
195
+ *
196
+ * @param session - The session of the visitor.
197
+ * @param visits - The list of visit responses.
198
+ * @param has_next - Whether there are more visits.
199
+ */
200
+ constructor(session, visits, has_next) {
201
+ super('');
202
+ this.session = session;
203
+ this.visits = visits;
204
+ this.has_next = has_next;
205
+ }
206
+ }
207
+ exports.VisitResponse = VisitResponse;
208
+ /**
209
+ * Response model for the delete endpoint.
210
+ */
211
+ class DeleteResponse extends BaseResponse {
212
+ /**
213
+ * Constructs a new DeleteResponse.
214
+ *
215
+ * @param message - The response message from the server.
216
+ */
217
+ constructor(message) {
218
+ super(message);
219
+ }
220
+ }
221
+ exports.DeleteResponse = DeleteResponse;
@@ -0,0 +1,15 @@
1
+ import { LookupResponse, TokenType, ActionType } from './models';
2
+ /**
3
+ * Verify the token.
4
+ * @param lookup - The response from the lookup endpoint.
5
+ * @param token_type - The type of token that was issued.
6
+ * @returns A boolean indicating if the token is valid.
7
+ */
8
+ export declare function verifyToken(lookup: LookupResponse, token_type: TokenType): boolean;
9
+ /**
10
+ * Determine the action to take based on the lookup response.
11
+ * @param lookup - The response from the lookup endpoint.
12
+ * @param token_type - The type of token that was issued.
13
+ * @returns The action to take.
14
+ */
15
+ export declare function determineAction(lookup: LookupResponse, token_type: TokenType): ActionType;
package/dist/utils.js ADDED
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.determineAction = exports.verifyToken = void 0;
4
+ const models_1 = require("./models");
5
+ /**
6
+ * Verify the token.
7
+ * @param lookup - The response from the lookup endpoint.
8
+ * @param token_type - The type of token that was issued.
9
+ * @returns A boolean indicating if the token is valid.
10
+ */
11
+ function verifyToken(lookup, token_type) {
12
+ return (lookup.consumed === false &&
13
+ (lookup.solved !== undefined ? lookup.solved : true) === true &&
14
+ token_type === lookup.token_type);
15
+ }
16
+ exports.verifyToken = verifyToken;
17
+ /**
18
+ * Determine the action to take based on the lookup response.
19
+ * @param lookup - The response from the lookup endpoint.
20
+ * @param token_type - The type of token that was issued.
21
+ * @returns The action to take.
22
+ */
23
+ function determineAction(lookup, token_type) {
24
+ if (verifyToken(lookup, token_type)) {
25
+ return lookup.risk_level;
26
+ }
27
+ return models_1.ActionType.BLOCK;
28
+ }
29
+ exports.determineAction = determineAction;
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@synthient/synthient",
3
+ "version": "1.0.0",
4
+ "main": "dist/index.js",
5
+ "scripts": {
6
+ "build": "tsc"
7
+ },
8
+ "dependencies": {
9
+ "axios": "^0.24.0"
10
+ },
11
+ "devDependencies": {
12
+ "typescript": "^4.5.4",
13
+ "@types/node": "^16.11.7"
14
+ },
15
+ "types": "./dist/index.d.ts",
16
+ "description": "Node.js client for the Synthient API.",
17
+ "author": "",
18
+ "license": "ISC"
19
+ }
package/src/client.ts ADDED
@@ -0,0 +1,106 @@
1
+ import axios, { AxiosInstance, AxiosProxyConfig } from 'axios';
2
+ import { API_URL, DEFAULT_TIMEOUT } from './constants';
3
+ import { ErrorResponse, InternalServerError } from './exceptions';
4
+ import { LookupResponse, VisitResponse, DeleteResponse } from './models';
5
+
6
+ /**
7
+ * Async client for interacting with the Synthient API.
8
+ */
9
+ class Client {
10
+ private _http: AxiosInstance;
11
+ default_timeout: number;
12
+
13
+ /**
14
+ * Initializes the client with an API key, default timeout, and optional proxy.
15
+ *
16
+ * @param api_key - The API key for authorization.
17
+ * @param default_timeout - The default timeout for requests.
18
+ * @param proxy - Optional proxy configuration.
19
+ * @param port - Optional port for the proxy.
20
+ */
21
+ constructor(api_key: string, default_timeout: number = DEFAULT_TIMEOUT, proxy?: string, port?: number) {
22
+ const proxyConfig: AxiosProxyConfig | undefined = proxy
23
+ ? {
24
+ host: proxy,
25
+ port: port ? port : 8000, // Use provided port or default to 8000
26
+ }
27
+ : undefined;
28
+
29
+ this._http = axios.create({
30
+ headers: { Authorization: api_key },
31
+ proxy: proxyConfig,
32
+ timeout: default_timeout,
33
+ validateStatus: () => true,
34
+ });
35
+ this.default_timeout = default_timeout;
36
+ }
37
+
38
+ /**
39
+ * Lookup a token and return its corresponding data.
40
+ *
41
+ * @param token - Token to lookup.
42
+ * @throws ErrorResponse | InternalServerError - If the server returns a 401, 404, 409, or 500 status code.
43
+ * @returns LookupResponse - The token data.
44
+ */
45
+ async lookup(token: string): Promise<LookupResponse> {
46
+ const resp = await this._http.get(`${API_URL}lookup/${token}`);
47
+ if (resp.status === 200) {
48
+ return new LookupResponse(
49
+ resp.data.token,
50
+ resp.data.token_type,
51
+ resp.data.session,
52
+ resp.data.device,
53
+ resp.data.browser,
54
+ resp.data.ip,
55
+ resp.data.ip_data,
56
+ resp.data.location,
57
+ resp.data.page,
58
+ resp.data.risk_score,
59
+ resp.data.is_incognito,
60
+ resp.data.is_bot,
61
+ resp.data.is_vm,
62
+ resp.data.solved,
63
+ resp.data.consumed
64
+ );
65
+ } else if ([401, 404, 409].includes(resp.status)) {
66
+ throw new ErrorResponse(resp.data.message);
67
+ } else {
68
+ throw new InternalServerError("Server failed to lookup token.");
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Lookup a session and return its corresponding visit data.
74
+ *
75
+ * @param session - Session to lookup.
76
+ * @throws ErrorResponse | InternalServerError - If the server returns a 401, 409, or 500 status code.
77
+ * @returns VisitResponse - The session data.
78
+ */
79
+ async visits(session: string): Promise<VisitResponse> {
80
+ const resp = await this._http.get(`${API_URL}visits/${session}`);
81
+ if (resp.status === 200) {
82
+ return new VisitResponse(resp.data.session, resp.data.visits, resp.data.has_next);
83
+ } else if ([401, 409].includes(resp.status)) {
84
+ throw new ErrorResponse(resp.data.message);
85
+ } else {
86
+ throw new InternalServerError("Server failed to lookup token.");
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Delete a token.
92
+ *
93
+ * @param token - Token to delete.
94
+ * @throws InternalServerError - If the server returns a 500 status code.
95
+ * @returns DeleteResponse - The response from the server.
96
+ */
97
+ async delete(token: string): Promise<DeleteResponse> {
98
+ const resp = await this._http.delete(`${API_URL}delete/${token}`);
99
+ if (resp.status === 500) {
100
+ throw new InternalServerError("Server failed to delete token.");
101
+ }
102
+ return new DeleteResponse(resp.data.message);
103
+ }
104
+ }
105
+
106
+ export { Client };
@@ -0,0 +1,2 @@
1
+ export const API_URL = "https://api.synthient.com/api/v1/";
2
+ export const DEFAULT_TIMEOUT = 5000;
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Base exception class
3
+ */
4
+ export class SynthientException extends Error {
5
+ /**
6
+ * Constructs a new SynthientException.
7
+ *
8
+ * @param message - The error message.
9
+ */
10
+ constructor(message: string) {
11
+ super(message);
12
+ this.name = "SynthientException";
13
+ }
14
+ }
15
+
16
+ /**
17
+ * Raised when the server returns a 500.
18
+ */
19
+ export class InternalServerError extends SynthientException {
20
+ /**
21
+ * Constructs a new InternalServerError.
22
+ *
23
+ * @param message - The error message.
24
+ */
25
+ constructor(message: string) {
26
+ super(message);
27
+ this.name = "InternalServerError";
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Raised when the server returns a 400 or 401.
33
+ */
34
+ export class ErrorResponse extends SynthientException {
35
+ /**
36
+ * Constructs a new ErrorResponse.
37
+ *
38
+ * @param message - The error message.
39
+ */
40
+ constructor(message: string) {
41
+ super(message);
42
+ this.name = "ErrorResponse";
43
+ }
44
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './client';
2
+ export * from './constants';
3
+ export * from './exceptions';
4
+ export * from './models';
5
+ export * from './utils';