@upcrawl/sdk 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/README.md +266 -0
- package/dist/index.d.mts +373 -0
- package/dist/index.d.ts +373 -0
- package/dist/index.js +252 -0
- package/dist/index.mjs +206 -0
- package/package.json +62 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var UpcrawlError = class extends Error {
|
|
3
|
+
constructor(message, status, code = "UNKNOWN_ERROR") {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "UpcrawlError";
|
|
6
|
+
this.status = status;
|
|
7
|
+
this.code = code;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// src/client.ts
|
|
12
|
+
import axios from "axios";
|
|
13
|
+
var DEFAULT_BASE_URL = "https://api.upcrawl.dev/api/v1";
|
|
14
|
+
var DEFAULT_TIMEOUT = 12e4;
|
|
15
|
+
var globalConfig = {
|
|
16
|
+
apiKey: void 0,
|
|
17
|
+
baseUrl: DEFAULT_BASE_URL,
|
|
18
|
+
timeout: DEFAULT_TIMEOUT
|
|
19
|
+
};
|
|
20
|
+
function createClient() {
|
|
21
|
+
if (!globalConfig.apiKey) {
|
|
22
|
+
throw new UpcrawlError(
|
|
23
|
+
"API key not set. Call Upcrawl.setApiKey(apiKey) before making requests.",
|
|
24
|
+
401,
|
|
25
|
+
"API_KEY_NOT_SET"
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
return axios.create({
|
|
29
|
+
baseURL: globalConfig.baseUrl,
|
|
30
|
+
timeout: globalConfig.timeout,
|
|
31
|
+
headers: {
|
|
32
|
+
"Content-Type": "application/json",
|
|
33
|
+
"Authorization": `Bearer ${globalConfig.apiKey}`
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function handleError(error) {
|
|
38
|
+
if (axios.isAxiosError(error)) {
|
|
39
|
+
const axiosError = error;
|
|
40
|
+
const status = axiosError.response?.status || 500;
|
|
41
|
+
const data = axiosError.response?.data;
|
|
42
|
+
if (data?.error) {
|
|
43
|
+
throw new UpcrawlError(data.error.message, status, data.error.code);
|
|
44
|
+
}
|
|
45
|
+
switch (status) {
|
|
46
|
+
case 401:
|
|
47
|
+
throw new UpcrawlError("Invalid or missing API key", 401, "UNAUTHORIZED");
|
|
48
|
+
case 403:
|
|
49
|
+
throw new UpcrawlError("Access forbidden", 403, "FORBIDDEN");
|
|
50
|
+
case 404:
|
|
51
|
+
throw new UpcrawlError("Resource not found", 404, "NOT_FOUND");
|
|
52
|
+
case 429:
|
|
53
|
+
throw new UpcrawlError("Rate limit exceeded", 429, "RATE_LIMIT_EXCEEDED");
|
|
54
|
+
case 500:
|
|
55
|
+
throw new UpcrawlError("Internal server error", 500, "INTERNAL_ERROR");
|
|
56
|
+
case 503:
|
|
57
|
+
throw new UpcrawlError("Service unavailable", 503, "SERVICE_UNAVAILABLE");
|
|
58
|
+
default:
|
|
59
|
+
throw new UpcrawlError(
|
|
60
|
+
axiosError.message || "An unknown error occurred",
|
|
61
|
+
status,
|
|
62
|
+
"UNKNOWN_ERROR"
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (error instanceof UpcrawlError) {
|
|
67
|
+
throw error;
|
|
68
|
+
}
|
|
69
|
+
throw new UpcrawlError(
|
|
70
|
+
error instanceof Error ? error.message : "An unknown error occurred",
|
|
71
|
+
500,
|
|
72
|
+
"UNKNOWN_ERROR"
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
function setApiKey(apiKey) {
|
|
76
|
+
if (!apiKey || typeof apiKey !== "string") {
|
|
77
|
+
throw new UpcrawlError("API key must be a non-empty string", 400, "INVALID_API_KEY");
|
|
78
|
+
}
|
|
79
|
+
globalConfig.apiKey = apiKey;
|
|
80
|
+
}
|
|
81
|
+
function setBaseUrl(baseUrl) {
|
|
82
|
+
if (!baseUrl || typeof baseUrl !== "string") {
|
|
83
|
+
throw new UpcrawlError("Base URL must be a non-empty string", 400, "INVALID_BASE_URL");
|
|
84
|
+
}
|
|
85
|
+
globalConfig.baseUrl = baseUrl.replace(/\/$/, "");
|
|
86
|
+
}
|
|
87
|
+
function setTimeout(timeout) {
|
|
88
|
+
if (typeof timeout !== "number" || timeout < 1e3) {
|
|
89
|
+
throw new UpcrawlError("Timeout must be a number >= 1000ms", 400, "INVALID_TIMEOUT");
|
|
90
|
+
}
|
|
91
|
+
globalConfig.timeout = timeout;
|
|
92
|
+
}
|
|
93
|
+
function configure(config) {
|
|
94
|
+
if (config.apiKey) setApiKey(config.apiKey);
|
|
95
|
+
if (config.baseUrl) setBaseUrl(config.baseUrl);
|
|
96
|
+
if (config.timeout) setTimeout(config.timeout);
|
|
97
|
+
}
|
|
98
|
+
function getConfig() {
|
|
99
|
+
return {
|
|
100
|
+
apiKeySet: !!globalConfig.apiKey,
|
|
101
|
+
baseUrl: globalConfig.baseUrl,
|
|
102
|
+
timeout: globalConfig.timeout
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function resetConfig() {
|
|
106
|
+
globalConfig = {
|
|
107
|
+
apiKey: void 0,
|
|
108
|
+
baseUrl: DEFAULT_BASE_URL,
|
|
109
|
+
timeout: DEFAULT_TIMEOUT
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
async function scrape(options) {
|
|
113
|
+
try {
|
|
114
|
+
const client = createClient();
|
|
115
|
+
const response = await client.post("/scrape/single", options);
|
|
116
|
+
return response.data;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
handleError(error);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async function batchScrape(options) {
|
|
122
|
+
try {
|
|
123
|
+
const client = createClient();
|
|
124
|
+
const response = await client.post("/scrape/batch", options);
|
|
125
|
+
return response.data;
|
|
126
|
+
} catch (error) {
|
|
127
|
+
handleError(error);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
async function search(options) {
|
|
131
|
+
try {
|
|
132
|
+
const client = createClient();
|
|
133
|
+
const response = await client.post("/search", options);
|
|
134
|
+
return response.data;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
handleError(error);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// src/index.ts
|
|
141
|
+
var Upcrawl = {
|
|
142
|
+
/**
|
|
143
|
+
* Set the API key globally
|
|
144
|
+
* @param apiKey - Your Upcrawl API key (starts with 'uc-')
|
|
145
|
+
*/
|
|
146
|
+
setApiKey,
|
|
147
|
+
/**
|
|
148
|
+
* Set a custom base URL (useful for self-hosted or testing)
|
|
149
|
+
* @param baseUrl - Custom API base URL
|
|
150
|
+
*/
|
|
151
|
+
setBaseUrl,
|
|
152
|
+
/**
|
|
153
|
+
* Set request timeout in milliseconds
|
|
154
|
+
* @param timeout - Timeout in milliseconds
|
|
155
|
+
*/
|
|
156
|
+
setTimeout,
|
|
157
|
+
/**
|
|
158
|
+
* Configure multiple options at once
|
|
159
|
+
* @param config - Configuration object
|
|
160
|
+
*/
|
|
161
|
+
configure,
|
|
162
|
+
/**
|
|
163
|
+
* Get current configuration (for debugging)
|
|
164
|
+
*/
|
|
165
|
+
getConfig,
|
|
166
|
+
/**
|
|
167
|
+
* Reset configuration to defaults
|
|
168
|
+
*/
|
|
169
|
+
resetConfig,
|
|
170
|
+
/**
|
|
171
|
+
* Scrape a single URL
|
|
172
|
+
* @param options - Scrape options including the URL to scrape
|
|
173
|
+
* @returns Promise with scrape response
|
|
174
|
+
*/
|
|
175
|
+
scrape,
|
|
176
|
+
/**
|
|
177
|
+
* Scrape multiple URLs in a batch
|
|
178
|
+
* @param options - Batch scrape options including URLs to scrape
|
|
179
|
+
* @returns Promise with batch scrape response
|
|
180
|
+
*/
|
|
181
|
+
batchScrape,
|
|
182
|
+
/**
|
|
183
|
+
* Search the web
|
|
184
|
+
* @param options - Search options including queries
|
|
185
|
+
* @returns Promise with search response
|
|
186
|
+
*/
|
|
187
|
+
search,
|
|
188
|
+
/**
|
|
189
|
+
* Error class for Upcrawl API errors
|
|
190
|
+
*/
|
|
191
|
+
UpcrawlError
|
|
192
|
+
};
|
|
193
|
+
var index_default = Upcrawl;
|
|
194
|
+
export {
|
|
195
|
+
UpcrawlError,
|
|
196
|
+
batchScrape,
|
|
197
|
+
configure,
|
|
198
|
+
index_default as default,
|
|
199
|
+
getConfig,
|
|
200
|
+
resetConfig,
|
|
201
|
+
scrape,
|
|
202
|
+
search,
|
|
203
|
+
setApiKey,
|
|
204
|
+
setBaseUrl,
|
|
205
|
+
setTimeout
|
|
206
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@upcrawl/sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"publishConfig": {
|
|
5
|
+
"access": "public"
|
|
6
|
+
},
|
|
7
|
+
"description": "Official Upcrawl SDK - Extract data from any website with a single API call",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"module": "dist/index.mjs",
|
|
10
|
+
"types": "dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.mjs",
|
|
15
|
+
"require": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
23
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
24
|
+
"lint": "eslint src/",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"prepublishOnly": "npm run build"
|
|
27
|
+
},
|
|
28
|
+
"keywords": [
|
|
29
|
+
"upcrawl",
|
|
30
|
+
"web-scraping",
|
|
31
|
+
"scraper",
|
|
32
|
+
"crawler",
|
|
33
|
+
"web-crawler",
|
|
34
|
+
"data-extraction",
|
|
35
|
+
"ai",
|
|
36
|
+
"llm",
|
|
37
|
+
"markdown",
|
|
38
|
+
"search",
|
|
39
|
+
"api"
|
|
40
|
+
],
|
|
41
|
+
"author": "Upcrawl",
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "https://github.com/upcrawl/upcrawl-js"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://upcrawl.dev",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/upcrawl/upcrawl-js/issues"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"axios": "^1.6.0"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/node": "^20.10.0",
|
|
56
|
+
"tsup": "^8.0.0",
|
|
57
|
+
"typescript": "^5.3.0"
|
|
58
|
+
},
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=16.0.0"
|
|
61
|
+
}
|
|
62
|
+
}
|