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 {
|
try {
|
||||||
// Only allow HTTPS URLs
|
// Only allow HTTPS URLs
|
||||||
return new URL(url).protocol === 'https:';
|
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>(
|
async function issueRequest<T extends keyof Endpoints>(
|
||||||
endpoint: T,
|
endpoint: T,
|
||||||
parameters: Endpoints[T]['params'],
|
parameters: Endpoints[T]['params'],
|
||||||
@@ -81,11 +91,11 @@ async function issueRequest<T extends keyof Endpoints>(
|
|||||||
|
|
||||||
// Handle `goggles` parameter(s)
|
// Handle `goggles` parameter(s)
|
||||||
if (key === 'goggles') {
|
if (key === 'goggles') {
|
||||||
if (typeof value === 'string') {
|
const candidates = Array.isArray(value) ? value : [value];
|
||||||
queryParams.set(key, value);
|
for (const candidate of candidates) {
|
||||||
} else if (Array.isArray(value)) {
|
const normalized = normalizeGoggle(candidate);
|
||||||
for (const url of value.filter(isValidGoggleURL)) {
|
if (normalized !== null) {
|
||||||
queryParams.append(key, url);
|
queryParams.append(key, normalized);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -191,7 +191,9 @@ export const RequestParamsSchema = z.object({
|
|||||||
.optional(),
|
.optional(),
|
||||||
goggles: z
|
goggles: z
|
||||||
.union([z.string(), z.array(z.string())])
|
.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(),
|
.optional(),
|
||||||
freshness: FreshnessSchema.optional(),
|
freshness: FreshnessSchema.optional(),
|
||||||
enable_local: z
|
enable_local: z
|
||||||
|
|||||||
@@ -75,9 +75,9 @@ export const params = z.object({
|
|||||||
)
|
)
|
||||||
.optional(),
|
.optional(),
|
||||||
goggles: z
|
goggles: z
|
||||||
.array(z.string())
|
.union([z.string(), z.array(z.string())])
|
||||||
.describe(
|
.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(),
|
.optional(),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -223,9 +223,9 @@ export const params = z.object({
|
|||||||
.describe("Result filter (default ['web', 'query'])")
|
.describe("Result filter (default ['web', 'query'])")
|
||||||
.optional(),
|
.optional(),
|
||||||
goggles: z
|
goggles: z
|
||||||
.array(z.string())
|
.union([z.string(), z.array(z.string())])
|
||||||
.describe(
|
.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(),
|
.optional(),
|
||||||
units: z
|
units: z
|
||||||
|
|||||||
Reference in new issue
Block a user