Skip to content

Commit e5bbf91

Browse files
committed
Added an optional generic type to API routes to enforce the return type
1 parent 340b3ab commit e5bbf91

File tree

7 files changed

+31
-19
lines changed

7 files changed

+31
-19
lines changed

apps/web/src/app/api/projects/[projectId]/runs/completed/route.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { authHandler } from '$/middlewares/authHandler'
22
import { errorHandler } from '$/middlewares/errorHandler'
33

4-
import { RunSourceGroup, SpanType } from '@latitude-data/constants'
4+
import {
5+
CompletedRun,
6+
RunSourceGroup,
7+
SpanType,
8+
} from '@latitude-data/constants'
59
import { listCompletedRuns } from '@latitude-data/core/services/runs/completed/listCompleted'
610
import { Workspace } from '@latitude-data/core/schema/models/types/Workspace'
711
import { NextRequest, NextResponse } from 'next/server'
812

9-
export const GET = errorHandler(
13+
export const GET = errorHandler<CompletedRun[]>(
1014
authHandler(
1115
async (
1216
request: NextRequest,
@@ -43,7 +47,7 @@ export const GET = errorHandler(
4347
next: runs.next ? JSON.stringify(runs.next) : null,
4448
}
4549

46-
return NextResponse.json(response, { status: 200 })
50+
return NextResponse.json(response.items, { status: 200 })
4751
},
4852
),
4953
)

apps/web/src/middlewares/adminHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { getCurrentUserOrRedirect } from '$/services/auth/getCurrentUser'
22
import { notFound } from 'next/navigation'
33
import { NextRequest } from 'next/server'
4+
import { RouteHandler } from './types'
45

5-
export function adminHandler(handler: any) {
6+
export function adminHandler<T>(handler: RouteHandler<T>) {
67
return async (
78
req: NextRequest,
89
{ params, ...rest }: { params?: Promise<Record<string, string>> } = {},

apps/web/src/middlewares/authHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { getDataFromSession } from '$/data-access'
22
import { NextRequest, NextResponse } from 'next/server'
3+
import { RouteHandler } from './types'
34

4-
export function authHandler(handler: any) {
5+
export function authHandler<T>(handler: RouteHandler<T>) {
56
return async (
67
req: NextRequest,
78
{

apps/web/src/middlewares/errorHandler.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { env } from '@latitude-data/env'
33
import { captureException } from '$/helpers/captureException'
44
import { NextRequest, NextResponse } from 'next/server'
55
import debug from '@latitude-data/core/lib/debug'
6+
import { RouteHandler } from './types'
67

78
interface AbortError extends DOMException {
89
name: 'AbortError'
@@ -18,7 +19,7 @@ export function isAbortError(error: unknown): error is AbortError {
1819
)
1920
}
2021

21-
export function errorHandler(handler: any) {
22+
export function errorHandler<T>(handler: RouteHandler<T>) {
2223
const isDev = env.NODE_ENV === 'development'
2324
return async (req: NextRequest, res: any) => {
2425
try {

apps/web/src/middlewares/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NextRequest, NextResponse } from 'next/server'
2+
3+
type ApiErrorResponse = {
4+
message: string
5+
}
6+
7+
export type RouteHandler<T> = (
8+
req: NextRequest,
9+
res: any,
10+
) => Promise<NextResponse<T> | NextResponse<ApiErrorResponse>>

apps/web/src/services/routes/api.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,6 @@ export const API_ROUTES = {
399399
completed: {
400400
root: `${projectRoot}/runs/completed`,
401401
count: `${projectRoot}/runs/completed/count`,
402-
detail: ({
403-
limit,
404-
sourceGroup,
405-
}: {
406-
limit?: number
407-
sourceGroup?: RunSourceGroup
408-
} = {}) => {
409-
const params = new URLSearchParams()
410-
if (limit) params.set('limit', limit.toString())
411-
if (sourceGroup) params.set('sourceGroup', sourceGroup)
412-
return `${projectRoot}/runs/completed?${params.toString()}`
413-
},
414402
},
415403
detail: (uuid: string) => ({
416404
root: `${projectRoot}/runs/${uuid}`,

apps/web/src/stores/runs/completedRuns.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515

1616
import { useCallback, useMemo } from 'react'
1717
import useSWR, { SWRConfiguration } from 'swr'
18+
import { compactObject } from '@latitude-data/core/lib/compactObject'
1819

1920
export function useCompletedRuns(
2021
{
@@ -32,7 +33,13 @@ export function useCompletedRuns(
3233
opts?: SWRConfiguration,
3334
) {
3435
const fetcher = useFetcher<CompletedRun[]>(
35-
ROUTES.api.projects.detail(project.id).runs.completed.detail(search),
36+
ROUTES.api.projects.detail(project.id).runs.completed.root,
37+
{
38+
searchParams: compactObject({
39+
limit: search?.limit ? search.limit.toString() : undefined,
40+
sourceGroup: search?.sourceGroup,
41+
}) as Record<string, string>,
42+
},
3643
)
3744

3845
const {

0 commit comments

Comments
 (0)