Skip to content

Add TypeScript types to API layer files in api-src/ #700

Description

@jeremyckahn

Problem

The API layer files in api-src/ have zero TypeScript type annotations. Both files use implicit any for request/response objects, data parameters, and local variables. This is inconsistent with the rest of the codebase which is 100% TypeScript.

Files to Type

api-src/get-market-data.ts

Currently untyped:

export default allowCors(async (req, res) => {
  const roomKey = getRoomName(req)
  const roomData = await getRoomData(roomKey, get, set)
  const { valueAdjustments } = roomData
  set(roomKey, JSON.stringify(roomData))
  res.status(200).json({ valueAdjustments })
})

Variables without types: req, res, roomKey, roomData, valueAdjustments

api-src/post-day-results.ts

Currently untyped:

const applyPositionsToMarket = (valueAdjustments, positions) => {
  return Object.keys(valueAdjustments).reduce(
    (acc, itemName) => { /* ... */ },
    { ...valueAdjustments }
  )
}

export default allowCors(async (req, res) => {
  const { body: { positions = {} } } = req
  const roomKey = getRoomName(req)
  const { valueAdjustments, ...roomData } = await getRoomData(roomKey, get, set)
  // ...
})

Variables without types: valueAdjustments, positions, acc, itemName, itemPositionChange, variance, req, res, roomKey, roomData

Implementation Plan

Step 1: Determine the framework types

Check if the project uses Express, Vercel serverless, or another framework for the API. Look at api-etc/utils.ts for clues about the request/response types.

Step 2: Define shared types

Create types for the data structures used by both API endpoints. These should import from src/farmhand.d.ts:

interface RoomData {
  valueAdjustments: Record<string, number>
  // ... other fields from the room state stored in Redis
}

interface MarketDataResponse {
  valueAdjustments: Record<string, number>
}

interface DayResultsRequest {
  body: {
    positions?: Record<string, number>
  }
}

interface DayResultsResponse {
  valueAdjustments: Record<string, number>
}

Step 3: Annotate api-src/get-market-data.ts

export default allowCors(async (req: Request, res: Response<MarketDataResponse>) => {
  const roomKey = getRoomName(req)
  const roomData = await getRoomData(roomKey, get, set) as RoomData
  const { valueAdjustments } = roomData
  set(roomKey, JSON.stringify(roomData))
  res.status(200).json({ valueAdjustments })
})

Step 4: Annotate api-src/post-day-results.ts

const applyPositionsToMarket = (
  valueAdjustments: Record<string, number>,
  positions: Record<string, number>
): Record<string, number> => {
  // ...
}

export default allowCors(async (req: Request<{}, DayResultsResponse, DayResultsRequest>, res: Response<DayResultsResponse>) => {
  // ...
})

Step 5: Run type checker

npm run check:types

Acceptance Criteria

  • api-src/get-market-data.ts has explicit types on all parameters and variables
  • api-src/post-day-results.ts has explicit types on all parameters and variables
  • Shared types are defined and reused across both API files
  • Zero implicit any types remain in api-src/
  • npm run check:types passes with no errors

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions