Skip to content

Commit

Permalink
fix(types): restore original client types which have more methods (#540)
Browse files Browse the repository at this point in the history
  • Loading branch information
rchl authored Mar 27, 2023
1 parent 8a04ad7 commit 16bc644
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export async function initializeServerSentry (nuxt: Nuxt, moduleOptions: ModuleC
sentryHandlerProxy.tracingHandler = Sentry.Handlers.tracingHandler()
}

process.sentry = Sentry.getCurrentHub().getClient() as Sentry.NodeClient
process.sentry = Sentry
}
}

Expand Down
17 changes: 8 additions & 9 deletions src/templates/plugin.client.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable import/order */
import Vue from 'vue'
import merge from '~lodash.mergewith'
import * as Sentry from '~@sentry/vue'
<%
const vueImports = ['getCurrentHub', 'init', 'Integrations', ...(options.tracing ? ['vueRouterInstrumentation'] : [])]
%>import { <%= vueImports.join(', ') %> } from '~@sentry/vue'
<%
if (options.tracing) {%>import { BrowserTracing } from '~@sentry/tracing'
if (options.tracing) {
%>import { BrowserTracing } from '~@sentry/tracing'
import { vueRouterInstrumentation } from '~@sentry/vue'
<%}
let integrations = options.BROWSER_PLUGGABLE_INTEGRATIONS.filter(key => key in options.integrations)
if (integrations.length) {%>import { <%= integrations.join(', ') %> } from '~@sentry/integrations'
Expand All @@ -16,7 +16,7 @@ if (options.customClientIntegrations) {%>import getCustomIntegrations from '<%=
<%}
integrations = options.BROWSER_INTEGRATIONS.filter(key => key in options.integrations)
if (integrations.length) {%>
const { <%= integrations.join(', ') %> } = Integrations
const { <%= integrations.join(', ') %> } = Sentry.Integrations
<%}
%>

Expand Down Expand Up @@ -79,8 +79,7 @@ export default async function (ctx, inject) {
}

/* eslint-enable object-curly-spacing, quote-props, quotes, key-spacing, comma-spacing */
init(config)
const sentryClient = getCurrentHub().getClient()
inject('sentry', sentryClient)
ctx.$sentry = sentryClient
Sentry.init(config)
inject('sentry', Sentry)
ctx.$sentry = Sentry
}
24 changes: 14 additions & 10 deletions src/types/extend.d.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import 'vue'
import 'vuex'
import '@nuxt/types'
import { Client } from '@sentry/types'
import * as SentryNode from '@sentry/node'
import * as SentryTypes from '@sentry/core'
import { PartialModuleConfiguration } from './configuration'

export type ModulePublicRuntimeConfig = Pick<PartialModuleConfiguration, 'config' | 'clientConfig' | 'serverConfig'>

type Sentry = typeof SentryTypes
type NodeSentry = typeof SentryNode

// add type to Vue context
declare module 'vue/types/vue' {
interface Vue {
readonly $sentry: Client
readonly $sentry: Sentry
$sentryLoad(): Promise<void>
$sentryReady(): Promise<Client>
$sentryReady(): Promise<Sentry>
}
}

// App Context and NuxtAppOptions
declare module '@nuxt/types' {
interface Context {
readonly $sentry: Client
readonly $sentry: Sentry
$sentryLoad(): Promise<void>
$sentryReady(): Promise<Client>
$sentryReady(): Promise<Sentry>
}

interface NuxtOptions {
sentry?: PartialModuleConfiguration
}

interface NuxtAppOptions {
readonly $sentry: Client
readonly $sentry: Sentry
$sentryLoad(): Promise<void>
$sentryReady(): Promise<Client>
$sentryReady(): Promise<Sentry>
}
}

Expand All @@ -43,16 +47,16 @@ declare module '@nuxt/types/config/runtime' {
// add types for Vuex Store
declare module 'vuex/types' {
interface Store<S> {
readonly $sentry: Client
readonly $sentry: Sentry
$sentryLoad(): Promise<void>
$sentryReady(): Promise<Client>
$sentryReady(): Promise<Sentry>
}
}

declare global {
namespace NodeJS {
interface Process {
sentry: Client
sentry: NodeSentry
}
}
}
10 changes: 7 additions & 3 deletions test/fixture/default/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@

<script>
export default {
asyncData ({ $sentry }) {
if (process.server) {
return {
serverSentryPresent: Boolean($sentry?.captureException),
}
}
},
data () {
return {
clientSentryPresent: false,
serverSentryPresent: false,
}
},
created () {
if (process.server) {
this.serverSentryPresent = Boolean(this.$sentry?.captureException)
}
if (process.client) {
this.clientSentryPresent = Boolean(this.$sentry?.captureException)
}
Expand Down
10 changes: 7 additions & 3 deletions test/fixture/lazy/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@

<script>
export default {
asyncData ({ $sentry }) {
if (process.server) {
return {
serverSentryPresent: Boolean($sentry?.captureException),
}
}
},
data () {
return {
isSentryReady: false,
serverSentryPresent: false,
}
},
created () {
if (process.server) {
this.serverSentryPresent = Boolean(this.$sentry?.captureException)
}
if (process.client) {
this.$sentryReady().then(() => {
this.isSentryReady = true
Expand Down
10 changes: 7 additions & 3 deletions test/fixture/with-lazy-config/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@

<script>
export default {
asyncData ({ $sentry }) {
if (process.server) {
return {
serverSentryPresent: Boolean($sentry?.captureException),
}
}
},
data () {
return {
isSentryReady: false,
serverSentryPresent: false,
}
},
created () {
if (process.server) {
this.serverSentryPresent = Boolean(this.$sentry?.captureException)
}
if (process.client) {
this.$sentryReady().then(() => {
this.isSentryReady = true
Expand Down

0 comments on commit 16bc644

Please sign in to comment.