Skip to content

Commit cec8316

Browse files
committed
chore: add logs on API tracking
1 parent 008c4e1 commit cec8316

2 files changed

Lines changed: 6 additions & 21 deletions

File tree

src/serverFunctions/callGMap.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
'use server'
2-
32
import axios from 'axios'
43
import { headers } from 'next/headers'
54
import { z } from 'zod'
65
import { CallGMapDistances, GMapCommand, GMapValidation, getCachedValue, insertCachedValue } from 'utils/gmaps'
7-
import { trackAPIRequestFromHeaders } from 'utils/middleware'
86

97
type GMapDistance = { elements: { status: string; distance: { value: number } }[] }
108
type GMapDistances = { rows: GMapDistance[] }
@@ -32,7 +30,6 @@ export const callGMap = async (data: GMapCommand): Promise<CallGMapDistances> =>
3230

3331
const cached = await getCachedValue(inputs.data)
3432
if (cached) {
35-
await trackAPIRequestFromHeaders(requestHeaders, 'callGMap-cache')
3633
return cached
3734
}
3835

@@ -42,8 +39,6 @@ export const callGMap = async (data: GMapCommand): Promise<CallGMapDistances> =>
4239
if (!process.env.NEXT_PUBLIC_URL || !referer?.startsWith(process.env.NEXT_PUBLIC_URL)) {
4340
throw new Error('Not authorized')
4441
}
45-
46-
await trackAPIRequestFromHeaders(requestHeaders, 'callGMap')
4742
}
4843

4944
const R = 6371e3 // metres

src/utils/middleware.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import * as Sentry from '@sentry/nextjs'
22
import { NextRequest } from 'next/server'
33
import { prismaClient } from 'utils/prismaClient'
44

5-
type RequestLike = {
6-
headers: Pick<Headers, 'get'>
7-
}
8-
9-
async function track(request: RequestLike, api: string) {
10-
if (!process.env.TRACK_API) {
5+
export const trackAPIRequest = async (request: NextRequest, api: string) => {
6+
console.log(`tracking API request - ${api}`)
7+
if (process.env.TRACK_API !== 'true') {
8+
console.log('tracking disabled')
119
return null
1210
}
1311

@@ -37,28 +35,20 @@ async function track(request: RequestLike, api: string) {
3735

3836
const param = `e_c=API&e_a=${name}&e_n=${api}`
3937

40-
await fetch(
38+
const result = await fetch(
4139
`${process.env.NEXT_PUBLIC_MATOMO_SITE_URL}/matomo.php?idsite=${process.env.NEXT_PUBLIC_MATOMO_SITE_ID}&rec=1&${param}`,
4240
{
4341
method: 'POST',
4442
}
4543
)
46-
44+
console.log(`tracking result - ${result.status} - ${referer} - ${authorization} - ${param}`)
4745
return name
4846
} catch (error) {
4947
await Sentry.captureException(error)
5048
console.error(`tracking failed - ${referer} - ${authorization}`, error)
5149
}
5250
}
5351

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-
6252
export const config = {
6353
matcher: '/api/:path*',
6454
}

0 commit comments

Comments
 (0)