|
1 | 1 | import { request } from "./http.js"; |
2 | 2 | import type { |
| 3 | + ApiCrawlOptions, |
| 4 | + ApiExtractOptions, |
| 5 | + ApiGenerateSchemaOptions, |
| 6 | + ApiHistoryFilterInput, |
| 7 | + ApiMonitorCreateInput, |
| 8 | + ApiScrapeOptions, |
| 9 | + ApiSearchOptions, |
3 | 10 | ClientConfig, |
4 | | - CrawlOptions, |
5 | | - MonitorCreateOptions, |
6 | 11 | RequestOptions, |
7 | 12 | } from "./types/index.js"; |
8 | 13 | import { DEFAULT_BASE_URL } from "./types/index.js"; |
9 | 14 | import { toJsonSchema } from "./zod.js"; |
10 | 15 |
|
11 | | -/** Create a ScrapeGraphAI client. All methods return `{ data, _requestId }`. */ |
| 16 | +/** Create a ScrapeGraphAI client. All methods return `{ data, requestId }`. */ |
12 | 17 | export function scrapegraphai(config: ClientConfig) { |
13 | 18 | const base = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, ""); |
14 | 19 | const key = config.apiKey; |
15 | 20 | const defaults = { maxRetries: config.maxRetries, timeout: config.timeout }; |
16 | 21 |
|
17 | | - function url(path: string) { |
| 22 | + function buildUrl(path: string) { |
18 | 23 | return `${base}${path}`; |
19 | 24 | } |
20 | 25 |
|
21 | | - function opts(ro?: RequestOptions) { |
22 | | - return { ...defaults, ...ro }; |
| 26 | + function mergeRequestOptions(requestOptions?: RequestOptions) { |
| 27 | + return { ...defaults, ...requestOptions }; |
23 | 28 | } |
24 | 29 |
|
25 | 30 | return { |
26 | | - async scrape( |
27 | | - targetUrl: string, |
28 | | - options?: { format?: string; fetchConfig?: Record<string, unknown> }, |
29 | | - ro?: RequestOptions, |
30 | | - ) { |
31 | | - return request("POST", url("/v2/scrape"), key, { url: targetUrl, ...options }, opts(ro)); |
| 31 | + async scrape(url: string, scrapeOptions?: ApiScrapeOptions, requestOptions?: RequestOptions) { |
| 32 | + return request( |
| 33 | + "POST", |
| 34 | + buildUrl("/v2/scrape"), |
| 35 | + key, |
| 36 | + { url, ...scrapeOptions }, |
| 37 | + mergeRequestOptions(requestOptions), |
| 38 | + ); |
32 | 39 | }, |
33 | 40 |
|
34 | | - async extract( |
35 | | - targetUrl: string, |
36 | | - options: { prompt: string; schema?: unknown; llmConfig?: Record<string, unknown> }, |
37 | | - ro?: RequestOptions, |
38 | | - ) { |
39 | | - const body: Record<string, unknown> = { url: targetUrl, prompt: options.prompt }; |
40 | | - if (options.schema) body.schema = toJsonSchema(options.schema); |
41 | | - if (options.llmConfig) body.llmConfig = options.llmConfig; |
42 | | - return request("POST", url("/v2/extract"), key, body, opts(ro)); |
| 41 | + async extract(url: string, extractOptions: ApiExtractOptions, requestOptions?: RequestOptions) { |
| 42 | + const body: Record<string, unknown> = { url, prompt: extractOptions.prompt }; |
| 43 | + if (extractOptions.schema) body.schema = toJsonSchema(extractOptions.schema); |
| 44 | + if (extractOptions.llmConfig) body.llmConfig = extractOptions.llmConfig; |
| 45 | + if (extractOptions.mode) body.mode = extractOptions.mode; |
| 46 | + if (extractOptions.contentType) body.contentType = extractOptions.contentType; |
| 47 | + if (extractOptions.fetchConfig) body.fetchConfig = extractOptions.fetchConfig; |
| 48 | + return request( |
| 49 | + "POST", |
| 50 | + buildUrl("/v2/extract"), |
| 51 | + key, |
| 52 | + body, |
| 53 | + mergeRequestOptions(requestOptions), |
| 54 | + ); |
43 | 55 | }, |
44 | 56 |
|
45 | | - async search(query: string, options?: { numResults?: number }, ro?: RequestOptions) { |
46 | | - return request("POST", url("/v2/search"), key, { query, ...options }, opts(ro)); |
| 57 | + async search(query: string, searchOptions?: ApiSearchOptions, requestOptions?: RequestOptions) { |
| 58 | + return request( |
| 59 | + "POST", |
| 60 | + buildUrl("/v2/search"), |
| 61 | + key, |
| 62 | + { query, ...searchOptions }, |
| 63 | + mergeRequestOptions(requestOptions), |
| 64 | + ); |
47 | 65 | }, |
48 | 66 |
|
49 | 67 | async schema( |
50 | 68 | prompt: string, |
51 | | - options?: { existingSchema?: Record<string, unknown> }, |
52 | | - ro?: RequestOptions, |
| 69 | + schemaOptions?: ApiGenerateSchemaOptions, |
| 70 | + requestOptions?: RequestOptions, |
53 | 71 | ) { |
54 | | - return request("POST", url("/v2/schema"), key, { prompt, ...options }, opts(ro)); |
| 72 | + return request( |
| 73 | + "POST", |
| 74 | + buildUrl("/v2/schema"), |
| 75 | + key, |
| 76 | + { prompt, ...schemaOptions }, |
| 77 | + mergeRequestOptions(requestOptions), |
| 78 | + ); |
55 | 79 | }, |
56 | 80 |
|
57 | | - async credits(ro?: RequestOptions) { |
58 | | - return request("GET", url("/v2/credits"), key, undefined, opts(ro)); |
| 81 | + async credits(requestOptions?: RequestOptions) { |
| 82 | + return request( |
| 83 | + "GET", |
| 84 | + buildUrl("/v2/credits"), |
| 85 | + key, |
| 86 | + undefined, |
| 87 | + mergeRequestOptions(requestOptions), |
| 88 | + ); |
59 | 89 | }, |
60 | 90 |
|
61 | | - async history( |
62 | | - options?: { page?: number; limit?: number; service?: string }, |
63 | | - ro?: RequestOptions, |
64 | | - ) { |
| 91 | + async history(historyFilter?: ApiHistoryFilterInput, requestOptions?: RequestOptions) { |
65 | 92 | const qs = new URLSearchParams(); |
66 | | - if (options?.page != null) qs.set("page", String(options.page)); |
67 | | - if (options?.limit != null) qs.set("limit", String(options.limit)); |
68 | | - if (options?.service) qs.set("service", options.service); |
| 93 | + if (historyFilter?.page != null) qs.set("page", String(historyFilter.page)); |
| 94 | + if (historyFilter?.limit != null) qs.set("limit", String(historyFilter.limit)); |
| 95 | + if (historyFilter?.service) qs.set("service", historyFilter.service); |
69 | 96 | const query = qs.toString(); |
70 | 97 | return request( |
71 | 98 | "GET", |
72 | | - url(`/v2/history${query ? `?${query}` : ""}`), |
| 99 | + buildUrl(`/v2/history${query ? `?${query}` : ""}`), |
73 | 100 | key, |
74 | 101 | undefined, |
75 | | - opts(ro), |
| 102 | + mergeRequestOptions(requestOptions), |
76 | 103 | ); |
77 | 104 | }, |
78 | 105 |
|
79 | 106 | crawl: { |
80 | | - async start(targetUrl: string, options?: CrawlOptions, ro?: RequestOptions) { |
81 | | - return request("POST", url("/v2/crawl"), key, { url: targetUrl, ...options }, opts(ro)); |
| 107 | + async start(url: string, crawlOptions?: ApiCrawlOptions, requestOptions?: RequestOptions) { |
| 108 | + return request( |
| 109 | + "POST", |
| 110 | + buildUrl("/v2/crawl"), |
| 111 | + key, |
| 112 | + { url, ...crawlOptions }, |
| 113 | + mergeRequestOptions(requestOptions), |
| 114 | + ); |
82 | 115 | }, |
83 | 116 |
|
84 | | - async status(id: string, ro?: RequestOptions) { |
85 | | - return request("GET", url(`/v2/crawl/${id}`), key, undefined, opts(ro)); |
| 117 | + async status(id: string, requestOptions?: RequestOptions) { |
| 118 | + return request( |
| 119 | + "GET", |
| 120 | + buildUrl(`/v2/crawl/${id}`), |
| 121 | + key, |
| 122 | + undefined, |
| 123 | + mergeRequestOptions(requestOptions), |
| 124 | + ); |
86 | 125 | }, |
87 | 126 |
|
88 | | - async stop(id: string, ro?: RequestOptions) { |
89 | | - return request("POST", url(`/v2/crawl/${id}/stop`), key, {}, opts(ro)); |
| 127 | + async stop(id: string, requestOptions?: RequestOptions) { |
| 128 | + return request( |
| 129 | + "POST", |
| 130 | + buildUrl(`/v2/crawl/${id}/stop`), |
| 131 | + key, |
| 132 | + {}, |
| 133 | + mergeRequestOptions(requestOptions), |
| 134 | + ); |
90 | 135 | }, |
91 | 136 |
|
92 | | - async resume(id: string, ro?: RequestOptions) { |
93 | | - return request("POST", url(`/v2/crawl/${id}/resume`), key, {}, opts(ro)); |
| 137 | + async resume(id: string, requestOptions?: RequestOptions) { |
| 138 | + return request( |
| 139 | + "POST", |
| 140 | + buildUrl(`/v2/crawl/${id}/resume`), |
| 141 | + key, |
| 142 | + {}, |
| 143 | + mergeRequestOptions(requestOptions), |
| 144 | + ); |
94 | 145 | }, |
95 | 146 | }, |
96 | 147 |
|
97 | 148 | monitor: { |
98 | | - async create(options: MonitorCreateOptions, ro?: RequestOptions) { |
99 | | - return request("POST", url("/v2/monitor"), key, { ...options }, opts(ro)); |
| 149 | + async create(monitorCreateInput: ApiMonitorCreateInput, requestOptions?: RequestOptions) { |
| 150 | + return request( |
| 151 | + "POST", |
| 152 | + buildUrl("/v2/monitor"), |
| 153 | + key, |
| 154 | + { ...monitorCreateInput }, |
| 155 | + mergeRequestOptions(requestOptions), |
| 156 | + ); |
100 | 157 | }, |
101 | 158 |
|
102 | | - async list(ro?: RequestOptions) { |
103 | | - return request("GET", url("/v2/monitor"), key, undefined, opts(ro)); |
| 159 | + async list(requestOptions?: RequestOptions) { |
| 160 | + return request( |
| 161 | + "GET", |
| 162 | + buildUrl("/v2/monitor"), |
| 163 | + key, |
| 164 | + undefined, |
| 165 | + mergeRequestOptions(requestOptions), |
| 166 | + ); |
104 | 167 | }, |
105 | 168 |
|
106 | | - async get(id: string, ro?: RequestOptions) { |
107 | | - return request("GET", url(`/v2/monitor/${id}`), key, undefined, opts(ro)); |
| 169 | + async get(id: string, requestOptions?: RequestOptions) { |
| 170 | + return request( |
| 171 | + "GET", |
| 172 | + buildUrl(`/v2/monitor/${id}`), |
| 173 | + key, |
| 174 | + undefined, |
| 175 | + mergeRequestOptions(requestOptions), |
| 176 | + ); |
108 | 177 | }, |
109 | 178 |
|
110 | | - async pause(id: string, ro?: RequestOptions) { |
111 | | - return request("POST", url(`/v2/monitor/${id}/pause`), key, {}, opts(ro)); |
| 179 | + async pause(id: string, requestOptions?: RequestOptions) { |
| 180 | + return request( |
| 181 | + "POST", |
| 182 | + buildUrl(`/v2/monitor/${id}/pause`), |
| 183 | + key, |
| 184 | + {}, |
| 185 | + mergeRequestOptions(requestOptions), |
| 186 | + ); |
112 | 187 | }, |
113 | 188 |
|
114 | | - async resume(id: string, ro?: RequestOptions) { |
115 | | - return request("POST", url(`/v2/monitor/${id}/resume`), key, {}, opts(ro)); |
| 189 | + async resume(id: string, requestOptions?: RequestOptions) { |
| 190 | + return request( |
| 191 | + "POST", |
| 192 | + buildUrl(`/v2/monitor/${id}/resume`), |
| 193 | + key, |
| 194 | + {}, |
| 195 | + mergeRequestOptions(requestOptions), |
| 196 | + ); |
116 | 197 | }, |
117 | 198 |
|
118 | | - async delete(id: string, ro?: RequestOptions) { |
119 | | - return request("DELETE", url(`/v2/monitor/${id}`), key, undefined, opts(ro)); |
| 199 | + async delete(id: string, requestOptions?: RequestOptions) { |
| 200 | + return request( |
| 201 | + "DELETE", |
| 202 | + buildUrl(`/v2/monitor/${id}`), |
| 203 | + key, |
| 204 | + undefined, |
| 205 | + mergeRequestOptions(requestOptions), |
| 206 | + ); |
120 | 207 | }, |
121 | 208 | }, |
122 | 209 | }; |
|
0 commit comments