Skip to content

Commit

Permalink
Merge pull request #3 from codex-team/catcher-options
Browse files Browse the repository at this point in the history
feat(config): js catcher options support
  • Loading branch information
neSpecc authored Oct 13, 2024
2 parents 0bb9b91 + 58604e0 commit c438fcf
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
node-version: 20

- name: Install dependencies
run: npx nypm@latest i
run: npm install

- name: Lint
run: npm run lint
Expand All @@ -36,7 +36,7 @@ jobs:
node-version: 20

- name: Install dependencies
run: npx nypm@latest i
run: npm install

- name: Playground prepare
run: npm run dev:prepare
Expand Down
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
20 changes: 10 additions & 10 deletions package-lock.json

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

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hawk.so/nuxt",
"version": "0.0.3",
"version": "0.0.4",
"description": "Hawk error tracker integration to Nuxt app",
"repository": "https://github.com/codex-team/hawk.nuxt",
"license": "MIT",
Expand All @@ -26,11 +26,12 @@
"lint": "eslint .",
"test": "vitest run",
"test:watch": "vitest watch",
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
"test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit",
"preinstall": "npx only-allow npm"
},
"dependencies": {
"@hawk.so/javascript": "^3.0.2",
"@hawk.so/vite-plugin": "^1.0.3",
"@hawk.so/javascript": "^3.0.8",
"@hawk.so/vite-plugin": "^1.0.4",
"@nuxt/kit": "^3.13.2"
},
"devDependencies": {
Expand Down
15 changes: 15 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
import type { HawkJavaScriptEvent } from '@hawk.so/javascript'

export default defineNuxtConfig({
modules: ['../src/module'],

hawk: {
token: process.env.HAWK_TOKEN,
catcherOptions: {
context: {
appName: 'Hawk Nuxt Playground',
},
beforeSend: (event: HawkJavaScriptEvent) => {
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,
}))

/**
* @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'>
}

0 comments on commit c438fcf

Please sign in to comment.