Skip to content
Draft
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
68 changes: 56 additions & 12 deletions fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ interface LaunchedCompany {
question_answers: boolean;
}

const loadExistingCompanies = async (
failureReason: string
): Promise<LaunchedCompany[]> => {
try {
const data = await Deno.readTextFile("companies/all.json");
return JSON.parse(data) as LaunchedCompany[];
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
const prefix =
error instanceof Deno.errors.NotFound
? `No cached data available and ${failureReason}.`
: `Failed to load cached data after ${failureReason}.`;
throw new Error(`${prefix} ${message}`);
}
};

const fetchAllCompanies = async (): Promise<LaunchedCompany[]> => {
const baseUrl = "https://45bwzj1sgc-dsn.algolia.net/1/indexes/*/queries";
const params = new URLSearchParams({
Expand All @@ -46,25 +62,53 @@ const fetchAllCompanies = async (): Promise<LaunchedCompany[]> => {
});

console.log("Fetching facets");
const res = await fetch(`${baseUrl}?${params}`, {
method: "POST",
body: JSON.stringify({
requests: [
{
indexName,
params: `facets=%5B%22app_answers%22%2C%22app_video_public%22%2C%22batch%22%2C%22demo_day_video_public%22%2C%22highlight_black%22%2C%22highlight_latinx%22%2C%22highlight_women%22%2C%22industries%22%2C%22isHiring%22%2C%22nonprofit%22%2C%22question_answers%22%2C%22regions%22%2C%22subindustry%22%2C%22tags%22%2C%22top_company%22%5D&hitsPerPage=1000&maxValuesPerFacet=1000&query=&tagFilters=`,
},
],
}),
});
let res: Response;
try {
res = await fetch(`${baseUrl}?${params}`, {
method: "POST",
body: JSON.stringify({
requests: [
{
indexName,
params: `facets=%5B%22app_answers%22%2C%22app_video_public%22%2C%22batch%22%2C%22demo_day_video_public%22%2C%22highlight_black%22%2C%22highlight_latinx%22%2C%22highlight_women%22%2C%22industries%22%2C%22isHiring%22%2C%22nonprofit%22%2C%22question_answers%22%2C%22regions%22%2C%22subindustry%22%2C%22tags%22%2C%22top_company%22%5D&hitsPerPage=1000&maxValuesPerFacet=1000&query=&tagFilters=`,
},
],
}),
});
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(
`Failed to fetch facets. Using existing data. Error: ${message}`
);
return loadExistingCompanies("facet fetch failed");
}

if (!res.ok) {
const errorText = await res.text();
console.warn(
`Failed to fetch facets (${res.status} ${res.statusText}). Using existing data. Response: ${errorText}`
);
return loadExistingCompanies(
`facet request returned ${res.status} ${res.statusText}`
);
}

const json = (await res.json()) as {
results: {
hits: LaunchedCompany[];
facets: { batch: Record<string, number> };
}[];
};
const batches = json.results[0].facets.batch;
if (!json.results?.length) {
console.warn("No results returned. Using existing data.");
return loadExistingCompanies("response missing results");
}

const batches = json.results[0].facets?.batch;
if (!batches) {
console.warn("No batch facets returned. Using existing data.");
return loadExistingCompanies("response missing batch facets");
}

let allCompanies: LaunchedCompany[] = [];
for (const [batch, count] of Object.entries(batches)) {
Expand Down