Skip to content

Commit 2881001

Browse files
committed
fixes
1 parent 842816b commit 2881001

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

js/botasaurus-desktop-api/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,8 @@ const api = new Api({ apiUrl: 'https://example.com' })`);
375375
const url = this._makeApiUrl(scraperName);
376376
try {
377377
const response = await axios.get(url, { params: data });
378-
379-
this._writeJson(`${scraperName}-result`, response.data);
378+
const fileScraperName = scraperName.replaceAll('/', '-');
379+
this._writeJson(`${fileScraperName}-result`, response.data);
380380
return response.data;
381381
} catch (error) {
382382
if (axios.isAxiosError(error)) {

js/botasaurus-server-js/src/api-config.ts

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ function addCorsHeaders(reply: any) {
3333
reply.header("Access-Control-Allow-Headers", "Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token");
3434
}
3535

36-
function addScraperRoutes(app:FastifyInstance, apiBasePath: string) {
36+
function addScraperRoutes(app: FastifyInstance, apiBasePath: string) {
3737
Object.values(Server.scrapers).forEach(scraper => {
38+
// Get the main route path
3839
const routePath = `${apiBasePath}/${kebabCase(scraper.scraper_name)}`
3940
const fn = scraper.function
4041
const key = Server.isScraperBasedRateLimit ? scraper.scraper_name : scraper.scraper_type
4142

42-
app.get(routePath, async (request, reply) => {
43+
const scrapingFunction = async (request: any, reply: any) => {
4344
try {
44-
const params: Record<string, any> = {};
45+
const params: Record<string, any> = {}
4546
for (const [key, value] of Object.entries(request.query as any)) {
4647
if (key.endsWith("[]")) {
47-
params[key.slice(0, -2)] = Array.isArray(value) ? value : [value];
48+
params[key.slice(0, -2)] = Array.isArray(value) ? value : [value]
4849
} else {
49-
params[key] = value;
50+
params[key] = value
5051
}
5152
}
52-
53-
53+
5454
// Validate params against scraper's input definition
5555
const [validatedData, metadata] = validateDirectCallRequest(scraper.scraper_name, params)
5656

@@ -100,7 +100,6 @@ function addScraperRoutes(app:FastifyInstance, apiBasePath: string) {
100100
finalResults = results.data
101101
}
102102

103-
104103
// Cache results if appropriate
105104
if (Server.cache && !isResultDontCached) {
106105
try {
@@ -118,15 +117,29 @@ function addScraperRoutes(app:FastifyInstance, apiBasePath: string) {
118117
}
119118
} catch (error: any) {
120119
if (error instanceof JsonHTTPResponseWithMessage) {
121-
throw error; // Re-throw the error to be handled elsewhere
122-
}
120+
throw error // Re-throw the error to be handled elsewhere
121+
}
123122
console.error('Scraping failed:', error)
124123
return reply.status(500).send({
125124
error: 'Scraping failed',
126125
message: error.message
127126
})
128127
}
129-
})
128+
}
129+
130+
// Register main route
131+
app.get(routePath, scrapingFunction)
132+
133+
// Get any aliases for this scraper
134+
const aliases = ApiConfig.routeAliases.get(scraper.function) || []
135+
136+
// Register all aliases if they exist
137+
if (aliases.length > 0) {
138+
aliases.forEach(alias => {
139+
const fullAliasPath = `${apiBasePath}${cleanBasePath(alias)}`
140+
app.get(fullAliasPath, scrapingFunction)
141+
})
142+
}
130143
})
131144
}
132145
export function buildApp(scrapers:any[], apiBasePath: string): FastifyInstance {
@@ -361,6 +374,7 @@ class ApiConfig {
361374
static apiOnlyMode: boolean;
362375
static routeSetupFn?: (server: FastifyInstance) => void;
363376
static apiBasePath: string = ''; // Default empty
377+
static routeAliases: Map<Function, string[]> = new Map();
364378

365379
/**
366380
* Enables API
@@ -434,6 +448,26 @@ class ApiConfig {
434448
this.apiBasePath = cleanBasePath(basePath) as any;
435449
}
436450

451+
452+
/**
453+
* Adds aliases for a specific scraper's routes.
454+
* @param {Scraper} scraper - The scraper instance to add aliases for
455+
* @param {...string} aliases - One or more alias paths (e.g., '/hotels', '/lodging')
456+
* @example
457+
* ApiConfig.addScraperAliases(hotelScraper, '/hotels', '/resort');
458+
*/
459+
static addScraperAliases(scraper: Function, ...aliases: string[]): void {
460+
if (!this.routeAliases.has(scraper)) {
461+
this.routeAliases.set(scraper, aliases);
462+
} else {
463+
const aliasArray = this.routeAliases.get(scraper)!;
464+
aliases.forEach(alias => {
465+
if (alias && !aliasArray.includes(alias)) {
466+
aliasArray.push(alias);
467+
}
468+
});
469+
}
470+
}
437471
}
438472

439473
export default ApiConfig ;

0 commit comments

Comments
 (0)