Skip to content

Commit

Permalink
feat: expose hooks for extensibility (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Sep 18, 2023
1 parent 46e9f25 commit 0ad51d2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "@nuxt/telemetry",
"packageManager": "pnpm@8.7.6",
"version": "2.4.1",
"repository": "nuxt/telemetry",
"license": "MIT",
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default defineNuxtModule<TelemetryOptions>({

const t = new Telemetry(nuxt, toptions)

nuxt.hook('modules:done', () => {
nuxt.hook('modules:done', async () => {
t.createEvent('project')
// Only send the session in development
if (nuxt.options.dev) {
Expand All @@ -58,6 +58,7 @@ export default defineNuxtModule<TelemetryOptions>({
}
t.createEvent('command')
t.createEvent('module')
await nuxt.callHook('telemetry:setup', t)
t.sendEvents(toptions.debug)
})
}
Expand Down
10 changes: 5 additions & 5 deletions src/telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Nuxt } from '@nuxt/schema'
import { postEvent } from './utils/post-event'
import * as events from './events/index'
import * as eventFactories from './events/index'
import { createContext } from './context'
import { EventFactory, TelemetryOptions, Context, EventFactoryResult } from './types'
import { logger } from './utils/log'
Expand All @@ -11,6 +11,7 @@ export class Telemetry {
storage: any // TODO
_contextPromise?: Promise<Context>
events: Promise<EventFactoryResult<any>>[] = []
eventFactories: Record<string, EventFactory<any>> = { ...eventFactories }

constructor (nuxt: Nuxt, options: TelemetryOptions) {
this.nuxt = nuxt
Expand All @@ -25,8 +26,7 @@ export class Telemetry {
}

createEvent (name: string, payload?: object): void | Promise<any> {
// @ts-ignore
const eventFactory: EventFactory<any> = (events as any)[name]
const eventFactory = this.eventFactories[name]
if (typeof eventFactory !== 'function') {
logger.warn('Unknown event:', name)
return
Expand All @@ -48,7 +48,7 @@ export class Telemetry {

async getPublicContext () {
const context = await this.getContext()
const eventContext = {}
const eventContext: Record<string, any> = {}
for (const key of [
'nuxtVersion',
'nuxtMajorVersion',
Expand All @@ -59,7 +59,7 @@ export class Telemetry {
'environment',
'projectHash',
'projectSession'
]) {
] as const) {
eventContext[key] = context[key]
}
return eventContext
Expand Down
7 changes: 7 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Nuxt } from '@nuxt/schema'
import { Telemetry } from './telemetry'

export interface TelemetryOptions {
debug: boolean
Expand Down Expand Up @@ -49,3 +50,9 @@ export interface GitData {
owner: string
name: string
}

declare module '@nuxt/schema' {
interface NuxtHooks {
'telemetry:setup': (telemetry: Telemetry) => void
}
}

0 comments on commit 0ad51d2

Please sign in to comment.