Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): js catcher options support #3

Merged
merged 9 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ export default defineNuxtConfig({
})
```

## Passing additional options

You can pass `user`, `context`, `beforeSend` and other [JS Catcher options](https://github.com/codex-team/hawk.javascript?tab=readme-ov-file#usage) via `catcherOptions` config property.

```ts
export default defineNuxtConfig({
modules: [
'@hawk.so/nuxt'
],

hawk: {
token: process.env.HAWK_TOKEN,
catcherOptions: {
context: {
// any data you want to send with all events
appName: 'Hawk Nuxt Playground',
},
// method for filtering/modifying an event to be sent
beforeSend: (event) => {
event.context.appVersion = '1.0.0'

return event
},
},
},
})
```

## Contribution

<details>
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"dependencies": {
"@hawk.so/javascript": "^3.0.2",
"@hawk.so/vite-plugin": "^1.0.3",
"@hawk.so/vite-plugin": "^1.0.4",
"@nuxt/kit": "^3.13.2"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
export default defineNuxtConfig({
modules: ['../src/module'],

hawk: {
token: process.env.HAWK_TOKEN,
catcherOptions: {
context: {
appName: 'Hawk Nuxt Playground',
},
beforeSend: (event) => {
event.context.appVersion = '1.0.0'

return event
},
},
},

devtools: { enabled: true },
compatibilityDate: '2024-10-07',
})
19 changes: 18 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defineNuxtModule, addPlugin, createResolver, updateRuntimeConfig, useRuntimeConfig, addImportsDir } from '@nuxt/kit'
import { defineNuxtModule, addPlugin, createResolver, updateRuntimeConfig, useRuntimeConfig, addImportsDir, addTemplate } from '@nuxt/kit'
import hawkVitePlugin from '@hawk.so/vite-plugin'
import type { HawkModuleConfig } from './types'

Expand All @@ -12,6 +12,23 @@ export default defineNuxtModule<HawkModuleConfig>({
const resolver = createResolver(import.meta.url)
const runtimeConfig = useRuntimeConfig()

/**
* Since we can't pass functions via the nuxt.config.js, we need to create a template
* @see https://nuxt.com/docs/guide/going-further/runtime-config#serialization
* @see https://nuxt.com/docs/guide/going-further/modules#adding-templatesvirtual-files
*/
addTemplate({
filename: 'hawk-before-send.mjs',
getContents: () => {
if (config.catcherOptions?.beforeSend) {
return `export default ${config.catcherOptions.beforeSend.toString()}`
}
else {
return `export default (event) => event`
}
},
})

/**
* Add runtimeConfig.public.hawk
*/
Expand Down
7 changes: 5 additions & 2 deletions src/runtime/plugin.client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import HawkCatcher from '@hawk.so/javascript'
import type { HawkModuleConfig } from '../types'
import beforeSend from '#build/hawk-before-send.mjs'
import { defineNuxtPlugin, useRuntimeConfig } from '#app'

/**
Expand Down Expand Up @@ -30,11 +31,13 @@ export default defineNuxtPlugin((nuxtApp) => {
const runtimeConfig = useRuntimeConfig()
const hawkConfig = runtimeConfig.public.hawk as HawkModuleConfig

const hawkInstance = new HawkCatcher({
const hawkInstance = new HawkCatcher(Object.assign({}, hawkConfig.catcherOptions, {
token: hawkConfig.token,
vue: nuxtApp.vueApp,
release: getReleaseId(),
})
}, {
beforeSend,
}))
e11sy marked this conversation as resolved.
Show resolved Hide resolved

/**
* @todo use NuxtApp to extract useful information:
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { HawkInitialSettings } from '@hawk.so/javascript'

/**
* Nuxt module configuration
*/
Expand All @@ -6,4 +8,10 @@ export interface HawkModuleConfig {
* Hawk Integration token
*/
token: string

/**
* Any additional options supported by Hawk JavaScript Catcher
* @see https://github.com/codex-team/hawk.javascript?tab=readme-ov-file#usage
*/
catcherOptions?: Omit<HawkInitialSettings, 'token' | 'vue' | 'release'>
}