How to use Dynamic Modules in NestJS with Stripe

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this episode i'm going to show you how to use dynamic modules to create reusable production ready code you can see an example of it here where we have a module with a for a static for root method where we're passing in our configuration from the global app module level this is fantastic for code reuse and it's an abstraction that i use not just for stripe but across basically any kind of third-party code that i use if you're new to nest i highly recommend checking out the docs on modules in addition to this video since this is a bit more of an advanced topic than just how nest is normally used before i dive in i just want to explain briefly how this is going to work normally you would use something like this at module decorator to configure your modules but in our case we're not going to use that we're instead going to be using using what's called a dynamic module and this lets us pass values into a function and then we return the module setup from that function this allows us to do something like pass in an environment variable in one project or a string reference in another project all right let's go ahead and get started i've already created a project called dynamic modules if you need to create one you can just do nest new app name replacing app name with whatever you want your app to be you can also use an existing project it really doesn't matter first thing we want to do though is install stripe and then we're going to create a module called stripe as well so nest gmo for module we'll call it stripe and then if we open up our source stripe stripe.module we can see that we now have our stripe module here and so for this video we're not going to be messing around with the app module decorator we are instead going to be using the dynamic module interface so the first thing we need to do is add a static for root method for root is just a convention that is often used in nest projects you can really name this whatever you want doesn't really matter and we're going to set the return type to dimet dynamic module so we'll go ahead and it's expecting us to return a dynamic module and so for this the only required property is module so we can go ahead and set this equal to stripe module so generally when you're creating a dynamic module this module reference here should almost always be whatever class you're putting the static method on there's no need to really use anything else so the next thing that we want to do is we'll need to set up and configure stripe so let's go ahead and we'll do const stripe we'll set it equal to a new stripe and i'm just going to hit enter here and it should auto import if it doesn't for you go ahead and type this out we'll open up our parentheses and it needs an api key and it needs a configuration object so for api key i'm going to use a variable that we haven't created yet called api key and i'll use config as the variable that we haven't created yet for the configuration object and so what we're going to do is pass these into the for root method and so we'll say api key here and that is of course going to be a string and then config well this is going to be equal to stripe dot stripe whoops stripe config great save that up compiler is happy and if we actually just take a quick look we can see that our config here is the same type as this so that is all good we have our stripe object now but we haven't told nest what to do with it we haven't even told nest about it yet so to do that we're going to use providers and we're also going to use an exports array to let the entire app know that hey you can use stripe if you need to use it so first things first we will say const stripe providers sorry stripe provider and this is going to be equal to an object of type provider and so we'll open up our curlies here and we're going to say when someone wants us to provide the stripe client we are going to use the value of stripe so again when someone wants us to provide stripe client so this is going to be a string that will be used we're going to use the value of stripe and so now we have our stripe provider here let's go ahead and tell nest about it so inside of the providers array we are going to add stripe provider make sure it's inside of an array and then we're going to make it available to other mod modules by using the exports array same thing make sure it's in an array and put your stripe provider in there save it up now there's one last property we need to add here and this is really a convenience for us this is global true and what this is going to do is it's going to mean that whenever when we import it so we import stripe module up here we only need to import it once and it's going to make it globally available so it's going to make these exports globally available now if we head back to our app module we can see that we're not actually using our for root method right now we're just using this at module decorator configuration so let's change that inside of module we will call our for root here and we need to pass it an api key and a configuration this is the beauty of using dynamic modules um this this uh stripe module here could be imported into 50 different projects and you could have 50 different ways of passing the api key in one it might be your api key passed as a string in another you might use process dot env.stripe key and in yet another you might actually use some kind of api call or a file on the file system it doesn't matter this is why we're using a dynamic module here so for us what i'm going to do is just simply use process.env.com key i'll show you how to set that up in a second but we also need to pass in our configuration object so we'll pop that open and stripe in typescript requires at least the api version so we'll go ahead and add that if you open up your quotes it should show this 2020-08-27 which we can just click and then we'll save it up so now we have our stripe module being configured but we haven't set up our api key yet so let's go ahead and do that so copy this value here head on over to your terminal i'll just clear this and the syntax is export and we will do stripe key and we will set it equal to value now just a quick note i'm using wsl ubuntu this also works on mac but it will not work on windows so if you're on windows you'll need to use whatever methods are available to you there and you can also if it's just for testing purposes just hard code it directly in here your api key just be warned um you definitely don't want to commit this to your repo so before you commit anything or push anything um to github or git or whatever make sure you replace it with the appropriate environment reference or file reference anyways i'm going to set my property off camera so i'll be back in a moment okay i've just set my property here you should set yours as well to whatever your stripe secret key is and then you just hit enter so i've already done it so i'm not going to hit enter um but that should be all set up now and so if we head over let's just sanity check everything in our terminal and do npm run start dev i just want to make sure that the dev server is actually working and that um there we go so our our app module starts up and we have everything working however we don't have a way to actually see that we're connected to stripe right now and so the way that i'm going to show you how to do this is just a quick little i'm going to create a quick module just for testing purposes and so i'm going to create a customers module as if i'm implementing some sort of customers api and then we'll go from there so we'll do nest gmo for module and we'll call this module customers i'm also going to create a controller really quick so nest gco customers co is controller and just a really quick tip here if you generate your modules before you generate your controllers or services or anything your module will automatically have whatever you generate after so i generated my controller after and so my controller gets added into my module automatically by the cli so if you're going to create a full like a service or something or a controller here and there this is a great way to do it in order now let's pop open our customers controller and take a look at how we can add in our stripe instance so what we want to do is have some kind of method where we can call slash customers and let's get a list of customers so let's go ahead and try that right now so we'll do at get for a get and we'll just do slash to denote that it is the base endpoint of our controller that we're going to use and we'll call this list customers whoops open and close and then open up our curlies so in this method we are going to return some list of customers or at least the the result of an api called a stripe however we don't have stripe yet if we have an injected stripe into our controller it's nowhere to be found so let's just review how to actually do that quick if we head over to our stripe module we can see we have this string reference now i don't want this string reference to be used anywhere outside of this module so before we even add stripe to our customers controller i'm going to get rid of this so we will create a new file called constants.ts and inside of this file let's just export that string so we'll do export const stripe client and this is of course going to be equal to that stripe client string save it up go back to stripe module and we will replace this now with stripe client and we can save that as well so now what we're saying is that when someone wants us to provide this constant value we'll use the value of stripe perfect now we're ready to actually start injecting it so let's create a constructor so that we can inject stripe open up the parentheses and we will use the at inject decorator and so if you haven't used this before it is a parameter decorator that takes in a token so in our case our token is stripe client so we'll import that as well and then we'll just set this as normal so private stripe is stripe then make sure to open and close those curly's and make sure that you also import stripe and save it all up so now we have our stripe here we are saying hey inject our stripe so right provide me with stripe if we go back over to our stripe module you can see that we're saying hey when someone wants to be provided with the stripe client use this value and so if we head back over we are saying hey give me whatever is the stripe client and put it into the variable stripe so now we have our stripe let's go ahead and use it so we'll say return this dot stripe dot customers.list won't pass any parameters this is just a demonstration to show that everything's working and let's go ahead and open our terminal and start up our dev server so npm run start dev all right this is all up and running okay and now if we open up our chrome and type in localhost colon 3000 slash customers and hit enter we should now get a list of customers from the stripe api and you can see i have one here to test my first customer spoiler it's me so there you have it if we check out our app module we have this nice clean for root method where we can pass in our stripe key and any additional configuration if we actually just take a look we can see we have all these different configuration options that might be different project to project and so with this pattern this is something that you can reuse across many different third-party libraries or many different libraries that you want to create and offer it's a fantastic pattern if you're getting into code reuse i mean for example i can just type in secret here and now rather than using my environment variables i'm just using a string reference and it gives people the power to kind of configure their application the way that they want thank you so much for watching if you liked it make sure to like and subscribe and leave me a comment if you have any suggestions for future videos thank you have a great day
Info
Channel: Harrison Milbradt
Views: 826
Rating: undefined out of 5
Keywords: nest, stripe-node, stripejs
Id: JTGgYVIBZjI
Channel Id: undefined
Length: 14min 21sec (861 seconds)
Published: Mon Sep 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.