Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ takoSearch({
baseUrl: 'https://tako.com', // optional; override for staging
effort: 'fast', // 'fast' (default) | 'instant' | 'deep'
sources: { // a source is searched iff its key is present; omit to search both
tako: { count: 5, includeContents: false, deferDataRetrieval: false },
data: { count: 5, includeContents: false, deferDataRetrieval: false }, // legacy alias: tako
web: { count: 5, includeContents: false },
},
countryCode: 'US', // default 'US'
Expand Down
16 changes: 9 additions & 7 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface SearchRequestBody {
country_code: string;
locale: string;
sources?: {
tako?: { count?: number; include_contents?: boolean; defer_data_retrieval?: boolean };
data?: { count?: number; include_contents?: boolean; defer_data_retrieval?: boolean };
web?: { count?: number; include_contents?: boolean };
};
timezone?: string;
Expand All @@ -34,12 +34,14 @@ export function buildSearchRequestBody(config: TakoRetrievalConfig, query: strin

if (config.sources) {
const sources: NonNullable<SearchRequestBody["sources"]> = {};
if (config.sources.tako) {
const tako: NonNullable<NonNullable<SearchRequestBody["sources"]>["tako"]> = {};
if (config.sources.tako.count !== undefined) tako.count = config.sources.tako.count;
if (config.sources.tako.includeContents !== undefined) tako.include_contents = config.sources.tako.includeContents;
if (config.sources.tako.deferDataRetrieval !== undefined) tako.defer_data_retrieval = config.sources.tako.deferDataRetrieval;
sources.tako = tako;
// `data` is the curated Tako source; `tako` is the deprecated legacy alias.
const dataSource = config.sources.data ?? config.sources.tako;
if (dataSource) {
const data: NonNullable<NonNullable<SearchRequestBody["sources"]>["data"]> = {};
if (dataSource.count !== undefined) data.count = dataSource.count;
if (dataSource.includeContents !== undefined) data.include_contents = dataSource.includeContents;
if (dataSource.deferDataRetrieval !== undefined) data.defer_data_retrieval = dataSource.deferDataRetrieval;
sources.data = data;
}
if (config.sources.web) {
const web: NonNullable<NonNullable<SearchRequestBody["sources"]>["web"]> = {};
Expand Down
10 changes: 8 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ export interface TakoCardSourceOptions extends TakoSourceOptions {
export interface TakoRetrievalConfig extends TakoBaseConfig {
/** "fast" (default) | "instant" | "deep". */
effort?: TakoSearchEffort;
/** Per-source settings. A source is searched iff its key is present. Omit to search tako + web. */
sources?: { tako?: TakoCardSourceOptions; web?: TakoSourceOptions };
/** Per-source settings. A source is searched iff its key is present. Omit to search data + web. */
sources?: {
/** The curated Tako data source. */
data?: TakoCardSourceOptions;
web?: TakoSourceOptions;
/** @deprecated Use `data`. Legacy alias for the curated Tako source. */
tako?: TakoCardSourceOptions;
};
/** ISO 3166-1 alpha-2 country code. Default "US". */
countryCode?: string;
/** BCP-47 locale tag. Default "en-US". */
Expand Down
16 changes: 14 additions & 2 deletions tests/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("takoSearch", () => {
const t = takoSearch({
apiKey: "key",
effort: "deep",
sources: { tako: { count: 10, deferDataRetrieval: true } },
sources: { data: { count: 10, deferDataRetrieval: true } },
timezone: "America/New_York",
outputSettings: { imageDarkMode: true },
});
Expand All @@ -39,12 +39,24 @@ describe("takoSearch", () => {
effort: "deep",
country_code: "US",
locale: "en-US",
sources: { tako: { count: 10, defer_data_retrieval: true } },
sources: { data: { count: 10, defer_data_retrieval: true } },
timezone: "America/New_York",
output_settings: { image_dark_mode: true },
});
});

it("maps the deprecated `tako` source alias onto the `data` wire key", async () => {
const fetchMock = stubFetch(200, OK);
const t = takoSearch({
apiKey: "key",
sources: { tako: { count: 10, deferDataRetrieval: true } },
});
await runTool(t, { query: "x" });
const init = fetchMock.mock.calls[0][1] as RequestInit;
const body = JSON.parse(init.body as string);
expect(body.sources).toEqual({ data: { count: 10, defer_data_retrieval: true } });
});

it("honors baseUrl override (trailing slash stripped)", async () => {
const fetchMock = stubFetch(200, OK);
const t = takoSearch({ apiKey: "key", baseUrl: "https://staging.trytako.com/" });
Expand Down
2 changes: 1 addition & 1 deletion tests/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe("types", () => {
apiKey: "k",
baseUrl: "https://staging.trytako.com",
effort: "deep",
sources: { tako: { count: 10, deferDataRetrieval: true }, web: { count: 3, includeContents: true } },
sources: { data: { count: 10, deferDataRetrieval: true }, web: { count: 3, includeContents: true } },
countryCode: "US",
locale: "en-US",
timezone: "America/New_York",
Expand Down
Loading