feat: add flexible region matching to servers API
- Add shared buildFlexibleRegionConditions() in utils.ts - Add COUNTRY_NAME_TO_REGIONS mapping for country/city expansion - Update servers.ts to use flexible region matching (korea, tokyo, japan, etc.) - Update recommend.ts to use shared function (remove duplicate code) - Fix servers GROUP BY to show all regions (it.id, r.id) - Update CLAUDE.md with single-line curl examples Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
*/
|
||||
|
||||
import type { Env } from '../types';
|
||||
import { jsonResponse, isValidServer, DEFAULT_REGION_FILTER_SQL } from '../utils';
|
||||
import { jsonResponse, isValidServer, DEFAULT_REGION_FILTER_SQL, buildFlexibleRegionConditions } from '../utils';
|
||||
|
||||
/**
|
||||
* GET /api/servers - Server list with filtering
|
||||
@@ -42,9 +42,9 @@ export async function handleGetServers(
|
||||
it.instance_family,
|
||||
it.gpu_count,
|
||||
it.gpu_type,
|
||||
MIN(pr.monthly_price) as monthly_price,
|
||||
MIN(r.region_name) as region_name,
|
||||
MIN(r.region_code) as region_code
|
||||
pr.monthly_price,
|
||||
r.region_name,
|
||||
r.region_code
|
||||
FROM instance_types it
|
||||
JOIN providers p ON it.provider_id = p.id
|
||||
JOIN pricing pr ON pr.instance_type_id = it.id
|
||||
@@ -79,11 +79,13 @@ export async function handleGetServers(
|
||||
}
|
||||
|
||||
if (region) {
|
||||
query += ` AND r.region_code = ?`;
|
||||
params.push(region);
|
||||
// Flexible region matching: supports country names, codes, city names
|
||||
const { conditions, params: regionParams } = buildFlexibleRegionConditions([region]);
|
||||
query += ` AND (${conditions.join(' OR ')})`;
|
||||
params.push(...regionParams);
|
||||
}
|
||||
|
||||
query += ` GROUP BY it.id ORDER BY MIN(pr.monthly_price) ASC LIMIT 100`;
|
||||
query += ` GROUP BY it.id, r.id ORDER BY pr.monthly_price ASC LIMIT 100`;
|
||||
|
||||
const result = await env.DB.prepare(query).bind(...params).all();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user