@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/LICENSE ADDED
@@ -0,0 +1,15 @@
1
+ Apache Software License 2.0
2
+
3
+ Copyright (c) 2024, synthientpy
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,148 @@
1
+ # Synthient Client Library
2
+
3
+ Node.js client for the Synthient API.
4
+
5
+ ## Installation
6
+
7
+ To install the library:
8
+
9
+ ```bash
10
+ npm install @synthient/synthient
11
+ ```
12
+
13
+ or
14
+
15
+ ```bash
16
+ yarn add @synthient/synthient
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Initialization
22
+
23
+ To use the client, you need to initialize it with your API key. Optionally, you can set a default timeout and proxy configuration.
24
+
25
+ ```javascript
26
+ const { Client } = require('synthient');
27
+
28
+ const apiKey = 'your-private-key-here';
29
+ const client = new Client(apiKey, 5000, 'proxy-host', 8000);
30
+ ```
31
+
32
+ or without a proxy or default timeout
33
+
34
+ ```javascript
35
+ const client = new Client(apiKey);
36
+ ```
37
+
38
+ ### Methods
39
+
40
+ #### Lookup
41
+
42
+ Lookup a token and return its corresponding data.
43
+
44
+ ```javascript
45
+ async function lookupToken() {
46
+ try {
47
+ const token = 'example-token';
48
+ const response = await client.lookup(token);
49
+ console.log('Lookup Response:', response);
50
+ } catch (error) {
51
+ console.error('Error during lookup:', error.message);
52
+ }
53
+ }
54
+
55
+ lookupToken();
56
+ ```
57
+
58
+ #### Visits
59
+
60
+ Lookup a session and return its corresponding visit data.
61
+
62
+ ```javascript
63
+ async function getVisits() {
64
+ try {
65
+ const session = 'example-session';
66
+ const response = await client.visits(session);
67
+ console.log('Visit Response:', response);
68
+ } catch (error) {
69
+ console.error('Error during visits lookup:', error.message);
70
+ }
71
+ }
72
+
73
+ getVisits();
74
+ ```
75
+
76
+ #### Delete
77
+
78
+ Delete a token.
79
+
80
+ ```javascript
81
+ async function deleteToken() {
82
+ try {
83
+ const token = 'token...';
84
+ const response = await client.delete(token);
85
+ console.log('Delete Response:', response);
86
+ } catch (error) {
87
+ console.error('Error during delete:', error.message);
88
+ }
89
+ }
90
+
91
+ deleteToken();
92
+ ```
93
+
94
+ ### Utilities
95
+
96
+ The library includes utility functions for verifying tokens and determining actions based on risk levels.
97
+
98
+ #### Verify Token
99
+
100
+ Verify the validity of a token.
101
+
102
+ ```javascript
103
+ const { verifyToken } = require('synthient');
104
+
105
+ const isValid = verifyToken(lookupResponse, TokenType.METRICS);
106
+ console.log('Token is valid:', isValid);
107
+ ```
108
+
109
+ #### Determine Action
110
+
111
+ Determine the action to take based on the lookup response and risk level.
112
+
113
+ ```javascript
114
+ const { determineAction, TokenType, ActionType } = require('synthient');
115
+
116
+ const action = determineAction(lookupResponse, TokenType.METRICS);
117
+ console.log('Action to take:', ActionType[action]);
118
+ ```
119
+
120
+ ## Models
121
+
122
+ ### TokenType
123
+
124
+ The three types of tokens that can be issued:
125
+ - `METRICS`: Metrics token
126
+ - `SIGN`: Sign token, used for verifying the challenge.
127
+ - `STRICT`: Strict token, used for strict verification.
128
+
129
+ ### ActionType
130
+
131
+ The actions to take based on the risk level:
132
+ - `ALLOW`: Allow the visitor to continue.
133
+ - `REDIRECT`: Redirect the visitor to a different page or have them perform another form of verification.
134
+ - `BLOCK`: Block the visitor from accessing.
135
+
136
+ ### RiskLevel
137
+
138
+ The risk level of the visitor:
139
+ - `LOW`: Low risk.
140
+ - `MEDIUM`: Medium risk.
141
+ - `HIGH`: High risk.
142
+
143
+
144
+ Check `models.ts` for all the models used in the library. Or refer to the [API documentation](https://docs.synthient.com) for more information.
145
+
146
+
147
+ ## Issues
148
+ For any issues or feature requests, please open an issue on the GitHub repository
@@ -0,0 +1,42 @@
1
+ import { LookupResponse, VisitResponse, DeleteResponse } from './models';
2
+ /**
3
+ * Async client for interacting with the Synthient API.
4
+ */
5
+ declare class Client {
6
+ private _http;
7
+ default_timeout: number;
8
+ /**
9
+ * Initializes the client with an API key, default timeout, and optional proxy.
10
+ *
11
+ * @param api_key - The API key for authorization.
12
+ * @param default_timeout - The default timeout for requests.
13
+ * @param proxy - Optional proxy configuration.
14
+ * @param port - Optional port for the proxy.
15
+ */
16
+ constructor(api_key: string, default_timeout?: number, proxy?: string, port?: number);
17
+ /**
18
+ * Lookup a token and return its corresponding data.
19
+ *
20
+ * @param token - Token to lookup.
21
+ * @throws ErrorResponse | InternalServerError - If the server returns a 401, 404, 409, or 500 status code.
22
+ * @returns LookupResponse - The token data.
23
+ */
24
+ lookup(token: string): Promise<LookupResponse>;
25
+ /**
26
+ * Lookup a session and return its corresponding visit data.
27
+ *
28
+ * @param session - Session to lookup.
29
+ * @throws ErrorResponse | InternalServerError - If the server returns a 401, 409, or 500 status code.
30
+ * @returns VisitResponse - The session data.
31
+ */
32
+ visits(session: string): Promise<VisitResponse>;
33
+ /**
34
+ * Delete a token.
35
+ *
36
+ * @param token - Token to delete.
37
+ * @throws InternalServerError - If the server returns a 500 status code.
38
+ * @returns DeleteResponse - The response from the server.
39
+ */
40
+ delete(token: string): Promise<DeleteResponse>;
41
+ }
42
+ export { Client };
package/dist/client.js ADDED
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Client = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const constants_1 = require("./constants");
18
+ const exceptions_1 = require("./exceptions");
19
+ const models_1 = require("./models");
20
+ /**
21
+ * Async client for interacting with the Synthient API.
22
+ */
23
+ class Client {
24
+ /**
25
+ * Initializes the client with an API key, default timeout, and optional proxy.
26
+ *
27
+ * @param api_key - The API key for authorization.
28
+ * @param default_timeout - The default timeout for requests.
29
+ * @param proxy - Optional proxy configuration.
30
+ * @param port - Optional port for the proxy.
31
+ */
32
+ constructor(api_key, default_timeout = constants_1.DEFAULT_TIMEOUT, proxy, port) {
33
+ const proxyConfig = proxy
34
+ ? {
35
+ host: proxy,
36
+ port: port ? port : 8000, // Use provided port or default to 8000
37
+ }
38
+ : undefined;
39
+ this._http = axios_1.default.create({
40
+ headers: { Authorization: api_key },
41
+ proxy: proxyConfig,
42
+ timeout: default_timeout,
43
+ validateStatus: () => true,
44
+ });
45
+ this.default_timeout = default_timeout;
46
+ }
47
+ /**
48
+ * Lookup a token and return its corresponding data.
49
+ *
50
+ * @param token - Token to lookup.
51
+ * @throws ErrorResponse | InternalServerError - If the server returns a 401, 404, 409, or 500 status code.
52
+ * @returns LookupResponse - The token data.
53
+ */
54
+ lookup(token) {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ const resp = yield this._http.get(`${constants_1.API_URL}lookup/${token}`);
57
+ if (resp.status === 200) {
58
+ return new models_1.LookupResponse(resp.data.token, resp.data.token_type, resp.data.session, resp.data.device, resp.data.browser, resp.data.ip, resp.data.ip_data, resp.data.location, resp.data.page, resp.data.risk_score, resp.data.is_incognito, resp.data.is_bot, resp.data.is_vm, resp.data.solved, resp.data.consumed);
59
+ }
60
+ else if ([401, 404, 409].includes(resp.status)) {
61
+ throw new exceptions_1.ErrorResponse(resp.data.message);
62
+ }
63
+ else {
64
+ throw new exceptions_1.InternalServerError("Server failed to lookup token.");
65
+ }
66
+ });
67
+ }
68
+ /**
69
+ * Lookup a session and return its corresponding visit data.
70
+ *
71
+ * @param session - Session to lookup.
72
+ * @throws ErrorResponse | InternalServerError - If the server returns a 401, 409, or 500 status code.
73
+ * @returns VisitResponse - The session data.
74
+ */
75
+ visits(session) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ const resp = yield this._http.get(`${constants_1.API_URL}visits/${session}`);
78
+ if (resp.status === 200) {
79
+ return new models_1.VisitResponse(resp.data.session, resp.data.visits, resp.data.has_next);
80
+ }
81
+ else if ([401, 409].includes(resp.status)) {
82
+ throw new exceptions_1.ErrorResponse(resp.data.message);
83
+ }
84
+ else {
85
+ throw new exceptions_1.InternalServerError("Server failed to lookup token.");
86
+ }
87
+ });
88
+ }
89
+ /**
90
+ * Delete a token.
91
+ *
92
+ * @param token - Token to delete.
93
+ * @throws InternalServerError - If the server returns a 500 status code.
94
+ * @returns DeleteResponse - The response from the server.
95
+ */
96
+ delete(token) {
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ const resp = yield this._http.delete(`${constants_1.API_URL}delete/${token}`);
99
+ if (resp.status === 500) {
100
+ throw new exceptions_1.InternalServerError("Server failed to delete token.");
101
+ }
102
+ return new models_1.DeleteResponse(resp.data.message);
103
+ });
104
+ }
105
+ }
106
+ exports.Client = Client;
@@ -0,0 +1,2 @@
1
+ export declare const API_URL = "https://api.synthient.com/api/v1/";
2
+ export declare const DEFAULT_TIMEOUT = 5000;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DEFAULT_TIMEOUT = exports.API_URL = void 0;
4
+ exports.API_URL = "https://api.synthient.com/api/v1/";
5
+ exports.DEFAULT_TIMEOUT = 5000;
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Base exception class
3
+ */
4
+ export declare class SynthientException extends Error {
5
+ /**
6
+ * Constructs a new SynthientException.
7
+ *
8
+ * @param message - The error message.
9
+ */
10
+ constructor(message: string);
11
+ }
12
+ /**
13
+ * Raised when the server returns a 500.
14
+ */
15
+ export declare class InternalServerError extends SynthientException {
16
+ /**
17
+ * Constructs a new InternalServerError.
18
+ *
19
+ * @param message - The error message.
20
+ */
21
+ constructor(message: string);
22
+ }
23
+ /**
24
+ * Raised when the server returns a 400 or 401.
25
+ */
26
+ export declare class ErrorResponse extends SynthientException {
27
+ /**
28
+ * Constructs a new ErrorResponse.
29
+ *
30
+ * @param message - The error message.
31
+ */
32
+ constructor(message: string);
33
+ }
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorResponse = exports.InternalServerError = exports.SynthientException = void 0;
4
+ /**
5
+ * Base exception class
6
+ */
7
+ class SynthientException extends Error {
8
+ /**
9
+ * Constructs a new SynthientException.
10
+ *
11
+ * @param message - The error message.
12
+ */
13
+ constructor(message) {
14
+ super(message);
15
+ this.name = "SynthientException";
16
+ }
17
+ }
18
+ exports.SynthientException = SynthientException;
19
+ /**
20
+ * Raised when the server returns a 500.
21
+ */
22
+ class InternalServerError extends SynthientException {
23
+ /**
24
+ * Constructs a new InternalServerError.
25
+ *
26
+ * @param message - The error message.
27
+ */
28
+ constructor(message) {
29
+ super(message);
30
+ this.name = "InternalServerError";
31
+ }
32
+ }
33
+ exports.InternalServerError = InternalServerError;
34
+ /**
35
+ * Raised when the server returns a 400 or 401.
36
+ */
37
+ class ErrorResponse extends SynthientException {
38
+ /**
39
+ * Constructs a new ErrorResponse.
40
+ *
41
+ * @param message - The error message.
42
+ */
43
+ constructor(message) {
44
+ super(message);
45
+ this.name = "ErrorResponse";
46
+ }
47
+ }
48
+ exports.ErrorResponse = ErrorResponse;
@@ -0,0 +1,5 @@
1
+ export * from './client';
2
+ export * from './constants';
3
+ export * from './exceptions';
4
+ export * from './models';
5
+ export * from './utils';
package/dist/index.js ADDED
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./client"), exports);
18
+ __exportStar(require("./constants"), exports);
19
+ __exportStar(require("./exceptions"), exports);
20
+ __exportStar(require("./models"), exports);
21
+ __exportStar(require("./utils"), exports);
@@ -0,0 +1,191 @@
1
+ /**
2
+ * The three types of tokens that can be issued.
3
+ * METRICS - Metrics token, used for gathering data.
4
+ * SIGN - Sign token, used for verifying the challenge.
5
+ * STRICT - Strict token, used for strict verification.
6
+ */
7
+ export declare enum TokenType {
8
+ METRICS = "metrics",
9
+ SIGN = "sign",
10
+ STRICT = "strict"
11
+ }
12
+ /**
13
+ * Translates the risk level into an action to take.
14
+ * ALLOW - Allow the visitor to continue.
15
+ * REDIRECT - Redirect the visitor to a different page or have them perform another form of verification.
16
+ * BLOCK - Block the visitor from accessing.
17
+ */
18
+ export declare enum ActionType {
19
+ ALLOW = 0,
20
+ REDIRECT = 1,
21
+ BLOCK = 2
22
+ }
23
+ /**
24
+ * Risk level of the visitor. Simplified for ease of use.
25
+ * Under certain circumstances, you may want to use the risk score.
26
+ */
27
+ export declare enum RiskLevel {
28
+ LOW = 0,
29
+ MEDIUM = 1,
30
+ HIGH = 2
31
+ }
32
+ /**
33
+ * Base response model.
34
+ * Represents a response containing a message.
35
+ */
36
+ export declare class BaseResponse {
37
+ message: string;
38
+ /**
39
+ * Constructs a new BaseResponse.
40
+ *
41
+ * @param message - The response message.
42
+ */
43
+ constructor(message: string);
44
+ }
45
+ /**
46
+ * Device model, contains core information relating to the visitor device.
47
+ */
48
+ export declare class Device {
49
+ model?: string;
50
+ brand: string;
51
+ os: string;
52
+ version: string;
53
+ /**
54
+ * Constructs a new Device.
55
+ *
56
+ * @param model - The model of the device, if available.
57
+ * @param brand - The brand of the device.
58
+ * @param os - The operating system of the device.
59
+ * @param version - The version of the operating system.
60
+ */
61
+ constructor(model: string | undefined, brand: string, os: string, version: string);
62
+ }
63
+ /**
64
+ * Browser model, contains information about the browser being used.
65
+ */
66
+ export declare class Browser {
67
+ name: string;
68
+ version: string;
69
+ /**
70
+ * Constructs a new Browser.
71
+ *
72
+ * @param name - The name of the browser.
73
+ * @param version - The version of the browser.
74
+ */
75
+ constructor(name: string, version: string);
76
+ }
77
+ /**
78
+ * IpData model, contains information about the IP address
79
+ */
80
+ export declare class IpData {
81
+ is_vpn: boolean;
82
+ is_proxy: boolean;
83
+ is_tor: boolean;
84
+ is_relay: boolean;
85
+ asn: string;
86
+ /**
87
+ * Constructs a new IpData.
88
+ *
89
+ * @param is_vpn - If the IP address is a VPN.
90
+ * @param is_proxy - If the IP address is a proxy.
91
+ * @param is_tor - If the IP address is a TOR node.
92
+ * @param is_relay - If the IP address is a relay.
93
+ * @param asn - The ASN of the IP address.
94
+ */
95
+ constructor(is_vpn: boolean, is_proxy: boolean, is_tor: boolean, is_relay: boolean, asn: string);
96
+ }
97
+ /**
98
+ * Location model, contains information about the location of the IP address.
99
+ */
100
+ export declare class Location {
101
+ city: string;
102
+ region: string;
103
+ country: string;
104
+ region_code: string;
105
+ country_code: string;
106
+ latitude: number;
107
+ longitude: number;
108
+ /**
109
+ * Constructs a new Location.
110
+ *
111
+ * @param city - The city of the IP address.
112
+ * @param region - The region of the IP address.
113
+ * @param country - The country of the IP address.
114
+ * @param region_code - The region code of the IP address.
115
+ * @param country_code - The country code of the IP address.
116
+ * @param latitude - The latitude of the IP address.
117
+ * @param longitude - The longitude of the IP address.
118
+ */
119
+ constructor(city: string, region: string, country: string, region_code: string, country_code: string, latitude: number, longitude: number);
120
+ }
121
+ /**
122
+ * Response model for the lookup endpoint.
123
+ */
124
+ export declare class LookupResponse {
125
+ token: string;
126
+ token_type: TokenType;
127
+ session: string;
128
+ device?: Device;
129
+ browser?: Browser;
130
+ ip: string;
131
+ ip_data?: IpData;
132
+ location?: Location;
133
+ page?: string;
134
+ risk_score: number;
135
+ is_incognito: boolean;
136
+ is_bot: boolean;
137
+ is_vm: boolean;
138
+ solved?: boolean;
139
+ consumed: boolean;
140
+ /**
141
+ * Constructs a new LookupResponse.
142
+ *
143
+ * @param token - The token of the visitor.
144
+ * @param token_type - The type of token that was issued (metrics, sign, strict).
145
+ * @param session - The persistent session of the visitor, will not change.
146
+ * @param device - The device information of the visitor.
147
+ * @param browser - The browser information of the visitor.
148
+ * @param ip - The IP address of the visitor.
149
+ * @param ip_data - The IP data of the visitor.
150
+ * @param location - The location of the visitor.
151
+ * @param page - The page the visitor is on.
152
+ * @param risk_score - The risk score of the visitor.
153
+ * @param is_incognito - If the visitor is in incognito mode.
154
+ * @param is_bot - If the visitor is a bot.
155
+ * @param is_vm - If the visitor is using a VM.
156
+ * @param solved - If the visitor has solved the captcha (if applicable).
157
+ * @param consumed - If the token has been consumed (used before).
158
+ */
159
+ constructor(token: string, token_type: TokenType, session: string, device: Device | undefined, browser: Browser | undefined, ip: string, ip_data: IpData | undefined, location: Location | undefined, page: string | undefined, risk_score: number, is_incognito: boolean, is_bot: boolean, is_vm: boolean, solved: boolean | undefined, consumed: boolean);
160
+ /**
161
+ * Computes the risk level based on the risk score.
162
+ */
163
+ get risk_level(): RiskLevel;
164
+ }
165
+ /**
166
+ * Response model for the visits endpoint.
167
+ */
168
+ export declare class VisitResponse extends BaseResponse {
169
+ session: string;
170
+ visits: LookupResponse[];
171
+ has_next: boolean;
172
+ /**
173
+ * Constructs a new VisitResponse.
174
+ *
175
+ * @param session - The session of the visitor.
176
+ * @param visits - The list of visit responses.
177
+ * @param has_next - Whether there are more visits.
178
+ */
179
+ constructor(session: string, visits: LookupResponse[], has_next: boolean);
180
+ }
181
+ /**
182
+ * Response model for the delete endpoint.
183
+ */
184
+ export declare class DeleteResponse extends BaseResponse {
185
+ /**
186
+ * Constructs a new DeleteResponse.
187
+ *
188
+ * @param message - The response message from the server.
189
+ */
190
+ constructor(message: string);
191
+ }