@spider-cloud/spider-client 0.0.18 → 0.0.20
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/README.md +36 -1
- package/dist/client.d.ts +23 -0
- package/dist/client.js +66 -0
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ Before using the SDK, you will need to provide it with your API key. Obtain an A
|
|
|
25
25
|
Here's a basic example to demonstrate how to use the SDK:
|
|
26
26
|
|
|
27
27
|
```javascript
|
|
28
|
-
import { Spider } from
|
|
28
|
+
import { Spider } from "@spider-cloud/spider-client";
|
|
29
29
|
|
|
30
30
|
// Initialize the SDK with your API key
|
|
31
31
|
const app = new Spider({ apiKey: "YOUR_API_KEY" });
|
|
@@ -59,6 +59,38 @@ app
|
|
|
59
59
|
});
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
### Data Operations
|
|
63
|
+
|
|
64
|
+
The Spider client can interact with specific data tables to create, retrieve, and delete data.
|
|
65
|
+
|
|
66
|
+
#### Retrieve Data from a Table
|
|
67
|
+
|
|
68
|
+
To fetch data from a specified table by applying query parameters, use the `getData` method. Provide the table name and an object containing query parameters:
|
|
69
|
+
|
|
70
|
+
```javascript
|
|
71
|
+
const tableName = "pages";
|
|
72
|
+
const queryParams = { limit: 20 };
|
|
73
|
+
spider
|
|
74
|
+
.getData(tableName, queryParams)
|
|
75
|
+
.then((response) => console.log(response))
|
|
76
|
+
.catch((error) => console.error(error));
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
This example retrieves data from the 'pages' table, limiting the results to 20 entries.
|
|
80
|
+
|
|
81
|
+
#### Delete Data from a Table
|
|
82
|
+
|
|
83
|
+
To delete data from a specified table based on certain conditions, use the `deleteData` method. Provide the table name and an object specifying the conditions for deletion:
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
const tableName = "websites";
|
|
87
|
+
const deleteParams = { domain: "www.example.com" };
|
|
88
|
+
spider
|
|
89
|
+
.deleteData(tableName, deleteParams)
|
|
90
|
+
.then((response) => console.log(response))
|
|
91
|
+
.catch((error) => console.error(error));
|
|
92
|
+
```
|
|
93
|
+
|
|
62
94
|
### Available Methods
|
|
63
95
|
|
|
64
96
|
- **`scrapeUrl(url, params)`**: Scrape data from a specified URL. Optional parameters can be passed to customize the scraping behavior.
|
|
@@ -69,6 +101,8 @@ app
|
|
|
69
101
|
- **`label(url, params)`**: Apply labeling to data extracted from the specified URL.
|
|
70
102
|
- **`getCrawlState(url, params)`**: Check the website crawl state.
|
|
71
103
|
- **`getCredits()`**: Retrieve account's remaining credits.
|
|
104
|
+
- **`getData(table, params)`**: Retrieve data records from the DB.
|
|
105
|
+
- **`deleteData(table, params)`**: Delete records from the DB.
|
|
72
106
|
|
|
73
107
|
## Error Handling
|
|
74
108
|
|
|
@@ -81,3 +115,4 @@ Contributions are always welcome! Feel free to open an issue or submit a pull re
|
|
|
81
115
|
## License
|
|
82
116
|
|
|
83
117
|
The Spider Cloud JavaScript SDK is open-source and released under the [MIT License](https://opensource.org/licenses/MIT).
|
|
118
|
+
```
|
package/dist/client.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export interface SpiderConfig {
|
|
|
9
9
|
*/
|
|
10
10
|
export declare class Spider {
|
|
11
11
|
private apiKey?;
|
|
12
|
+
private dataEndPoint;
|
|
12
13
|
/**
|
|
13
14
|
* Create an instance of Spider.
|
|
14
15
|
* @param {string | null} apiKey - The API key used to authenticate to the Spider API. If null, attempts to source from environment variables.
|
|
@@ -84,6 +85,28 @@ export declare class Spider {
|
|
|
84
85
|
* @returns {Promise<any>} The current credit balance.
|
|
85
86
|
*/
|
|
86
87
|
getCredits(): Promise<any>;
|
|
88
|
+
/**
|
|
89
|
+
* Send a POST request to insert data into a specified table.
|
|
90
|
+
* @param {string} table - The table name in the database.
|
|
91
|
+
* @param {object} data - The data to be inserted.
|
|
92
|
+
* @returns {Promise<any>} The response from the server.
|
|
93
|
+
*/
|
|
94
|
+
postData(table: string, data: object): Promise<any>;
|
|
95
|
+
/**
|
|
96
|
+
* Send a GET request to retrieve data from a specified table.
|
|
97
|
+
* @param {string} table - The table name in the database.
|
|
98
|
+
* @param {object} params - The query parameters for data retrieval.
|
|
99
|
+
* @returns {Promise<any>} The response from the server.
|
|
100
|
+
*/
|
|
101
|
+
getData(table: string, params: object): Promise<any>;
|
|
102
|
+
/**
|
|
103
|
+
* Send a DELETE request to remove data from a specified table.
|
|
104
|
+
* @param {string} table - The table name in the database.
|
|
105
|
+
* @param {object} params - Parameters to identify records to delete.
|
|
106
|
+
* @returns {Promise<any>} The response from the server.
|
|
107
|
+
*/
|
|
108
|
+
deleteData(table: string, params: object): Promise<any>;
|
|
109
|
+
private _apiDataPost;
|
|
87
110
|
/**
|
|
88
111
|
* Prepares common headers for each API request.
|
|
89
112
|
* @returns {HeadersInit} A headers object for fetch requests.
|
package/dist/client.js
CHANGED
|
@@ -12,6 +12,7 @@ class Spider {
|
|
|
12
12
|
*/
|
|
13
13
|
constructor(props) {
|
|
14
14
|
var _a;
|
|
15
|
+
this.dataEndPoint = "https://api.spider.cloud:3280";
|
|
15
16
|
this.apiKey = (props === null || props === void 0 ? void 0 : props.apiKey) || ((_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.SPIDER_API_KEY);
|
|
16
17
|
if (!this.apiKey) {
|
|
17
18
|
throw new Error("No API key provided");
|
|
@@ -130,6 +131,71 @@ class Spider {
|
|
|
130
131
|
async getCredits() {
|
|
131
132
|
return this._apiGet("credits");
|
|
132
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Send a POST request to insert data into a specified table.
|
|
136
|
+
* @param {string} table - The table name in the database.
|
|
137
|
+
* @param {object} data - The data to be inserted.
|
|
138
|
+
* @returns {Promise<any>} The response from the server.
|
|
139
|
+
*/
|
|
140
|
+
async postData(table, data) {
|
|
141
|
+
const endpoint = `/data/${table}`;
|
|
142
|
+
return this._apiDataPost(endpoint, data);
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Send a GET request to retrieve data from a specified table.
|
|
146
|
+
* @param {string} table - The table name in the database.
|
|
147
|
+
* @param {object} params - The query parameters for data retrieval.
|
|
148
|
+
* @returns {Promise<any>} The response from the server.
|
|
149
|
+
*/
|
|
150
|
+
async getData(table, params) {
|
|
151
|
+
const endpoint = `/data/${table}?${new URLSearchParams(params).toString()}`;
|
|
152
|
+
const headers = this.prepareHeaders();
|
|
153
|
+
const response = await fetch(`${this.dataEndPoint}${endpoint}`, {
|
|
154
|
+
method: "GET",
|
|
155
|
+
headers: headers,
|
|
156
|
+
});
|
|
157
|
+
if (response.ok) {
|
|
158
|
+
return response.json();
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
this.handleError(response, `get data from ${table}`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Send a DELETE request to remove data from a specified table.
|
|
166
|
+
* @param {string} table - The table name in the database.
|
|
167
|
+
* @param {object} params - Parameters to identify records to delete.
|
|
168
|
+
* @returns {Promise<any>} The response from the server.
|
|
169
|
+
*/
|
|
170
|
+
async deleteData(table, params) {
|
|
171
|
+
const endpoint = `/data/${table}?${new URLSearchParams(params).toString()}`;
|
|
172
|
+
const headers = this.prepareHeaders();
|
|
173
|
+
const response = await fetch(`${this.dataEndPoint}${endpoint}`, {
|
|
174
|
+
method: "DELETE",
|
|
175
|
+
headers,
|
|
176
|
+
});
|
|
177
|
+
if (response.ok) {
|
|
178
|
+
return response.json();
|
|
179
|
+
}
|
|
180
|
+
else {
|
|
181
|
+
this.handleError(response, `delete data from ${table}`);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// Create wrapper methods for external API access:
|
|
185
|
+
async _apiDataPost(endpoint, data) {
|
|
186
|
+
const headers = this.prepareHeaders();
|
|
187
|
+
const response = await fetch(`${this.dataEndPoint}${endpoint}`, {
|
|
188
|
+
method: "POST",
|
|
189
|
+
headers,
|
|
190
|
+
body: JSON.stringify(data),
|
|
191
|
+
});
|
|
192
|
+
if (response.ok) {
|
|
193
|
+
return response.json();
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
this.handleError(response, `post to ${endpoint}`);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
133
199
|
/**
|
|
134
200
|
* Prepares common headers for each API request.
|
|
135
201
|
* @returns {HeadersInit} A headers object for fetch requests.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export { Spider };
|
|
1
|
+
export { Spider } from "./client";
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Spider = void 0;
|
|
4
|
-
|
|
4
|
+
var client_1 = require("./client");
|
|
5
5
|
Object.defineProperty(exports, "Spider", { enumerable: true, get: function () { return client_1.Spider; } });
|