Skip to content

Commit

Permalink
chore: clean imports
Browse files Browse the repository at this point in the history
  • Loading branch information
giraud florent committed Jun 19, 2023
1 parent 926d239 commit bc3a877
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
22 changes: 9 additions & 13 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export default defineNuxtModule<ModuleOptions>({
apiVersion: "2022-11-15"
},
setup (options, nuxt) {
const resolver = createResolver(import.meta.url)
const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
const { resolve } = createResolver(import.meta.url)

if (!options.publishableKey) {
throw new Error('Missing publishableKey option')
Expand All @@ -55,35 +54,32 @@ export default defineNuxtModule<ModuleOptions>({
apiVersion: options.apiVersion
}

nuxt.options.runtimeConfig.stripe = {
nuxt.options.runtimeConfig.stripe = defu(options, {
apiKey: options.apiKey
}

// Transpile runtime
nuxt.options.build.transpile.push(runtimeDir)
})

addPlugin(resolver.resolve(runtimeDir, 'plugins', 'stripe.client'))
addImportsDir(resolver.resolve('./runtime/composables'))
addPlugin(resolve('./runtime/plugins/stripe.client'))
addImportsDir(resolve('./runtime/composables'))

nuxt.hook('nitro:config', (nitroConfig) => {
nitroConfig.alias = nitroConfig.alias || {}
nitroConfig.externals = defu(typeof nitroConfig.externals === 'object' ? nitroConfig.externals : {}, {
inline: [resolver.resolve('./runtime')]
inline: [resolve('./runtime')]
})
nitroConfig.alias['#stripe/server'] = resolver.resolve('./runtime/server/services')
nitroConfig.alias['#stripe/server'] = resolve('./runtime/server/services')
})

addTemplate({
filename: 'types/stripe.d.ts',
getContents: () => [
'declare module \'#stripe/server\' {',
` const useServerStripe: typeof import('${resolver.resolve('./runtime/server/services')}').useServerStripe`,
` const useServerStripe: typeof import('${resolve('./runtime/server/services')}').useServerStripe`,
'}'
].join('\n')
})

nuxt.hook('prepare:types', (options) => {
options.references.push({ path: resolver.resolve(nuxt.options.buildDir, 'types/stripe.d.ts') })
options.references.push({ path: resolve(nuxt.options.buildDir, 'types/stripe.d.ts') })
})
}
})
2 changes: 1 addition & 1 deletion src/runtime/composables/useClientStripe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useNuxtApp } from '#app'
import { useNuxtApp } from "#imports"

export default function useClientStripe() {
return useNuxtApp().$stripe
Expand Down
3 changes: 1 addition & 2 deletions src/runtime/plugins/stripe.client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { defineNuxtPlugin } from '#app'

import { defineNuxtPlugin } from '#imports'
import { loadStripe } from '@stripe/stripe-js';

export default defineNuxtPlugin(async(nuxtApp) => {
Expand Down
1 change: 1 addition & 0 deletions src/runtime/server/services/useServerStripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const useServerStripe = async(event: any) => {

if ( event.context._stripe ) return event.context._stripe

// As Stripe make ts fail when not setting the apiVersion, we need to ignore the error
// @ts-ignore
const stripe = new Stripe(apiKey, { apiVersion })
event.context._stripe = stripe
Expand Down

0 comments on commit bc3a877

Please sign in to comment.