Skip to content

Commit 116a012

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 8d1982c + 883b967 commit 116a012

5 files changed

Lines changed: 43 additions & 60 deletions

File tree

.env.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
GMAP_API_KEY=<secret>
21
SENDINBLUE_API_KEY=<secret>
32
NEXT_PUBLIC_URL=http://localhost:3000
43
NEXT_PUBLIC_IMAGE_URL=http://localhost:3000

src/hooks/useItineraries.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useQuery } from '@tanstack/react-query'
2-
import { CallGMapDistances } from 'utils/gmaps'
2+
import { callGMap } from 'src/serverFunctions/callGMap'
33
import { track } from 'utils/matomo'
44

55
export type Point = {
@@ -29,21 +29,16 @@ export default function useItineraries(
2929
}
3030
}
3131

32-
const axiosClient = (await import('utils/axios')).default
33-
return axiosClient
34-
.post<CallGMapDistances>('/api/callGMap', {
35-
destinations: {
36-
latitude: start.latitude,
37-
longitude: start.longitude,
38-
},
39-
origins: {
40-
latitude: end.latitude,
41-
longitude: end.longitude,
42-
},
43-
})
44-
.then((res) => {
45-
return res.data
46-
})
32+
return callGMap({
33+
destinations: {
34+
latitude: start.latitude,
35+
longitude: start.longitude,
36+
},
37+
origins: {
38+
latitude: end.latitude,
39+
longitude: end.longitude,
40+
},
41+
})
4742
},
4843
enabled: start && end && !!start.latitude && !!end.latitude,
4944
refetchOnWindowFocus: false,
Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
'use server'
2+
13
import axios from 'axios'
2-
import { NextRequest, NextResponse } from 'next/server'
4+
import { headers } from 'next/headers'
35
import { z } from 'zod'
4-
import { GMapValidation, getCachedValue, insertCachedValue } from 'utils/gmaps'
5-
import { trackAPIRequest } from 'utils/middleware'
6+
import { CallGMapDistances, GMapCommand, GMapValidation, getCachedValue, insertCachedValue } from 'utils/gmaps'
7+
import { trackAPIRequestFromHeaders } from 'utils/middleware'
68

79
type GMapDistance = { elements: { status: string; distance: { value: number } }[] }
810
type GMapDistances = { rows: GMapDistance[] }
@@ -16,38 +18,37 @@ const getValue = (distance: GMapDistances) => {
1618
return (element.status === 'OK' && element.distance.value / 1000) || 0
1719
}
1820

19-
export async function POST(req: NextRequest) {
20-
const body = await req.json()
21-
22-
const inputs = GMapValidation.safeParse(body)
21+
export const callGMap = async (data: GMapCommand): Promise<CallGMapDistances> => {
22+
const inputs = GMapValidation.safeParse(data)
2323
if (!inputs.success) {
24-
return NextResponse.json(z.treeifyError(inputs.error), { status: 400 })
24+
throw new Error(JSON.stringify(z.treeifyError(inputs.error)))
2525
}
2626

2727
if (!process.env.GMAP_API_KEY) {
28-
// Fake Paris Lyon
29-
return NextResponse.json({ car: 465.021, foot: 440.747, rail: 456.409, plane: 391.8120136890189 }, { status: 200 })
28+
return { car: 91.021, foot: 87.914, rail: 91.153, plane: 80.69557099482829 }
3029
}
3130

31+
const requestHeaders = await headers()
32+
3233
const cached = await getCachedValue(inputs.data)
3334
if (cached) {
34-
await trackAPIRequest(req, 'callGMap-cache')
35-
return NextResponse.json(cached, { status: 200 })
35+
await trackAPIRequestFromHeaders(requestHeaders, 'callGMap-cache')
36+
return cached
3637
}
3738

3839
if (process.env.LIMIT_API === 'true') {
39-
const referer = req.headers.get('referer')
40+
const referer = requestHeaders.get('referer')
4041

4142
if (!process.env.NEXT_PUBLIC_URL || !referer?.startsWith(process.env.NEXT_PUBLIC_URL)) {
42-
return NextResponse.json('Not authorized', { status: 403 })
43+
throw new Error('Not authorized')
4344
}
4445

45-
const name = await trackAPIRequest(req, 'callGMap')
46+
const name = await trackAPIRequestFromHeaders(requestHeaders, 'callGMap')
4647
if (name !== 'Impact CO2') {
4748
if (name === 'HACK') {
48-
console.error('--- Wrong usage of CallGMAP API ---', req)
49+
console.error('--- Wrong usage of CallGMAP API ---', { referer })
4950
}
50-
return NextResponse.json('Not authorized', { status: 401 })
51+
throw new Error('Not authorized')
5152
}
5253
}
5354

@@ -87,5 +88,5 @@ export async function POST(req: NextRequest) {
8788
plane: planeDistance,
8889
}
8990
await insertCachedValue(inputs.data, result)
90-
return NextResponse.json(result, { status: 200 })
91+
return result
9192
}

src/utils/middleware.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as Sentry from '@sentry/nextjs'
22
import { NextRequest } from 'next/server'
33
import { prismaClient } from 'utils/prismaClient'
44

5-
export async function trackAPIRequest(request: NextRequest, api: string) {
5+
type RequestLike = {
6+
headers: Pick<Headers, 'get'>
7+
}
8+
9+
async function track(request: RequestLike, api: string) {
610
if (!process.env.TRACK_API) {
711
return null
812
}
@@ -47,6 +51,14 @@ export async function trackAPIRequest(request: NextRequest, api: string) {
4751
}
4852
}
4953

54+
export async function trackAPIRequest(request: NextRequest, api: string) {
55+
return track(request, api)
56+
}
57+
58+
export async function trackAPIRequestFromHeaders(headers: Pick<Headers, 'get'>, api: string) {
59+
return track({ headers }, api)
60+
}
61+
5062
export const config = {
5163
matcher: '/api/:path*',
5264
}

teste/mock-routes/mock-routes-itinerary.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -617,28 +617,4 @@ export const mockRoutesItinerary = async (page: Page) => {
617617
})
618618
}
619619
)
620-
await page.route('http://localhost:3000/api/callGMap', async (route) => {
621-
await route.fulfill({
622-
headers: {
623-
Etag: 'mocked, because it was run in a E2E environment',
624-
},
625-
body: JSON.stringify({ car: 91.021, foot: 87.914, rail: 91.153, plane: 80.69557099482829 }),
626-
})
627-
})
628-
await page.route('http://127.0.0.1:3000/api/callGMap', async (route) => {
629-
await route.fulfill({
630-
headers: {
631-
Etag: 'mocked, because it was run in a E2E environment',
632-
},
633-
body: JSON.stringify({ car: 91.021, foot: 87.914, rail: 91.153, plane: 80.69557099482829 }),
634-
})
635-
})
636-
await page.route('https://impactco2.fr/api/callGMap', async (route) => {
637-
await route.fulfill({
638-
headers: {
639-
Etag: 'mocked, because it was run in a E2E environment',
640-
},
641-
body: JSON.stringify({ car: 91.021, foot: 87.914, rail: 91.153, plane: 80.69557099482829 }),
642-
})
643-
})
644620
}

0 commit comments

Comments
 (0)