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.
refactor: optimize data fetching in page.tsx with caching
- Loading branch information
1 parent
5ff089f
commit 1dea77f
Showing
5 changed files
with
68 additions
and
40 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,44 +1,26 @@ | ||
import { | ||
EstimatedPackage, | ||
PackageOwnership, | ||
tideliftMeUp, | ||
} from "tidelift-me-up"; | ||
import NodeCache from "node-cache"; | ||
import { EstimatedPackage, tideliftMeUp } from "tidelift-me-up"; | ||
|
||
export type OptionsType = Record<string, unknown>; | ||
export type SearchParamsType = Record<string, unknown>; | ||
export type DataOptions = Record<string, unknown>; | ||
export type DataResults = Error | EstimatedPackage[] | undefined; | ||
|
||
export async function fetchData(searchParams: SearchParamsType) { | ||
const options = getOptions(searchParams); | ||
const result = await getTideliftData(options); | ||
return { options, result }; | ||
} | ||
const cache = new NodeCache(); | ||
|
||
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, | ||
}; | ||
} | ||
export async function fetchData(options: DataOptions) { | ||
const cacheKey = JSON.stringify(options); | ||
|
||
if (cache.has(cacheKey)) { | ||
return cache.get<DataResults>(cacheKey); | ||
} | ||
|
||
async function getTideliftData(options: OptionsType) { | ||
let result: Error | EstimatedPackage[] | undefined; | ||
let result: DataResults; | ||
|
||
try { | ||
result = options.username ? await tideliftMeUp(options) : undefined; | ||
cache.set(cacheKey, result); | ||
} catch (error) { | ||
result = error as Error; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
function undefinedIfEmpty<T>(items: T[]) { | ||
return items.length === 0 ? undefined : items; | ||
} |
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,21 @@ | ||
import { PackageOwnership } from "tidelift-me-up"; | ||
|
||
export type SearchParams = Record<string, unknown>; | ||
|
||
export function getOptions(searchParams: SearchParams) { | ||
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, | ||
}; | ||
} | ||
|
||
function undefinedIfEmpty<T>(items: T[]) { | ||
return items.length === 0 ? undefined : items; | ||
} |