-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nextjs): Add body data to transaction request context
#3672
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
lobsterkatie
merged 3 commits into
master
from
kmclb-nextjs-add-body-data-to-request-context
Jun 14, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -200,30 +200,30 @@ export function extractRequestData( | |
| const requestData: { [key: string]: any } = {}; | ||
|
|
||
| // headers: | ||
| // node, express: req.headers | ||
| // node, express, nextjs: req.headers | ||
| // koa: req.header | ||
| const headers = (req.headers || req.header || {}) as { | ||
| host?: string; | ||
| cookie?: string; | ||
| }; | ||
| // method: | ||
| // node, express, koa: req.method | ||
| // node, express, koa, nextjs: req.method | ||
| const method = req.method; | ||
| // host: | ||
| // express: req.hostname in > 4 and req.host in < 4 | ||
| // koa: req.host | ||
| // node: req.headers.host | ||
| // node, nextjs: req.headers.host | ||
| const host = req.hostname || req.host || headers.host || '<no host>'; | ||
| // protocol: | ||
| // node: <n/a> | ||
| // node, nextjs: <n/a> | ||
| // express, koa: req.protocol | ||
| const protocol = | ||
| req.protocol === 'https' || req.secure || ((req.socket || {}) as { encrypted?: boolean }).encrypted | ||
| ? 'https' | ||
| : 'http'; | ||
| // url (including path and query string): | ||
| // node, express: req.originalUrl | ||
| // koa: req.url | ||
| // koa, nextjs: req.url | ||
| const originalUrl = (req.originalUrl || req.url || '') as string; | ||
| // absolute url | ||
| const absoluteUrl = `${protocol}://${host}${originalUrl}`; | ||
|
|
@@ -242,26 +242,27 @@ export function extractRequestData( | |
| case 'cookies': | ||
| // cookies: | ||
| // node, express, koa: req.headers.cookie | ||
| // vercel, sails.js, express (w/ cookie middleware): req.cookies | ||
| // vercel, sails.js, express (w/ cookie middleware), nextjs: req.cookies | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
| requestData.cookies = req.cookies || cookie.parse(headers.cookie || ''); | ||
| break; | ||
| case 'query_string': | ||
| // query string: | ||
| // node: req.url (raw) | ||
| // express, koa: req.query | ||
| // express, koa, nextjs: req.query | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
| requestData.query_string = url.parse(originalUrl || '', false).query; | ||
| requestData.query_string = req.query || url.parse(originalUrl || '', false).query; | ||
| break; | ||
| case 'data': | ||
| if (method === 'GET' || method === 'HEAD') { | ||
| break; | ||
| } | ||
| // body data: express, koa: req.body | ||
|
|
||
| // when using node by itself, you have to read the incoming stream(see | ||
| // https://nodejs.dev/learn/get-http-request-body-data-using-nodejs); if a user is doing that, we can't know | ||
| // where they're going to store the final result, so they'll have to capture this data themselves | ||
| // body data: | ||
| // express, koa, nextjs: req.body | ||
| // | ||
| // when using node by itself, you have to read the incoming stream(see | ||
| // https://nodejs.dev/learn/get-http-request-body-data-using-nodejs); if a user is doing that, we can't know | ||
| // where they're going to store the final result, so they'll have to capture this data themselves | ||
| if (req.body !== undefined) { | ||
| requestData.data = isString(req.body) ? req.body : JSON.stringify(normalize(req.body)); | ||
| } | ||
|
|
@@ -345,7 +346,7 @@ export function parseRequest(event: Event, req: ExpressRequest, options?: ParseR | |
| } | ||
|
|
||
| // client ip: | ||
| // node: req.connection.remoteAddress | ||
| // node, nextjs: req.connection.remoteAddress | ||
| // express, koa: req.ip | ||
| if (options.ip) { | ||
| const ip = req.ip || (req.connection && req.connection.remoteAddress); | ||
|
|
@@ -358,6 +359,8 @@ export function parseRequest(event: Event, req: ExpressRequest, options?: ParseR | |
| } | ||
|
|
||
| if (options.transaction && !event.transaction) { | ||
| // TODO do we even need this anymore? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Afair it will be dropped in the next major. |
||
| // TODO make this work for nextjs | ||
| event.transaction = extractTransaction(req, options.transaction); | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm glad it worked despite incorrect comment 😶