improves Goggles handling across project
This commit is contained in:
4 files changed
+23
-11
No files matched your search
+16
-6
@@ -21,7 +21,7 @@ const getDefaultRequestHeaders = (): Record<string, string> => {
|
||||
};
|
||||
};
|
||||
|
||||
const isValidGoggleURL = (url: string) => {
|
||||
const isValidGoggleURL = (url: string): boolean => {
|
||||
try {
|
||||
// Only allow HTTPS URLs
|
||||
return new URL(url).protocol === 'https:';
|
||||
@@ -30,6 +30,16 @@ const isValidGoggleURL = (url: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
const normalizeGoggle = (value: unknown): string | null => {
|
||||
if (typeof value !== 'string') return null;
|
||||
const trimmed = value.trim();
|
||||
if (trimmed.length === 0) return null;
|
||||
if (/^https?:\/\//i.test(trimmed)) {
|
||||
return isValidGoggleURL(trimmed) ? trimmed : null;
|
||||
}
|
||||
return trimmed;
|
||||
};
|
||||
|
||||
async function issueRequest<T extends keyof Endpoints>(
|
||||
endpoint: T,
|
||||
parameters: Endpoints[T]['params'],
|
||||
@@ -81,11 +91,11 @@ async function issueRequest<T extends keyof Endpoints>(
|
||||
|
||||
// Handle `goggles` parameter(s)
|
||||
if (key === 'goggles') {
|
||||
if (typeof value === 'string') {
|
||||
queryParams.set(key, value);
|
||||
} else if (Array.isArray(value)) {
|
||||
for (const url of value.filter(isValidGoggleURL)) {
|
||||
queryParams.append(key, url);
|
||||
const candidates = Array.isArray(value) ? value : [value];
|
||||
for (const candidate of candidates) {
|
||||
const normalized = normalizeGoggle(candidate);
|
||||
if (normalized !== null) {
|
||||
queryParams.append(key, normalized);
|
||||
}
|
||||
}
|
||||
continue;
|
||||
|
||||
@@ -191,7 +191,9 @@ export const RequestParamsSchema = z.object({
|
||||
.optional(),
|
||||
goggles: z
|
||||
.union([z.string(), z.array(z.string())])
|
||||
.describe('The goggle url(s) or definition(s) to rerank search results.')
|
||||
.describe(
|
||||
"Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. Multiple goggle URLs and/or definitions can be provided in an array. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)."
|
||||
)
|
||||
.optional(),
|
||||
freshness: FreshnessSchema.optional(),
|
||||
enable_local: z
|
||||
|
||||
@@ -75,9 +75,9 @@ export const params = z.object({
|
||||
)
|
||||
.optional(),
|
||||
goggles: z
|
||||
.array(z.string())
|
||||
.union([z.string(), z.array(z.string())])
|
||||
.describe(
|
||||
"Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)."
|
||||
"Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. Multiple goggle URLs and/or definitions can be provided in an array. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)."
|
||||
)
|
||||
.optional(),
|
||||
});
|
||||
|
||||
@@ -223,9 +223,9 @@ export const params = z.object({
|
||||
.describe("Result filter (default ['web', 'query'])")
|
||||
.optional(),
|
||||
goggles: z
|
||||
.array(z.string())
|
||||
.union([z.string(), z.array(z.string())])
|
||||
.describe(
|
||||
"Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)."
|
||||
"Goggles act as a custom re-ranking on top of Brave's search index. The parameter supports both a url where the Goggle is hosted or the definition of the Goggle. Multiple goggle URLs and/or definitions can be provided in an array. For more details, refer to the Goggles repository (i.e., https://github.com/brave/goggles-quickstart)."
|
||||
)
|
||||
.optional(),
|
||||
units: z
|
||||
|
||||
Reference in new issue
Block a user