From 671b594f9aaf92ba4d1bacb4d71bf885366d02e5 Mon Sep 17 00:00:00 2001 From: adriangohjw Date: Fri, 15 Nov 2024 09:24:45 +0800 Subject: [PATCH] better typechecking --- .../modules/resource/resource.router.ts | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/apps/studio/src/server/modules/resource/resource.router.ts b/apps/studio/src/server/modules/resource/resource.router.ts index 406065150..51e448002 100644 --- a/apps/studio/src/server/modules/resource/resource.router.ts +++ b/apps/studio/src/server/modules/resource/resource.router.ts @@ -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() === "" @@ -573,19 +582,14 @@ export const resourceRouter = router({ const getResourcesWithFullPermalink = async ({ resources, }: { - resources: { - id: string - title: string - type: string - parentId: string | null - }[] - }) => { + resources: Omit[] + }): Promise => { return await Promise.all( resources.map(async (resource) => ({ ...resource, fullPermalink: await getWithFullPermalink({ resourceId: resource.id, - }).then((r) => r?.fullPermalink), + }).then((r) => r?.fullPermalink ?? ""), })), ) } @@ -593,9 +597,9 @@ export const resourceRouter = router({ // 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)) { @@ -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[], }), }, } @@ -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")