Closed sandros94 closed 1 year ago
Yes, @Sandros94, this should be good 👍 But you could also perhaps initialise them to empty strings in the default options.
Yes, @Sandros94, this should be good 👍 But you could also perhaps initialise them to empty strings in the default options.
Sorry to be redundant, asking to make sure I'm 100% of understanding, do you mean in the module.ts
right?
Becoming something like is enough?:
defaults: {
publishableKey: '' as string,
apiKey: '' as string,
apiVersion: '2022-11-15'
},
I decided to revert the commit for now, this means that stripe-js is loaded twice (first from the plugin then from the component) and it will be loaded globally breaking the lazy loading and a number of page optimizations (that impact negatively on Lighthouse scores).
I've found the issue, and I didn't think about it at first.
Following the stripe-js doc:
This function returns a Promise that resolves with a newly created Stripe object once Stripe.js has loaded. [...] If you call loadStripe in a server environment it will resolve to null.
import {loadStripe} from '@stripe/stripe-js'; const stripe = await loadStripe('pk_test_TYooMQauvdEDq54NiTphI7jx');
Since loadStripe
function already checks for server/client side loading, we don't need to add any logics for that.
But, loadStripe
returns a Promise
, so we need to await
for it. Since the async
nature of it, this lets us load the package only on the pages defined by the devs that want it.
<template>
<main>
<h1>Stripe client</h1>
<pre>
{{ stripeClient ? stripeClient : 'Loading...' }}
</pre>
</main>
</template>
<script setup lang="ts">
const stripeClient = await useClientStripe()
</script>
Not to mention that this new useClientStripe
lets us expose all the stripe-js types for better ts support.
IMO this PR is currently complete. @fuentesloic
I still not fully understand why #22 was created, but it pointed out an important, missing, feature in the current module: the ability to config and change stripeConfig for useServerStripe
. I've made sure to include and export the related types and update the vitests.
This introduce a breaking change, because apiVersion
is now configured inside the new serverConfig
.
I still need to understand if stripe-js
does have such options or are all defined at load.
I've checked the stripe-js
docs and I've found the options mentioned, they aren't many but it was an easy addition so I decided to add them regardless.
@Sandros94 I'm going to merge your PR and check with #22 the following path to keep in order to limit futur breaking change. I think all the work propose make sense so it will be release once every breaking change has been merged. Thanks again for your contribution 💪
Fixes #18 Fixes #20 Fixes #23
What does this PR do?
This PR fixes a number of things:
publishableKey
andapiKey
configurable and editable at runtime via.env
.stripe-js
only on those pages thatuseClientStripe
is used.stripe-js
types for better ts DX.What are the breaking changes?
changed env naming
Following the naming conventions for Nuxt the env variables should be followed:
apiVersion
configTo make server side configurations available for nuxt-stripe, the
apiVersion
placement has been changed and the following must be taken in consideration:Original Post
Still not fully sure if this is the correct way to do this, but basically this small change already enables us to provide and edit stripe keys at runtime.
@danielroe Sorry to disturb, could you take a quick look if I'm doing things decently?