Skip to content
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

Added generic type for body in NextApiRequest #15139

Closed

Conversation

Nirzhuk
Copy link

@Nirzhuk Nirzhuk commented Jul 13, 2020

Generic Type for the body in NextApiRequests to implement your custom interfaces to handle better the data.

@ijjk
Copy link
Member

ijjk commented Jul 13, 2020

Stats from current PR

Default Server Mode
General Overall increase ⚠️
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
buildDuration 12s 11.5s -486ms
nodeModulesSize 66.5 MB 66.5 MB ⚠️ +14 B
Page Load Tests Overall increase ✓
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
/ failed reqs 0 0
/ total time (seconds) 2.065 2.03 -0.04
/ avg req/sec 1210.49 1231.3 +20.81
/error-in-render failed reqs 0 0
/error-in-render total time (seconds) 1.311 1.323 ⚠️ +0.01
/error-in-render avg req/sec 1907.44 1889.77 ⚠️ -17.67
Client Bundles (main, webpack, commons)
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
main-HASH.js gzip 6.72 kB 6.72 kB
webpack-HASH.js gzip 751 B 751 B
19b7e98f51cc..cead.js gzip 9.77 kB 9.77 kB
framework.HASH.js gzip 39.1 kB 39.1 kB
Overall change 56.4 kB 56.4 kB
Client Bundles (main, webpack, commons) Modern
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
main-HASH.module.js gzip 5.79 kB 5.79 kB
webpack-HASH..dule.js gzip 751 B 751 B
19b7e98f51cc..dule.js gzip 6.17 kB 6.17 kB
framework.HA..dule.js gzip 39.1 kB 39.1 kB
Overall change 51.9 kB 51.9 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
polyfills-HASH.js gzip 26.4 kB 26.4 kB
Overall change 26.4 kB 26.4 kB
Client Build Manifests
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
_buildManifest.js gzip 267 B 267 B
_buildManife..dule.js gzip 273 B 273 B
Overall change 540 B 540 B
Rendered Page Sizes
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
index.html gzip 955 B 955 B
link.html gzip 962 B 962 B
withRouter.html gzip 947 B 947 B
Overall change 2.86 kB 2.86 kB

Serverless Mode
General Overall increase ⚠️
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
buildDuration 12.7s 12.7s -41ms
nodeModulesSize 66.5 MB 66.5 MB ⚠️ +14 B
Client Bundles (main, webpack, commons)
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
main-HASH.js gzip 6.72 kB 6.72 kB
webpack-HASH.js gzip 751 B 751 B
19b7e98f51cc..cead.js gzip 9.77 kB 9.77 kB
framework.HASH.js gzip 39.1 kB 39.1 kB
Overall change 56.4 kB 56.4 kB
Client Bundles (main, webpack, commons) Modern
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
main-HASH.module.js gzip 5.79 kB 5.79 kB
webpack-HASH..dule.js gzip 751 B 751 B
19b7e98f51cc..dule.js gzip 6.17 kB 6.17 kB
framework.HA..dule.js gzip 39.1 kB 39.1 kB
Overall change 51.9 kB 51.9 kB
Legacy Client Bundles (polyfills)
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
polyfills-HASH.js gzip 26.4 kB 26.4 kB
Overall change 26.4 kB 26.4 kB
Client Build Manifests
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
_buildManifest.js gzip 267 B 267 B
_buildManife..dule.js gzip 273 B 273 B
Overall change 540 B 540 B
Serverless bundles
vercel/next.js canary Nirzhuk/next.js next_api_request_generic_type_body Change
_error.js 881 kB 881 kB
404.html 4.17 kB 4.17 kB
hooks.html 3.79 kB 3.79 kB
index.js 881 kB 881 kB
link.js 923 kB 923 kB
routerDirect.js 917 kB 917 kB
withRouter.js 917 kB 917 kB
Overall change 4.53 MB 4.53 MB
Commit: 310e7b4

@lfades
Copy link
Member

lfades commented Jul 13, 2020

Hi there @Nirzhuk thank you for the PR, I'm going to close the PR as this change is not required for the following reason:

body can be anything, it's value doesn't depend on the API route but on the request, if you want to type it do it after doing an actual validation on top of it, otherwise the type will do more harm than good, that's a similar reasoning to why req.query doesn't accept a custom type either.

For example:

function validate(body: any) {
  // ...
  return body as MyType
}

// ...
const body = validate(req.body)

In the above example your code will properly set the type, and only after validating that such type is correct.

@lfades lfades closed this Jul 13, 2020
@Nirzhuk
Copy link
Author

Nirzhuk commented Jul 14, 2020

@lfades Thanks for answering.
In the code what you wrote affects directly to the bundle adding maybe 1-3kb for every request you need to handle custom type. So I don't feel it's better to handle that way, but you're know better than me what NextJS needs, so thanks again for your time answering the PR.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants