Skip to content

Commit

Permalink
better typechecking
Browse files Browse the repository at this point in the history
  • Loading branch information
adriangohjw committed Nov 15, 2024
1 parent d3af5c3 commit 671b594
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions apps/studio/src/server/modules/resource/resource.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,15 @@ export const resourceRouter = router({
search: protectedProcedure
.input(searchSchema)
.query(async ({ input: { siteId, query = "", cursor: offset, limit } }) => {
interface ResourceInterface {
id: string
title: string
type: string
parentId: string | null
lastUpdatedAt: Date | null
fullPermalink: string
}

// check if the query is only whitespaces (including multiple spaces)
function isWhitespaces(input: string): boolean {
return input.trim() === ""
Expand All @@ -573,29 +582,24 @@ export const resourceRouter = router({
const getResourcesWithFullPermalink = async ({
resources,
}: {
resources: {
id: string
title: string
type: string
parentId: string | null
}[]
}) => {
resources: Omit<ResourceInterface, "fullPermalink">[]
}): Promise<ResourceInterface[]> => {
return await Promise.all(
resources.map(async (resource) => ({
...resource,
fullPermalink: await getWithFullPermalink({
resourceId: resource.id,
}).then((r) => r?.fullPermalink),
}).then((r) => r?.fullPermalink ?? ""),
})),
)
}

// defined here to ensure the return type is correct
async function getResults(): Promise<{
totalCount: number | null
resources: { id: string }[]
resources: ResourceInterface[]
suggestions: {
recentlyEdited: { id: string }[]
recentlyEdited: ResourceInterface[]
}
}> {
if (isWhitespaces(query)) {
Expand All @@ -605,7 +609,9 @@ export const resourceRouter = router({
suggestions: {
recentlyEdited: await getResourcesWithFullPermalink({
// Hardcoded for now to be 5
resources: await getAllResourcesFound().limit(5).execute(),
resources: (await getAllResourcesFound()
.limit(5)
.execute()) as ResourceInterface[],
}),
},
}
Expand Down Expand Up @@ -651,12 +657,12 @@ export const resourceRouter = router({
sql`GREATEST("Resource"."updatedAt", "Blob"."updatedAt") DESC`,
)

const resourcesToReturn = await orderedResources
const resourcesToReturn: ResourceInterface[] = (await orderedResources
.offset(offset)
.limit(limit)
.execute()
.execute()) as ResourceInterface[]

const totalCount = (
const totalCount: number = (
await db
.with("queriedResources", () => queriedResources)
.selectFrom("queriedResources")
Expand Down

0 comments on commit 671b594

Please sign in to comment.