@spider-cloud/spider-client 0.0.19 → 0.0.21
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 +27 -0
- package/dist/client.js +45 -0
- 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
|
@@ -29,6 +29,12 @@ export declare class Spider {
|
|
|
29
29
|
* @returns {Promise<any>} The data returned from the endpoint in JSON format.
|
|
30
30
|
*/
|
|
31
31
|
private _apiGet;
|
|
32
|
+
/**
|
|
33
|
+
* Internal method to handle DELETE requests.
|
|
34
|
+
* @param {string} endpoint - The API endpoint from which data should be retrieved.
|
|
35
|
+
* @returns {Promise<any>} The data returned from the endpoint in JSON format.
|
|
36
|
+
*/
|
|
37
|
+
private _apiDelete;
|
|
32
38
|
/**
|
|
33
39
|
* Scrapes data from a specified URL.
|
|
34
40
|
* @param {string} url - The URL to scrape.
|
|
@@ -84,6 +90,27 @@ export declare class Spider {
|
|
|
84
90
|
* @returns {Promise<any>} The current credit balance.
|
|
85
91
|
*/
|
|
86
92
|
getCredits(): Promise<any>;
|
|
93
|
+
/**
|
|
94
|
+
* Send a POST request to insert data into a specified table.
|
|
95
|
+
* @param {string} table - The table name in the database.
|
|
96
|
+
* @param {object} data - The data to be inserted.
|
|
97
|
+
* @returns {Promise<any>} The response from the server.
|
|
98
|
+
*/
|
|
99
|
+
postData(table: string, data: object): Promise<any>;
|
|
100
|
+
/**
|
|
101
|
+
* Send a GET request to retrieve data from a specified table.
|
|
102
|
+
* @param {string} table - The table name in the database.
|
|
103
|
+
* @param {object} params - The query parameters for data retrieval.
|
|
104
|
+
* @returns {Promise<any>} The response from the server.
|
|
105
|
+
*/
|
|
106
|
+
getData(table: string, params: object): Promise<any>;
|
|
107
|
+
/**
|
|
108
|
+
* Send a DELETE request to remove data from a specified table.
|
|
109
|
+
* @param {string} table - The table name in the database.
|
|
110
|
+
* @param {object} params - Parameters to identify records to delete.
|
|
111
|
+
* @returns {Promise<any>} The response from the server.
|
|
112
|
+
*/
|
|
113
|
+
deleteData(table: string, params: object): Promise<any>;
|
|
87
114
|
/**
|
|
88
115
|
* Prepares common headers for each API request.
|
|
89
116
|
* @returns {HeadersInit} A headers object for fetch requests.
|
package/dist/client.js
CHANGED
|
@@ -59,6 +59,24 @@ class Spider {
|
|
|
59
59
|
this.handleError(response, `get from ${endpoint}`);
|
|
60
60
|
}
|
|
61
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Internal method to handle DELETE requests.
|
|
64
|
+
* @param {string} endpoint - The API endpoint from which data should be retrieved.
|
|
65
|
+
* @returns {Promise<any>} The data returned from the endpoint in JSON format.
|
|
66
|
+
*/
|
|
67
|
+
async _apiDelete(endpoint) {
|
|
68
|
+
const headers = this.prepareHeaders();
|
|
69
|
+
const response = await fetch(`https://api.spider.cloud/v1/${endpoint}`, {
|
|
70
|
+
method: "DELETE",
|
|
71
|
+
headers,
|
|
72
|
+
});
|
|
73
|
+
if (response.ok) {
|
|
74
|
+
return response.json();
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
this.handleError(response, `get from ${endpoint}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
62
80
|
/**
|
|
63
81
|
* Scrapes data from a specified URL.
|
|
64
82
|
* @param {string} url - The URL to scrape.
|
|
@@ -130,6 +148,33 @@ class Spider {
|
|
|
130
148
|
async getCredits() {
|
|
131
149
|
return this._apiGet("credits");
|
|
132
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Send a POST request to insert data into a specified table.
|
|
153
|
+
* @param {string} table - The table name in the database.
|
|
154
|
+
* @param {object} data - The data to be inserted.
|
|
155
|
+
* @returns {Promise<any>} The response from the server.
|
|
156
|
+
*/
|
|
157
|
+
async postData(table, data) {
|
|
158
|
+
return this._apiPost(`data/${table}`, data);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Send a GET request to retrieve data from a specified table.
|
|
162
|
+
* @param {string} table - The table name in the database.
|
|
163
|
+
* @param {object} params - The query parameters for data retrieval.
|
|
164
|
+
* @returns {Promise<any>} The response from the server.
|
|
165
|
+
*/
|
|
166
|
+
async getData(table, params) {
|
|
167
|
+
return this._apiGet(`data/${table}?${new URLSearchParams(params).toString()}`);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Send a DELETE request to remove data from a specified table.
|
|
171
|
+
* @param {string} table - The table name in the database.
|
|
172
|
+
* @param {object} params - Parameters to identify records to delete.
|
|
173
|
+
* @returns {Promise<any>} The response from the server.
|
|
174
|
+
*/
|
|
175
|
+
async deleteData(table, params) {
|
|
176
|
+
return this._apiDelete(`data/${table}?${new URLSearchParams(params).toString()}`);
|
|
177
|
+
}
|
|
133
178
|
/**
|
|
134
179
|
* Prepares common headers for each API request.
|
|
135
180
|
* @returns {HeadersInit} A headers object for fetch requests.
|