generated from JoshuaKGoldberg/create-typescript-app
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add dynamic metadata for search results
- Loading branch information
1 parent
774563e
commit 5ff089f
Showing
3 changed files
with
65 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
EstimatedPackage, | ||
PackageOwnership, | ||
tideliftMeUp, | ||
} from "tidelift-me-up"; | ||
|
||
export type OptionsType = Record<string, unknown>; | ||
export type SearchParamsType = Record<string, unknown>; | ||
|
||
export async function fetchData(searchParams: SearchParamsType) { | ||
const options = getOptions(searchParams); | ||
const result = await getTideliftData(options); | ||
return { options, result }; | ||
} | ||
|
||
function getOptions(searchParams: SearchParamsType) { | ||
return { | ||
ownership: undefinedIfEmpty( | ||
[ | ||
searchParams.author === "on" && "author", | ||
searchParams.maintainer === "on" && "maintainer", | ||
searchParams.publisher === "on" && "publisher", | ||
].filter(Boolean) as PackageOwnership[], | ||
), | ||
since: (searchParams.since || undefined) as string | undefined, | ||
username: searchParams.username as string, | ||
}; | ||
} | ||
|
||
async function getTideliftData(options: OptionsType) { | ||
let result: Error | EstimatedPackage[] | undefined; | ||
|
||
try { | ||
result = options.username ? await tideliftMeUp(options) : undefined; | ||
} catch (error) { | ||
result = error as Error; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function undefinedIfEmpty<T>(items: T[]) { | ||
return items.length === 0 ? undefined : items; | ||
} |