mirror of
https://github.com/mendableai/firecrawl.git
synced 2024-11-16 11:42:24 +08:00
feat(v1/batch/scrape): webhooks
This commit is contained in:
parent
86a78a03cb
commit
df05124ef5
|
@ -66,6 +66,7 @@ export async function batchScrapeController(
|
||||||
crawl_id: id,
|
crawl_id: id,
|
||||||
sitemapped: true,
|
sitemapped: true,
|
||||||
v1: true,
|
v1: true,
|
||||||
|
webhook: req.body.webhook,
|
||||||
},
|
},
|
||||||
opts: {
|
opts: {
|
||||||
jobId: uuidv4(),
|
jobId: uuidv4(),
|
||||||
|
|
|
@ -175,9 +175,21 @@ export const scrapeRequestSchema = scrapeOptions.extend({
|
||||||
export type ScrapeRequest = z.infer<typeof scrapeRequestSchema>;
|
export type ScrapeRequest = z.infer<typeof scrapeRequestSchema>;
|
||||||
export type ScrapeRequestInput = z.input<typeof scrapeRequestSchema>;
|
export type ScrapeRequestInput = z.input<typeof scrapeRequestSchema>;
|
||||||
|
|
||||||
|
export const webhookSchema = z.preprocess(x => {
|
||||||
|
if (typeof x === "string") {
|
||||||
|
return { url: x };
|
||||||
|
} else {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
}, z.object({
|
||||||
|
url: z.string().url(),
|
||||||
|
headers: z.record(z.string(), z.string()).default({}),
|
||||||
|
}).strict(strictMessage))
|
||||||
|
|
||||||
export const batchScrapeRequestSchema = scrapeOptions.extend({
|
export const batchScrapeRequestSchema = scrapeOptions.extend({
|
||||||
urls: url.array(),
|
urls: url.array(),
|
||||||
origin: z.string().optional().default("api"),
|
origin: z.string().optional().default("api"),
|
||||||
|
webhook: webhookSchema.optional(),
|
||||||
}).strict(strictMessage).refine(
|
}).strict(strictMessage).refine(
|
||||||
(obj) => {
|
(obj) => {
|
||||||
const hasExtractFormat = obj.formats?.includes("extract");
|
const hasExtractFormat = obj.formats?.includes("extract");
|
||||||
|
@ -220,17 +232,6 @@ const crawlerOptions = z.object({
|
||||||
|
|
||||||
export type CrawlerOptions = z.infer<typeof crawlerOptions>;
|
export type CrawlerOptions = z.infer<typeof crawlerOptions>;
|
||||||
|
|
||||||
export const webhookSchema = z.preprocess(x => {
|
|
||||||
if (typeof x === "string") {
|
|
||||||
return { url: x };
|
|
||||||
} else {
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
}, z.object({
|
|
||||||
url: z.string().url(),
|
|
||||||
headers: z.record(z.string(), z.string()).default({}),
|
|
||||||
}).strict(strictMessage))
|
|
||||||
|
|
||||||
export const crawlRequestSchema = crawlerOptions.extend({
|
export const crawlRequestSchema = crawlerOptions.extend({
|
||||||
url,
|
url,
|
||||||
origin: z.string().optional().default("api"),
|
origin: z.string().optional().default("api"),
|
||||||
|
|
|
@ -543,16 +543,18 @@ export default class FirecrawlApp {
|
||||||
* @param params - Additional parameters for the scrape request.
|
* @param params - Additional parameters for the scrape request.
|
||||||
* @param pollInterval - Time in seconds for job status checks.
|
* @param pollInterval - Time in seconds for job status checks.
|
||||||
* @param idempotencyKey - Optional idempotency key for the request.
|
* @param idempotencyKey - Optional idempotency key for the request.
|
||||||
|
* @param webhook - Optional webhook for the batch scrape.
|
||||||
* @returns The response from the crawl operation.
|
* @returns The response from the crawl operation.
|
||||||
*/
|
*/
|
||||||
async batchScrapeUrls(
|
async batchScrapeUrls(
|
||||||
urls: string[],
|
urls: string[],
|
||||||
params?: ScrapeParams,
|
params?: ScrapeParams,
|
||||||
pollInterval: number = 2,
|
pollInterval: number = 2,
|
||||||
idempotencyKey?: string
|
idempotencyKey?: string,
|
||||||
|
webhook?: CrawlParams["webhook"],
|
||||||
): Promise<BatchScrapeStatusResponse | ErrorResponse> {
|
): Promise<BatchScrapeStatusResponse | ErrorResponse> {
|
||||||
const headers = this.prepareHeaders(idempotencyKey);
|
const headers = this.prepareHeaders(idempotencyKey);
|
||||||
let jsonData: any = { urls, ...(params ?? {}) };
|
let jsonData: any = { urls, ...(params ?? {}), webhook };
|
||||||
try {
|
try {
|
||||||
const response: AxiosResponse = await this.postRequest(
|
const response: AxiosResponse = await this.postRequest(
|
||||||
this.apiUrl + `/v1/batch/scrape`,
|
this.apiUrl + `/v1/batch/scrape`,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user