From 17a00659d38c96852306ab8e6a9c766f44ea139b Mon Sep 17 00:00:00 2001 From: lucacicada <100947658+lucacicada@users.noreply.github.com> Date: Fri, 1 Dec 2023 19:30:34 +0100 Subject: [PATCH] chore: Remove nuxt Plugin in favor of composable --- src/module.ts | 10 ++--- src/runtime/composables.ts | 44 +++++++++++++++++++- src/runtime/plugin.ts | 82 -------------------------------------- 3 files changed, 47 insertions(+), 89 deletions(-) delete mode 100644 src/runtime/plugin.ts diff --git a/src/module.ts b/src/module.ts index c3a8bb2..2434829 100644 --- a/src/module.ts +++ b/src/module.ts @@ -3,7 +3,7 @@ import type { ChildProcess } from 'node:child_process' import { spawn } from 'node:child_process' import type { StripeConstructorOptions } from '@stripe/stripe-js' -import { addImports, addPlugin, addTemplate, createResolver, defineNuxtModule, findPath, useLogger } from '@nuxt/kit' +import { addImports, addTemplate, createResolver, defineNuxtModule, findPath, useLogger } from '@nuxt/kit' import { normalize, relative } from 'pathe' import { joinURL } from 'ufo' import defu from 'defu' @@ -68,8 +68,6 @@ export default defineNuxtModule>({ const resolver = createResolver(import.meta.url) - addPlugin(resolver.resolve('./runtime/plugin')) - addImports([ { name: 'useStripe', @@ -93,9 +91,11 @@ declare module '#stripe' { // Inline module runtime in Nitro bundle nitroConfig.externals = defu( - typeof nitroConfig.externals === 'object' ? nitroConfig.externals : {}, { + typeof nitroConfig.externals === 'object' ? nitroConfig.externals : {}, + { inline: [resolver.resolve('./runtime')], - }) + }, + ) nitroConfig.alias['#stripe'] = serverRuntime }) diff --git a/src/runtime/composables.ts b/src/runtime/composables.ts index add0528..f7704e4 100644 --- a/src/runtime/composables.ts +++ b/src/runtime/composables.ts @@ -1,4 +1,22 @@ -import { useNuxtApp } from '#app' +import type { Stripe, StripeConstructorOptions } from '@stripe/stripe-js' +import { loadScript } from '@fixers/stripe-js' + +import type { ModulePublicRuntimeConfig } from '../module' +import { useRuntimeConfig } from '#imports' + +export interface CreateStripeOptions extends StripeConstructorOptions { + /** + * Override the runtime configured publishable key. + */ + publishableKey?: string + + /** + * Set it to `false` to [disable advanced fraud detection](https://stripe.com/docs/disputes/prevention/advanced-fraud-detection#disabling-advanced-fraud-detection) + * + * @default true + */ + advancedFraudSignals?: boolean +} /** * Use Stripe. @@ -14,4 +32,26 @@ import { useNuxtApp } from '#app' * }) * ``` */ -export const useStripe = () => useNuxtApp().$stripe +export function useStripe() { + return { + createClient: async (options?: CreateStripeOptions): Promise => { + const stripeRuntimeConfig = useRuntimeConfig().public.stripe as ModulePublicRuntimeConfig | undefined + + const { publishableKey, advancedFraudSignals, ...stripeOptions } = options || {} + + const stripeConstructor = await loadScript({ + advancedFraudSignals: advancedFraudSignals ?? true, + }) + + if (stripeConstructor) { + return stripeConstructor(publishableKey ?? stripeRuntimeConfig?.publishableKey ?? '', { + ...stripeRuntimeConfig?.client, + ...stripeOptions, + }) + } + + // Stripe is null in SSR + return null! + }, + } +} diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts deleted file mode 100644 index 501f685..0000000 --- a/src/runtime/plugin.ts +++ /dev/null @@ -1,82 +0,0 @@ -import type { Stripe, StripeConstructorOptions } from '@stripe/stripe-js' -import { loadScript } from '@fixers/stripe-js' - -import type { ModulePublicRuntimeConfig } from '../module' - -import type { Plugin } from '#app' -import { defineNuxtPlugin, useRuntimeConfig } from '#app' - -export interface CreateStripeOptions extends StripeConstructorOptions { - /** - * Override the runtime configured publishable key. - */ - publishableKey?: string - - /** - * Set it to `false` to [disable advanced fraud detection](https://stripe.com/docs/disputes/prevention/advanced-fraud-detection#disabling-advanced-fraud-detection) - * - * @default true - */ - advancedFraudSignals?: boolean -} - -async function createClient(options?: CreateStripeOptions): Promise { - const stripeRuntimeConfig: ModulePublicRuntimeConfig | undefined = useRuntimeConfig().public.stripe - - const { publishableKey, advancedFraudSignals, ...stripeOptions } = options || {} - - const stripeConstructor = await loadScript({ - advancedFraudSignals: advancedFraudSignals || true, - }) - - let stripe: Stripe - - if (stripeConstructor) { - stripe = stripeConstructor(publishableKey || stripeRuntimeConfig?.publishableKey, { - ...stripeRuntimeConfig?.client, - ...stripeOptions, - }) - } - - // Stripe is null in SSR - return stripe! -} - -/** - * Represents a Nuxt Stripe plugin. - */ -export interface StripePlugin { - /** - * Create a new {@link Stripe} Client instance. - * - * ### Note: - * In SSR this method return null, as the client library cannot be loaded. - */ - createClient: (options?: CreateStripeOptions) => Promise -} - -/** - * The Nuxt Stripe plugin. - * - * ### Example: - * ```ts - * const { $stripe } = useNuxtApp() - * - * const stripe = await $stripe.createClient() - * - * await stripe.redirectToCheckout({ - * sessionId: session.id, - * }) - * ``` - */ -const plugin: Plugin<{ stripe: StripePlugin }> = defineNuxtPlugin(() => { - return { - provide: { - stripe: { - createClient, - }, - }, - } -}) - -export default plugin