Skip to content

Commit

Permalink
add module for dynamic startegy import
Browse files Browse the repository at this point in the history
  • Loading branch information
audiBookning committed May 21, 2021
1 parent 02377d0 commit 4853d12
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 61 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@

## Notes

- The whole stripe code is in the `StripeAppModule`.
- The stripe code of the different subscription strategies is in 5 different modules, each with a controller managing some "main" routes and with its own service (used at this time only for the Webhooks).

- The stripe code is in 5 different modules, each with controllers controlling some "main" routes with its own service (used at this time only for the Webhooks), implementing different strategies.

- Leave only one module imported in the app.module by uncommenting it (comment the others) to avoid well deserved instabilities when using the @golevelup/nestjs-stripe module.
- The StrategyModule is used just to dynamically load the different strategy modules depending on the env variable `STRIPE_STRATEGY`, so as to avoid well deserved instabilities when instantiating multiple times the @golevelup/nestjs-stripe module.

- Their controllers are:

Expand Down Expand Up @@ -54,6 +52,4 @@

- Errors are too generic and many try catch missing, although Nestjs catch them by default...

- It would be cleaner to create a dynamic module that would load the files of the different strategies depending on env variables. The problem is that one would loose the quick boilerplate possibility.

- Integrate with a database. Maybe do it in a different repo in order to separate the basic "routing" implementation on its own?
12 changes: 2 additions & 10 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import fixedPriceConfig from './config/fixed-price.config';
import meteredUsageConfig from './config/metered-usage.config';
import multiPlanConfig from './config/multi-plan.config';
import perSeatConfig from './config/per-seat.config';
import { StripeCheckoutModule } from './stripe-checkout/stripe-checkout.module';
// import { StripeFixedPriceModule } from './stripe-fixed-price/stripe-fixed-price.module';
// import { StripeMeteredUsageModule } from './stripe-metered-usage/stripe-metered-usage.module';
// import { StripeMultiPlanModule } from './stripe-multi-plan/stripe-multiple-plan.module';
// import { StripePerSeatModule } from './stripe-per-seat/stripe-per-seat.module';
import { StrategyModule } from './loading-module/strategy.module';

@Module({
imports: [
Expand All @@ -27,11 +23,7 @@ import { StripeCheckoutModule } from './stripe-checkout/stripe-checkout.module';
],
isGlobal: true,
}),
StripeCheckoutModule,
// StripeFixedPriceModule,
// StripeMeteredUsageModule,
// StripeMultiPlanModule,
// StripePerSeatModule,
StrategyModule.forRoot(),
],
controllers: [AppController],
providers: [AppService],
Expand Down
40 changes: 40 additions & 0 deletions src/loading-module/strategy.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { DynamicModule, Module } from '@nestjs/common';
import { config } from 'dotenv';
import { StripeCheckoutModule } from '../stripe-checkout/stripe-checkout.module';
import { StripeFixedPriceModule } from '../stripe-fixed-price/stripe-fixed-price.module';
import { StripeMeteredUsageModule } from '../stripe-metered-usage/stripe-metered-usage.module';
import { StripeMultiPlanModule } from '../stripe-multi-plan/stripe-multiple-plan.module';
import { StripePerSeatModule } from '../stripe-per-seat/stripe-per-seat.module';
config();

enum StrategyEnum {
Checkout = 'Checkout',
Fixed = 'Fixed',
Metered = 'Metered',
Multi = 'Multi',
Seat = 'Seat',
}

@Module({})
export class StrategyModule {
public static forRoot(): DynamicModule {
const strategy = process.env.STRIPE_STRATEGY;
const modulesImports = {
Checkout: StripeCheckoutModule,
Fixed: StripeFixedPriceModule,
Metered: StripeMeteredUsageModule,
Multi: StripeMultiPlanModule,
Seat: StripePerSeatModule,
};

if (!modulesImports[strategy]) {
throw new Error('Wrong module string');
}
return {
module: StrategyModule,
imports: [modulesImports[strategy]],
providers: [],
exports: [],
};
}
}
13 changes: 4 additions & 9 deletions src/stripe-checkout/stripe-checkout.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,13 @@ config();
imports: [
JsonBodyMiddleware,
RawBodyMiddleware,
/* StripeModule.forRoot(StripeModule, {
apiKey: process.env.STRIPE_API_KEY,
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
},
}), */

StripeModule.forRootAsync(StripeModule, {
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
apiKey: process.env.STRIPE_API_KEY,
useFactory: (configSvc: ConfigService) => ({
apiKey: configSvc.get<string>('stripe.apiKey'),
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
stripeWebhookSecret: configSvc.get<string>('stripe.webhookSecret'),
},
}),
inject: [ConfigService],
Expand Down
12 changes: 3 additions & 9 deletions src/stripe-fixed-price/stripe-fixed-price.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ config();
imports: [
JsonBodyMiddleware,
RawBodyMiddleware,
/* StripeModule.forRoot(StripeModule, {
apiKey: process.env.STRIPE_API_KEY,
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
},
}), */
StripeModule.forRootAsync(StripeModule, {
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
apiKey: process.env.STRIPE_API_KEY,
useFactory: (configSvc: ConfigService) => ({
apiKey: configSvc.get<string>('stripe.apiKey'),
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
stripeWebhookSecret: configSvc.get<string>('stripe.webhookSecret'),
},
}),
inject: [ConfigService],
Expand Down
12 changes: 3 additions & 9 deletions src/stripe-metered-usage/stripe-metered-usage.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ config();
imports: [
JsonBodyMiddleware,
RawBodyMiddleware,
/* StripeModule.forRoot(StripeModule, {
apiKey: process.env.STRIPE_API_KEY,
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
},
}), */
StripeModule.forRootAsync(StripeModule, {
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
apiKey: process.env.STRIPE_API_KEY,
useFactory: (configSvc: ConfigService) => ({
apiKey: configSvc.get<string>('stripe.apiKey'),
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
stripeWebhookSecret: configSvc.get<string>('stripe.webhookSecret'),
},
}),
inject: [ConfigService],
Expand Down
12 changes: 3 additions & 9 deletions src/stripe-multi-plan/stripe-multiple-plan.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ config();
imports: [
JsonBodyMiddleware,
RawBodyMiddleware,
/* StripeModule.forRoot(StripeModule, {
apiKey: process.env.STRIPE_API_KEY,
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
},
}), */
StripeModule.forRootAsync(StripeModule, {
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
apiKey: process.env.STRIPE_API_KEY,
useFactory: (configSvc: ConfigService) => ({
apiKey: configSvc.get<string>('stripe.apiKey'),
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
stripeWebhookSecret: configSvc.get<string>('stripe.webhookSecret'),
},
}),
inject: [ConfigService],
Expand Down
12 changes: 3 additions & 9 deletions src/stripe-per-seat/stripe-per-seat.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ config();
imports: [
JsonBodyMiddleware,
RawBodyMiddleware,
/* StripeModule.forRoot(StripeModule, {
apiKey: process.env.STRIPE_API_KEY,
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
},
}), */
StripeModule.forRootAsync(StripeModule, {
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
apiKey: process.env.STRIPE_API_KEY,
useFactory: (configSvc: ConfigService) => ({
apiKey: configSvc.get<string>('stripe.apiKey'),
webhookConfig: {
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
stripeWebhookSecret: configSvc.get<string>('stripe.webhookSecret'),
},
}),
inject: [ConfigService],
Expand Down

0 comments on commit 4853d12

Please sign in to comment.