Skip to content

Commit d7bbbd2

Browse files
author
Lukas Holzer
authored
refactor: improve telemetry types (#5942)
1 parent 15c40c4 commit d7bbbd2

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/utils/proxy.mjs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,35 @@ const formatEdgeFunctionError = (errorBuffer, acceptsHtml) => {
7575
})
7676
}
7777

78-
const isInternal = function (url) {
78+
/**
79+
* @param {string} url
80+
*/
81+
function isInternal(url) {
7982
return url.startsWith('/.netlify/')
8083
}
81-
const isFunction = function (functionsPort, url) {
84+
85+
/**
86+
* @param {boolean|number|undefined} functionsPort
87+
* @param {string} url
88+
*/
89+
function isFunction(functionsPort, url) {
8290
return functionsPort && url.match(/^\/.netlify\/(functions|builders)\/.+/)
8391
}
8492

85-
const getAddonUrl = function (addonsUrls, req) {
86-
const matches = req.url.match(/^\/.netlify\/([^/]+)(\/.*)/)
93+
/**
94+
* @param {Record<string, string>} addonsUrls
95+
* @param {http.IncomingMessage} req
96+
*/
97+
function getAddonUrl(addonsUrls, req) {
98+
const matches = req.url?.match(/^\/.netlify\/([^/]+)(\/.*)/)
8799
const addonUrl = matches && addonsUrls[matches[1]]
88100
return addonUrl ? `${addonUrl}${matches[2]}` : null
89101
}
90102

103+
/**
104+
* @param {string} pathname
105+
* @param {string} publicFolder
106+
*/
91107
const getStatic = async function (pathname, publicFolder) {
92108
const alternatives = [pathname, ...alternativePathsFor(pathname)].map((filePath) =>
93109
path.resolve(publicFolder, filePath.slice(1)),

src/utils/telemetry/telemetry.mjs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ import isValidEventName from './validation.mjs'
1313

1414
const dirPath = dirname(fileURLToPath(import.meta.url))
1515

16-
const send = function (type, payload) {
16+
/**
17+
* @param {'track' | 'identify'} type
18+
* @param {object} payload
19+
*/
20+
function send(type, payload) {
1721
const requestFile = join(dirPath, 'request.mjs')
1822
const options = JSON.stringify({
1923
data: payload,
2024
type,
2125
})
2226

23-
const args = [process.execPath, [requestFile, options]]
27+
const args = /** @type {const} */ ([process.execPath, [requestFile, options]])
2428
if (process.env.NETLIFY_TEST_TELEMETRY_WAIT === 'true') {
2529
return execa(...args, {
2630
stdio: 'inherit',
@@ -46,7 +50,12 @@ const eventConfig = {
4650
],
4751
}
4852

49-
export const track = async function (eventName, payload = {}) {
53+
/**
54+
* Tracks a custom event with the provided payload
55+
* @param {string} eventName
56+
* @param {{status?: string, duration?: number, [key: string]: unknown}} [payload]
57+
*/
58+
export async function track(eventName, payload = {}) {
5059
if (isCI) {
5160
return
5261
}
@@ -83,7 +92,14 @@ export const track = async function (eventName, payload = {}) {
8392
return send('track', defaultData)
8493
}
8594

86-
export const identify = async function (payload) {
95+
/**
96+
* @param {object} payload
97+
* @param {string} payload.name
98+
* @param {string} payload.email
99+
* @param {string} payload.userId
100+
* @returns
101+
*/
102+
export async function identify(payload) {
87103
if (isCI) {
88104
return
89105
}

src/utils/validation.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// @ts-check
22
import { BANG, chalk } from './command-helpers.mjs'
33

4+
/**
5+
* @param {string} exampleCommand
6+
* @returns {(value:string, previous: unknown) => unknown}
7+
*/
48
export const getGeoCountryArgParser = (exampleCommand) => (arg) => {
59
// Validate that the arg passed is two letters only for country
610
// See https://en.wikipedia.org/wiki/List_of_ISO_3166_country_codes

0 commit comments

Comments
 (0)