Skip to content

feat: migrate API routes to TypeScript#25

Open
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1776394798-migrate-api-routes-to-ts
Open

feat: migrate API routes to TypeScript#25
devin-ai-integration[bot] wants to merge 1 commit into
mainfrom
devin/1776394798-migrate-api-routes-to-ts

Conversation

@devin-ai-integration
Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot commented Apr 17, 2026

Summary

Migrates pages/api/hello.js and pages/api/stripe.js from JavaScript to TypeScript, and sets up the TypeScript foundation for the project.

TypeScript setup:

  • Added tsconfig.json with strict: true, allowJs: true (to coexist with remaining JS files)
  • Added next-env.d.ts (auto-generated by Next.js)
  • Added shared type definitions in types/index.ts (SanityProduct, CartItem, SanityBanner)
  • Installed typescript, @types/react@17.0.39, @types/react-dom@17.0.11, @types/node

Babel removal:

  • Removed .babelrc and uninstalled @babel/core + @babel/preset-react — Next.js 12's built-in SWC compiler handles JSX transformation

API route migrations:

  • hello.ts: Added NextApiRequest/NextApiResponse typing to handler
  • stripe.ts: Typed handler params, added CartItemBody interface for req.body.map(), typed params as Stripe.Checkout.SessionCreateParams, typed error in catch block

Review & Testing Checklist for Human

  • Stripe apiVersion change: The original JS code called new Stripe(key) without an API version. The TS migration now explicitly passes { apiVersion: '2020-08-27' } (required by the Stripe v8 types). Verify this matches the version your Stripe account expects — if your account uses a different default API version, this could subtly change Stripe API behavior.
  • Babel removal: .babelrc and Babel packages are removed in favor of Next.js SWC. If any other part of the build pipeline or tooling relied on Babel, this could break. Run the full app locally and test checkout flow end-to-end.
  • NEXT_PUBLIC_STRIPE_SECRET_KEY! non-null assertion: The env var access uses ! — the app will throw at runtime if this var is missing. Consider whether a startup check or fallback would be safer.
  • CartItemBody vs CartItem: stripe.ts defines its own CartItemBody interface rather than reusing CartItem from types/index.ts. The shapes differ (CartItemBody.image is typed as { asset: { _ref: string } }[] while CartItem.image is any[]). Verify CartItemBody matches what the frontend actually sends in the request body.

Recommended test plan: Start the dev server, add items to cart, complete a Stripe checkout flow, and confirm the /api/stripe endpoint still creates sessions correctly.

Notes

  • @types/react is pinned to 17.0.39 because newer 17.x patches added an exports field that breaks Next.js 12's require.resolve('@types/react/index.d.ts') check during build.
  • tsconfig.json includes "ignoreDeprecations": "6.0" because typescript@6.0.3 deprecates moduleResolution: "node".
  • npx tsc --noEmit and npm run build both pass cleanly.

Link to Devin session: https://app.devin.ai/sessions/2aff6d8e68fb4c6fb96f729410bec23d
Requested by: @WesternConcrete


Open with Devin

- Set up TypeScript foundation (tsconfig.json, types/index.ts)
- Remove .babelrc and babel dependencies
- Install typescript, @types/react, @types/react-dom, @types/node
- Rename pages/api/hello.js -> hello.ts with NextApiRequest/NextApiResponse types
- Rename pages/api/stripe.js -> stripe.ts with full type annotations
  - Type handler params (NextApiRequest, NextApiResponse)
  - Type cart item in req.body.map() with CartItemBody interface
  - Type params as Stripe.Checkout.SessionCreateParams
  - Type catch clause error properly

Verified: npx tsc --noEmit passes, npm run build succeeds
Co-Authored-By: Wes Convery <2wconvery@gmail.com>
@devin-ai-integration
Copy link
Copy Markdown
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Copy link
Copy Markdown
Author

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 3 additional findings.

Open in Devin Review

@devin-ai-integration
Copy link
Copy Markdown
Author

Testing Results

Ran dev server locally and tested all migrated API routes end-to-end.

API Route Tests (4/4 passed)
Test Result
GET /api/hello returns {"name":"John Doe"} ✅ Passed
GET /api/stripe returns 405 Method Not Allowed ✅ Passed
POST /api/stripe with cart item body reaches Stripe API ✅ Passed (400 from Stripe — expected, hardcoded shipping_rate doesn't exist in test account)
Homepage renders correctly after Babel removal ✅ Passed
Evidence

/api/hello

/api/hello response

Homepage (Babel removal regression check)

Homepage

/api/stripe shell output

# GET → 405
$ curl -s -o /dev/null -w "%{http_code}" http://localhost:3000/api/stripe
405

# POST → 400 (Stripe API error, not a runtime crash)
$ curl -s -X POST http://localhost:3000/api/stripe ...
No such shipping rate: 'shr_1Kn3IaEnylLNWUqj5rqhg9oV'
HTTP_STATUS:400

Devin session

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant