Skip to content

fix: add longitude, latitude, and timezone to RequestData.geo #1777

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 11 commits into from
Nov 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cypress/integration/middleware/enhanced.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NextRequest } from 'next/server'
describe('Enhanced middleware', () => {
it('adds request headers', () => {
cy.request('/api/hello').then((response) => {
Expand Down Expand Up @@ -25,4 +26,15 @@ describe('Enhanced middleware', () => {
.that.includes('This was static but has been transformed in')
})
})

it('adds geo data', () => {
cy.request('/api/geo-test').then((response) => {
expect(response.body).to.have.nested.property('headers.x-geo-country')
expect(response.body).to.have.nested.property('headers.x-geo-region')
expect(response.body).to.have.nested.property('headers.x-geo-city')
expect(response.body).to.have.nested.property('headers.x-geo-longitude')
expect(response.body).to.have.nested.property('headers.x-geo-latitude')
expect(response.body).to.have.nested.property('headers.x-geo-timezone')
})
})
})
10 changes: 10 additions & 0 deletions demos/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ export async function middleware(req: NextRequest) {
return request.next()
}

if (pathname.startsWith('/api/geo')) {
req.headers.set('x-geo-country', req.geo.country)
req.headers.set('x-geo-region', req.geo.region)
req.headers.set('x-geo-city', req.geo.city)
req.headers.set('x-geo-longitude', req.geo.longitude)
req.headers.set('x-geo-latitude', req.geo.latitude)

return request.next()
}

if (pathname.startsWith('/headers')) {
// Add a header to the rewritten request
req.headers.set('x-hello', 'world')
Expand Down
3 changes: 3 additions & 0 deletions demos/middleware/pages/api/geo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function handler(req, res) {
res.status(200).json({ name: 'geo-test', headers: req.headers })
}
4 changes: 3 additions & 1 deletion packages/runtime/src/templates/edge/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ const handler = async (req: Request, context: Context) => {
return
}

const geo = {
const geo: RequestData['geo'] = {

Choose a reason for hiding this comment

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

Yay type!

country: context.geo.country?.code,
region: context.geo.subdivision?.code,
city: context.geo.city,
latitude: context.geo.latitude?.toString(),
longitude: context.geo.longitude?.toString(),
}

const requestId = req.headers.get('x-nf-request-id')
Expand Down