Skip to content

Improve NextApiResponse typing #7841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 9, 2019
Merged

Improve NextApiResponse typing #7841

merged 2 commits into from
Jul 9, 2019

Conversation

VincentCordobes
Copy link
Contributor

@VincentCordobes VincentCordobes commented Jul 9, 2019

Introducing two changes here

Fix NextApiResponse.status type signature 989e549

According to the doc we can chain the function call (thanks to here)

Improve NextApiResponse typing fb11fd2

Makes the NextApiResponse accept a type argument
Currently res.json(...) is not type checked but it could be helpful to see at a glance the handler signature, like so:

export type MyResponse =
  | {ok: true; blabla: message}
  | {ok: false; message: string}

export default function handle(
  req: NextApiRequest,
  res: NextApiResponse<MyResponse>,
) {
  //...
  res.json({ok: true, blabla: ...}) // typecheck!!
}

I didn't make the NextApiRequest generic because—we can't trust the request input—we would have to validate it anyway.

What do you think ?

@github-actions
Copy link
Contributor

github-actions bot commented Jul 9, 2019

Stats from current PR

Click to expand stats
zeit/next.js canary VincentCordobes/next.js canary Change
Build Duration 14.1s 14.2s ⚠️ +127ms
node_modules Size 45.4 MB 45.4 MB ⚠️ +109 B
Total Bundle (main, webpack, commons) Size 210 kB 210 kB
Total Bundle (main, webpack, commons) gzip Size 68.8 kB 68.8 kB
Client _app Size 2.39 kB 2.39 kB
Client _app gzip Size 1.08 kB 1.08 kB
Client _error Size 8.45 kB 8.45 kB
Client _error gzip Size 3.24 kB 3.24 kB
Client pages/index Size 296 B 296 B
Client pages/index gzip Size 222 B 222 B
Client pages/link Size 4.76 kB 4.76 kB
Client pages/link gzip Size 2.03 kB 2.03 kB
Client pages/routerDirect Size 423 B 423 B
Client pages/routerDirect gzip Size 306 B 306 B
Client pages/withRouter Size 435 B 435 B
Client pages/withRouter gzip Size 301 B 301 B
Client main Size 24.3 kB 24.3 kB
Client main gzip Size 7.97 kB 7.97 kB
Client commons Size 183 kB 183 kB
Client commons gzip Size 59.5 kB 59.5 kB
Client webpack Size 1.49 kB 1.49 kB
Client webpack gzip Size 770 B 770 B
Base Rendered Size 1.36 kB 1.36 kB
Build Dir Size 785 kB 785 kB
Click to expand serverless stats
zeit/next.js canary VincentCordobes/next.js canary Change
Build Duration 16.1s 15.9s -170ms
node_modules Size 45.4 MB 45.4 MB ⚠️ +109 B
Total Bundle (main, webpack, commons) Size 210 kB 210 kB
Total Bundle (main, webpack, commons) gzip Size 68.8 kB 68.8 kB
Client _app Size 2.39 kB 2.39 kB
Client _app gzip Size 1.08 kB 1.08 kB
Client _error Size 8.45 kB 8.45 kB
Client _error gzip Size 3.24 kB 3.24 kB
Client pages/index Size 296 B 296 B
Client pages/index gzip Size 222 B 222 B
Client pages/link Size 4.76 kB 4.76 kB
Client pages/link gzip Size 2.03 kB 2.03 kB
Client pages/routerDirect Size 423 B 423 B
Client pages/routerDirect gzip Size 306 B 306 B
Client pages/withRouter Size 435 B 435 B
Client pages/withRouter gzip Size 301 B 301 B
Client main Size 24.3 kB 24.3 kB
Client main gzip Size 7.97 kB 7.97 kB
Client commons Size 183 kB 183 kB
Client commons gzip Size 59.5 kB 59.5 kB
Client webpack Size 1.49 kB 1.49 kB
Client webpack gzip Size 770 B 770 B
Serverless pages/link Size 338 kB 338 kB
Serverless pages/link gzip Size 86.7 kB 86.7 kB ⚠️ +1 B
Serverless pages/index Size Error getting size Error getting size
Serverless pages/index gzip Size Error getting size Error getting size
Serverless pages/_error Size 328 kB 328 kB
Serverless pages/_error gzip Size 84 kB 84 kB ⚠️ +1 B
Serverless pages/routerDirect Size 329 kB 329 kB
Serverless pages/routerDirect gzip Size 84.3 kB 84.3 kB ⚠️ +2 B
Serverless pages/withRouter Size 329 kB 329 kB
Serverless pages/withRouter gzip Size 84.2 kB 84.2 kB ⚠️ +2 B
Build Dir Size 2.04 MB 2.04 MB

Copy link
Member

@Timer Timer left a comment

Choose a reason for hiding this comment

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

This LGTM. Let's wait for @huv1k.

@Timer Timer merged commit a137447 into vercel:canary Jul 9, 2019
@vercel vercel locked as resolved and limited conversation to collaborators Feb 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants