1+ 'use server'
2+
13import axios from 'axios'
2- import { NextRequest , NextResponse } from 'next/server '
4+ import { headers } from 'next/headers '
35import { 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
79type GMapDistance = { elements : { status : string ; distance : { value : number } } [ ] }
810type 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}
0 commit comments